190 likes | 337 Views
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.
E N D
Outline • Array Implementation • Singly linked lists • Doubly linked lists • Circular linked lists Data Structure: Lists
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
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
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
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
A2 A3 A4 Implementation header A1 List with a header header Empty list with a header Data Structure: Lists
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
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
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
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
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
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
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
A1 A4 A2 A3 Doubly Linked Lists Data Structure: Lists
A1 A4 A2 A3 Circular Linked Lists Data Structure: Lists
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
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
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