Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/21/20161 List Operations 15-111 Advanced Programming Ananda Gunawardena.

Similar presentations


Presentation on theme: "2/21/20161 List Operations 15-111 Advanced Programming Ananda Gunawardena."— Presentation transcript:

1 2/21/20161 List Operations 15-111 Advanced Programming Ananda Gunawardena

2 2/21/20162 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 2/21/20163 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 2/21/20164 Linked List Operations

5 2/21/20165 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 2/21/20166 List Traversal head null

7 2/21/20167 Insertion 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 head

8 2/21/20168 Insert Operation P New node

9 2/21/20169 Delete Operation P Node

10 2/21/201610 Other Operations

11 2/21/201611 Prepend head New node Insert to the beginning of the list null

12 2/21/201612 Append head New node Insert to the end of the list null

13 2/21/201613 Length head Insert to the end of the list null

14 2/21/201614 List Types

15 2/21/201615 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 2/21/201616 Inserting into a Doubly Linked List We have to manipulate both next and previous pointers. Insert new node N between P and Q Node PNode QNode R Node N

17 2/21/201617 Deleting from a Doubly Linked List Write the code to delete Node Q Node PNode QNode R

18 2/21/201618 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 2/21/201619 Make it a Circular Doubly Linked List Write the code to create a circular doubly linked list.

20 2/21/201620 Multi Linked List - An Example


Download ppt "2/21/20161 List Operations 15-111 Advanced Programming Ananda Gunawardena."

Similar presentations


Ads by Google