1 / 19

Lists

Lists. Outline. Array Implementation Singly linked lists Doubly linked lists Circular linked lists. List ADT. List of size N : A 1 , A 2 , …, A N If N=0, it is an empty list. Methods find insert remove makeEmpty findKth. Array Implementation. The size of the list must be known.

erica
Download Presentation

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

  2. Outline • Array Implementation • Singly linked lists • Doubly linked lists • Circular linked lists Data Structure: Lists

  3. List ADT List of size N: A1, A2, …, AN • If N=0, it is an empty list. Methods • find • insert • remove • makeEmpty • findKth Data Structure: Lists

  4. Array Implementation • The size of the list must be known. • For insert and remove, elements succeeding the inserted/deleted element must be moved down/up. Data Structure: Lists

  5. A2 A1 A2 A3 A4 A1 A3 A4 A6 A6 Equal to x ? Equal to x ? Equal to x ? Equal to x ? Equal to x ? Linked Lists node header find(x) Data Structure: Lists

  6. A1 A3 A4 A1 A4 A6 A6 p x x Linked Lists:insert and remove insert(x, p) A2 remove(x) A2 Data Structure: Lists

  7. A2 A3 A4 Implementation header A1 List with a header header Empty list with a header Data Structure: Lists

  8. Class ListNode class ListNode { //Constructors ListNode(Object theElement) { this(theElement, null); } ListNode(Object theElement, ListNode n) { element=theElement; next=n; } Object element; ListNode next; } Data Structure: Lists

  9. Class LinkedListItr public class LinkedListItr { LinkedListItr(ListNode the Node) { current=theNode; } public boolean isPastEnd() { return current==null; } public Object retrieve() { return isPastEnd()?null:current.element;} public void advance() { if (!isPastEnd()) current=current.next; } ListNode current; //current position } Data Structure: Lists

  10. Class LinkedList public class LinkedList { public LinkedList() { header = new ListNode(null); } public boolean isEmpy() { return header.next==null; } public void nakeEmpty() { header.next=null; } ... private ListNode header; } Data Structure: Lists

  11. Method find public LinkedLitItr find(Object x) { ListNode itr = header.next; while (itr!=null && itr.element.equals(x)) itr=itr.next; return new LinkedListItr(Itr); } Data Structure: Lists

  12. Method remove public void remove(Object x) { LinkedLisrItr p = findPrevious(x); if (p.current.next != null) // Bypass deleted node p.current.next = p.current.next.next; } Data Structure: Lists

  13. Method findPrevious public LinkedListItr findPrevious(Object x) { ListNode itr = header; while (itr.next != null && itr.next.element.equals(x)) itr =itr.next; return new LinkedListItr(itr); } Data Structure: Lists

  14. Method insert public void insert(Object x, LinkedListItr p) { if (p != null && p.current != null) p.current.next = new ListNode (x, p.current.next); } Data Structure: Lists

  15. A1 A4 A2 A3 Doubly Linked Lists Data Structure: Lists

  16. A1 A4 A2 A3 Circular Linked Lists Data Structure: Lists

  17. Application Polynomial ADT header 3x5 + x3 + 2x + 8 5 3 3 1 1 2 0 8 header 4x4 + 2x3 + 7x2 + 2 4 4 3 2 2 7 0 2 Data Structure: Lists

  18. Polynomial Addition header 3x5 + x3 + 2x + 8 5 3 3 1 1 2 0 8 header 4x4 + 2x3 + 7x2 + 2 4 4 3 2 2 7 0 2 3x5 4x4 3x3 7x2 2x 10 Data Structure: Lists

  19. Polynomial Multiplication header 3x2 + 3x+ 1 2 3 1 3 0 1 header 5x2 + 1 2 5 0 1 15x4 15x3 5x2 3x2 3x 1 Data Structure: Lists

More Related