Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.

Similar presentations


Presentation on theme: "© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data."— Presentation transcript:

1 © 2004 Goodrich, Tamassia Linked Lists1

2 © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node next elem node ABCD 

3 © 2004 Goodrich, Tamassia Linked Lists3 Linked List node in Java public class ListNode { Object elem; ListNode next; public ListNode (ListNode next, Object elem) {this.next = next; this.elem = elem; } public Object getElem() {return elem; } public void setElem(Object elem) {this.elem = elem; } public ListNode getNext() {return next; } public void setNext(ListNode next) {this.next = next; } }

4 © 2004 Goodrich, Tamassia Linked Lists4 Inserting at the Head 1. Allocate a new node 2. Insert new element 3. Have new node point to old head 4. Update head to point to new node

5 © 2004 Goodrich, Tamassia Linked Lists5 Removing at the Head 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node

6 © 2004 Goodrich, Tamassia Linked Lists6 Stack with a Singly Linked List We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time  t nodes elements

7 © 2004 Goodrich, Tamassia Linked Lists7 Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node

8 © 2004 Goodrich, Tamassia Linked Lists8 Queue with a Singly Linked List We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time f r  nodes elements

9 © 2004 Goodrich, Tamassia Linked Lists9 Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node

10 © 2004 Goodrich, Tamassia Linked Lists10 Doubly Linked List A doubly linked list is a more complex type of list that allows insertion in the middle Nodes store: element (sometimes combined into the node instead of a reference) link to the previous node link to the next node Special trailer and header nodes prevnext elem trailer header nodes/positions elements node

11 © 2004 Goodrich, Tamassia Linked Lists11 Insertion We visualize operation insertAfter(p, X), which returns position q ABXC ABC p ABC p X q pq

12 © 2004 Goodrich, Tamassia Linked Lists12 Deletion We visualize remove(p), where p = last() ABCD p ABC D p ABC


Download ppt "© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data."

Similar presentations


Ads by Google