Presentation is loading. Please wait.

Presentation is loading. Please wait.

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1.

Similar presentations


Presentation on theme: "IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1."— Presentation transcript:

1 IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Implementation Of Linked-Lists

2 2 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Outline Linked list nodes Linked list operations Insertion Append Deletion Linked list representation & implementation Other types of linked lists Sorted Doubly-linked Circular

3 3 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 ListNode Linked Lists Stores a collection of items non-contiguously. Each item in the list is stored with an indication of where the next item is. Must know where first item is. The list will be a chain of objects, called nodes, of type ListNode that contain the data and a reference to the next ListNode in the list. Allows addition or deletion of items in the middle of collection with only a constant amount of data movement. Contrast this with array. A0A1A2A3 first ListNode

4 4 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 ListNode: Definition public class ListNode { DataType data; ListNode next; // constructors ListNode(DataType d, ListNode n) { data = d; next = n; } ListNode(DataType d) { this (d, null); } ListNode() { this (null); } }

5 5 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 a Linked List: Insertion Insert X immediately after current position a current b c d b c d x

6 6 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode (); a current b tmp

7 7 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode (); // place x in the element field tmp.data = x; a current b tmp x

8 8 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode (); // place x in the element field tmp.data = x; // x ’ s next node is b tmp.next = current.next; a current b tmp x

9 9 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode (); // place x in the element field tmp.data = x; // x ’ s next node is b tmp.next = current.next; // a ’ s next node is x current.next = tmp; a current b tmp x

10 10 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Implementing Insertion: Shorter Version A shorter version: // create a new node tmp = new ListNode (x,current.next); // a ’ s next node is x current.next = tmp; a current b tmp x

11 11 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Implementing Insertion: Shorter Version A shorter version: // create a new node tmp = new ListNode (x,current.next); // a ’ s next node is x current.next = tmp; a current b tmp x

12 12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Implementing Insertion: Shortest Version An even shorter version: // create a new node current.next = new ListNode (x,current.next); a current b x

13 13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Insert X immediately at the end of the list // last refers to the last node in the linked list last.next = new ListNode (); last = last.next;// adjust last last.data = x;// place x in the node last.next = null;// adjust next Most efficient approach last = last.next = new ListNode (x, null); Implementing Append a b c d last a b c X d

14 14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Implementing Basic Deletion Delete an item immediately after current position Basic deletion is a bypass in the linked list. a b x a b current

15 15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Implementing Basic Deletion Need a reference to node prior to the one to be deleted. current.next = current.next.next; a b x a b x a b current

16 16 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Important Note Remember!!! What we store in a ListNode is a reference to the object, NOT the object itself nor a copy of the object!

17 17 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Iterate Through The Linked List If items are stored in contiguous array: //step through array, outputting each item for (int index = 0; index < a.length; index++) System.out.println (a[index]); If items are stored in a linked list: // step through list, outputting each item for(ListNode p=l.first; p!=null; p=p.next) System.out.println (p.data); A0A1A2A3 first

18 18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Implementing a Linked List So what would a Linked List implementation look like? What happens if we want to Delete the first item? Insert an item before the first item? class MyLinkedList { // Field ListNode first; // Methods void insert(DataType x, ???); void delete(DataType x, ???); void append(DataType x);... }

19 19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Header Nodes Deletion of first item and insertion of new first item are special cases. Can be avoided by using header node; contains no data, but serves to ensure that first "real" node in linked has a predecessor. To go to the first element, set current to header.next; List is empty if header.next == null; Searching routines will skip header. A1A2A3 header

20 20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Linked Lists: List Implementation (partial) public class LinkedList implements List { ListNode header; // Constructor public LinkedList() { header = new ListNode (null); } // Test if the list is logically empty. public boolean isEmpty( ) { return header.next == null; } // Make the list logically empty public void makeEmpty( ) { header.next = null; } }

21 21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Representing the “current” position How do we specify where an operation should occur? Index position (int?) ListNode // Methods void insert(DataType x, ???); void delete(DataType x, ???); void insert(DataType x, int current); void delete(DataType x, int current); void insert(DataType x, ListNode current); void delete(DataType x, ListNode current); a x current

22 22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 List Iterator Class Maintains a notion of the current position (aka cursor). The List class provides methods that do not depend on any position (such as isEmpty, and makeEmpty ). A List iterator ( ListItr ) provides other methods such which act on the current position stored in the iterator: next() / advance() hasNext() / isValid() retrieve()

23 23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Linked List Iterator: Implementation public class ListItr { ListNode current; // Current position ListItr(ListNode node) { } public boolean hasNext() { } public void next() { } public DataType retrieve() { } }

24 24 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Example Usage A method that computes the number of elements in any list: public static int listSize (List theList) { int size = 0; ListItr itr; for(itr=theList.first();itr.hasNext();itr.next()) size++; return size; }

25 25 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Linked Lists: Implementation public class List { // Header node private ListNode header; // Check if list is empty boolean isEmpty() {???} // Make the list empty void makeEmpty () {???} // Cursor to header node public ListItr zeroth() {???} // Cursor to first node public ListItr first() {???} // Cursor to (first) node containing x public ListItr find(T x) {???} // Cursor to node before node containing x public ListItr findPrevious(T x) {???} // Insert x after current cursor position public void insert(T x, ListItr current) {???} // Remove (first) node containing x public void remove(T x) {???} }

26 26 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Java Implementations Mostly straightforward; all routines are short. ListItr maintains a reference to the list that it is bound to as a private data member. Because ListItr is in the same package as List, if List 's data access is (package) friendly, it can be accessed by ListItr.

27 27 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Note on Exceptions Some routines throw ItemNotFound exceptions. However, do not overuse exceptions, because they must always be caught or propagated. As an example, advance does not throw an exception, even if we have already advanced past the end. Otherwise, listSize would need a try/catch block.

28 28 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Linked List Properties Running Time Analysis insert next, prepend - O(1) delete next, delete first - O(1) find - O(n) retrieve current position - O(1) Advantages Growable (compared to array) Easy (quick) in read/insert/delete the first and the last element (if we also store the last position not just the head/current position) Disadvantages Calling to operator new (compare to array) overhead one reference for each node

29 29 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Print all elements of Linked List Method 1: Without Iterator, Simple Looping public class LinkedList { public void print() { // step through list, outputting each item ListNode p = header.next; while (p != null) { System.out.println (p.data); p = p.next; }

30 30 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Print all elements of Linked List(2) Method 2: Without Iterator, Using Recursion public class LinkedList { private void printRec (ListNode node) { if (node != null) { System.out.println (node.data); printRec (node.next); } public void print () {printRec (header.next);} }

31 31 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Print all elements of Linked List(3) Method 3: Recursion class ListNode { public void print () { System.out.println (data); if (next != null) next.print (); } class LinkedList { public void print() { if (header.next != null) header.next.print (); }

32 32 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Print all elements of Linked List(4) Method 4: Using iterator class LinkedList { public void print() { ListItr > itr = first(); while(itr.hasNext()) { itr.next(); System.out.println(itr.retrieve()); }

33 33 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Sorted Linked Lists Maintains items in sorted order. Almost all operations are the same as linked lists, except for insert. In fact, a sorted linked list IS-A linked list, suggesting that inheritance may be used. Extend a SortListItr from ListItr. However, items in a sorted linked list should be Comparable.

34 34 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Simple Implementation Use inheritance, and create Insert method public void insert( Comparable X ) Note however, that in this implementation, we have no assurance that the list is sorted, because it can be accessed by either a SortListItr or a ListItr. See the textbook for details.

35 35 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Important Note ListItr used the equals member function in find and remove. Must make sure MyInteger has an equals member function if we are storing a linked list of MyInteger s. Signature MUST BE public boolean equals( Object Rhs ) The following signature is wrong! public boolean equals( Comparable Rhs ) If you try it, the method from class Object will be inherited and used: public boolean equals (Object obj) { return (this == obj); }

36 36 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Doubly-linked lists: Each list node stores both the previous and next nodes in the list. Useful for traversing linked lists in both directions. Circular-linked lists: Last node's next references the first node. Works with or without headers. Other Linked Lists A headtail prev next A B C first prev next

37 37 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Doubly-linked lists: Wrong InsertNext newNode = new DoublyLinkedListNode ( x ); 1newNode.prev = current; 2 newNode.prev.next = newNode; … x b a 1 2

38 38 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Doubly-linked lists: insertNext 1 newNode = new DoublyLinkedListNode (x); 2 newNode.prev = current; 3 newNode.next = current.next; 4 newNode.prev.next = newNode; 5 newNode.next.prev = newNode; 6 current = newNode; A B current X prev next newNode 1 3 25 4 6

39 39 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Doubly-linked lists: DeleteCurrent 1. current.prev.next = current.next; 2. current.next.prev = current.prev; 3. current = current.prev; x b a current 1 2 3

40 40 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 JDK: package java.util Collection - List - LinkedList Iterator - ListIterator

41 41 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 ListNode List, LinkedList Iterator class a class that maintains a current position and performs all routines that depend on knowing the position in the list. Advantage & Disadvantage of linked list Growable Overhead a pointer, new operator Sequential access only Summary

42 42 Ruli Manurung (Fasilkom UI)IKI10100: Lecture1 st March 2007 Find and study available data structure in Java API: http://telaga.cs.ui.ac.id/WebKuliah/java5/docs/api/java/ut il/package-summary.html Generics in Java 2 SE 5: http://telaga.cs.ui.ac.id/WebKuliah/java5/ Java5-Generics.pdf Chapter 6 & 17 Further Reading


Download ppt "IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1."

Similar presentations


Ads by Google