Linked List (Part2)
Linked List (Part2)
Data Structures
Linked lists – Part 2
1
Singly Linked List ADT-Extra operation
Search List
Traverse List
Retrieve Node
Destroy List
2
Singly Linked List ADT-Extra operation
Search List
Locating specific data in a list.
In linked list we use sequential search.
We need a key field for search operation.
Traverse List
Start with the head and access each node until you reach null.
Do not change the head reference.
3
Singly Linked List ADT-Extra operation
Retrieve Element
By using search method.
Destroy List
Deletes all nodes still in the list.
It then sets the metadata to a null list condition
4
Singly Linked List ADT: Extra methods
5
Singly linked list implementation:
helper methods
6
Singly linked list implementation:
helper methods
7
Singly linked list implementation:
helper methods
8
Comparison of List Implementations
9
Linked List
Advantages
Data easily inserted and deleted
No need to shift elements of LL to make room for a new
element or to delete an element
Disadvantages
We are limited to a sequential search
10
Circularly Linked Lists
11
Round-robin scheduling
12
Round-robin scheduling
13
Circularly linked list
14
Circularly linked list
15
CircularlyLinkedList class
Round-robin scheduling can be efficiently implemented by
repeatedly performing the following steps on a circularly
linked list C
1. Give a time slice to process C.first( )
2. C.rotate( )
Additional optimization
We no longer explicitly maintain the head reference
We can locate the head as tail.getNext( )
16
CircularlyLinkedList
17
CircularlyLinkedList
18
CircularlyLinkedList
tail
X Y Z
tail
Y Z X
19
CircularlyLinkedList (addFirst)
Empty list:
tail
A
20
CircularlyLinkedList (addFirst)
Nonempty list:
tail
X Y Z
newest
21
CircularlyLinkedList (addFirst)
Nonempty list:
tail
A X Y Z
newest
22
CircularlyLinkedList (addFirst)
Nonempty list:
tail
A X Y Z
newest
23
CircularlyLinkedList (addLast)
tail
X Y Z
tail
A X Y Z
tail
A X Y Z
tail
X Y Z A
24
CircularlyLinkedList (removeFirst)
25
CircularlyLinkedList (removeFirst)
26
CircularlyLinkedList (removeFirst)
27
CircularlyLinkedList (removeFirst)
X Y Z
28
CircularlyLinkedList (removeFirst)
X Y Z
29