1 / 20

List Operations

List Operations. 15-111 Advanced Programming. Ananda Gunawardena. Advantages of Linked Lists. The advantages of linked lists include: Overflow can never occur unless the memory is actually full.

denisedavis
Download Presentation

List Operations

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. List Operations 15-111 Advanced Programming Ananda Gunawardena

  2. Advantages of Linked Lists • The advantages of linked lists include: • Overflow can never occur unless the memory is actually full. • Insertions and deletions are easier than for contiguous (array) lists. • With large records, moving pointers is easier and faster than moving the items themselves.

  3. Disadvantage of Linked Lists • The disadvantages of linked lists include: • The pointers require extra space. • Linked lists do not allow random access. • Time must be spent traversing and changing the pointers. • Programming is typically trickier with pointers.

  4. Linked List Operations

  5. List Operations • Basic Operations on a Linked List are • Insertion • Deletion • Other operations include • build list • Append and prepend (special cases of insert) • empty • length • toString • traversing

  6. List Traversal head null

  7. Insertion head • Two cases to consider • List is empty • Insert to the beginning of the list • List is non-empty • Locate the place to insert • Manipulate the references to make new links

  8. Insert Operation P New node

  9. Delete Operation Node P

  10. Other Operations

  11. Prepend head New node null Insert to the beginning of the list

  12. Append head null New node Insert to the end of the list

  13. Length head null Insert to the end of the list

  14. List Types

  15. Doubly Linked Lists • Typically a linked list with two pointers (next and previous) is called a doubly linked list class Node { Object data; Node next; Node previous; ….. } head

  16. Inserting into a Doubly Linked List • We have to manipulate both next and previous pointers. Insert new node N between P and Q Node P Node Q Node R Node N

  17. Deleting from a Doubly Linked List • Write the code to delete Node Q Node P Node Q Node R

  18. Building a Doubly Linked List • Build a doubly linked list with 3 nodes (always insert to the front of the list) • Node Head = new Node(new Integer(3), null, null); • Node N = new Node(new Integer(4), null, null); • Connect the two nodes • Insert the next node to the list

  19. Make it a Circular Doubly Linked List • Write the code to create a circular doubly linked list.

  20. Multi Linked List - An Example

More Related