Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:

Similar presentations


Presentation on theme: "Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:"— Presentation transcript:

1 Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming: Program Design Including Data Structures, Fourth Edition 1 Linked Lists Link field in last node is NULL

2 C++ Programming: Program Design Including Data Structures, Fourth Edition 2 Linked Lists (continued) Example: – Suppose that the first node is at memory location 1200, and the second node is at memory location 1575

3 C++ Programming: Program Design Including Data Structures, Fourth Edition 3 Linked Lists (continued) Because each node of a linked list has two components, we need to declare each node as a class or struct – Data type of a node depends on the specific application – The link component of each node is a pointer

4 C++ Programming: Program Design Including Data Structures, Fourth Edition 4 Linked Lists: Some Properties

5 C++ Programming: Program Design Including Data Structures, Fourth Edition 5 Linked Lists: Some Properties (continued) current = head; – Copies value of head into current

6 C++ Programming: Program Design Including Data Structures, Fourth Edition 6 Linked Lists: Some Properties (continued) current = current->link;

7 exercise ????????????????

8 C++ Programming: Program Design Including Data Structures, Fourth Edition 8 Linked Lists: Some Properties (continued)

9 C++ Programming: Program Design Including Data Structures, Fourth Edition 9 Traversing a Linked List The basic operations of a linked list are: – Search to determine if an item is in the list – Insert an item in the list – Delete an item from the list Traversal: given a pointer to the first node of the list, step through the nodes of the list

10 C++ Programming: Program Design Including Data Structures, Fourth Edition 10 Traversing a Linked List (continued) To traverse a linked list: Example:

11 C++ Programming: Program Design Including Data Structures, Fourth Edition 11 Item Insertion and Deletion Consider the following definition of a node: We will use the following variable declaration:

12 C++ Programming: Program Design Including Data Structures, Fourth Edition 12 Insertion Consider the following linked list: A new node with info 50 is to be created and inserted after p

13

14 The sequence of statements to insert the node is very important. Suppose that we reverse the sequence of the statements and execute the statements in the following order:

15 C++ Programming: Program Design Including Data Structures, Fourth Edition 15 Deletion Node with info 34 is removed from the list, but memory is still occupied; node is dangling

16


Download ppt "Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:"

Similar presentations


Ads by Google