Download presentation
Presentation is loading. Please wait.
Published byScot Payne Modified over 9 years ago
1
Linked List
2
List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique successor. Length The number of items in a list; the length can vary over time.
3
Abstract Data Type (ADT) A data type whose properties (domain and operations) are specified independently of any particular implementation. Logical (or ADT) level: abstract view of the domain and operations. Implementation level: specific representation of the structure to hold the data items, and the coding for operations.
4
4 Basic Kinds of ADT Operations Constructor/Destructor – creates/deletes a new instance (object) of an ADT. Transformer -- changes the state of one or more of the data values of an instance. Observer -- allows us to observe the state of one or more of the data values of an instance without changing them. Iterator -- allows us to process all the components in a data structure sequentially.
5
ADT List Operations Transformers –MakeEmpty( ) –InsertItem ( ) –DeleteItem( ) Observers –LengthIs( ) –RetrieveItem( ) Iterators –ResetList( ) –GetNextItem( ) change stateobserve stateprocess all
6
class List { public : void UnsortedType ( ) ; int LengthIs ( ) const ; void RetrieveItem (ItemType& item, bool& found ) ; void InsertItem ( ItemType item ) ; void DeleteItem ( ItemType item ); void ResetList ( ); void GetNextItem ( ItemType& item ); private : int length ; ItemTypeinfo[MAX_ITEMS] ; intcurrentPos ; } ; Class List use array to store data
7
Singly Linked List Diagram Pointer to front node ABC NULL Nodes Data carried in nodePointer to next node
8
Node from Shop Program Diagram A Item item;Node *link; class Node { public: Node(Item i); private: Item item; Node *link; };
9
Empty Insert Example (1 of 6) size prevcurpos 0 anItem A ?? void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting A at position 0 in empty list front 0
10
Empty Insert Example (2 of 6) size prevcurpos 0 anItem A ?? assert(pos >= 0 && pos <= size); size++; front 1
11
Empty Insert Example (3 of 6) size prevcurpos 0 anItem A ? if (pos == 0) { // Inserting at the front cur = front; front 1
12
Empty Insert Example (4 of 6) size prevcurpos 0 anItem A ? front = new Node(anItem); front 1 A itemlink ?
13
Empty Insert Example (5 of 6) size prevcurpos 0 anItem A ? front->link = cur; front 1 A itemlink
14
Empty Insert Example (6 of 6) size return; } front 1 A itemlink
15
Front Insert Example (1 of 6) front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D ?? void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting D at position 0 size 3
16
Front Insert Example (2 of 6) front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D ?? assert(pos >= 0 && pos <= size); size++; size 4
17
Front Insert Example (3 of 6) front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D ? if (pos == 0) { // Inserting at the front cur = front; size 4
18
Front Insert Example (4 of 6) front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D ? front = new Node(anItem); size 4 D itemlink ?
19
Front Insert Example (5 of 6) front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D ? front->link = cur; size 4 D itemlink
20
Front Insert Example (6 of 6) front ABC itemlinkitemlinkitemlink return; } size 4 D itemlink
21
Middle Insert Example (1 of 8) void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting D at position 2 front ABC itemlinkitemlinkitemlink prevcurpos 2 anItem D ?? size 3
22
Middle Insert Example (2 of 8) assert(pos >= 0 && pos <= size); size++; front ABC itemlinkitemlinkitemlink prevcurpos 2 anItem D ?? size 4
23
Middle Insert Example (3 of 8) prev = NULL; cur = front; front ABC itemlinkitemlinkitemlink prevcurpos 2 anItem D size 4
24
Middle Insert Example (4 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } First Iteration front ABC itemlinkitemlinkitemlink prevcurpos 1 anItem D size 4
25
Middle Insert Example (5 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } Second Iteration front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D size 4
26
Middle Insert Example (6 of 8) prev->link = new Node(anItem); front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D size 4 D ? itemlink
27
Middle Insert Example (7 of 8) prev->link->link = cur; front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D size 4 D itemlink
28
Middle Insert Example (8 of 8) } front ABC itemlinkitemlinkitemlink size 4 D itemlink
29
End Insert Example (1 of 8) void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting D at position 3 front ABC itemlinkitemlinkitemlink prevcurpos 3 anItem D ?? size 3
30
End Insert Example (2 of 8) assert(pos >= 0 && pos <= size); size++; front ABC itemlinkitemlinkitemlink prevcurpos 3 anItem D ?? size 4
31
End Insert Example (3 of 8) prev = 0; cur = front; front ABC itemlinkitemlinkitemlink prevcurpos 3 anItem D 0 size 4
32
End Insert Example (4 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } First Iteration front ABC itemlinkitemlinkitemlink prevcurpos 2 anItem D size 4
33
End Insert Example (5 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } Second Iteration front ABC itemlinkitemlinkitemlink prevcurpos 1 anItem D size 4
34
End Insert Example (6 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } Third Iteration front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D size 4
35
End Insert Example (7 of 8) prev->link = new Node(anItem); prev->link->link = cur; front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D size 4 D itemlink
36
End Insert Example (8 of 8) } front ABC itemlinkitemlinkitemlink size 4 D itemlink
37
Remove Example (1 of 7) void ShoppingList::remove(int pos) { Node *cur, *prev; Removing at position 1 front ABC itemlinkitemlinkitemlink prevcurpos 1 ?? size 3
38
Remove Example (2 of 7) assert(pos >= 0 && pos < size); size--; front ABC itemlinkitemlinkitemlink prevcurpos 1 ?? size 2
39
Remove Example (3 of 7) prev = NULL; cur = front; front ABC itemlinkitemlinkitemlink prevcurpos 1 size 2
40
Remove Example (4 of 7) while (pos > 0) { prev = cur; cur = cur->link; pos--; } front ABC itemlinkitemlinkitemlink prevcurpos 0 size 2 First iteration
41
Remove Example (5 of 7) prev->link = cur->link; front ABC itemlinkitemlinkitemlink prevcurpos 0 size 2
42
Remove Example (6 of 7) delete cur; front ABC itemlinkitemlinkitemlink prevcurpos 0 size 2
43
Remove Example (7 of 7) } front AC itemlinkitemlink size 2
44
Delete All Example (1 of 8) void ShoppingList::deleteAll() { Node *kil; front ABC itemlinkitemlinkitemlink kil ? size 3
45
Delete All Example (2 of 8) while (front != NULL) { kil = front; front = front->link; ABC itemlinkitemlinkitemlink kil frontsize 3 First iteration
46
Delete All Example (3 of 8) kil->link = NULL; delete kil; } ABC itemlinkitemlinkitemlink kil frontsize 3 First iteration
47
Delete All Example (4 of 8) while (front != NULL) { kil = front; front = front->link; BC itemlinkitemlink kil frontsize 3 Second iteration
48
Delete All Example (5 of 8) kil->link = NULL; delete kil; } BC itemlinkitemlink kil frontsize 3 Second iteration
49
Delete All Example (6 of 8) while (front != NULL) { kil = front; front = front->link; C itemlink kil frontsize 3 Third iteration
50
Delete All Example (7 of 8) kil->link = 0; delete kil; } C itemlink kil frontsize 3 Third iteration 0
51
Delete All Example (8 of 8) size = 0; } frontsize 0
52
Copy Constructors and Overloaded = Operators Copy constructors used to make copies when passing by value and returning by value in functions Operator = used for assignment Both have default behaviors if not user-defined, which is to do a bitwise copy of all member data The default behavior can be a problem when there are pointers in member data (shallow copying) Users should define their own copy constructor and overloaded operator = when they are creating a class with pointer data members
53
Deep Copying (1 of 3) front ABC 0 itemlinkitemlinkitemlinksize 3 slist1 slist2 front XY 0 itemlinkitemlinksize 2 Consider the following two ShoppingList objects What would the result of slist2 = slist1; be if the default copy behavior took place?
54
Deep Copying (2 of 3) front ABC 0 itemlinkitemlinkitemlinksize 3 slist1 slist2 front XY 0 itemlinkitemlinksize 3 slist2 becomes a “shallow copy” of slist1 slist2 doesn’t get a copy of slist1 data All references to slist2 data are lost (mem. leak)
55
Deep Copying (3 of 3) front ABC 0 itemlinkitemlinkitemlinksize 3 slist1 slist2 frontsize 3 Here is what slist2 would look like as a “deep copy” of slist1 Note that slist2 has its own set of data ABC 0 itemlinkitemlinkitemlink
56
Clone Example (1 of 8) void ShoppingList::clone(const ShoppingList &slist) { Node *cur1, *cur2; front ABC 0 itemlinkitemlinkitemlinksize 3 slist *this front XY 0 itemlinkitemlinksize 2 cur1 ? cur2 ?
57
Clone Example (2 of 8) this->deleteAll(); this->size = slist.size; front ABC 0 itemlinkitemlinkitemlinksize 3 front 0 size 3 cur1 ? cur2 ? slist *this
58
Clone Example (3 of 8) this->front = new Node(*(slist.front)); front ABC 0 itemlinkitemlinkitemlinksize 3 frontsize 3 cur1 ? cur2 ? A itemlink 0 slist *this
59
Clone Example (4 of 8) cur1 = this->front; cur2 = slist.front; front ABC 0 itemlinkitemlinkitemlinksize 3 frontsize 3 cur1cur2 A itemlink 0 slist *this
60
Clone Example (5 of 8) while (cur2->link) { cur2 = cur2->link; cur1->link = new Node(*cur2); cur1 = cur1->link; } front ABC 0 itemlinkitemlinkitemlinksize 3 frontsize 3 cur1cur2 A itemlink slist *this B itemlink 0 First iteration
61
Clone Example (6 of 8) while (cur2->link) { cur2 = cur2->link; cur1->link = new Node(*cur2); cur1 = cur1->link; } front ABC 0 itemlinkitemlinkitemlinksize 3 frontsize 3 cur1cur2 A itemlink slist *this B itemlink Second iteration C 0 itemlink
62
Clone Example (7 of 8) cur1 = cur2 = 0; front ABC 0 itemlinkitemlinkitemlinksize 3 frontsize 3 cur1 0 cur2 0 A itemlink slist *this B itemlink C 0 itemlink
63
Clone Example (8 of 8) } front ABC 0 itemlinkitemlinkitemlinksize 3 frontsize 3 A itemlink slist *this B itemlink C 0 itemlink
64
Doubly Linked List Each node has 2 pointers, one to the node before and one to the node after No longer limited to left-to-right pointer movement Typically, a rear pointer is employed to simplify reverse traversals front A datarlinkllink B datarlinkllink C datarlinkllink rear
65
Circular Linked List “Last” node’s link points at the “front” node Concept of “front” optional (alternative: “current”) cur A datalink B datalink C datalink E datalink D datalink
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.