1 / 12

Linked Lists

Linked Lists. 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. node. elem. . A. B. C. D. Linked List node in Java. public class ListNode { Object elem ;

signa
Download Presentation

Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists Linked Lists

  2. 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 node elem  A B C D Linked Lists

  3. Linked List node in Java public classListNode{ 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;} } Linked Lists

  4. Allocate a new node Insert new element Have new node point to old head Update head to point to new node Inserting at the Head Linked Lists

  5. Removing at the Head • Update head to point to next node in the list • Allow garbage collector to reclaim the former first node Linked Lists

  6. 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 nodes t  elements Linked Lists

  7. Inserting at the Tail • Allocate a new node • Insert new element • Have new node point to null • Have old last node point to new node • Update tail to point to new node Linked Lists

  8. 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 r nodes f  elements Linked Lists

  9. 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 Linked Lists

  10. Doubly Linked List prev next • 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 elem node trailer nodes/positions header elements Linked Lists

  11. Insertion p • We visualize operation insertAfter(p, X), which returns position q A B C p q A B C X p q A B X C Linked Lists

  12. p A B C D Deletion • We visualize remove(p), where p = last() A B C p D A B C Linked Lists

More Related