1 chapter 16 linked structures dale/weems. 2 chapter 16 topics l meaning of a linked list l meaning...
TRANSCRIPT
![Page 1: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/1.jpg)
1
Chapter 16
Linked Structures
Dale/Weems
![Page 2: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/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: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/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
To implement the List ADT , the rogrammer must1) choose a concrete data representation for the list,
and2) implement the list operations
![Page 4: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/4.jpg)
List OperationsTransformers
Insert Delete Sort
Observers IsEmpty IsFull Length IsPresent
Iterator Reset GetNextItem
change state
observe state
4
Iteration Pair
![Page 5: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/5.jpg)
5
Array-based class List
Reset
IsFull
Length
IsPresent
Delete
IsEmpty
Insert
GetNexItem
Private data:
lengthdata [0] [1] [2]
[MAX_LENGTH-1]
currentPos
SelSort
![Page 6: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/6.jpg)
6
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 7: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/7.jpg)
7
Implementation Possibilities for a List ADT
List
Linked listBuilt-in array
Built-in dynamic data and pointers
Built-in arrayof structs
![Page 8: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/8.jpg)
8
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
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
head ‘X’ ‘C’ ‘L’
![Page 9: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/9.jpg)
9
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 10: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/10.jpg)
10
// Type declarations struct NodeType { char info; NodeType* link;}
typedef NodeType* NodePtr;
// Variable DECLARATIONSNodePtr head;NodePtr ptr;
10
Declarations for a Dynamic Linked List
. info . link
‘A’ 6000
![Page 11: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/11.jpg)
1111
Pointer Dereferencing and Member Selection
. info . link
‘A’ 6000 ptr
ptr
ptr
. info . link
‘A’ 6000
*ptr
ptr
. info . link
(*ptr).info
ptr->info
‘A’ 6000
![Page 12: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/12.jpg)
12
Pointer Dereferencing (cont.)
ptr is a pointer to a node *ptr is the entire node pointed to by ptr ptr->info is a node member ptr->link is a node member
![Page 13: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/13.jpg)
13
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
![Page 14: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/14.jpg)
14
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// 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 15: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/15.jpg)
15
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// 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 16: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/16.jpg)
16
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// 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 17: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/17.jpg)
17
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// 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 18: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/18.jpg)
18
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// 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 19: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/19.jpg)
19
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// 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 20: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/20.jpg)
20
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// 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 21: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/21.jpg)
21
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// 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 22: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/22.jpg)
22
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// 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 23: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/23.jpg)
23
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// 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 24: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/24.jpg)
24
// Pre: head points to a dynamic linked listptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// 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 25: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/25.jpg)
Using Operator new
Recall If memory is available in the free store (or heap),
operator new allocates the requested object and returns a pointer to the memory allocated
The dynamically allocated object exists until the delete operator destroys it
25
![Page 26: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/26.jpg)
26
Inserting a Node at the Front of a List
char item = ‘B’;NodePtr location;location = new NodeType;location->info = item;location->link = head;head = location;
head ‘X’ ‘C’ ‘L’
‘B’item
![Page 27: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/27.jpg)
27
Inserting a Node at the Front of a List
char item = ‘B’;NodePtr location;location = new NodeType;location->info = item;location->link = head;head = location;
head ‘X’ ‘C’ ‘L’
‘B’item
location
![Page 28: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/28.jpg)
28
Inserting a Node at the Front of a List
char item = ‘B’;NodePtr location;location = new NodeType;location->info = item;location->link = head;head = location;
head ‘X’ ‘C’ ‘L’
‘B’item
location
![Page 29: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/29.jpg)
29
Inserting a Node at the Front of a List
char item = ‘B’;NodePtr location;location = new NodeType;location->info = item;location->link = head;head = location;
head ‘X’ ‘C’ ‘L’
‘B’item
location ‘B’
![Page 30: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/30.jpg)
30
Inserting a Node at the Front of a List
char item = ‘B’;NodePtr location;location = new NodeType;location->info = item;location->link = head;head = location;
head ‘X’ ‘C’ ‘L’
‘B’item
location ‘B’
![Page 31: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/31.jpg)
31
Inserting a Node at the Front of a List
char item = ‘B’;NodePtr location;location = new NodeType;location->info = item;location->link = head;head = location;
head ‘X’ ‘C’ ‘L’
‘B’item
location ‘B’
![Page 32: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/32.jpg)
When you use the operator delete The object currently pointed to by the pointer is
deallocated and the pointer is considered undefined
The object’s memory is returned to the free store
Using Operator delete
32
![Page 33: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/33.jpg)
33
Deleting the First Node from the List
NodePtr tempPtr;
item = head->info;tempPtr = head;head = head->link;delete tempPtr;
head
item
‘B’ ‘X’ ‘C’ ‘L’
tempPtr
![Page 34: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/34.jpg)
34
Deleting the First Node from the List
NodePtr tempPtr;
item = head->info;tempPtr = head;head = head->link;delete tempPtr;
head
item
‘B’ ‘X’ ‘C’ ‘L’
tempPtr
‘B’
![Page 35: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/35.jpg)
35
Deleting the First Node from the List
NodePtr tempPtr;
item = head->info;tempPtr = head;head = head->link;delete tempPtr;
head
item
‘B’ ‘X’ ‘C’ ‘L’
tempPtr
‘B’
![Page 36: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/36.jpg)
36
Deleting the First Node from the List
NodePtr tempPtr;
item = head->info;tempPtr = head;head = head->link;delete tempPtr;
head
item
‘B’ ‘X’ ‘C’ ‘L’
tempPtr
‘B’
![Page 37: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/37.jpg)
37
Deleting the First Node from the List
NodePtr tempPtr;
item = head->info;tempPtr = head;head = head->link;delete tempPtr;
head
item
‘X’ ‘C’ ‘L’
tempPtr
‘B’
![Page 38: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/38.jpg)
38
What is a Sorted List?
A sorted list is a variable-length, linear collection of homogeneous elements, ordered according to the value of one or more data members
The transformer operations must maintain the ordering
In addition to Insert and Delete, let’s add two new operations to our list
InsertAsFirst and RemoveFirst
![Page 39: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/39.jpg)
ADT HybridList Operations
Transformers InsertAsFirst Insert RemoveFirst Delete
Same observers and iterators as ADT List
Since we have two insertion and two deletion
operations, let’s call this a Hybrid List
change state
39
![Page 40: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/40.jpg)
40
// Specification file sorted list (“slist2.h”)
typedef int ItemType; // Type of each component is // a simple type or a string
struct NodeType{ ItemType item; // Pointer to person’s name NodeType* link; // Link to next node in list};
typedef NodeType* NodePtr;
40
struct NodeType
![Page 41: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/41.jpg)
41
// Specification file hybrid sorted list(“slist2.h”)
class HybridList{public:
bool IsEmpty () const;
void InsertAsFirst (/* in */ ItemType item);
void Insert (/* in */ ItemType item);
void RemoveFirst(/* out */ ItemType& item);
void Delete (/* in */ ItemType item); void Print () const; HybridList (); // Constructor ~HybridList (); // Destructor HybridList (const HybridList& otherList); // Copy-constructor
private:
NodeType* head;}; 41
![Page 42: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/42.jpg)
42
class HybridList
~HybridList
Insert
InsertASFirst
HybridList
IsEmpty
Delete
‘C’ ‘L’ ‘X’
Private data:
head
RemoveFirst
![Page 43: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/43.jpg)
43
// Implementation file for HybridList (“slist.cpp”)HybridList::HybridList () // Constructor
// Post: head == NULL
{
head = NULL;
}
HybridList::~HybridList () // Destructor
// Post: All linked nodes deallocated
{
ItemType temp;
// Keep deleting top node
while (!IsEmpty)
RemoveFirst (temp);
}
43
![Page 44: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/44.jpg)
44
Insert Algorithm
What will be the algorithm to Insert an item into its proper place in a sorted linked list?
That is, for a linked list whose elements are maintained in ascending order?
![Page 45: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/45.jpg)
45
Insert algorithm for HybridList
Find proper position for the new element in the sorted list using two pointers prevPtr and currPtr, where prevPtr trails behind currPtr
Obtain a new node and place item in it
Insert the new node by adjusting pointers
![Page 46: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/46.jpg)
46
Inserting ‘S’ into a List
‘C’ ‘L’ ‘X’
Private data:
head
prevPtr currPtr
![Page 47: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/47.jpg)
47
Finding Proper Position for ‘S’
‘C’ ‘L’ ‘X’
Private data:
head
prevPtr currPtr
NULL
![Page 48: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/48.jpg)
48
‘C’ ‘L’ ‘X’
Private data:
head
prevPtr currPtr
Finding Proper Position for ‘S’
![Page 49: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/49.jpg)
49
‘C’ ‘L’ ‘X’
Private data:
head
prevPtr currPtr
Finding Proper Position for ‘S’
![Page 50: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/50.jpg)
50
‘C’ ‘L’ ‘X’
Private data:
head
prevPtr currPtr
Inserting ‘S’ into Proper Position
‘S’
![Page 51: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/51.jpg)
51
void HybridList::Insert(/* in */ ItemType item) // Pre: item is assigned && components in ascending order// Post: new node containing item is in its proper place // && components in ascending order{ NodePtr currPtr; NodePtr prevPtr; NodePtr location;
location = new NodeType; location->item= item; prevPtr = NULL; currPtr = head; while (currPtr != NULL && item > currPtr->info ) {
prevPtr = currPtr; // Advance both pointers currPtr = currPtr->link;
}
location->link = currPtr;// Insert new node here if (prevPtr == NULL)
head = location; else
prevPtr->link = location;}
51
![Page 52: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/52.jpg)
52
void HybridList::InsertAsFirst(/* in */ ItemType item) // Pre: item is assigned && components in ascending order// Post: New node containing item is the first item in the list // && components in ascending order{
NodePtr newNodePtr = new NodeType;
newNodePtr -> item= item; newNodePtr -> link = head; head = newNodePtr;}Void HybridList::Print() const// Post: All values within nodes have been printed{ NodePtr currPtr = head; // Loop control pointer while (currPtr != NULL) { cout << currPtr->item << endl; currPtr = currPtr->link; }}
52
![Page 53: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/53.jpg)
53
void HybridList::RemoveFirst ( /* out */ ItemType& item) // Pre: list is not empty && components in ascending order// Post: item == element of first list node @ entry// && node containing item is no longer in list// && list components in ascending order
{ NodePtr tempPtr = head; // Obtain item and advance head item = head->item; head = head->link; delete tempPtr;}
53
![Page 54: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/54.jpg)
54
void HybridList::Delete (/* in */ ItemType item) // Pre: list is not empty && components in ascending order// && item == component member of some list node// Post: item == element of first list node @ entry// && node containing first occurrence of item no longer // in list && components in ascending order{ NodePtr delPtr; NodePtr currPtr; // Is item in first node?
if (item == head->item) { // If so, delete first node delPtr = head; head = head->link; } else {// Search for item in rest of list { currPtr = head; while (currPtr->link->item != item) currPtr = currPtr->link; delPtr = currPtr->link; currPtr->link = currPtr->link->link;
} delete delPtr;}
54
![Page 55: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/55.jpg)
55
Copy Constructor
Most difficult algorithm so farIf the original is empty, the copy is
empty Otherwise, make a copy of the head
with pointer to itLoop through original, copying each
node and adding it to the copy until you reach the end
See Chapter 18 for an easy, elegant solution by using recursion
![Page 56: 1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and](https://reader036.vdocuments.us/reader036/viewer/2022062722/56649f355503460f94c53e25/html5/thumbnails/56.jpg)
56
HybridList::HybridList(const SortedList2& otherlist){
NodePtr fromPtr;NodePtr toPtr;if (otherList.head == NULL){
head = NULL;return;
}fromPtr = otherList.head; //copy first nodehead = new NodeType;head->item = fromPtr->item;toPtr = head; //copy remaining nodesfromPtr=fromPtr->link;while (fromPtr != NULL){
toPtr->link = new NodeType;toPtr = toPtr->link;toPtr->item = fromPtr->item;fromPtr = fromPtr->link;
}toPtr->link = NULL;
}