lec6 mod linked list
TRANSCRIPT
![Page 1: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/1.jpg)
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 2: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/2.jpg)
To implement the List ADT
The programmer must
1) choose a concrete data representation for the list, and
2) implement the list operations
![Page 3: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/3.jpg)
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 4: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/4.jpg)
List Operations
Transformers Insert Delete Sort
Observers IsEmpty IsFull Length IsPresent
change state
observe state
4
![Page 5: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/5.jpg)
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 6: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/6.jpg)
Array-based class List
Reset
IsFull
Length
IsPresent
Delete
IsEmpty
Insert
GetNexItem
Private data:
lengthdata [0] [1] [2]
[MAX_LENGTH-1]
currentPos
SelSort
![Page 7: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/7.jpg)
// 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 members int length; // Number of values currently stored ItemType data[MAX_LENGTH];
int CurrentPos; // Used in iteration };
7
![Page 8: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/8.jpg)
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 9: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/9.jpg)
Implementation Possibilities for a List
List
Linked listBuilt-in array
Built-in dynamic data and pointers
Built-in arrayof structs
![Page 10: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/10.jpg)
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 11: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/11.jpg)
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 12: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/12.jpg)
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 13: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/13.jpg)
// Type declarations struct NodeType { char info; NodeType* link;}
typedef NodeType* NodePtr;
// Variable DECLARATIONSNodePtr head;NodePtr ptr;
13
Declarations for a Dynamic Linked List
. info . link
‘A’ 6000
![Page 14: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/14.jpg)
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 15: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/15.jpg)
15
ptr is a pointer to a node
. info . link
‘A’ 6000 ptr
ptr
![Page 16: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/16.jpg)
16
*ptr is the entire node pointed to by ptr
ptr
. info . link
‘A’ 6000
*ptr
![Page 17: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/17.jpg)
17
ptr->info is a node member
ptr
. info . link
ptr->info
(*ptr).info // Equivalent
‘A’ 6000
![Page 18: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/18.jpg)
18
ptr->link is a node member
ptr
. info . link
ptr->link
(*ptr).link // Equivalent
‘A’ 6000
![Page 19: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/19.jpg)
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = 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 20: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/20.jpg)
// Pre: head points to a dynamic linked list
ptr = 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 21: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/21.jpg)
// Pre: head points to a dynamic linked list
ptr = 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 22: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/22.jpg)
// Pre: head points to a dynamic linked list
ptr = 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 23: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/23.jpg)
// Pre: head points to a dynamic linked list
ptr = 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 24: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/24.jpg)
// Pre: head points to a dynamic linked list
ptr = 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 25: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/25.jpg)
// Pre: head points to a dynamic linked list
ptr = 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 26: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/26.jpg)
// Pre: head points to a dynamic linked list
ptr = 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 27: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/27.jpg)
// Pre: head points to a dynamic linked list
ptr = 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 28: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/28.jpg)
// Pre: head points to a dynamic linked list
ptr = 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 29: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/29.jpg)
// Pre: head points to a dynamic linked list
ptr = 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 30: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/30.jpg)
// Pre: head points to a dynamic linked list
ptr = 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 31: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/31.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
31
![Page 32: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/32.jpg)
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 33: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/33.jpg)
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 34: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/34.jpg)
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 35: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/35.jpg)
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 36: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/36.jpg)
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 37: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/37.jpg)
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 38: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/38.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
38
![Page 39: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/39.jpg)
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 40: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/40.jpg)
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;tempPtr = head;head = head->link;delete tempPtr;
head ‘B’ ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
![Page 41: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/41.jpg)
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;tempPtr = head;head = head->link;delete tempPtr;
head ‘B’ ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
![Page 42: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/42.jpg)
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;tempPtr = head;head = head->link;delete tempPtr;
head ‘B’ ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
![Page 43: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/43.jpg)
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;tempPtr = head;head = head->link;delete tempPtr;
head ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
![Page 44: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/44.jpg)
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 45: Lec6 mod linked list](https://reader036.vdocuments.us/reader036/viewer/2022062405/55509918b4c90595208b475b/html5/thumbnails/45.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
45