0% found this document useful (0 votes)
8 views107 pages

Linked Lists

Uploaded by

anaamaysingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views107 pages

Linked Lists

Uploaded by

anaamaysingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 107

Lists

Chapters 3 and 4 (Malik)


Chapter 4 (Shaffer)
Lists 2
Chapter 3 (Malik)
List Definitions 3

• A list is a sequence of data


• We call these data the elements of a list
• Every element in a list has a unique position
• A list is empty if it contains no elements
• The number of elements in a list is the length of the list
List ADT 4

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

Hi there my name is Jin

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

• For linked lists, elements are called nodes


• A node consists of (at least) two things
1. A value Value Link
2. A link
12

Value Link Value Link Value Link Value Link

Node 0 Node 1 Node 2 Node 3


13
14

9 4 0 13

node0 node1 node2 node3


15

9 4 0 13

node0 node1 node2 node3


16

9 4 0 13

node0 node1 node2 node3


17

9 4 0 13

node0 node1 node2 node3


Linked Lists 18

• A linked list is a sequence of connected nodes


• Head is the first node in the sequence
• Last is the last node in the sequence
• Tail is the sequence of nodes after the head
• Init is the sequence of nodes before the last
19

Learn You a Haskell for Great Good!. Lipovača, Miran. http://learnyouahaskell.com/starting-out#an-intro-to-lists


Traversing Through a Linked List 20

• Many linked list operations require traversing the nodes


• “Follow the links”
• Assume we only have access to the head
• Start at the head to visit every node in the list
• Be careful not to override the head!
Beginning to End 21

Node* curr = head;


while (curr != nullptr) {
// do something

curr = curr->next;
}
Appending to a Linked List 22

• Appending is one way to add new elements to the linked list


• The new element is added to the end of the list
• When thinking of the algorithm
• Start with the empty scenario
• Then try appending one element at a time
• Adjust your algorithm as needed
23

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

• All nodes in a linked list are using dynamic memory


• To avoid memory leaks, we need to ensure we release that
memory in the destructor
• The algorithm uses the concept of a previous pointer
29

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

• Insertion is the process of adding a new element into an existing


position of the list
• Increases the length by 1
• Shifts the elements after the position to the right
43

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

• There is still one other scenario to consider


• Inserting a new element to position 0
• This will affect the head
Removing From a Linked List 52

• Removing is the process of deleting an existing element from the


list at a given position
• Decreases the length by 1
• Shifts the elements after the position to the left
Remove the value at position 2
[naïve version]

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

• There are still two other scenarios to consider:


1. Removing the element in position 0 when the length is 1
2. Removing the element in position 0 when the length is greater than 1
• Both scenarios have to do with updating the head
Copying a Linked List 62

• Copying one linked list to another means copying the state


• Need to be careful since nodes are dynamic
• Requires keeping track of your positions in both linked lists
head length = 4 Copy Object

63
9 4 0 13
0 1 2 3

head

New Object length = 0


head length = 4 Copy Object

64
9 4 0 13
0 1 2 3

head

9
0

New Object length = 1


head copy length = 4 Copy Object

65
9 4 0 13
0 1 2 3

head curr

9
0

New Object length = 1


head copy length = 4 Copy Object

66
9 4 0 13
0 1 2 3

head curr n

9 4
0

New Object length = 1


head copy length = 4 Copy Object

67
9 4 0 13
0 1 2 3

head curr n

9 4
0 1

New Object length = 2


head copy length = 4 Copy Object

68
9 4 0 13
0 1 2 3

head n curr

9 4
0 1

New Object length = 2


head length = 4 copy Copy Object

69
9 4 0 13
0 1 2 3

head n curr

9 4
0 1

New Object length = 2


head length = 4 copy Copy Object

70
9 4 0 13
0 1 2 3

head curr n

9 4 0
0 1

New Object length = 2


head length = 4 copy Copy Object

71
9 4 0 13
0 1 2 3

head curr n

9 4 0
0 1 2

New Object length = 3


head length = 4 copy Copy Object

72
9 4 0 13
0 1 2 3

head n curr

9 4 0
0 1 2

New Object length = 3


head length = 4 Copy Object copy

73
9 4 0 13
0 1 2 3

head n curr

9 4 0
0 1 2

New Object length = 3


head length = 4 Copy Object copy

74
9 4 0 13
0 1 2 3

head curr n

9 4 0 13
0 1 2

New Object length = 3


head length = 4 Copy Object copy

75
9 4 0 13
0 1 2 3

head curr n

9 4 0 13
0 1 2 3

New Object length = 4


head length = 4 Copy Object copy

76
9 4 0 13
0 1 2 3

head n curr

9 4 0 13
0 1 2 3

New Object length = 4


head length = 4 Copy Object
copy

77
9 4 0 13
0 1 2 3

head n curr

9 4 0 13
0 1 2 3

New Object length = 4


head length = 4 Copy Object
copy

