Linked List A group of data which is linked each other
Linked List via Array Linked List ADT Linked List Iteration ADT Ordered List ADT Sorted List ADT Double Linked List ADT Circular Linked List ADT
List Representation Via Array Difficult to organize a2a2 a1a1 unusedanan A n-1 A n-2 … a3a n-2 n-1 n m Size n
a2a2 a1a1 a9a9 A8A8 A7A7 a3a a4a4 a5a5 a6a6 a4a4 List Using Array a2a2 a1a1 a3a Insertion a4a4 a4a4 Insert(‘new’, 4) Shift right 1 position Position to insert a2a2 a1a1 a9a9 A8A8 A7A7 a3a a4a4 a5a5 a6a6 ‘new’a9a9 A8A8 A7A7 a6a6 a5a5
a2a2 a1a1 a9a9 a8a8 a7a7 a3a a4a4 a5a5 a6a6 ‘new’ List Using Array Deletion delete(7) Shift left 1 position Position to delete a2a2 a1a1 a9a9 a8a8 a7a7 a3a a4a4 a5a5 ‘new’a2a2 a1a1 a9a9 a8a8 a7a7 a3a a4a4 a5a5 a6a6 ‘new’
Linked-list Insertion/deletion is slow cause it has to shift items in contiguous memory HIGH COST Items is not need to be contiguous
List Node a Element next class ListNode{ Object element; ListNode next; ListNode(Object obj) { element = obj; next = null; } ListNode(Object obj, ListNode node) { element = obj; next = node; }
List Node a1a1 a2a2 a3a3 head a4a4 null ListNode node4 = new ListNode(a4);// new ListNode(a4, null); ListNode node3 = new ListNode(a3, node4); ListNode node2 = new ListNode(a2, node3); ListNode head = new ListNode(a1, node2);
Linked-List Linked-List ADT a1a1 a2a2 a3a3 head a4a4 null LinkedList() addHead(obj) isEmpty() deleteHead() retrieveHead() getHead()
Linked-List list Linked-List LinkedList list = new LinkedList(); list.add(a4); list.add(a3);// list.insert(a3,a4); list.add(a2); // list.insert (a2,a3); list.add(a1); a1a1 a2a2 a3a3 head a4a4 LinkedList()
Linked-List Inserting Node a1a1 a2a2 a3a3 a4a4 head current X obj temp
Linked-List Inserting Node We need a help to point node before inserted node (say it ‘current’) Inserted node is named newNode a1a1 a2a2 a4a4 head current X obj newNode public void insertNext (Object obj) { ListNode newNode; if (current != null) { newNode = new ListNode(obj, current.next); current.next = newNode; } else if (zeroflag) head = new ListNode(obj,head); else {throw new ItemNotFound(“insert fails”)} }
Linked-List Deleting Node We need a help to point node before deleted node (say it ‘current’) Deleted node is node after current a1a1 a2a2 a4a4 head current public void deleteNext (Object obj) { current = head; while (current.element != obj && current.next != null) current = current.next; if (current.next != null) { ListNode temp = current.next;// code in C++ current.next = current.next.next; release(temp); // code in C++ } else {throw new ItemNotFound(“delete fails”)} } a3a3 X temp
Linked-List Linked-List Iteration ADT LinkedListItr() zeroth() retrieve() deleteNext() isLast() isInList() first() advance() insertNext(obj) Linked-ListItr current a1a1 a2a2 a3a3 head a4a4 null acces and change nodes acces and change current pointer
Linked-List Inserting Node in Linked List Iteration We have had current that points to a node before inserted position Node is inserted after current position Inserted node is named newNode a1a1 a2a2 a4a4 head current X obj newNode public void insertNext (Object obj) { ListNode newNode; if (isInList()) { newNode = new ListNode(obj, current.next); current.next = newNode; } else if (zeroflag) head = new ListNode(obj,head); else {throw new ItemNotFound(“insert fails”)} }
Linked-List Deleting Node in Linked List Iteration Deleted node is node after current a1a1 a2a2 a4a4 head current public void deleteNext () { current = head; while (current.element != obj && current.next != null) current = current.next; if (current.next != null) { ListNode temp = current.next;// code in C++ current.next = current.next.next; release(temp); // code in C++ } else {throw new ItemNotFound(“delete fails”)} } a3a3 X temp
Retrieve Next Element in Linked List Iteration Linked-List public int retrieveNext() { if (current.next() <>null)//if(isInList()) return current.next.element; else return null; }
OrderedList Ordered List ADT list OrderedList () insert (obj, index) delete(index) retrieve (index) length() Linked-List Iteration ADT
Ordered List Implementation Linked-List class OrderedList ( private LinkedListItr list; private int pos=0; public OrderedList() {list=new LinkedListItr ();} public int length() { int count=0; for (list.first(); list.isInList(); list.advance()) {count++;} return count; } public int retrieve(int i) { if (this.setPos(i)) return list.retrieve(); else return null; } private setPos(int i) { if (pos>i) {list.first(); pos=0;} for (int i=i-pos; list.isInList(); list.advance()) { pos++; } public advance() { if (! list.isLast()) { list.advance(); pos++; }
OrderedList Sorted List ADT list SortedList () insert (obj) delete(index) retrieve (index) length() Linked-List Iteration ADT