Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified...

14
Linked List

Transcript of Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified...

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

Linked List

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

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.

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

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(...){LinkList theList = new LinkList(); // make listListIterator iter1 = theList.getIterator(); // make iterLink aLink = iter1.getCurrent(); // access link at iteratoriter1.nextLink(); // move iter to next link}

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

• 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.

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

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

Page 7: Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Page 8: Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Page 9: Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Page 10: Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Page 11: Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Page 12: Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Page 13: Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.

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.

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

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).