lecture 2: linked lists - prakash gautam · deleting an element from a list searching a list ... in...

45
Lecture 2: Linked Lists Prakash Gautam https://prakashgautam.com.np/dipit02/ [email protected] 15 March, 2018

Upload: others

Post on 31-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

Lecture 2: Linked Lists

Prakash Gautamhttps://prakashgautam.com.np/dipit02/[email protected] 15 March, 2018

Page 2: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

Objectives● Why Linked Lists?● Basic Concepts of Linked Lists● Difference between Array & Linked Lists● Types of Linked Lists● Operations

2

Page 3: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

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: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

Introduction to Linked List● Linear data structure● It is series of connected nodes● Each node contains at least: A piece of Data (Any Type)

& Pointer to the next node in the list● Head: Pointer to the first node ● Last node to Null

4

1

Tail

2 3Head

Page 5: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

5

A

Data Pointer

Node

Page 6: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Nodes make up Linked List● Nodes are structures made up of data and a pointer to

another node● Usually, the pointer is called next

Null

Head

6

10 12 143 102

Page 7: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

Array Vs Linked List

7

Arrays Linked List● Fixed size: Resizing is

expensive● Dynamic size

● Insertions and Deletions are inefficient: Elements are usually shifted

● Insertions and Deletions are efficient: No shifting

● Random access i.e., efficient indexing

● No random access● Not suitable for operations requiring

accessing elements by index, e.g., Sorting

Page 8: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

8

Arrays Linked List

● No memory waste if thearray is full otherwise may result in much memory waste

● Memory is allocated Dynamically so, there is no waste of memory

● Sequential access is faster [Elements in contiguous memory locations]

● Sequential access is slow[Elements not in contiguous memory locations]

Page 9: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

Types of Linked Lists

● Singly Linked List● Doubly Linked List● Circular Linked List

9

Page 10: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Singly Linked List✔ Each node has only one link part✔ Each link part contains the address of the next node in

the list✔ Link part of the last node contains NULL value which

signifies the end of the node✔ This is Singly Linked List(SLL)

10Header Pointer

Null10 12 143 102

myList

Page 11: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Singly Linked List✔ Each node contains a data & a pointer to the next node in

the list

✔ myList is the header pointer which points at the first node in the list

11

Header Pointer

Null10 12 143 102

myList

Page 12: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

Basic Operations

● Creating a List● Inserting an element on a List● Deleting an element from a List● Searching a List● Reversing a List

12

Page 13: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

13

A

Data Pointer

Node

Page 14: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Inserting a node in the SLL✔ Insertion at the beginning✔ Insertion at the end✔ Insertion after a particular node

14

Page 15: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Insertion at the beginning

15

Header Pointer

Null10 12 143 102myList

Page 16: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Insertion at the beginning

16

Header Pointer

Null10 12 143 102myList

25

Page 17: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Insertion at the end

17

Header Pointer

Null10 12 143 102myList

Page 18: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Insertion at the end

18

Header Pointer

Null10 12 143 102myList

73

Page 19: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Inserting after a particular node

19

Header Pointer

Null10 12 143 102myList

Page 20: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Inserting after a particular node

20

Header Pointer

Null10 12 143 102myList

11

Page 21: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Deleting a node in the SLL✔ Deleting the first node✔ Deleting the last node✔ Deleting the intermediate node

21

Page 22: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Deleting the first node

22

Header Pointer

Null10 12 143 102myList

Page 23: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Deleting the first node

23

Header Pointer

Null10 12 143 102myList

Page 24: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Deleting the last node

24

Header Pointer

Null10 12 143 102myList

Page 25: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Deleting the last node

25

Header Pointer

Null10 12 143 102myList

Page 26: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Deleting the particular node

26

Header Pointer

Null10 12 143 102myList

Page 27: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Deleting the particular node

27

Header Pointer

Null10 12 143 102myList

Page 28: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Searching a node in the SLL✔ Searching involves finding the required element in the list✔ Sequential search or Binary search?✔ In Linked list since random access is not available it would

become complex to do binary search✔ We can perform simple linear search traversal

28

Page 29: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Reversing a Linked List✔ We can reverse a linked list by reversing the direction of the

links between 2 nodes

29

Header Pointer10 12 143 102myList Null

102 143 12 10myList Null

Page 30: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

30

Operation Array SLL

Insert at beginning O(n) O(1)

Insert at end O(1)O(1): Tail Reference

O(n): No Tail Reference

Insert at middle O(n) O(n)

Delete at beginning O(n) O(1)Delete at end O(1) O(n)

SearchO(n)

O(logn)O(n)

Indexing O(1) O(n)

Page 31: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Doubly Linked List✔ A doubly linked list is a list that has two references, one to

the next node and another to previous node.

Head Tail

31

11 12 17 20

Previous Data Next

NullNull

Page 32: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● A doubly linked list contain three fields✔ An Integer Value✔ The link to the next node✔ The link to the previous node

● Structure

32

Previous Data Next

Page 33: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

DLL vs SLL● Advantages

✔ Can be traversed in either direction✔ Some operations, such as deletion and inserting before a

node, become easier

● Disadvantages✔ Requires more space✔ List manipulations are slower✔ Greater chance of having bugs

33

Page 34: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Inserting at beginning

34

11 12 17 20 Null Null

Page 35: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Inserting at beginning

35

11 12 17 20 Null

Null 15

Null

Page 36: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Inserting at the end

36

11 12 17 20 Null Null

Page 37: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Inserting at the end

37

11 12 17 20 Null

Null 16

Null

Page 38: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Inserting after a node

38

11 12 17 20 Null Null

Page 39: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Inserting after a node

39

11 12 17 20 Null Null

13

Page 40: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Deleting a node

40

11 12 17 20 Null Null

Page 41: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Deleting a node

41

11 12 17 20 Null Null

Page 42: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

● Circular Linked List✔ The last node points to the first node of the list

✔ How do we know when we have finished traversing the list?

Check if the pointer of the current node is equal to the head

42

10 12 143 102myList

Page 43: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do
Page 44: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

Applications● MRU Lists● A linked list of URLs (Back: Chrome)● A linked list of state (Ctrl+Z)● A stack, hash table can be implemented using a DLL● Polynomial Algebra

44

Page 45: Lecture 2: Linked Lists - Prakash Gautam · Deleting an element from a List Searching a List ... In Linked list since random access is not available it would become complex to do

...?

Thank You45