Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views110 pages

2 Linklist

Uploaded by

rehaan986750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views110 pages

2 Linklist

Uploaded by

rehaan986750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 110

Linked List

Chapter 2

Safia Sadruddin
Linked List

● A linked list is a linear data structure, in which elements are not stored at
contiguous memory location.
● The elements in a linked list are linked using pointers (entity that points
to the next element).
● In simple words, a linked list contains a nodes where each node contains
a data field and a reference (link) to the next node in the list.
Linked List

● Dynamically allocated data structures can be linked together to form


a chain.

● A linked list can grow or shrink in size as the program runs.


● This is possible because the nodes in a linked list are dynamically
allocated.
Memory Representation of Linked List

Link List to store the start


sequence 7,12,9,3,8,y
Dynamic Memory Allocation

 We can allocate memory manually during run-time.


 This is known as dynamic memory allocation in C
programming.
 Functions used for Dynamic Memory Allocation :
 malloc()
 calloc()
 free()
These functions are defined in the <stdlib.h > header file
malloc() Function

• The name "malloc" stands for memory allocation.


• The malloc() function reserves a block of memory of the specified number of
bytes. And, it returns a pointer of void which can be casted into pointers of any
form.
• Syntax :
ptr = (castType*) malloc(size);
• Example :
ptr = (float*) malloc(100 * sizeof(float));
 Allocates 400 bytes of memory. It's because the size of float is 4 bytes.
 The pointer ptr holds the address of the first byte in the allocated memory.

• The expression results in a NULL pointer if the memory cannot be allocated.


Example on malloc() Function :

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main() Output :
{
1442
int *ptr;
ptr=(int*)malloc(sizeof(int)); 20
*ptr=20;
printf("%u\n",ptr);
printf("%d\n",*ptr);
getch();
}
free() Function

 Dynamically allocated memory created with either calloc() or


malloc() doesn't get freed on their own.
 We must explicitly use free() to release the space.
 Syntax :
free(ptr);
 statement frees the space allocated in the memory pointed
by ptr.
Creation of Linked List Structure

Linked List Node

struct
struct node
node {{
data_type
data_type var;
var;
struct
struct node
node *next;
*next;
};
};
Linked List Node Creation
Creation of Node at Runtime

Creation of Linked List Node

struct
struct node
node *new_node
*new_node ;;
new_node
new_node =(struct
=(struct node*)malloc(sizeof(struct
node*)malloc(sizeof(struct node));
node));

// to assign value to node


new_node->data=value
// to make it to point other node
new_node->next=other_node
Creation Linked List vs Array

Linked List Node Array Elements

struct node { data_type var[size];


data_type var; Or
data_type
struct node *next;
var[]={elements_list};
};
Comparison Linked List vs Array

Linked List Array

Dynamic Data Structure Static Data Structure

No need to pre-allocate memory Pre-Allocate memory

Only Sequential Access Both Random and Sequential Access

Memory Allocation is at runtime and thus Non- Memory allocation is contiguous as size of array
contiguous need to be predefined

Insertion and Deletion is faster Insertion and Deletion is costlier

Requires extra storage space for storing address As Memory Allocation is contiguous, all elements
of next node can be accessed with index

Can grow and shrink at runtime Size is fixed cannot be changed.

Types :- Singly, Doubly, Circular Types :- 1D, 2D,3D...


Types of Link List

 Singly Link List (SLL)


 Doubly Link List(DLL)
 Circular Link List (CLL)
Singly Linked Lists
 A singly linked list is a concrete data structure consisting of a
sequence of nodes
 Each node stores
 element
 link to the next node

A B C D
Linked List Operations

There are 5 basic linked list operations:

Appending a node (to the start, middle or to the


end)
Traversing a list
Deleting a node (from the start, end or middle)
Searching
Reversing a list
Creating a Linked List
Just like any other data type, the information about the node has to be first
be declared.

Step 1) Declare a data structure for the nodes.

struct Node
{ next
int data;
struct Node *next; data
};
node
Creating a Linked List

a) In this example, the first member of the Node struct is a object


called element. It holds the node’s data. This could just as well be
just an integer x, or a structure of student records.

