1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
-
Upload
ruth-sherman -
Category
Documents
-
view
227 -
download
2
Transcript of Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
![Page 1: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/1.jpg)
Department of Computer Science
Data Structures Using C++ 2E
Chapter 5Linked Lists
![Page 2: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/2.jpg)
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
![Page 3: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/3.jpg)
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
![Page 4: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/4.jpg)
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
![Page 5: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/5.jpg)
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
![Page 6: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/6.jpg)
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
![Page 7: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/7.jpg)
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
![Page 8: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/8.jpg)
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
![Page 9: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/9.jpg)
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
![Page 10: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/10.jpg)
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
![Page 11: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/11.jpg)
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
![Page 12: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/12.jpg)
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
![Page 13: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/13.jpg)
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
![Page 14: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/14.jpg)
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
![Page 15: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/15.jpg)
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
![Page 16: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/16.jpg)
Department of Computer Science 16
Visual Studio Example
buildListForward
![Page 17: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/17.jpg)
Department of Computer Science 17
Text
![Page 18: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/18.jpg)
Department of Computer Science 18
Text
![Page 19: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/19.jpg)
Department of Computer Science 19
Text
![Page 20: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/20.jpg)
Department of Computer Science 20
Text
![Page 21: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/21.jpg)
Department of Computer Science 21
Text
![Page 22: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/22.jpg)
Department of Computer Science 22
Text
![Page 23: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/23.jpg)
Department of Computer Science 23
Text
![Page 24: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/24.jpg)
Department of Computer Science 24
Text
![Page 25: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/25.jpg)
Department of Computer Science 25
Text
![Page 26: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/26.jpg)
Department of Computer Science 26
Text
![Page 27: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/27.jpg)
Department of Computer Science 27
Text
![Page 28: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/28.jpg)
Department of Computer Science 28
Text
![Page 29: Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.](https://reader036.fdocuments.net/reader036/viewer/2022062518/56649f355503460f94c5385f/html5/thumbnails/29.jpg)
Department of Computer Science 29
Text