1 / 18

Linked List

Linked List. Part 2. Sorted Lists.

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 Part 2

  2. Sorted Lists • In a sorted list, the items are arranged in sorted order by key value. Deletion is often limited to the smallest (or the largest) item in the list, which is at the start of the list, although sometimes find() and delete() methods, which search through the list for specified links, are used as well. • In general you can use a sorted list in most situations in which you use a sorted array. The advantages of a sorted list over a sorted array are speed of insertion (because elements don’t need to be moved) and the fact that a list can expand to fill available memory, while an array is limited to a fixed size. However, a sorted list is somewhat more difficult to implement than a sorted array.

  3. Efficiency of Sorted Linked Lists • Insertion and deletion of items in the sorted linked list require N comparisons because the appropriate location must be found by stepping through the list. • However, the minimum value can be found, or deleted, in 1 time because it’s at the beginning of the list. If an application frequently accesses the minimum item, and fast insertion isn’t critical, then a sorted linked list is an effective choice. • A priority queue might be implemented by a sorted linked list, for example.

  4. Doubly Linked Lists • A potential problem with ordinary linked lists is that it’s difficult to traverse backward along the list. • A statement like current=current.nextsteps conveniently to the next link, but there’s no corresponding way to go to the previous link. • In an ordinary linked list, you would need to return current (or its equivalent) to the start of the list and then step all the way down again to the new current link. This isn’t very efficient. • The doubly linked list provides this capability. It allows you to traverse backward as well as forward through the list. • The secret is that each link has two references to other links instead of one. The first is to the next link, as in ordinary lists. The second is to the previous link.

  5. The downside of doubly linked lists is that every time you insert or delete a link you must deal with four links instead of two: two attachments to the previous link and two attachments to the following one. Also, of course, each link is a little bigger (memory spaces) because of the extra reference.

  6. Traversal • Two display methods demonstrate traversal of a doubly linked list. • The displayForward() method is the same as the displayList() method we’ve seen in ordinary linked lists. • The displayBackward() method is similar but starts at the last element in the list and proceeds toward the start of the list, going to each element’s previous field. This code fragment shows how this process works: Link current = last; // start at end while(current != null) // until start of list, current = current.previous; // move to previous link • Because you can go either way equally easily on a doubly linked list, there is no preferred direction and therefore terms like previous and next are not necessary. If you prefer, you can substitute direction-neutral terms such as left and right.

  7. Insertion • The insertFirst() method inserts at the beginning of the list, insertLast() inserts at the end, and insertAfter() inserts following an element with a specified key.

  8. The insertLast() method is the same process applied to the end of the list; it’s a mirror image of insertFirst(). • The insertAfter() method:

  9. Deletion • There are three deletion routines: deleteFirst(), deleteLast(), and deleteKey(). The first two are fairly straightforward. • In deleteKey(), the key being deleted is current. Assuming the link to be deleted is neither the first nor the last one in the list • the next field of current.previous (the link before the one being deleted) is set to point to current.next (the link following the one being deleted), and the previous field of current.next is set to point to current.previous. This disconnects the current link from the list.

More Related