b) The second member is a pointer called next. It holds the address


of any object that is a structure of type Node. Hence each Node
struct can point to the next node in the list.

The Node struct contains a pointer to an object of the same type as


that being declared. It is called a self-referential data structure. This
makes it possible to create nodes that point to other nodes of the same
type.
Creating a Linked List

Next, since there is no physical relationship between nodes, a pointer needs to be


created to point to the first logical node in the list.

Step 2) Declare a pointer to serve as the head of the list:

Node *head = NULL;

Once you have done these 2 steps (i.e. declared a node data structure, and created a
NULL start pointer, you have an empty linked list.

hea null
d
Operations on Singly
Linked List

19
Singly Linked List: Creation

Steps for creating linked list shown in previous slide.


– Create head pointer and set to null head null

– Create node1 and intilize to 10 10

– Create node2 and node3 similarly 20 30

– Set head = node 1 head 10

– Set node1 → next=node2 head 10 20

– Set node2->next=node3 head 10 20 30

20
Singly Linked List : Insertion at Beginning
 If Initial List is empty
head= NULL

 Create new_node new_node

 Set its value : new_node -> data=13 new_node

13

 To insert at beginning requires 2 steps


 Set new_node→next=Null
head
 Set head = new_node

13

21
Singly Linked List : Insertion at Beginning
 If Initial List is : head 10 20

new_node
 Create new_node

new_node
 Set its value : new_node -> data=13
13

 To insert at beginning requires 2 steps


 Set new_node→next=head head

new_node 13 10 20
 Set head = new_node

head 13 10 20
22
Algorithm: Singly Linked List : Insertion at Beginning

1. Create a new_node
2. Set new_node -> data= 13
3. If head = = NULL
3.1 head = new_node
3.2 new_node -> next = NULL
4. Else
4.1 new_node -> next = head
4.2 head = new_node
5. End

23
Singly Linked List : Insertion at End

 If Initial List is empty


head= NULL

 Create new_node new_node

 Set its value : new_node -> data=13 new_node

30

 To insert at beginning requires 2 steps


 Set new_node→next=Null
head
 Set head = new_node

30

24
Singly Linked List : Insertion at End

 Initial List head 10 20

 Create new_node with required value and set node →next = null e.g : say 30

new_node 30
 To insert at end Traverse the list
 Set temp=head
 While temp→next!=null

 temp=temp→next

 Set temp -> next = new_node

25
Algorithm: Singly Linked List : Insertion at End

1. Create a new_node
2. Set new_node -> data= 30 and new_node -> next = NULL
3. If head = = NULL
3.1 head = new_node
3.2 head -> next = NULL
4. Else
4.1 Set temp=head
4.2 while temp -> next != NULL
4.2.1 temp= temp ->next
4.3 temp -> next = new_node
5. End

26
Singly Linked List : Inserting after any node

 Create new_node with required value and set node →next = null e.g : say 25

new_node 25

 To insert after node with data 20 Traverse the list


 Set temp=head

 While temp→data != 20

 temp=temp→next

1. Set new_node -> next = temp -> next

2. Set temp -> next = new_node

27
Algorithm: Singly Linked List : Insertion after any node

1. Create a new_node
2. Set new_node -> data= 25
3. Set temp=head
4. while temp -> data != 20
4.1 temp = temp ->next
5. new_node -> next = temp -> next
6. temp -> next = new_node
7. End

28
Singly Linked List : Inserting before any node

 Create new_node with required value and set node →next = null e.g : say 25

new_node 25

 To insert before node with data 35 Traverse the list


 Set temp=head

 Set pre =NULL

 While temp→data != 35

 pre= temp

 temp=temp→next

1. Set new_node -> next = temp


2. Set pre -> next = new_node

29
head
15 35 45

pre= null
temp
head
15 35 45
While temp→data != 35
temp pre= temp
pre
head temp=temp→next
15 35 45

temp
pre

15 35 45 new_node -> next = temp


pre -> next = new_node

25
Algorithm: Singly Linked List : Insertion before any
node

1. Create a new_node
2. Set new_node -> data= 25
3. Set temp=head
4. pre= NULL
5. while temp -> data != 20
4.1 pre =temp
4.2 temp= temp ->next
6. new_node -> next = temp
7. pre -> next = new_node
8. End
Deletion
Operations on
Singly Linked List

32
Singly Linked List

Caution:
On Deleting node, deallocate memory using free()

• An allocated block can be returned to the system for future


use by using the free function.

• General syntax:
free (ptr);
• Here, ptr is a pointer to a memory block which has been
previously created using malloc

33
Singly Linked List: Deletion at Beginning

 Initial List head 10 20 30

 Set temp=head

 Now

1) head=head→next
2) free(temp)

34
Singly Linked List: Deletion at Beginning

1. If head = NULL
Print Underflow
End
2. Else
2.1 Set temp=head
2.2 head=head→next
2.3 free(temp)
3. End

35
Singly Linked List : Deletion at End

 Initial List

head 10 20 30

 Set p=null, q=head

36
Singly Linked List : Deletion at End

Traverse List: repeat while q→next!=null


1) p=q
2) q=q→next

1 2

3
4

37
Singly Linked List : Deletion at End

 Now set

1)p→next=null
2)free(q)

38
Singly Linked List: Deletion at End

1.If head = NULL


Print Underflow
End
2. Else
2.1 p= NULL and q= head
2.2 while q -> next != NULL
p=q
q = q -> next
2.3 p -> next = NULL
2.4 free (q)
3. End

39
Singly Linked List : Deletion of specific Node

Deletion of specific Node: say data 25...


 Initial List

head 10 20 25 30

 Set p=null, q=head

40
Singly Linked List : Deletion of specific Node
Traverse List: repeat while q→data!=25
1) p=q
2) q=q→next

1 2

3
4

41
Singly Linked List : Deletion of specific Node

 Now set

1)p→next=q→next
2)free(q)

42
Singly Linked List : Deletion of specific Node

1.If head = NULL


Print Underflow
End
2. Else
2.1 p= NULL and q= head
2.2 while q -> data != 25
p=q
q = q -> next
2.3 p -> next = q -> next
2.4 free (q)
3. End

43
Insertion
Operations on
Doubly Linked List

44
Doubly Linked List

struct node{
struct node *prev;
int data;
struct node *next;
};

45
Doubly Linked List: Creation

head null
– Create head pointer and set to null

– Create node1 with initial value 10


– Set next=null and prev=null head 10
– Set head=node1

temp
– Create node with initial value 20
– Set temp = head and traverse while temp→next !=null head
10

– Set temp→next=node2 temp


– node2→prev=temp and node2→next=null
head 10 20

46
Doubly Linked List: Insertion at Beginning

 Initial List

 Create new_node with value=8 , new_node →next = null ,


new_node→prev=null

 Now set,
1)head→prev=new_node
2)new_node→next=head
3)head=new_node

47
Doubly Linked List: Insertion at Beginning

1. Create a new_node
2. Set new_node -> data= 13
3. If head = = NULL
3.1 head = new_node
3.2 new_node -> next = NULL
3.3 new_node -> prev = NULL
4. Else
4.1 new_node -> next = head
4.2 new_node -> prev= NULL
4.3 head-> prev= new_node
4.4 head = new_node
5. End
48
Doubly Linked List: Insertion at End:

 Initial List

 Create new_node with value=13 , new_node →next = null ,


new_node→prev=null

49
Doubly Linked List: Insertion at End:
 Set temp=head, start traversing
 While temp→next!=null

 temp=temp→next

1
2

3
4
50
Doubly Linked List: Insertion at End:

To insert:
1) temp→next=new_node
2) new_node→prev=temp

51
Doubly Linked List: Insertion at End

1. Create a new_node
2. Set new_node -> data= 13
3. If head = = NULL
3.1 head = new_node
3.2 head -> next = NULL
3.3 head -> prev = NULL
4. Else
4.1Set temp=head
4.2 while temp -> next != NULL
4.2.1 temp= temp ->next
4.3 temp -> next = new_node
4.4 new_node -> prev = temp
4.5 new_node -> next= NULL
5. End

52
Doubly Linked List : Insertion after specific Node

Insertion after specific Node: 20


 Initial List

 Create new_node with value=13 , new_node →next = null , new_node→prev=null

53
Doubly Linked List

 Set temp=head, start traversing


 While temp→data!=20

 temp=temp→next

54
Doubly Linked List

To insert:
1)new_node→next=temp→next
2)temp→next→prev=new_node
3)new_node→prev=temp
4)temp→next=new_node

55
Doubly Linked List: Insertion after specific node

1. Create a new_node
2. new_node -> data = 13
3. Set temp=head
4. while temp -> data != 20
4.1 temp = temp-> next
5. new_node -> prev= temp
6. new_node -> next = temp -> next
7. temp-> next -> prev = new_node
8. temp -> next = new_node
9. End

56
Doubly Linked List : Insertion before specific Node

Insertion before specific Node: 25


 Initial List

 Create new_node with value=13 , node →next = null ,


node→prev=null

57
Doubly Linked List : Insertion before specific Node

Set temp=head, start traversing


 While temp→!=25

temp=temp→next
1 2

58 Lecture 17 – Insert Operation


3
Doubly Linked List : Insertion before specific Node

 To insert:

1)new_node→prev=temp→prev
2)temp→prev→next=new_node
3)new_node→next=temp
4)temp→prev=new_node

59
Doubly Linked List: Insertion after specific node

1. Create a new_node
2. new_node -> data = 13
3. temp = head
4. while temp -> data ! = 25
temp = temp -> next
5, new_node -> next = temp
6. new_node -> prev= temp -> prev
7. temp -> prev -> next = new_node
8. temp -> prev= new_node
9. End

60
Deletion
Operations on
Doubly Linked List

61
Doubly Linked List: Deletion at Beginning

 Initial List

 Set temp=head

 Now
1) head=head→next
2)head->prev=null
3) free(temp)

62
Doubly Linked List: Deletion at Beginning

1. head = NULL
Print underflow
End
2. Else
2.1 temp = head
2.2 head = head -> next
2.3 head -> prev = NULL
2.4 free (temp)
3. End

63
Doubly Linked List : Deletion at End

 Initial List

 Traverse List: temp=head


 while temp→next!=null

temp=temp→next
1 2

3 4
64
Doubly Linked List : Deletion at End

 Now set

1)temp→prev→next=null
2)free(temp)

65
Doubly Linked List: Deletion at End

1. If head = NULL
Print Underflow
End
2. Else
2.1 temp = head
2.2 while temp -> next ! = NULL
temp = temp -> next
2.3 temp -> prev -> next = NULL
2.4 free(temp)
3. End

66
Doubly Linked List: Deletion After a specific node
 Initial List

 Traverse List: temp= head


 while temp->data != 20

temp=temp→next

67
Doubly Linked List: Deletion After a specific node
 Now set

1) Set temp1 = temp → next


2)temp -> next = temp1-> next
3)temp1-> next -> prev= temp
4)free(temp1)

temp1

68
Doubly Linked List: Deletion After a specific node

1. If head= NULL
Print Underflow
End
2. Else
2.1 temp = head
2.2 while temp -> data != 20
temp = temp->next
2.3 temp1= temp -> next
2.4 temp1-> next -> prev = temp
2.5 temp -> next = temp1-> next
2.6 free(temp1)
3. End

69
Doubly Linked List: Deletion Before a specific node

 Initial List

 Traverse List:
 temp1=NULL
 temp2=head
 while temp→ data !=25

temp1= temp2

temp2= temp2-> next

70
Doubly Linked List: Deletion Before a specific node
 Now set

1) temp1 -> prev-> next = temp2


2)temp2-> prev = temp1 -> prev
3)free(temp1)

temp1 temp2

71
Doubly Linked List: Deletion Before a specific node

1. If head = NULL
Print Underflow
End
2. Else
2.1 temp1= NULL
2.2 temp2= head
2.3 while temp2->data !=25
temp1= temp2
temp2= temp2-> next
2.4 temp1-> prev-> next = temp2
2.5 temp2-> prev = temp1 -> prev
2.6 free(temp1)
3. End
72
73
Doubly Link List

 Undo and Redo operations in text editors


 Doubly linked lists are useful for playing video
