Tutorial 2: Linked Lists
Tutorial 2: Linked Lists
…?
2
Agenda● Introduction of Linked List● Arrays Vs Linked Lists● Types of Linked Lists
✔ Singly Linked List✔ Doubly Linked List✔ Circular Linked List
● Operations✔ Insertion✔ Deletion 3
● Data Structures where objects will be arranged linearly● Array: Linear order determined by indices● Linked List: Order determined by pointer of each object● Linked list provides simple & flexible representation
for dynamic sets supporting various operations
Null
Head4
10 12 143 102
Array Vs Linked List● Array
✔ Pre-determined fixed size✔ Easy access to any element a[i] in constant time✔ No space overhead
Size = N x sizeOf (element)
● Linked List✔ No fixed size; grow one element at a time
5
● Linked List✔ No fixed size; grow one element at a time✔ Space overhead
● Each element must store an additional reference● Size = n x sizeof (element) + n x sizeof(reference)
✔ No easy access to ith element wrt the head of the list
6
10 12 143 102 Null
Head
5
Newest
10 12 143 102
NewestNull
5
Inserting an element at the head of SLL
8
Algorithm
addFirst(e)newest = Node(e)
newest.next = headhead = newest size = size+1
Algorithm
addFirst(e)newest = Node(e)
newest.next = headhead = newest size = size+1
O(1)
10 12 143 102 Null
Head
5
Newest
10 12 143 102Null
5
Inserting an element at the tail of SLL
Head
10
Algorithm
addLast(e)newest = Node(e)
newest.next = nulltail.next = newesttail = newest size = size+1
Algorithm
addLast(e)newest = Node(e)
newest.next = nulltail.next = newesttail = newest size = size+1
O(1)
10 12 143 102 Null
Head
12 143 102
Null
Remove an element from a SLL
Head
12
Algorithm
removeFirst()If (head == null)List is empty
else, head = head.nextsize = size-1
Algorithm
removeFirst()If (head == null)List is empty
else, head = head.nextsize = size-1
O(1)
Implementing SLL & DLL● LinkedList extends AbstractSequentialList
& implements List interface which extends Collection
13
● size(): returns no. of elements
● isEmpty(): returns true or false
● first(): returns the first element in the list
● last(): returns the last element in the list
● addFirst(e): adds new element to front of the list
● addLast(e): adds new element to end of the list
● removeFirst(): removes & returns first element
14
Doubly Linked List● SLL and Asymmetry● In SLL, difficult to delete a tail & any arbitrary node,
Why?● To provide greater symmetry: DLL● O(1) time operations (insertion, deletion at anywhere)● Previous & Next Pointer
15
● Header & Trailer Sentinels✔ To carry out efficient operations nearby boundaries of DLL✔ They won’t store elements of primary sequence✔ To simplify the logic of our operations✔ Header & Trailer nodes never change
16
17
11 12 17 20
● Insert new node at beginning?● Insert new node at middle?● Insert new node at end?
18
Implementing SLL & DLL● LinkedList extends AbstractSequentialList
& implements List interface which extends Collection
19
● size(): returns no. of elements
● isEmpty(): returns true or false
● first(): returns the first element in the list
● last(): returns the last element in the list
● addFirst(e): adds new element to front of the list
● addLast(e): adds new element to end of the list
● removeFirst(): removes & returns first element
20
21
Searching a DLL
LIST-SEARCH(L,k)1. X=L.head2. While X!=NULL & X.key!=k3. X=X.next4. Return X
Searching a DLL
LIST-SEARCH(L,k)1. X=L.head2. While X!=NULL & X.key!=k3. X=X.next4. Return X
O(n)
22
Inserting into a DLL
LIST-INSERT(L,X)1. X.next=L.head2. If L.head.next!=NULL3. L.head.prev=X4. L.head=X5. X.prev=NULL
Inserting into a DLL
LIST-INSERT(L,X)1. X.next=L.head2. If L.head.next!=NULL3. L.head.prev=X4. L.head=X5. X.prev=NULL
O(1)
23
Deleting from a DLL
LIST-DELETE(L,X)1. If X.prev!=NULL2. X.prev.next=X.next3. If X.next!=Null4. X.next.prev=X.prev
Deleting from a DLL
LIST-DELETE(L,X)1. If X.prev!=NULL2. X.prev.next=X.next3. If X.next!=Null4. X.next.prev=X.prev
O(1)
Next Tutorial...
Demo & Programming Exercises
24
Assignments● Implement Singly Circular & Doubly Circular linked
list using similar techniques.● How to reverse any linked list? Discuss various
methods with pseudocode & write a program in JAVA using a recursive technique, asking any 10 random integers from user.
Deadline: 29 March 2018, 11:59 NPT
https://prakashgautam.com.np/dipit02/
…?
Thank You
26