Download presentation
Presentation is loading. Please wait.
Published byMelissa Lyons Modified over 6 years ago
1
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
2
Singly Linked Lists (§ 3.2)
Sequences 07/25/16 10:31 Singly Linked Lists (§ 3.2) A singly linked list is a sequence of nodes. Each node stores: an element, a link to the next node. head points to first node. next node elem head A B C D Singly Linked Lists 2
3
Inserting at the Head Allocate new node. Insert element.
Sequences 07/25/16 10:31 Inserting at the Head Allocate new node. Insert element. Make new node point to old head. Update head to point to new node. Singly Linked Lists 3
4
Removing the Head Make head point to its next node.
Sequences 07/25/16 10:31 Removing the Head Make head point to its next node. Free old first node. Singly Linked Lists 4
5
Inserting at the Tail Use tail pointer. Allocate new node.
Sequences 07/25/16 10:31 Inserting at the Tail Use tail pointer. Allocate new node. Insert element. Set next to null. Make tail node point to new node. Update tail. Singly Linked Lists 5
6
Sequences 07/25/16 10:31 Removing the Tail No constant-time way to update the tail to point to the previous node. Singly Linked Lists 6
7
Doubly Linked Lists (§ 3.3)
Sequences 07/25/16 10:31 Doubly Linked Lists (§ 3.3) Node stores pointers to prev and next nodes. Special trailer and header nodes. prev next elem node trailer header nodes/positions elements Doubly Linked Lists 7
8
Insertion Insert x before p. p a b c p a b c q x q p a b x c
Sequences 07/25/16 10:31 Insertion Insert x before p. p a b c p a b c q x q p a b x c Doubly Linked Lists 8
9
Insertion Algorithm Algorithm insert(p, e): {insert e before p}
Sequences 07/25/16 10:31 Insertion Algorithm Algorithm insert(p, e): {insert e before p} Create a new node v velement = e u = pprev vnext = p; pprev = v {link in v before p} vprev = u; unext = v {link in v after u} Doubly Linked Lists 9
10
Deletion Delete node p. p a b c d a b c p d a b c Doubly Linked Lists
Sequences 07/25/16 10:31 Deletion Delete node p. p a b c d a b c p d a b c Doubly Linked Lists 10
11
Deletion Algorithm Algorithm delete(p): u = pprev w = pnext
Sequences 07/25/16 10:31 Deletion Algorithm Algorithm delete(p): u = pprev w = pnext unext = w {linking out p} wprev = u Doubly Linked Lists 11
12
Performance A list of n elements takes O(n) space.
Sequences 07/25/16 10:31 Performance A list of n elements takes O(n) space. Inserting at a position takes O(1) time. Deleting at a position takes O(1) time. Finding an element takes O(n) time. Doubly Linked Lists 12
13
Array Implementation of Lists
Sequences 07/25/16 10:31 Array Implementation of Lists Represent a list with an array A of size N. Store the size of the list in a variable n. We will handle overflow soon. A 1 2 i n N Array Lists 13
14
Sequences 07/25/16 10:31 Insertion Make room for new element i by shifting forward the n i elements A[i], …, A[n 1]. In the worst case (i 0), this takes O(n) time. A 1 2 i n A 1 2 i n A o 1 2 i n Array Lists 14
15
Sequences 07/25/16 10:31 Removal Fill the hole left by element i by shifting backward the n i 1 elements A[i 1], …, A[n 1]. In the worst case (i 0), this takes O(n) time. A 1 2 n o i A 1 2 n i A 1 2 n i Array Lists 15
16
Dynamic Array List Insert at the head, which is element t of array S.
Sequences 07/25/16 10:31 Dynamic Array List Insert at the head, which is element t of array S. When S is full, copy it to a larger array then insert. How large? Incremental strategy: increase the size by a constant c. Doubling strategy: double the size. Algorithm insert(o) if t = S.length 1 A new array of size … for i 0 to n1 do A[i] S[i] S A n n + 1 S[n1] o Array Lists 16
17
Comparison of the Strategies
Sequences 07/25/16 10:31 Comparison of the Strategies We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n insertions. We start with an empty list represented by an array of size 1. The amortized time of an insertion is the average over the n operations, T(n)/n. Array Lists 17
18
Incremental Strategy Analysis
Sequences 07/25/16 10:31 Incremental Strategy Analysis We replace the array k = n/c times. The total time T(n) of a series of n insert operations is proportional to n + c + 2c + 3c + 4c + … + kc = n + c( … + k) = n + ck(k + 1)/2. Since c is a constant, T(n) is O(n + k2) = O(n2). The amortized time of an insertion is O(n). Array Lists 18
19
Doubling Strategy Analysis
Sequences 07/25/16 10:31 Doubling Strategy Analysis We replace the array k = log2 n times. The total time T(n) of n insertions is proportional to n …+ 2k = n 2k + 1 1 = 3n 1. T(n) is O(n). The amortized time is O(1). Array Lists 19
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.