and sound files with “rewind” and “instant
replay”
 They are also useful for other linked data which
require “rewind” and “fast forward” functions in
playlist
 They are used to implement Double ended queue
 Web Browser forward and backward button
Operations on
Circular Linked
List

74
Circular Linked List

Representation:

head 10 20 30

Characteristics:
– Unidirectional
– Last Node points back to First Node

75
Circular Linked List

Structure of Node same as singly link list.

Linked List Node

struct
structnode
node
{{
data_type
data_typevar;
var;
struct
structnode
node*next;
*next;
};
};

76
Circular Linked List

Creation of Node at Runtime same as singly list

Creation of Linked List Node

struct
structnode
node*new_node
*new_node;;
new_node
new_node=(struct
=(structnode*)
node*)malloc(sizeof(struct
malloc(sizeof(structnode));
node));

77
Circular Linked List : Insertion at Beginning
 If Initial List is empty
head= NULL

 Create new_node new_node

 Set its value : new_node -> data=13 new_node

13

 To insert at beginning requires 2 steps


 Set head = new_node
head
 Set new_node→next=head

13

78
Circular Linked List : Insertion at Beginning
 If Initial List is : head 10 20

new_node
 Create new_node

 Set its value : new_node -> data=13


 Set new_node-> next =head

new_node head

13 10 20

79
Circular Linked List : Insertion at Beginning
 To insert at beginning requires to traverse the list to find last node. So
that the next pointer of last node can be updated to point to newly
inserted node.
new_node head temp
 Set temp=head
 While temp→next!=head
13 10 20
 temp=temp→next

 Set temp-> next = new_node


 head= new_node head temp

13 10 20

80
Algorithm: Circular Linked List : Insertion at Beginning

1. Create a new_node
2. Set new_node -> data= 13
3. If head = = NULL
3.1 head = new_node
3.2 head -> next = new_node
4. Else
4.1new_node-> next =head
4. 2 while temp -> next != head
4.2.1 temp= temp ->next
4.3 temp -> next = new_node
4.4 head = new_node
5. End
81
Circular Linked List : Insertion at End
 If Initial List is empty
head= NULL

 Create new_node new_node

 Set its value : new_node -> data=13 new_node

13

 To insert at beginning requires 2 steps


 Set head = new_node
head
 Set new_node→next=head

13

82
Circular Linked List : Insertion at End
 To insert at end requires to traverse the list to find last node.
 Set temp=head
 While temp→next!=head
head temp
 temp=temp→next

10 20

 Set temp-> next = new_node


 Set new_node ->next = head
temp new_node
head

10 20 13

83
Algorithm: Circular Linked List : Insertion at End

1. Create a new_node
2. Set new_node -> data= 13
3. If head = = NULL
3.1 head = new_node
3.2 head -> next = new_node
4. Else
4.1 while temp -> next != head
4.2.1 temp= temp ->next
4.2 temp -> next = new_node
4.2 new_node -> next = head
5. End

84
Circular Linked List: Deletion at Beginning

 Initial List head 10 20 30

 Set p=head
p
While p→next!=head
p=p -> next
head 10 20 30

p
p

head 10 20 30
head 10 20 30

85
Circular Linked List: Deletion at Beginning

q p
Set q=head
Set head = head-> next head 10 20 30
Set p -> next =head
free(q)
head p

10 20 30

head

20 30

86
Circular Linked List: Deletion at Beginning

1. If head = NULL
Print Underflow
End
2. Else
2.1 Set p=head
2.2 while p-> next != head
2.2.1 p= p ->next
2.3 Set q= head
2.4 head= head- > next
2.5 p-> next = head
2.6 free(q)
3. End
87
Circular Linked List: Deletion at End

 Initial List head 10 20 30

 Set q= NULL, p=head


p
While p→next!=head
q= p
head 10 20 30
p=p -> next

q
q p
q p

head 10 20 30
head 10 20 30

88
Circular Linked List: Deletion at End

q p
Set q=head
Set q -> next = head head 10 20 30
free(p)

q p

head 10 20 30

89
Circular Linked List: Deletion at End