78
9 4 0 13
0 1 2 3

head n curr

9 4 0 13
0 1 2 3

New Object length = 4


Doubly Linked Lists 79
Chapter 5 (Malik)
Chapter 4 (Shaffer)
So Far . . . 80

• Nodes have two attributes


• Value Value Link
• Pointer that links to the next one
• Linked lists that use this kind of node are called singly linked lists
New Node Definition 81

• Can modify the node definition to include two pointers


• One pointer for the next node
• Another pointer for the previous node

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

• Forward traversal still works the same as a singly linked list


• Start at the head and just “follow the links”
• The last node in the list is the only one whose next pointer is
empty
• Just like singly linked lists
• The head is the only node with its previous pointer as empty
• Different from singly linked lists
More Linked List Variations! 84
Chapter 5 (Malik)
Chapter 4 (Shaffer)
Linked Lists with Last Pointers 85

• Implementations thus far only used a head


• Can modify the linked list to also keep track of its last
• Can simplify some algorithms
• At the small cost of additional storage
Memory-Efficient Doubly Linked Lists 86

• Possible to create a doubly linked list without the using the


modified node (next and previous links)
• Saves space, but makes code slower
• An example of space/time tradeoff
• Each node saves both previous and next addresses in one variable
• Link = Previous + Next
• To traverse forward, subtract the previous address
• To traverse backward, subtract the next address
Circular Linked Lists 87

• Can connect the end of the linked list back to its beginning

head

9 4 0 13
Header & Trailer Pointers 88

• So far, many algorithms have had edge cases


• These have to do with the head
• Possible to simplify these algorithms
• Header pointer is a special head that is never removed
• Trailer pointer is a special last that is never removed
89

header trailer

? ?

length = 0
90

header trailer

? 9 ?

length = 1
UnsortedList ADT 91

• Elements in the list are in no particular order


• Just a list with some additional functionality
• Searching for a value in the list
• Removing a value from the list
SortedList ADT 92

• Elements in the list are in a particular order


• Ascending (smallest to largest)
• Descending (largest to smallest)
• Due to the existence of order, some list functionality can no longer be
allowed
• Appending a value at the end of the list
• Inserting a value at a specific position in the list
• Replacing the value at a specific position with another value
• Additional functionality
• Searching for a value in the list
• Inserting a value into the list
• Removing a value from the list
Freelists 93

• Allocating and deallocating memory is expensive


• Can reduce time costs by keeping “deallocated” memory in
another linked list (called the freelist)
• But, this increases the space usage
• Another example of space/time tradeoff
• When allocation is requested, check if the freelist has any nodes
available
• If not, need to do actual memory allocation
• When deallocating, instead of deleting the memory, just append it
to the freelist
Unsorted Linked Lists 94
Chapter 5 (Malik)
Unsorted Linked Lists 95

• Elements in the list are in no particular order


• Just a linked list with some additional functionality
• Searching for a value in the list
• Removing a value from the list
Searching by Value 96

• Traverse the unsorted linked list starting at the head


• If a node is found with a matching value, then success
• Otherwise, if the traversal is over, then failure
Removing by Value 97

• Traverse the unsorted linked list starting at the head


• If a node is found with a matching value, then remove it
• But the above only works if the value is in the list
Sorted Linked Lists 98
Chapter 5 (Malik)
Sorted Linked Lists 99

• Elements in the list are in a particular order


• Ascending (smallest to largest)
• Descending (largest to smallest)
• Due to the existence of order, some list functionality can no longer be
allowed
• Appending a value at the end of the list
• Inserting a value at a specific position in the list
• Replacing the value at a specific position with another value
• Additional functionality
• Searching for a value in the list
• Inserting a value into the list
• Removing a value from the list
Searching by Value 100

• Traverse the sorted linked list starting at the head


• If a node is found with a matching value, then success
• In ascending mode
• If the node’s value is larger than the target, then failure
• In descending mode
• If the node’s value is smaller than the target, then failure
• Otherwise, if the traversal is over, then failure
Inserting by Value 101

• Keep track of the current and previous nodes


• Traverse the sorted linked list until either
1. The end of the list is reached
2. In ascending mode
a. The current node’s value is larger than the element
3. In descending mode
a. The current node’s value is smaller than the element
• Link the nodes correctly
• Increment the length of the list
Removing by Value 102

• Equivalent logic to the unsorted version


The STL list 103
Chapter 5 (Malik)
The list Sequence Container 104

• C++ has their own implementation of the linked list available


• Their implementation is a doubly linked list
• In practice, use their list class since it has optimizations built-in
• http://www.cplusplus.com/reference/list/list/
• They also have a singly linked list implementation called the
forward_list
• http://www.cplusplus.com/reference/forward_list/forward_list/
Linked List Analysis 105
Chapter 5 (Malik)
Chapter 4 (Shaffer)
Linked List: Pros and Cons 106

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

You might also like