List Operations 15 111 Advanced Programming Ananda Gunawardena

  • Slides: 20
Download presentation
List Operations 15 -111 Advanced Programming Ananda Gunawardena 11/29/2020 1

List Operations 15 -111 Advanced Programming Ananda Gunawardena 11/29/2020 1

Advantages of Linked Lists • The advantages of linked lists include: – Overflow can

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. 11/29/2020 2

Disadvantage of Linked Lists • The disadvantages of linked lists include: – The pointers

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. 11/29/2020 3

Linked List Operations 11/29/2020 4

Linked List Operations 11/29/2020 4

List Operations • Basic Operations on a Linked List are – Insertion – Deletion

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 – to. String – traversing 11/29/2020 5

List Traversal head null 11/29/2020 6

List Traversal head null 11/29/2020 6

Insertion • Two cases to consider head – List is empty • Insert to

Insertion • Two cases to consider head – 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 11/29/2020 7

Insert Operation P New node 11/29/2020 8

Insert Operation P New node 11/29/2020 8

Delete Operation P 11/29/2020 Node 9

Delete Operation P 11/29/2020 Node 9

Other Operations 11/29/2020 10

Other Operations 11/29/2020 10

Prepend head null New node 11/29/2020 Insert to the beginning of the list 11

Prepend head null New node 11/29/2020 Insert to the beginning of the list 11

Append head null New node 11/29/2020 Insert to the end of the list 12

Append head null New node 11/29/2020 Insert to the end of the list 12

Length head null 11/29/2020 Insert to the end of the list 13

Length head null 11/29/2020 Insert to the end of the list 13

List Types 11/29/2020 14

List Types 11/29/2020 14

Doubly Linked Lists • Typically a linked list with two pointers (next and previous)

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

Inserting into a Doubly Linked List • We have to manipulate both next and

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 11/29/2020 16

Deleting from a Doubly Linked List • Write the code to delete Node Q

Deleting from a Doubly Linked List • Write the code to delete Node Q Node P 11/29/2020 Node Q Node R 17

Building a Doubly Linked List • Build a doubly linked list with 3 nodes

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); • Node N = new Node(new Integer(4), null); • Connect the two nodes 11/29/2020 18

Make it a Circular Doubly Linked List • Write the code to create a

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

Multi Linked List - An Example 11/29/2020 20

Multi Linked List - An Example 11/29/2020 20