tutorial 2: linked lists - prakash gautam · doubly linked list sll and asymmetry in sll, difficult...
TRANSCRIPT
![Page 1: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/1.jpg)
Tutorial 2: Linked Lists
![Page 2: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/2.jpg)
…?
2
![Page 3: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/3.jpg)
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
![Page 4: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/4.jpg)
● 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
![Page 5: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/5.jpg)
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
![Page 6: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/6.jpg)
● 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
![Page 7: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/7.jpg)
10 12 143 102 Null
Head
5
Newest
10 12 143 102
NewestNull
5
Inserting an element at the head of SLL
![Page 8: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/8.jpg)
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)
![Page 9: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/9.jpg)
10 12 143 102 Null
Head
5
Newest
10 12 143 102Null
5
Inserting an element at the tail of SLL
Head
![Page 10: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/10.jpg)
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)
![Page 11: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/11.jpg)
10 12 143 102 Null
Head
12 143 102
Null
Remove an element from a SLL
Head
![Page 12: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/12.jpg)
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)
![Page 13: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/13.jpg)
Implementing SLL & DLL● LinkedList extends AbstractSequentialList
& implements List interface which extends Collection
13
![Page 14: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/14.jpg)
● 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
![Page 15: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/15.jpg)
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
![Page 16: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/16.jpg)
● 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
![Page 17: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/17.jpg)
17
11 12 17 20
![Page 18: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/18.jpg)
● Insert new node at beginning?● Insert new node at middle?● Insert new node at end?
18
![Page 19: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/19.jpg)
Implementing SLL & DLL● LinkedList extends AbstractSequentialList
& implements List interface which extends Collection
19
![Page 20: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/20.jpg)
● 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
![Page 21: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/21.jpg)
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)
![Page 22: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/22.jpg)
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)
![Page 23: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/23.jpg)
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)
![Page 24: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/24.jpg)
Next Tutorial...
Demo & Programming Exercises
24
![Page 25: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/25.jpg)
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/
![Page 26: Tutorial 2: Linked Lists - Prakash Gautam · Doubly Linked List SLL and Asymmetry In SLL, difficult to delete a tail & any arbitrary node, Why? To provide greater symmetry: DLL O(1)](https://reader033.vdocument.in/reader033/viewer/2022042922/5f6e6cf10351fc3bd766bf51/html5/thumbnails/26.jpg)
…?
Thank You
26