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, forming a linear ordering. Each node stores Element (data) link to the next node next elem node ABCD 

3 © 2004 Goodrich, Tamassia Linked Lists3 Empty Linked List An empty linked list is defined by: Head = Tail = Null List with Single Node Head = Tail ≠Null  Head Tail

4 © 2004 Goodrich, Tamassia Linked Lists4 The Node Class for List Nodes public classNode{ // Instance variables: private Object element; private Node next; /** Creates a node with null references to its element and next node. */ public Node(){ this(null, null); } /** Creates a node with the given element and next node. */ public Node(Object e, Node n) { element = e; next = n; } // Accessor methods: public Object getElement() { return element; } public Node getNext() { return next; } // Modifier methods: public void setElement(Object newElem) { element = newElem; } public void setNext(Node newNext) { next = newNext; }

5 © 2004 Goodrich, Tamassia Linked Lists5 Inserting at the Head 1. Allocate a new node 2. Insert new element- data 3. Have new node point to old head 4. Update head to point to new node 5. Increment size of list

6 © 2004 Goodrich, Tamassia Linked Lists6 Algorithm for Inserting at Head new (T);//Create a new Node T.data = A;// store element data T.next = Head; //new node points to old Head node If (Head = Null) then Tail = T// we have a single node-list Head = T;// Update Head to point to the new node

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

8 © 2004 Goodrich, Tamassia Linked Lists8 Algorithm for Inserting at Tail of List new (T); //Create a new Node T.data = A; // store element data T.next = Null; //new node points to Null object If (Head = Null) then Head = T // we have a single else Tail.next = T; node-list Tail= T;// Update Tail to point to the new node

9 © 2004 Goodrich, Tamassia Linked Lists9 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 3. Decrement size of list

10 © 2004 Goodrich, Tamassia Linked Lists10 Algorithm for Removing at the Head If (Head = Null) then Output “error” //empty list else { T = Head; // temporary pointer y = T.data; //save elem data Head = T.next; //update Head If (Head = Null) then Tail = Null; //test for single node Delete(T);//Garbage collection }//End of Algorithm Notice That: Any Removal Algorithm consists of a single statement.

11 © 2004 Goodrich, Tamassia Linked Lists11 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

12 © 2004 Goodrich, Tamassia Linked Lists12 Algorithm for Removing at the Tail We must cycle through the list (link hopping), starting at the Head, to determine the new Tail-node. If (Head = Null) then Output “error” //empty list else { T = Head; // temporary pointer If (Head=Tail) then {Head=Null; T=Null} // test for single node else { while T.next≠Tail do T=T.next; T.next = Null} y = Tail.data; //save elem data Delete(Tail);//Garbage collection Tail = T; //update Tail }//End of Algorithm

13 © 2004 Goodrich, Tamassia Linked Lists13 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

14 © 2004 Goodrich, Tamassia Linked Lists14 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


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