Linked Lists
Linked Lists
List
# length : int
+ List()
+ virtual ~List()
+ virtual append(const T&) : void = 0
+ virtual clear() : void = 0
+ virtual getElement(int) const : T = 0
+ virtual getLength() const : int = 0
+ virtual insert(int, const T&) : void = 0
+ virtual isEmpty() const : bool = 0
+ virtual remove(int) : void = 0
+ virtual replace(int, const T&) : void = 0
Array-Based Lists 5
Chapter 3 (Malik)
Array-Based Lists 6
• One natural choice for implementing the List ADT is the array data
structure
• Can create a new class that inherits from the List abstract class
• Implements all the methods using a fixed-size, dynamic array
ArrayList ADT 7
ArrayList
- buffer : T*
- maxSize : int
+ ArrayList(int = 100)
+ ArrayList(const ArrayList<T>&)
+ operator=(const ArrayList<T>&) : ArrayList<T>&
+ virtual ~ArrayList()
+ virtual append(const T&) override : void
- copy(const ArrayList<T>&) : void
+ virtual clear() override : void
+ virtual getElement(int) const override : T
+ virtual getLength() const override : int
+ getMaxSize() const : int
+ virtual insert(int, const T&) override : void
+ virtual isEmpty() const override : bool
+ isFull() const : bool
+ virtual remove(int) override : void
+ virtual replace(int, const T&) override : void
+ friend operator<<(ostream&, const ArrayList&) : ostream&
Length
8
0 1 2 3 4 5 6 7 8 9
Max Size
Array-Based List: Pros and Cons 9
Pros Cons
• There is no “wasted • Size must be
space” for individual predetermined
elements • Small lengths with large
• Memory is contiguous sizes means a lot of empty
• Makes random access fast space
• Memory is contiguous
• Makes insertion/removal
costly
Linked Lists 10
Chapter 5 (Malik)
Chapter 4 (Shaffer)
Nodes 11
9 4 0 13
9 4 0 13
9 4 0 13
9 4 0 13
curr = curr->next;
}
Appending to a Linked List 22
head
length = 0
24
head
length = 1
25
head
9 4
length = 2
26
head
9 4 0
length = 3
27
head
9 4 0 13
length = 4
Releasing the Memory in a Linked List 28
head
9 4 0 13
30
head
9 4 0 13
prev
31
head
9 4 0 13
prev
32
head
9 4 0 13
prev
33
head
9 4 0 13
prev
34
head
9 4 0 13
prev
35
head
9 4 0 13
prev
36
head
9 4 0 13
prev
37
head
9 4 0 13
prev
38
head
9 4 0 13
prev
39
head
9 4 0 13
prev
40
head
9 4 0 13
prev
41
head
9 4 0 13
prev
Inserting Into a Linked List 42
head
9 4 0 13
0 1 2 3
length = 4
Insert the value -7 to position 2 n
44
-7
head
9 4 0 13
0 1 2 3
length = 4
Insert the value -7 to position 2 n
45
-7
head curr
9 4 0 13
0 1 2 3
length = 4
Insert the value -7 to position 2 n
46
-7
head curr
9 4 0 13
0 1 2 3
prev
length = 4
Insert the value -7 to position 2 n
47
-7
head curr
9 4 0 13
0 1 2 3
prev
length = 4
Insert the value -7 to position 2 n
48
-7
head curr
9 4 0 13
0 1 2 3
prev
length = 4
Insert the value -7 to position 2 n
49
-7
head curr
9 4 0 13
0 1 2 3
prev
length = 4
Insert the value -7 to position 2 n
50
-7
head 2 curr
9 4 0 13
0 1 3 4
prev
length = 5
Inserting Into a Linked List 51
53
head
9 4 0 13
0 1 2 3
length = 4
Remove the value at position 2
[naïve version]
54
head
9 4 0 13
0 1 2 3
curr
length = 4
Remove the value at position 2
[naïve version]
curr->next = curr->next->next;
55
head
9 4 0 13
0 1 2
curr
length = 3
Remove the value at position 2
[naïve version]
56
head
9 4 0 13
0 1 2
curr
length = 3
Remove the value at position 2
[efficient version]
57
head
9 4 0 13
0 1 2 3
length = 4
Remove the value at position 2
[efficient version]
58
head
9 4 0 13
0 1 2 3
prev curr
length = 4
Remove the value at position 2
[efficient version]
59
head
9 4 0 13
0 1 2
prev curr
length = 3
Remove the value at position 2
[efficient version]
60
head
9 4 0 13
0 1 2
prev curr
length = 3
Removing From a Linked List 61
63
9 4 0 13
0 1 2 3
head
64
9 4 0 13
0 1 2 3
head
9
0
65
9 4 0 13
0 1 2 3
head curr
9
0
66
9 4 0 13
0 1 2 3
head curr n
9 4
0
67
9 4 0 13
0 1 2 3
head curr n
9 4
0 1
68
9 4 0 13
0 1 2 3
head n curr
9 4
0 1
69
9 4 0 13
0 1 2 3
head n curr
9 4
0 1
70
9 4 0 13
0 1 2 3
head curr n
9 4 0
0 1
71
9 4 0 13
0 1 2 3
head curr n
9 4 0
0 1 2
72
9 4 0 13
0 1 2 3
head n curr
9 4 0
0 1 2
73
9 4 0 13
0 1 2 3
head n curr
9 4 0
0 1 2
74
9 4 0 13
0 1 2 3
head curr n
9 4 0 13
0 1 2
75
9 4 0 13
0 1 2 3
head curr n
9 4 0 13
0 1 2 3
76
9 4 0 13
0 1 2 3
head n curr
9 4 0 13
0 1 2 3
77
9 4 0 13
0 1 2 3
head n curr
9 4 0 13
0 1 2 3
78
9 4 0 13
0 1 2 3
head n curr
9 4 0 13
0 1 2 3
Previous Next
Value
Link Link
Doubly Linked Lists 82
• A linked list that uses this new type of node is called a doubly
linked list
• Allows programmers to traverse the list in either direction
• Eliminates the need for a previous pointer variable in algorithms
• However, algorithms are slightly longer because we need to
maintain two links per node, instead of just one
Similarities and Differences 83
• Can connect the end of the linked list back to its beginning
head
9 4 0 13
Header & Trailer Pointers 88
header trailer
? ?
length = 0
90
header trailer
? 9 ?
length = 1
UnsortedList ADT 91
Pros Cons
• No empty space • Storage overhead
• No limit on size • Elements require
• Except OS limitations additional space
• Memory is non-contiguous • Value, next, previous,
• Makes insertion/removal etc.
fast • Memory is non-contiguous
• Makes random access
costly
Storage Space 107
• Suppose
• 𝑛 is the number of elements in a list
• 𝑃 is the size of the pointer
• 𝐸 is the size of the element
• 𝐷 is the maximum size in the array
• Array-based lists require 𝐷𝐸 space
• Linked lists require 𝑛(𝑃 + 𝐸) space