Department of Computer Science
Data Structures Using C++ 2E
Chapter 5Linked Lists
Department of Computer Science 2 Data Structures Using C++ 2E
Objectives
Learn about linked listsBecome aware of the basic properties of
linked listsExplore the insertion and deletion operations
on linked listsDiscover how to build and manipulate a
linked list
Department of Computer Science 3 Data Structures Using C++ 2E
Objectives (cont’d.)
Learn how to construct a doubly linked listDiscover how to use the STL container listLearn about linked lists with header and
trailer nodesBecome aware of circular linked lists
Department of Computer Science 4
Linked Lists
Collection of components (nodes)Every node (except last)
Contains address of the next node
Node componentsData: stores relevant informationLink: stores address
Data Structures Using C++ 2E
FIGURE 5-1 Structure of a node
Department of Computer Science 5 Data Structures Using C++ 2E
Linked Lists (cont’d.)Head (first)
Address of the first node in the list
Arrow points to node addressStored in node
Down arrow in last node indicates NULL link field
FIGURE 5-2 Linked list
FIGURE 5-3 Linked list and values of the links
Department of Computer Science 6 Data Structures Using C++ 2E
Linked Lists (cont’d.)Two node components
Declared as a class or structData type depends on specific application
Link component: pointerData type of pointer variable: node type itself
Department of Computer Science 7 Data Structures Using C++ 2E
Linked Lists: Some Properties
Head stores address of first node Info stores informationLink stores address of next node
Assume info type int
FIGURE 5-4 Linked list with four nodes
TABLE 5-1 Values of head and some ofthe nodes of the linked list in Figure 5-4
Department of Computer Science 8 Data Structures Using C++ 2E
Linked Lists: Some Properties (cont’d.)Pointer current: same type as pointer
headcurrent = head;
Copies value of head into current
current = current->link;Copies value of current->link (2800) into current
FIGURE 5-5 List after the statement current = current->link; executes
Department of Computer Science Data Structures Using C++ 2E 9
Linked Lists: Some Properties (cont’d.)
TABLE 5-2 Values of current, head, and some of the nodes of the linked list in Figure 5-5
Department of Computer Science 10
Traversing a Linked List
Basic linked list operationsSearch list to determine if particular item is in the
list Insert item in listDelete item from list
These operations require list traversalGiven pointer to list first node, we must step
through list nodes
Data Structures Using C++ 2E
Department of Computer Science 11
Traversing a Linked List (cont’d.)
Suppose head points to a linked list of numbersCode outputting data stored in each node
Data Structures Using C++ 2E
Department of Computer Science 12 Data Structures Using C++ 2E 12
Item Insertion and Deletion
Generic definition of a node on page 270
TABLE 5-3 Inserting a node in a linked list
Department of Computer Science 13 Data Structures Using C++ 2E
Item Insertion and Deletion (cont’d.)
Sequence of statements to insert nodeVery important
Use only one pointer (p) to adjust links of the nodes
Using two pointersCan simplify insertion code somewhat
Department of Computer Science 14 Data Structures Using C++ 2E 14
Item Insertion and Deletion (cont’d.)
Memory still occupied by node after deletionMemory is inaccessibleDeallocate memory using a pointer to this
node
FIGURE 5-10 List after the statement p->link = p->link->link; executes
Department of Computer Science 15 Data Structures Using C++ 2E
Building a Linked List
If linked list data unsortedLinked list unsorted
Ways to build linked listForward
New node always inserted at end of the linked listSee example on page 274See function buildListForward on page 277
BackwardNew node always inserted at the beginning of the listSee example on page 277See function buildListBackward on page 278
Department of Computer Science 16
Visual Studio Example
buildListForward
Department of Computer Science 17
Text
Department of Computer Science 18
Text
Department of Computer Science 19
Text
Department of Computer Science 20
Text
Department of Computer Science 21
Text
Department of Computer Science 22
Text
Department of Computer Science 23
Text
Department of Computer Science 24
Text
Department of Computer Science 25
Text
Department of Computer Science 26
Text
Department of Computer Science 27
Text
Department of Computer Science 28
Text
Department of Computer Science 29
Text
Top Related