1. If head = NULL
Print Underflow
End
2. Else
2.1 Set q= NULL , p=head
2.2 while p-> next != head
2.2.1 q= p
2.2.2 p= p ->next
2.3 Set q -> next = head
2.4 free(p)
3. End

90
Stack and Queue
using Linked List

91
Stack using Linked List

Stack Revision
1) Stack is linear data structure?

2) Stack works in ______________ order?


a. LIFO
b. FIFO

3)Select operations which cannot be performed on stack.


a) PUSH
b)POP
c)INSERT

92
Stack using Linked List

 For implementing stack using linked list

 for push which insert operation can be used?


a) Insert at Beginning
b) Insert at End
c) Insert after specific node
d) Insert before specific node

 for pop which insert operation can be used?


a) Delete at Beginning
b) Delete at End
c) Delete Specific node

93
Stack using Linked List

 For implementing stack using linked list

 Which combination of insert and delete for push & pop, won’t be able to apply
LIFO order.
a) Insert and Delete at Beginning
b) Insert and Delete at End
c) Insert after/before specific node and Delete specific node

 Which combination of insert and delete for Push and pop operation result in
best case complexity
 a) Insert and Delete at Beginning

 b) Insert and Delete at End

94
Stack using Linked List

 Stack representation using Linked List

95
Queue using Linked List

1) Queue is non-linear data structure?

2) Queue works in ______________ order?


a. LIFO
b. FIFO

3)Select operations which can be performed on Queue.


a) PEEP
b)INSERT
c)DELETE
d) Enqueue

96
Queue using Linked List

 For implementing Queue using linked list

 for Enqueue which insert operation can be used?


a) Insert at Beginning
b) Insert at End
c) Insert after specific node
d) Insert before specific node

 for dequeue which insert operation can be used?


a) Delete at Beginning
b) Delete at End
c) Delete Specific node

97
Queue using Linked List

 For implementing Queue using linked list

 Which combination of insert and delete for Enqueue & Dequeue, won’t be able
to apply FIFO order.
a) Insert and Delete at Beginning
b) Insert and Delete at End
c) Insert at Beginning and Delete at End
d) Insert at End and Delete at Beginning

 Which combination of insert and delete for Enqueue & Dequeue operation
result in best case complexity
a) Insert at end and Delete at Beginning
b) Insert at Beginning and Delete at End

98
Queue using Linked List

 Queue Representation using Linked List

99
Queue using Linked List

 Which Representation will be efficient?

10
0
Demo of Stack and Queue Using Linked List

https://visualgo.net/en/list

10
1
Application of Linked
List

102
Polynomial List

 Each term in polynomial list consist of


 Exponent
 Coefficient

 Polynomial term in Linked List can be represented as

10
3
Polynomial List

 Polynomial representation

10
4
Polynomial List

 Polynomial standard form has decreasing order from left to right.

3x + 4x + 2x + 5
9 3

Can we have
Sorted linked list?..

10
5
Polynomial List

 Polynomial standard form has decreasing order from left to right.

So we can sort list


On Exponent
If yes then we and
cancombine
List with + or -

Can we have
Sorted linked list?..

10
6
Addition of Polynomial List

List 1
Copy remaining
1<
4
9 =7
> 2
4
nodes of list 1

List 2

List 3

10
7
Sorted Linked List

Insertion in Queue:
 Initial List

head 10 20 30
 Create new_node , with data 40
 Set p=null, q=head

 Traverse List: repeat while q→next!=null and q→data <= new_node→data


1) p=q
2) q=q→next

10
8
Sorted Linked List

 Case 1: p==null (1st Node)


1) new_node→next=head For case1 try with new_node→data=5
2) head=new_node

 Case 2: q→next==null (last Node)


1) new_node→next=q→next
2) q→next=new_node

 Case 3: q→data<new_node→data
1) new_node→next=p→next For case 3 try with new_node→data=25
2) p→next=new_node

10
9
VLab Demo

http://ds1-iiith.vlabs.ac.in/data-structures-1/exp/poly-arithmetic/exp.ht
ml#Demo

Perform and share below exercise

http://ds1-iiith.vlabs.ac.in/data-structures-1/exp/poly-arithmetic/exp.ht
ml#Practice

11
0

You might also like