struktur data algoritme data structures algorithms
play

Struktur Data & Algoritme ( Data Structures & Algorithms ) - PDF document

Struktur Data & Algoritme ( Data Structures & Algorithms ) Linked-List Denny ( denny@cs.ui.ac.id ) Suryana Setiawan ( setiawan@cs.ui.ac.id ) Fakultas I lm u Kom puter Universitas I ndonesia Sem ester Genap - 2 0 0 4 / 2 0 0 5 Version 2


  1. Struktur Data & Algoritme ( Data Structures & Algorithms ) Linked-List Denny ( denny@cs.ui.ac.id ) Suryana Setiawan ( setiawan@cs.ui.ac.id ) Fakultas I lm u Kom puter Universitas I ndonesia Sem ester Genap - 2 0 0 4 / 2 0 0 5 Version 2 .0 - I nternal Use Only Objectives � dapat memahami pentingnya penggunaan struktur data yang tepat dalam program � dapat memahami cara kerja Linked-List dan analisa kompleksitas waktu insert, delete dan akses � dapat membuat/implementasi Linked-List SDA/ LL/ V2.0/ 2 1

  2. Outline � Importance and properties of Data Structure � Basics of Linked Lists vs. Array � Linked Lists and Iterators � Linked Lists variant: � Doubly Linked Lists � Circular Linked Lists � Sorted Linked Lists SDA/ LL/ V2.0/ 3 Data Structure � Algorithms require the use of a proper representation of data to achieve efficiency � Data structure (aka ADT – Abstract Data Type) = representation + operations � operation allowed may vary between data structure • insert • delete • access • update � Kita perlu menganalisa waktu yang dibutuhkan untuk setiap operasi tersebut. SDA/ LL/ V2.0/ 4 2

  3. Mengapa Butuh Struktur Data � Struktur data membantu tercapainya Component Reusability . � Menerapkan konsep OO paradigm: encapsulation , information- hiding dan abstraction . SDA/ LL/ V2.0/ 5 Macam Struktur Data yang Umum � linked lists � stacks � queues � trees � binary search trees � AVL trees � B-Trees � Trie � hash tables � priority queues SDA/ LL/ V2.0/ 6 3

  4. 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 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. ListNode A0 A1 A2 A3 first last SDA/ LL/ V2.0/ 7 ListNode: Definition public class ListNode { Object element; // some element ListNode next; // constructors ListNode (Object theElement, ListNode n) { element = theElement; next = n; } ListNode (Object theElement) { this (theElement, null); } ListNode () { this (null, null); } } SDA/ LL/ V2.0/ 8 4

  5. Linked List: Insertion a b c d current � Insert X immediately after current position a b c d x current SDA/ LL/ V2.0/ 9 Insertion’s Steps � Insertion immediately after current position // create a new node tmp = new ListNode( ); // place x in the element field tmp.element = x; // x’s next node is b tmp.next = current.next; // a’s next node is x current.next = tmp; a b x current SDA/ LL/ V2.0/ 10 5

  6. Insertion’s Steps � Insertion immediately after current position // create a new node tmp = new ListNode( ); // place x in the element field tmp.element = x; // x’s next node is b tmp.next = current.next; // a’s next node is x current.next = tmp; a b x current SDA/ LL/ V2.0/ 11 Simplification of insertion’s code � More efficient approach tmp = new ListNode (x, current.next); a b x current current.next = tmp; a b x current SDA/ LL/ V2.0/ 12 6

  7. Simplification of insertion’s code � Most efficient approach current.next = new ListNode (x, current.next); SDA/ LL/ V2.0/ 13 Linked List: append � 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.element = x; // place x in the node last.next = null; // adjust next a b c d last a b c d X last � Most efficient approach last = last.next = new ListNode (x, null); SDA/ LL/ V2.0/ 14 7

  8. Linked Lists: Basic Deletion � Delete an item immediately after current position � Basic deletion is a bypass in the linked list. a b x current a b current SDA/ LL/ V2.0/ 15 Deletion’s Steps � Need a reference to node prior to the one to be deleted. current.next = current.next.next; a x b current a x b current a b SDA/ LL/ V2.0/ 16 current 8

  9. Important Note � Ingat!!! Yang disimpan dalam ListNode adalah reference dari object-nya, BUKAN object-nya itu sendiri atau salinan dari object-nya SDA/ LL/ V2.0/ 17 Iterate the Linked List � Jika item disimpan dalam contiguous array: //step through array a, outputting each item for (int index = 0; index < a.length; index++) System.out.println (a[index]); � Jika item disimpan dalam sebuah linked list: // step through List theList, outputting each item for (ListNode p = theList.first; p != null; p = p.next) { System.out.println (p.data); } A0 A1 A2 A3 first last SDA/ LL/ V2.0/ 18 9

  10. 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. � Go to the first element = current set to header.next; � Empty list if header.next == null; � Searching routines will skip header. Header A B C SDA/ LL/ V2.0/ 19 Linked Lists: List interface public interface List { /** Test if the list is logically empty. @return true if empty, false otherwise. */ boolean isEmpty (); /** Make the list logically empty. */ void makeEmpty (); } SDA/ LL/ V2.0/ 20 10

  11. Linked Lists: List implementation public class LinkedList implements List { // friendly data, so LinkedListItr can have access ListNode header; /** Construct the list */ public LinkedList () { header = new ListNode (null); } /** Test if the list is logically empty. @return true if empty, false otherwise. */ public boolean isEmpty( ) { return header.next == null; } SDA/ LL/ V2.0/ 21 Linked Lists: List implementation /** Make the list logically empty. */ public void makeEmpty( ) { header.next = null; } } SDA/ LL/ V2.0/ 22 11

  12. Iterator Class � Maintains notion of the current position (aka cursor). � List class provides method that do not depend on any position (such as isEmpty , and makeEmpty ). � List iterator ( ListItr ) provides most other methods such as advance , retrieve , first , which act on the current position stored in the iterator. SDA/ LL/ V2.0/ 23 Iterator Class // Insert x after current position void insert (x); // Remove x void remove (x); // Remove item after current position void removeNext( ); // Set current position to view x boolean find( x ); // Set current position to prior to first void zeroth (); // Set current position to first void first( ); // Set current to the next node void advance (); // True if at valid position in list boolean isInList (); // Return item in current position Object retrieve() Exceptions thrown for illegal access, insert, or remove. � SDA/ LL/ V2.0/ 24 12

  13. Example � A static member function that computes the number of elements in any list: public static int listSize (List theList) { int size = 0; ListItr itr = new ListItr (theList); for (itr.first(); itr.isInList(); itr.advance()) { size++; } return size; } SDA/ LL/ V2.0/ 25 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 is friendly, it can be accessed by ListItr . SDA/ LL/ V2.0/ 26 13

  14. LinkedListItr implementation � public class LinkedListItr implements ListItr /** contains List header. */ protected LinkedList theList; /** stores current position. */ protected ListNode current; /** Construct the list. As a result of the construction, the current position is the first item, unless the list is empty, in which case the current position is the zeroth item. @param anyList a LinkedList object to which this iterator is permanently bound. */ public LinkedListItr( LinkedList anyList ) { theList = anyList; current = theList.isEmpty( ) ? theList.header : theList.header.next; } SDA/ LL/ V2.0/ 27 /** Construct the list. @param anyList a LinkedList object to which this iterator is permanently bound. This constructor is provided for convenience. If anyList is not a LinkedList object, a ClassCastException will result. */ public LinkedListItr( List anyList ) throws ClassCastException { this( ( LinkedList ) anyList ); } /** * Advance the current position to the next node in the list. * If the current position is null, then do nothing. * No exceptions are thrown by this routine because in the * most common use (inside a for loop), this would require the * programmer to add an unnecessary try/catch block. */ public void advance( ) { if( current != null ) current = current.next; } SDA/ LL/ V2.0/ 28 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend