Arrays and Linked Lists: Data Structures and Algorithms
Arrays and Linked Lists: Data Structures and Algorithms
Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++ Goodrich, Tamassia and Mount (Wiley, 2004)
Outline
Arrays Singly Linked Lists Doubly Linked Lists
Linked Lists
Arrays
Built-in in most programming languages Two kinds (programmer responsibility):
Unordered: attendance tracking Ordered: high scorers
Operations:
Insertion Deletion Search
Linked Lists
Arrays
Operation Insertion Deletion Search Unordered O(1) O(n) O(n) Ordered O(n) O(n) O(logn)
Linked Lists
Linked Lists
Linked Lists
Position ADT
The Position ADT models the notion of place within a data structure where a single object is stored It gives a unified view of diverse ways of storing data, such as
a cell of an array a node of a linked list
Linked Lists
List ADT
The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods:
size(), isEmpty()
Accessor methods:
first(), last() prev(p), next(p)
Update methods:
replace(p, e) insertBefore(p, e), insertAfter(p, e), insertFirst(e), insertLast(e) remove(p)
Linked Lists
node
C
Linked Lists
D
9
Node struct
struct Node { // Instance variables Object* element; Node* next; // Initialize a node Node() { this(null, null); } Node(Object* e, Node* n) element = e; next = n; } }
Linked Lists 10
next
element
node
Linked Lists
11
head
1. Allocate a new node 2. Insert new element 3. Make new node point to
old head
head
head
Linked Lists
12
Algorithm addFirst
Algorithm addFirst(v) Input node v to be added to the beginning of the list Output v.setNext (head) head v size size + 1
{make v point to the old head node} {make variable head point to new node} {increment the node count}
Linked Lists
13
head
1. 2.
Update head to point to next node in the list Disallocate the former first node
the garbage collector to reclaim (Java), or the programmer does the job (C/C++)
Linked Lists
head
14
Algorithm removeFirst
Algorithm removeFirst() if head = null then Indicate an error: the list is empty t head head head.getNext() {make head point to next node or null} Disallocate node t {null out t's next pointer or free t's memory} size size - 1 {decrement the node count}
Linked Lists
15
Rome struct SLinkedListWithTail { Node* head; // head node Node* tail; // tail node of the list Seattle Toronto
/* Default constructor that creates an empty list */ SLinkedListWithTail() { head = null; tail = null; } // ... update and search methods would go here ... }
Linked Lists
16
Rome head
Seattle
Toronto tail
head
tail
Linked Lists
17
Algorithm addLast
Algorithm addLast(v) Input node v to be added to the end of the list Output v.setNext (NULL) {make new node v point to null object} tail.setNext(v) {make old tail node point to new node} tail size; {make variable tail node point to new node} size size + 1 {increment the node count}
Linked Lists
18
head
tail
Linked Lists
19
prev
next
elem
node
elements
Linked Lists 20
prev
next
Node struct
elem node
/* Node of a doubly linked list of strings */ struct DNode { string* element; DNode *next, *prev; // Pointers to next and previous node /* Initialize a node. */ DNode(string* e, DNode* p, DNode *n) { element = e; prev = p; next = n; } string* getElement() { return element; } };
Linked Lists 21
Linked Lists
22
Insertion
We visualize operation insertAfter(p, X), which returns position q
A p A p A
B X q B
Linked Lists
C
23
Insertion Algorithm
Algorithm insertAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p) v.setNext(p.getNext()) (p.getNext()).setPrev(v) p.setNext(v) return v
{ link v to its predecessor } { link v to its successor } { link ps old successor to v } {link p to its new successor, v } {the position for the element e }
Linked Lists
24
Deletion
We visualize remove(p), where p == last()
p A B C D
p D
B
Linked Lists
C
25
Deletion Algorithm
Algorithm remove(p): t = p.element {temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()) {linking out p} (p.getNext()).setPrev(p.getPrev()) Disallocate node p {invalidating the position p} return t
Linked Lists
26
Linked Lists
27