1 / 14

Linked List

Linked List. Iterators. Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to find the specified link.

phila
Download Presentation

Linked List

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. Linked List

  2. Iterators • Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to find the specified link. • Question: How to perform some operation on certain links? For example, imagine a personnel file stored as a linked list. You might want to increase the wages of all employees who were being paid minimum wage, without affecting employees already above the minimum. Or suppose that in a list of mailorder customers, you decided to delete all customers who had not ordered anything in six months. • We could repeatedly use find() to look for appropriate items in a list, but that approach requires many comparisons to find each link. It’s far more efficient to step from link to link, checking whether each one meets certain criteria and performing the appropriate operation if it does.

  3. An Iterator Class • Objects containing references to items in data structures, used to traverse these structures, are commonly called iterators (or sometimes, as in certain Java classes, enumerators). Here’s a preliminary idea of how they look: class ListIterator() { private Link current; ... } • The current field contains a reference to the link the iterator currently points to. • To use such an iterator, the user might create a list and then create an iterator object associated with the list. Actually, as it turns out, letting the list create the iterator is easier, so it can pass the iterator certain information, such as a reference to its first field. Thus, we add a getIterator() method to the list class; this method returns a suitable iterator object to the user. Here’s some abbreviated code in main() that shows how the class user would invoke an iterator: public static void main(...) { LinkListtheList = new LinkList(); // make list ListIterator iter1 = theList.getIterator(); // make iter Link aLink = iter1.getCurrent(); // access link at iterator iter1.nextLink(); // move iter to next link }

  4. After we’ve made the iterator object, we can use it to access the link it points to or increment it so it points to the next link, as shown in the second two statements. We call the iterator object iter1 to emphasize that you could make more iterators (iter2 and so on) the same way. • The iterator always points to some link in the list. It’s associated with the list, but it’s not the same as the list or the same as a link.

  5. Iterator Methods • Additional methods can make the iterator a flexible and powerful class. All operations previously performed by the class that involve iterating through the list, such as insertAfter(), are more naturally performed by the iterator. In our example the iterator includes the following methods: • reset()—Sets the iterator to the start of the list • nextLink()—Moves the iterator to the next link • getCurrent()—Returns the link at the iterator • atEnd()—Returns true if the iterator is at the end of the list • insertAfter()—Inserts a new link after the iterator • insertBefore()—Inserts a new link before the iterator • deleteCurrent()—Deletes the link at the iterator

  6. Summary • A linked list consists of one linkedList object and a number of Link objects. • The linkedList object contains a reference, often called first, to the first link in the list. • Each Link object contains data and a reference, often called next, to the next link in the list. • A next value of null signals the end of the list. • Inserting an item at the beginning of a linked list involves changing the new link’s next field to point to the old first link and changing first to point to the new item. • Deleting an item at the beginning of a list involves setting first to point to first.next. • To traverse a linked list, you start at first and then go from link to link, using each link’s next field to find the next link. • A link with a specified key value can be found by traversing the list. Once found, an item can be displayed, deleted, or operated on in other ways. • A new link can be inserted before or after a link with a specified key value, following a traversal to find this link.

  7. Summary (Cont’) • A double-ended list maintains a pointer to the last link in the list, often called last, as well as to the first. • A double-ended list allows insertion at the end of the list. • An Abstract Data Type (ADT) is a data storage class considered without reference to its implementation. • Stacks and queues are ADTs. They can be implemented using either arrays or linked lists. • In a sorted linked list, the links are arranged in order of ascending (or sometimes descending) key value. • Insertion in a sorted list takes O(N) time because the correct insertion point must be found. Deletion of the smallest link takes O(1) time. • In a doubly linked list, each link contains a reference to the previous link as well as the next link. • A doubly linked list permits backward traversal and deletion from the end of the list. • An iterator is a reference, encapsulated in a class object, that points to a link in an associated list. • Iterator methods allow the user to move the iterator along the list and access the link currently pointed to. • An iterator can be used to traverse through a list, performing some operation on selected links (or all links).

More Related