chapter 16-1 linked structures
DESCRIPTION
Chapter 16-1 Linked Structures. Dale/Weems. Chapter 16 Topics. Meaning of a Linked List Meaning of a Dynamic Linked List Traversal, Insertion and Deletion of Elements in a Dynamic Linked List Specification of a Dynamic Linked Sorted List - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/1.jpg)
1
Chapter 16-1
Linked Structures
Dale/Weems
![Page 2: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/2.jpg)
2
Chapter 16 Topics
Meaning of a Linked List Meaning of a Dynamic Linked List Traversal, Insertion and Deletion of Elements
in a Dynamic Linked List Specification of a Dynamic Linked Sorted List Insertion and Deletion of Elements in a
Dynamic Linked Sorted List
![Page 3: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/3.jpg)
3
What is a List?
A list is a varying-length, linear collection of homogeneous elements
Linear means that each list element (except the first) has a unique predecessor and each element (except the last) has a unique successor
![Page 4: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/4.jpg)
4
To implement the List ADT
The programmer must
1) choose a concrete data representation for the list, and
2) implement the list operations
![Page 5: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/5.jpg)
5
Recall:4 Basic Kinds of ADT Operations
Constructors -- create a new instance (object) of an ADT
Transformers -- change the state of one or more of the data values of an instance
Observers -- allow client to observe the state of one or more of the data values of an instance without changing them
Iterators -- allow client to access the data values in sequence
![Page 6: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/6.jpg)
List Operations
Transformers Insert Delete Sort
Observers IsEmpty IsFull Length IsPresent
change state
observe state
6
![Page 7: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/7.jpg)
7
ADT List Operations
Iterator Reset GetNextItem
Reset prepares for the iteration GetNextItem returns the next item in
sequence No transformer can be called between calls
to GetNextItem (Why?)
Iteration Pair
![Page 8: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/8.jpg)
8
Array-based class List
Reset
IsFull
Length
IsPresent
Delete
IsEmpty
Insert
GetNexItem
Private data:
lengthdata [0] [1] [2]
[MAX_LENGTH-1]
currentPos
SelSort
![Page 9: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/9.jpg)
9
// Specification file array-based list (“list.h”)const int MAX_LENGTH = 50;typedef int ItemType;
class List // Declares a class data type{public: // Public member functions
List(); // constructor bool IsEmpty () const; bool IsFull () const; int Length () const; // Returns length of list void Insert (ItemType item); void Delete (ItemType item); bool IsPresent(ItemType item) const; void SelSort (); void Reset (); ItemType GetNextItem ();
private: // Private data membersint length; // Number of values currently storedItemType data[MAX_LENGTH];
int CurrentPos; // Used in iteration };
9
![Page 10: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/10.jpg)
10
Implementation Structures
Use a built-in array stored in contiguous memory locations, implementing operations Insert and Delete by moving list items around in the array, as needed
Use a linked list in which items are not necessarily stored in contiguous memory locations
A linked list avoids excessive data movement from insertions and deletions
![Page 11: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/11.jpg)
11
Implementation Possibilities for a List ADT
List
Linked listBuilt-in array
Built-in dynamic data and pointers
Built-in arrayof structs
![Page 12: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/12.jpg)
12
Array Representation of a Linked List
2
head 58 -1
4 5
46 0
16 7
39 4
component link
Node[0]
Node[1]
Node[2]
Node[3]
Node[4]
Node[5]
Node[6]
Node[7]
![Page 13: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/13.jpg)
13
Data Structure of Array Based Linked List
struct NodeType
{
int component;
int link;
};
NodeType node[1000]; // Max. 1000 nodes
int head;
![Page 14: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/14.jpg)
14
Insert a New Node into a Linked List
2
head 58 -1
25 7
4 5
46 0
16 1
39 4
component link
Node[0]
Node[1]
Node[2]
Node[3]
Node[4]
Node[5]
Node[6]
Node[7]
Insert 25,Setting link to 7
Change link from 7 to 1
![Page 15: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/15.jpg)
15
Delete a Node from a Linked List
2
head 58 -1
25 7
4 5
46 0
16 1
39 0
component link
Node[0]
Node[1]
Node[2]
Node[3]
Node[4]
Node[5]
Node[6]
Node[7]Change the link
From 4 to 0
Deleted
![Page 16: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/16.jpg)
16
A Linked List
A linked list is a list in which the order of the components is determined by an explicit link member in each node
Each node is a struct containing a data member and a link member that gives the location of the next node in the list
head ‘X’ ‘C’ ‘L’
![Page 17: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/17.jpg)
17
Dynamic Linked List
head “Ted” “Irv” “Lee”
A dynamic linked list is one in which the nodes are linked together by pointers and an external pointer (or head pointer) points to the first node in the list
![Page 18: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/18.jpg)
18
Nodes can be located anywhere in memory
The link member holds the memory address of the next node in the list
head 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
![Page 19: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/19.jpg)
19
// Type declarations struct NodeType { char component; NodeType* link;}
typedef NodeType* NodePtr;
// Variable DECLARATIONSNodePtr head;NodePtr ptr;
19
Declarations for a Dynamic Linked List
. component . link
‘A’ 6000
![Page 20: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/20.jpg)
2020
Pointer Dereferencing and Member Selection
. Component . link
‘A’ 6000 ptr
ptr
ptr
. component . link
‘A’ 6000
*ptr
ptr
. component . link
(*ptr).component
ptr->component
‘A’ 6000
![Page 21: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/21.jpg)
2121
ptr is a pointer to a node
. component . link
‘A’ 6000 ptr
ptr
![Page 22: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/22.jpg)
2222
*ptr is the entire node pointed to by ptr
ptr
. component . link
‘A’ 6000
*ptr
![Page 23: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/23.jpg)
2323
ptr->component is a node member
ptr
. component . link
ptr->component
(*ptr).component // Equivalent
‘A’ 6000
![Page 24: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/24.jpg)
2424
ptr->link is a node member
ptr
. component . link
ptr->link
(*ptr).link // Equivalent
‘A’ 6000
![Page 25: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/25.jpg)
25
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
![Page 26: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/26.jpg)
26
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 3000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
![Page 27: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/27.jpg)
27
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 3000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
![Page 28: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/28.jpg)
28
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 3000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
![Page 29: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/29.jpg)
29
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 5000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
![Page 30: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/30.jpg)
30
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 5000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
![Page 31: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/31.jpg)
31
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 5000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
![Page 32: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/32.jpg)
32
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 2000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
![Page 33: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/33.jpg)
33
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 2000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
![Page 34: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/34.jpg)
34
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 2000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
![Page 35: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/35.jpg)
35
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr NULL
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
![Page 36: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/36.jpg)
36
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->component;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr NULL
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
![Page 37: Chapter 16-1 Linked Structures](https://reader035.vdocument.in/reader035/viewer/2022062408/56813126550346895d979988/html5/thumbnails/37.jpg)
37
The End of Chapter 16 Part 1