Top Banner
Tutorial 2: Linked Lists
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)

Jul 25, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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)

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)

…?

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)

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)

● 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)

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)

● 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)

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)

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)

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)

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)

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)

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)

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)

● 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)

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)

● 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)

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)

● 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)

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)

● 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)

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)

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)

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)

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)

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/

[email protected]

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)

…?

Thank You

26