You are on page 1of 73

DATA STRUCTURES

(CS3401)

Dr. Somaraju Suvvari,


Asst. Prof, Dept. of CSE, NIT Patna.
somaraju@nitp.ac.in;
soma2402@gmail.com;
Dr Somaraju Suvvari NITP -- CS3401 1
The Course

DATA STRUCTURES

Dr Somaraju Suvvari
2
NITP -- CS3401
LINKED LIST

Dr Somaraju Suvvari
3
NITP -- CS3401
UNIT II: Arrays

1-D arrays, multi-dimensional arrays, operating on arrays, Dynamic memory


allocation, Storage – Column major order and Row major order, Address
calculation of 1-D, 2-D, different form of matrix, Sparse Matrix.

Linked lists – singly, doubly and circularly linked lists, operations on linked lists.

Dr Somaraju Suvvari 4
NITP -- CS3401
Linked Lists
• Limitations of arrays
• Fixed size – The size of the array is static, so wastage of memory.

• One block allocation – Not always possible.

• Complex position based insertion/deletion – To insert an element we need to shift the existing
elements.

• Solution - Linked List

Dr Somaraju Suvvari 5
NITP -- CS3401
Linked List 10 20

• A linked list is a collection of data elements called nodes in which the linear
representation is given by links from one node to the next node. struct Node
{
• Each node contains int data;
struct Node *next;
• Data will store the information part }n1, n2, n3, n4;
n1.data = 10;
• Next will store the address of the next node. n2.data = 20;
n3 n4 n3.data = 30;
n1 n2
n4.data = 40;
10 x2000 20 x100 30 x2500 5 NULL
x1000 x2000 x100 x2500

n1.next=&n2; n2.next=&n3; n3.next=&n4; n4.next=NULL;

Dr Somaraju Suvvari 6
NITP -- CS3401
Linked List 10 20

struct Node
{
int data;
struct Node *next;
}*n1, *n2, *n3, *n4;

10 x2000 20 x100 30 x2500 5 NULL


x1000 x2000 x100 x2500

X1000 X2000 X100 X2500


n1 n2 n4
n3

n1next=n2; n2next=n3; n3next=n4; n4next=NULL;

Dr Somaraju Suvvari 7
NITP -- CS3401
Node creation n1
n1.next = NULL
Create the new Data Structure struct Node n1; 2 NULL
struct Node x1000
{ int data; n1.data = 2
struct Node *next;};

At first node will be created n2


x2000 n2 = n3;
struct Node *n2; x100

Dynamic Memory Allocation n3=(struct Node*) malloc(sizeof(struct Node *);


// Assume that memory is available
n3next = n1;
Then update the <Next> of the new node; n3
5 x1000
x2000

Insert the data in the new node n3->data = 5;

Dr Somaraju Suvvari 8
NITP -- CS3401
Types of Linked List
1. Singly Linked List
• A singly linked list is the simplest type of linked list in which every node contains some data and a
pointer to the next node of the same data type.

head
x2000 20 x3000 30 X100 10 NULL
x2000 x3000 x1000

2. Circular Singly Linked List


• A Singly linked list where the last node contains a pointer to the first node of the list.

head
x2000 20 x3000 30 X100 10 X2000
x2000 x3000 x1000

Dr Somaraju Suvvari 9
NITP -- CS3401
Types of Linked List
3. Doubly Linked List
• Two-way linked list which contains a pointer to the next as well as the previous node in the sequence.

• prev pointer-points to the address of previous node

• next-pointer points to the address of the next node

head NULL 10 30 20 NULL

4. Circular Doubly Linked List


• A doubly linked list where the first node <prev pointer> is connected to the last node and the last
node <next pointer> will connect to the first node.

head 10 30 20

Dr Somaraju Suvvari 10
NITP -- CS3401
SLL/CSLL/DLL/CDLL

• NOTE –
1. When implementing the Linked list we will call first node as header node;

2. Some programmers considers the first node in the list as header node.

3. Some programmers create a dummy node and considers it as a header node and it links
to the first node of the list.

Dr Somaraju Suvvari 11
NITP -- CS3401
Operations-Linked List
• Create
• Insert
• Insert at the beginning
• Insert at the end
• Insert before a given node
• Insert after a given node
• Delete
• Delete a node from beginning
• Delete a node from the end
• Delete a given node
• Delete after a given node
• Display/Traversing
• Sort
Dr Somaraju Suvvari 12
NITP -- CS3401
Singly Linked List
• A singly linked list is list of elements, where each element consists of data and next
pointer as values.

• These Elements are connected through a link(address).


head
x2000 20 x3000 30 X100 10 NULL
x2000 x3000 x1000
Starting node
in start/head
End node
next is NULL

Dr Somaraju Suvvari 13
NITP -- CS3401
SLL ADT
// Define the structure of node
typedef struct Node SLL;
struct Node
{ ElementType Element;
struct Node *next;
};

// Define the set of operations


void Insert_End(SLL *h, ElementType);
void Insert_Before_X(SLL *h, ElementType, ElementType);
void Insert_After_X(SLL *h, ElementType, ElementType);
ElementType Delete_X(SLL *, ElementType);
ElementType Delete_Begin(SLL *);
ElementType Delete_End(SLL *);
void Traverse(SLL *);
void Search(SLL *, Element Type);

Dr Somaraju Suvvari 14
NITP -- CS3401
SLL – Insert_End()
The setup
Initially the node structure will be defined and header node will be created.
typedef struct Node SLL;
struct Node
{int data;
SLL *next;
}*head = NULL;

head
NULL

x100

Dr Somaraju Suvvari 15
NITP -- CS3401
SLL – Insert_End()
Insert_End(SLL *head, int x)
Input: New data element x, The list h;
Output: A new node with data x is added to the end of the list h;

• Step 1 - Create a new_node with given value x and make new_node → next as NULL.
• Step 2 - Check whether list is Empty (head == NULL).
• Step 3 - If list is Empty then, set head = new_node.
• Step 4 - If list is Not Empty then
• Step 4.1 - Declare a node pointer temp and initialize with head.
• Step 4.2 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).
• Step 4.3 - Set temp → next = new_node.
• Step 5 – STOP.
Note: This function is also used to create a list with n elements

Dr Somaraju Suvvari 16
NITP -- CS3401
SLL – Insert_End()
• Step 1 - Create a new_node with given value x and make new_node → next as NULL. head
NULL

SLL *new_node; x100


new_node=(SLL *) malloc(sizeof(SLL *)); // Assume that memory is available
new_node data = x; new_nodenext = NULL;

new_node
10 NULL

x1000

Dr Somaraju Suvvari 17
NITP -- CS3401
SLL – Insert_End()
• Step 2 - check whether list is Empty (head == NULL). head new_node
YES NULL 10 NULL
x100 x1000
• Step 3 - If list is Empty then, set head = new_node.
x1000

• Step 4 - If list is Not Empty then,


• Step 4.1 - Declare a node pointer temp and initialize with head.
// Assume that there are already some nodes are there in the list.

SLL *temp = head; head


x2000 20 x2000 30 NULL

For Example - x100 x2000 x3000


temp

Dr Somaraju Suvvari 18
NITP -- CS3401
SLL – Insert_End()
Step 4.2 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).

head
Step 5 x2000 20 x3000 30 NULL
x2000 x3000
temp
Step 4.3 - Set temp → next = new_node.
head
Step 6 x2000 20 x3000 30 NULL 10 NULL
x2000 x3000 x1000
temp
x1000

Dr Somaraju Suvvari
19
NITP -- CS3401
Create or Insert_End (Logic) in SLL
Insert_End(ele, head) // ele is the element to be added, head is the SLL header
//new node creation Ɵ(1)
• struct Node *new_node, *t;
• new_node = (struct Node *) malloc(sizeof(struct Node *));
• if (new_node == NULL) then, print (“NO memory”) exit(0);
//new node assignment Ɵ(1)
• new_nodedata = x;
• new_nodenext=NULL; Ɵ(1)
// if list is empty then make new_node as first node
• if( head==NULL)
• head=new_node;
// if list is not empty, then reach the last node and then add the address of new_node in last node next
• else Ɵ(n) // n is the number of nodes in the list
• t=head;
//move to the end of list
• while(tnext!=NULL) Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (n)
• t=tnext;
//adding node at the end
= Ɵ (n)
• tnext=new_node; Dr Somaraju Suvvari
20
NITP -- CS3401
Creation: We want to create a list with 10, 20 and 30 values in SLL
new_node 10 new_node
Step 1: new_nodedata=10;
10 NULL
new_nodenext=NULL
x1000 x1000
head
head
step 3: NULL 10 NULL
Step 2: NULL
x100 x1000
x100

20
head x1000
Step 1: x1000
Step 4.1: new_node
x100
Step 4.3: 10 20 NULL
NULL
x1000 x2000
30
head x2000
Step 1: x1000
new_node
Step 4.1: x100
10 X2000 20 NULL 30 NULL
Step 4.2:
Step 4.3: x1000 x2000 x3000 Dr Somaraju Suvvari 21
x3000 NITP -- CS3401
new_node
Insert at the beginning of the list in SLL 10 NULL
x1000
p1
•Step 1 ste head
• Create a new_node with a given value and NULL
2
new_node → next as NULL. step
•Step 2 head new_node
• Check whether list is Empty (head == NULL).
NULL 10 NULL
•Step 3
• If it is Empty then, set head = new_node. step3 x1000
•Step 4 x1000
• If it is Not Empty then,
head
• Set new_node → next = head. step4
x2000 20 x3000 30 NULL
• Set head=new_node;
x2000 x3000
head=new_node x2000
New_node

10
10 NULL
head x1000 new_nodenext=head
x1000 10 x2000 20 x3000 30 NULL x1000
x1000 x2000 x3000

Dr Somaraju Suvvari 22
NITP -- CS3401
Insert_Begin (Logic) in SLL
Insert_Begin(ele, head) // ele is the element to add and head is the LL header
• //new node creation
• Struct Node *new_node, *t; Ɵ(1)
• new_node=(struct Node *) malloc(sizeof(struct Node *));
• if (new_node == NULL) then, print (“NO memory”) exit(0);
• //new node assignment Ɵ(1)
• new_nodedata=ele;
• new_nodenext=NULL;
• // check if list is empty then make new_node as first node
Ɵ(1)
• if( head==NULL)
• head=new_node;
• else // if list is not empty, then update the new_node next and update head
• new_nodenext=head;
Ɵ(1)
• head=new_node; Time Complexity = Ɵ(1) + Ɵ(1) + Ɵ(1) + Ɵ(1)
= Ɵ(1)
Dr Somaraju Suvvari 23
NITP -- CS3401
new
Insert after a given node (After ele) 10 NULL

•Step 1 x1000
p 1
• Create a new_node with given value ste head
and new_node → next as NULL.
2 NULL
•Step 2 s t e p
• Check whether list is Empty (head == NULL).
•Step 3 step3
• If list is Empty then, print “Insertion is not Insertion is not possible
possible”, return;
•Step 4
• If list is Not Empty then, (move to the desired Step 4
node) define a node pointer temp and initialize
with head. head
•Step 5 x2000 20 x3000 30 x4000 40 NULL
• Keep moving the temp to its next node until it
x2000 x3000 x4000
reaches to the desired node in the list (until temp x4000
→ data is not equal to ele and temp != NULL). temp new
•Step 6 - head 10
• If(temp != NULL) Insert after 30 NULL
x2000
• Set new_node → next = temp →next; x1000
• Set temp →next=new_node;
x1000
• Step 7 20 x3000
• else if(temp == NULL)
• If it doesn’t math with any node, then display x2000 30 x1000 10 x4000 40 NULL
error message “element not found”. Dr Somaraju Suvvari 24
NITP -- CS3401 x3000 x1000 x4000
Insert after a specific data element (Logic)
• Insert(ele, aele, head) • // if list is not empty, then mover the desired location
• // ele-to insert, aele-after element, head is the LL header • else
• //create a new node • temp=head;
• Struct Node *new_node, *t; • while(tempdata!=aele && temp!= NULL)
• temp=tempnext;
• new_node=(struct Node *) malloc(sizeof(struct Node *));
• If(temp != NULL)
• if (new_node == NULL) then, print (“NO memory”) exit(0); • //changing new_node next value
• // Assign data to new_node and update the address pointer • newnext=tempnext;
• // changing temp->next to new_node
• new_nodedata=ele;
• tempnext=new_node;
• new_nodenext=NULL; • else print(“Element Not Found”);
• // if list is empty, assign new_node address in head
• if( head==NULL)
Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (n)
• print(“ Insertion not possible”);
= Ɵ (n)

Dr Somaraju Suvvari 25
NITP -- CS3401
Display/Traversing
head
It Is a process of visiting each node and displaying 20 x3000 30 x1000 10 NULL
x2000
the data in it.
x2000 x3000 x1000

•Step 1 List elements are: 20


• Check whether list is Empty (head == NULL) temp
•Step 2 head
• If it is Empty then, display 'List is Empty!!!' and 20 x3000 30 x1000 10 NULL
x2000
terminate the function.
•Step 3 x2000 x3000 x1000
• If it is Not Empty then, define a Node pointer 'temp' and
initialize with head. temp
•Step 4
List elements are: 20, 30
• Keep displaying temp → data until temp reaches to the
last node. head
x2000 20 x3000 30 x1000 10 NULL

x2000 x3000 x1000

Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (n) temp


List elements are: 20, 30, 10
= Ɵ (n) Dr Somaraju Suvvari 26
NITP -- CS3401
Displaying (Logic) in SLL
Display(head) // head is the LL header
• //create temp node
• Struct Node *temp = head; Ɵ(1)
• // check if list is empty then print empty message
• if( temp ==NULL)
• printf(“ List is Empty !!!!”); Ɵ(1)
• else // print the data in all the nodes
• while(temp != NULL)
• printf(“%d “, tempdata);
• temp = tempnext;
Ɵ(n)

Time Complexity = Ɵ(1) + Ɵ(1) + Ɵ(n)


= Ɵ(n)

Dr Somaraju Suvvari 27
NITP -- CS3401
Search
Search for 30
head
It is a process of searching for an element in the list. 20 x3000 30 x1000 10 NULL
x2000
Search (head, ele) x2000 x3000 x1000

•Step 1 20 and 30 did not match


• Check whether list is Empty (head == NULL) temp
•Step 2 head
• If list is Empty then, display 'List is Empty!!!' and 20 x3000 30 x1000 10 NULL
x2000
terminate the function.
•Step 3 x2000 x3000 x1000
• If list is Not Empty then, define a Node
pointer 'temp' and initialize with head. temp
•Step 4
30 and 30 are matched
• Keep traversing to each node and compare the data
element in each node with ele, if match display “Element
“ Element Found”
Found” and terminate the function.
•Step 5
Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (n) + Ɵ (1)
• If not terminated the function in step4, display “Element
not Found” and terminate the function.
= Ɵ (n)

Dr Somaraju Suvvari 28
NITP -- CS3401
Delete (From the beginning) Many ways • Delete a node from the beginning
• Delete a node from the end of the list
To remove an element from a list of elements • Delete a specific node
•Step 1 • Delete a node after a specific element
• Check whether list is Empty (head == NULL)
•Step 2
head head
• If list is Empty then, display 'List is Empty!!! Deletion
x1000 10 NULL NULL
is not possible' and terminate the function.
•Step 3 ,5 x1000
• If list is Not Empty then, define a Node 3,4
p
pointer 'temp' and initialize with head. Ste temp
•Step 4
• Check whether list is having only one node (temp → head
next == NULL) Step 6 x2000 20 x3000 30 NULL
•Step 5 x2000 x3000
• If there is only one node then head = NULL and
delete temp (Setting Empty list conditions) Time Complexity = Ɵ(1) + Ɵ(1) +
•Step 6 temp
• If there are more than one node then set head = temp → Ɵ(1) + Ɵ(1) +
next, and delete temp. head Ɵ(1) + Ɵ(1)
30 NULL = Ɵ(1)
x3000
Dr Somaraju Suvvari x3000 29
NITP -- CS3401
• Delete a node from the beginning
•Step 1
• Check whether list is Empty (head == NULL)
•Step 2
• Delete a node from the end of the list
• Delete a specific node
Delete
• If list is Empty then, display 'List is Empty!!! • Delete a node after a specific element
Deletion is not possible' and terminate the function.
•Step 3
Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) +Ɵ (n) + Ɵ (1)
• If list is Not Empty then, define two Node
pointers 'temp' and ‘prev' and initialize 'temp' = Ɵ (n)
head head
with head.
x1000 10 NULL NULL
•Step 4
• Check whether list has only one Node (temp → x1000
next == NULL)
•Step 4.1 temp
• If list is empty, then,
• set head = NULL
• delete temp.
• terminate the function. head
•Step 4.2 30 x1000
x2000 20 x3000 10 NULL
• If list not empty, then,
• set ‘prev = temp ’
• move temp to its next node.
temp prev temp
• Repeat these steps until it reaches to the last head
node in the list. (while temp → next != NULL)
•Step 5 x2000 20 x3000 30 NULL 10 NULL
• Finally, set prev → next = NULL and delete temp.
Dr Somaraju Suvvari
NITP -- CS3401
prev 30
• Delete a node from the beginning
Delete (a node data is ele) • Delete a node from the end of the list
• Delete a specific node
•Step 1 • Delete a node after a specific element
• Check whether list is Empty (head == NULL)
•Step 2 Delete node contains data 30
• If list is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function. head
•Step 3 x2000 20 x3000 30 x1000 10 NULL
• If list is Not Empty then, define two Node pointers 'temp' and
‘prev' and initialize 'temp' with head.
•Step 4 temp prev
• Step 4.1
• set ‘prev = temp ’
•Step 4.2
• if temp -> data is ‘ele’) goto step 5.
•Step 4.3
• else prev = temp and move temp to its next node. head
• Step 4.4 x2000 20 X1000 30 x1000 10 NULL
• Repeat the steps 4.2 to 4.3 until there are nodes.
(while temp != NULL) x3000
x1000
•Step 5 prev
•Step 5.1 temp
x2000
• if (temp == head) then, head = temp->next; delete temp;
•Step 5.2
• else set prev → next = temp->next and delete temp
Dr Somaraju Suvvari 31
NITP -- CS3401
PRACTISE QUESTIONS

1. Find the nth node from the end of SLL.


2. Check whether the given singly linked list is NULL terminated or ends in a
cycle (instead of NULL value in the last node address if it contains the address
of some other node in the list)?
3. Insert a node in a sorted linked list.
4. How will you find the middle element in a SLL?
5. Given two sorted SLLs, how to merge them into third list in sorted order?

Note: Mention the time and space complexities of the solution you provided for the
above problems.

Dr Somaraju Suvvari 32
NITP -- CS3401
Circular Singly Linked List
• A circular singly linked list is list of elements, where each element consists of data and
next pointer as values and last node points to first node.

• These Elements are connected through a link(address).

head
x2000 20 x3000 30 X100 10 X2000
x2000 x3000 x1000

Dr Somaraju Suvvari 33
NITP -- CS3401
CSLL ADT
// Define the structure of node
typedef struct Node CSLL;
struct Node
{ ElementType Element;
struct Node *next;
};

// Define the set of operations


void Insert_End(CSLL *h, ElementType);
void Insert_Before_X(CSLL *h, ElementType, ElementType);
void Insert_After_X(CSLL *h, ElementType, ElementType);
ElementType Delete_X(CSLL *, ElementType);
ElementType Delete_Begin(CSLL *);
ElementType Delete_End(CSLL *);
void Traverse(CSLL *);
void Search(CSLL *, Element Type);

Dr Somaraju Suvvari 34
NITP -- CS3401
CSLL – Insert_End()
The setup
Initially the node structure will be defined and header node will be created.
typedef struct Node CSLL;
struct Node
{int data;
CSLL *next;
}*head = NULL;

head
NULL

x100

Dr Somaraju Suvvari 35
NITP -- CS3401
CSLL – Insert_End()
Insert_End(CSLL *h, int x)
Input: New data element x, The list h;
Output: A new node with data x is added to the end of the list h;

• Step 1 - Create a new_node with given value x and make new_node → next as NULL.
• Step 2 - Check whether list is Empty (head == NULL).
• Step 3 - If list is Empty then, set head = new_node and new_node->next = new_node.
• Step 4 - If list is Not Empty then
• Step 4.1 - Declare a node pointer temp and initialize with head.
• Step 4.2 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is not equal to head).
• Step 4.3 - Set temp → next = new_node and new_node->next = head
• Step 5 – STOP.
Note: This function is also used to create a list with n elements

Dr Somaraju Suvvari 36
NITP -- CS3401
CSLL – Insert_End()
• Step 1 - Create a new_node with given value x and make new_node → next as NULL. head
NULL

CSLL *new_node; x100


new_node=(CSLL *) malloc(sizeof(CSLL )); // Assume that memory is available
new_node data = x; new_nodenext = NULL;

new_node
10 NULL

x1000

Dr Somaraju Suvvari 37
NITP -- CS3401
CSLL – Insert_End()
• Step 2 - check whether list is Empty (head == NULL). head new_node
YES NULL 10 NULL
x100 x1000
• Step 3 - If list is Empty then, set head = new_node x1000
x1000
and new_nodenext = new_node;

• Step 4 - If list is Not Empty then,


• Step 4.1 - Declare a node pointer temp and initialize with head.
// Assume that there are already some nodes are there in the list.
head
CSLL *temp = head;
x2000 20 x2000 30 x2000
x100 x2000 x3000
temp

Dr Somaraju Suvvari
38
NITP -- CS3401
CSLL – Insert_End()
Step 4.2 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to head).
New_node
head
x2000 20 x3000 30 x2000 10 NULL

x2000 x3000 x1000


temp
Step 4.3 - Set temp → next = new_node and new_nodenext = head;
head New_node
x2000 20 x3000 30 x1000 10 x2000
x2000 x3000 x1000
temp

Dr Somaraju Suvvari
39
NITP -- CS3401
Create or Insert_End (Logic) in CSLL
Insert_End(ele, head) // ele is the element to be added, head is the SLL header
//new node creation Ɵ(1)
• struct Node *new_node, *t;
• new_node = (struct Node *) malloc(sizeof(struct Node));
• if (new_node == NULL) then, print (“NO memory”) exit(0);
//new node assignment Ɵ(1)
• new_nodedata = x;
• new_nodenext=NULL; Ɵ(1)
// if list is empty then make new_node as first node
• if( head==NULL)
• head=new_node and new_nodenext = new_node;
// if list is not empty, then reach the last node and then add the address of new_node in last node next
• else Ɵ(n) // n is the number of nodes in the list
• t=head;
//move to the end of list
• while(tnext!=head) Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (n)
• t=tnext;
//adding node at the end
= Ɵ (n)
• tnext=new_node and new_nodenext = headDr Somaraju Suvvari
40
NITP -- CS3401
new_node
Insert at the beginning of the list in CSLL 10 NULL
x1000
p1
•Step 1 ste head
• Create a new_node with a given value and new_node →
2 NULL
next as NULL. ste p
•Step 2
• Check whether list is Empty (head == NULL). head new_node
•Step 3 NULL 10 NULL
• If list is Empty then, set head = new_node step3
and new_nodenext = new_node; x1000
x1000
•Step 4 x1000
• If list is Not Empty then,
• Declare a node pointers temp, temp1 and initialize with head head
• Set new_node → next = head. x2000 20 x3000 30 x2000
• Set head=new_node. step4
x2000 x3000
• Keep moving the temp to its next node until it reaches to the x2000
last node in the list (until temp → next is not equal to temp1). x1000
• Set temp → next = head. new_node

10
10 NULL
head=new_node x1000 new_nodenext=head
x1000 temp
Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (n)
• temp → next = head.
= Ɵ (n)
Dr Somaraju Suvvari 41
NITP -- CS3401
new_node
Insert after a given node (After ele) 10 NULL

•Step 1 x1000
• Create a new_node with given value head Time Complexity = Ɵ (n)
and new_node → next as NULL.
NULL
•Step 2
• Check whether list is Empty (head == NULL).
•Step 3
• If list is Empty then, print “Insertion is not Insertion is not possible
possible”, return;
•Step 4
• If list is Not Empty then, (move to the desired
node) define a node pointer temp and initialize
with head. head
•Step 5 x2000 20 x3000 30 x4000 40 x2000
• Keep moving the temp to its next node until it
x2000 x3000 x4000
reaches to the desired node in the list (until temp x4000
→ data is not equal to ele and temp != head). temp new_node
•Step 6 - 10 NULL
• If(temp != head) Insert after 30
• Set new_node → next = temp →next; x1000
• Set temp →next=new_node; new_node → next = temp →next
x1000
• else if(temp == head) temp →next=new_node;
• Display error message “element not found”.

Dr Somaraju Suvvari
42
NITP -- CS3401
Display/Traversing
head
It Is a process of visiting each node and displaying 20 x3000 30 x1000 10 X2000
x2000
the data in it.
x2000 x3000 x1000

•Step 1 List elements are: 20


• Check whether list is Empty (head == NULL) temp
•Step 2 head
• If list is Empty then, display 'List is Empty!!!' and 20 x3000 30 x1000 10 X2000
x2000
terminate the function.
•Step 3 x2000 x3000 x1000
• If list is Not Empty then, define a Node
pointer 'temp' and initialize with head. temp
•Step 4
List elements are: 20, 30
• Keep displaying temp → data until temp reaches to the
last node. head
x2000 20 x3000 30 x1000 10 X2000

x2000 x3000 x1000

Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (n) temp


List elements are: 20, 30, 10
= Ɵ (n) Dr Somaraju Suvvari 43
NITP -- CS3401
Delete in CSLL
• Delete a node from the beginning
• Delete a node from the end of the list
• Delete a specific node
• Delete a node after a specific element

Practice question – Write the code for the above tasks in CSLL

Dr Somaraju Suvvari
44
NITP -- CS3401
Doubly Linked List
Node
• A doubly linked list or a two-way linked list is a more prev Data next
complex type of linked list which contains
a pointer to the next as well as the previous node in the
Points to the previous node Points to the next node
sequence.
• It is a sequence of elements/nodes and each element
consists of three components,
• Data: data / value of an element struct Node
• Next: pointer points to the next node in a list {
• Prev: pointer points to the previous node in a list int data;
struct Node *next;
struct Node *prev;
head };
NULL 10 30 20 NULL

head
NULL 10 x1000 x500 30 x2000 x1000 20 NULL

x500 x1000 x2000


Dr Somaraju Suvvari 45
NITP -- CS3401
Important things to remember
• In double linked list,

• The first node must be always pointed by head.

• Always the previous field of the first node must be NULL.

• Always the next field of the last node must be NULL.

Dr Somaraju Suvvari 46
NITP -- CS3401
DLL – Insert_End()
The setup
Initially the node structure will be defined and header node will be created.
typedef struct Node DLL;
struct Node
{
int data;
DLL *prev;
DLL *next;
}*head = NULL;
head
NULL

x100

Dr Somaraju Suvvari 47
NITP -- CS3401
Struct Node *new_node;
DLL - Insert at beginning new_node = (struct Node*)malloc(sizeof(struct Node));
• Step 1 New_nodeprev=new_nodenext=NULL;
• Create a new_node with given value and new_node → new_node
1
prev as NULL and new_nodenext as NULL. NULL 10 NULL
• Step 2 x1000
• Check whether list is Empty (head == NULL) 2 head
• Step 3 NULL new_node
NULL 10 NULL
• If list is Empty then, assign new_node to head, and return.
head x1000
• Step 4
x1000
• If list is not Empty then, assign head to new_node → next,
new_node to headprev, and new_node to head.
NULL 10 NULL
3
x1000
head head
x2000 x1000
4 NULL 20 NULL
x2000
new_node new_node
NULL 10 NULL x1000 20 NULL
NULL 10 x2000
x1000 x1000
Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (1) x2000
Dr Somaraju Suvvari 48
NITP -- CS3401
Struct Node *new_node;
DLL - Insert at end new= (struct Node*)malloc(sizeof(struct Node
• Step 1 newprev=newnext=NULL;
• Create a new_node with given value and new_node → prev and new_node
new_nodenext as NULL. NULL 10 NULL
• Step 2
x1000
• Check whether list is Empty (head == NULL) head
• Step 3 NULL new_node
NULL 10 NULL
• If list is Empty then, assign new_node to head and return.
head x1000
• Step 4 x1000
• If list is not Empty, then, define a node pointer temp and initialize NULL 10 NULL
with head.
x1000
• Step 5
• Keep moving the temp to its next node until it reaches to the last
node in the list (until temp → next is not equal to NULL).
• Step 6
• set temp → next =new_node and new_node → prev=temp.

Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (n)

= Ɵ (n) Dr Somaraju Suvvari


49
NITP -- CS3401
DLL - Insert at end
new_node→prev=temp

temp → next =new_node


Insert
10

new_node
head x2000 NULL 20 x3000 x2000 30 x4000 x3000 40 NULL NULL 10 NULL

x2000 x3000 x4000 x1000


x4000 x1000
temp

Dr Somaraju Suvvari
50
NITP -- CS3401
DLL - Insert after a specific location
• Step 1
• Create a new_node with given value and initialize its
• Step 6
pointers with NULL. • If temp is NULL then Display 'Given node
is not found in the list!!! Insertion not
• Step 2
possible!!!' and terminate the function.
• Check whether list is Empty (head == NULL)
• Step 7
• Step 3
• new_node→prev=temp,
• If list is Empty then display insertion is not possible,
new_node → next=tempnext;
and return. tempnext=new_node;
• Step 4 // check if there are nodes or not after temp
• If list is not Empty then, define a pointer temp and if(new_nodenext != NULL)
initialize temp with head. new_node->next-
>prev=new_node;
• Step 5
• Keep moving the temp until it reaches to the node after Time Complexity = Ɵ (n)
which we want to insert the new_node or the list
exhausted ( while( (temp→data!= desired) && temp!
=NULL) ).

Dr Somaraju Suvvari
51
NITP -- CS3401
DLL - Insert after a specific location
new_node
NULL 10 NULL
Insert
head 10 after
x1000 30
x2000
NULL 20 x3000 x2000 30 x4000 x3000 40
head
x2000 x3000 x4000
NULL

x1000
head x1000
temp
Insertion is not possible x2000
NULL 20 x3000 x2000 30 x4000 x3000 40 NULL
1. new_nodeprev= temp; 3
x2000 x3000 4 1new_node 2 x4000
2.new_nodenext= tempnext;
NULL 10 NULL
3. tempnext->prev= new_node; x3000
x1000 x4000
4. tempnext=new_node; NULL 20 x3000 x2000 30 x1000 x3000 10 x4000 x1000 40 NULL
x2000
head x2000 x3000 x1000 x4000
Dr Somaraju Suvvari
52
NITP -- CS3401
DLL - Insert before a specific location
• Step 1
• Step 6
• Create a new_node with given value and
• If list is exhausted then display 'Given node
initialize its pointers with NULL.
is not found in the list!!! Insertion is not
• Step 2 possible!!!' and terminate the function.
• Check whether list is Empty (head == NULL) • Step 7
• Step 3 • new_node→prev=tempprev,
• If list is Empty then Display insertion is not new_nodenext = temp;
possible, return; tempprev=new_node;
• Step 4 // if temp is not the first node in the list
• If list is not Empty then, define a if(new_nodeprev != NULL)
pointer temp and initialize temp with head.
new_nodeprevnext =new_node;
• Step 5 else // new_node is new first node
• Keep moving the temp until it reaches to the head = new_node;
node before which we want to insert the
new_node (while ((temp → data!= desired)
&& temp!=NULL) ). Time Complexity = Ɵ(n)

Dr Somaraju Suvvari 53
NITP -- CS3401
DLL - Insert before a specific location
head
new_node NULL new_node
NULL 10 NULL Insertion is not possible
NULL 10 NULL
x1000
x1000
temp
head
x2000 NULL 20 x3000 x2000 30 x4000 x3000 40 NULL
x2000 x3000 x4000
NULL 10 NULL
Insert new_node
10 x1000
before
40
x1000 x1000 temp

x2000 NULL 20 x3000 x2000 30 x4000 x3000 40 NULL


head x2000 x3000 1 4 2 3 x4000
new_node
1. new_nodeprev=tempprev; x3000 NULL 10 NULL
3. tempprev=new_node;
2. new_nodenext= temp; x1000 x4000 4. new_nodeprevnext = new_node;
Dr Somaraju Suvvari
54
NITP -- CS3401
DLL - Delete Delete a node from the beginning of the list

Step 1 - Check whether list is Empty (head == NULL) head temp


Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not x2000
possible' and terminate the function. head
NULL 20 NULL
NULL
Step 3 - If list is not Empty then, define a Node pointer 'temp' and
initialize with head. x2000
Step 4 - Check whether list is having only one node (temp → next
== NULL)
Step 4.1 - If list is having only one node, then set head to NULL and
delete temp (Setting Empty list conditions)
Step 4.2 - If list is with more than one node, then assign temp →
next to head, NULL to head → prev and delete temp.
head temp head=tempnext;
x2000 headprev=NULL;
NULL 20 x3000 x2000 30 NULL
Time Complexity = Ɵ (1)
x2000 x3000
head
x3000
NULL 30 NULL

x3000
Dr Somaraju Suvvari 55
NITP -- CS3401
DLL - Delete Delete a node from the end of the list

head temp
Step 1 - Check whether list is Empty (head == NULL)
x2000
Step 2 - If list is Empty, then display 'List is Empty!!! Deletion head
is not possible' and terminate the function. NULL 20 NULL
NULL
Step 3 - If list is not Empty then, define a Node pointer 'temp' x2000
and initialize with head.
Step 4 - Check whether list has only one Node (temp → next =
=NULL)
Step 5 - If list has only one node, then assign NULL to head head temp
and delete temp. Terminate the function. (Setting Empty list x2000
condition)
Step 6 - If list has more than one node, then keep moving temp NULL 20 x3000 x2000 30 NULL

until it reaches to the last node in the list. (until temp → next is x2000 x3000
equal to NULL)
tempprevnext=NULL;
Step 7 - Assign NULL to temp → previous → next and delete
temp.
Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (n) + Ɵ (1 )

= Ɵ (n) NULL 20 NULL


x2000
head x2000
Dr Somaraju Suvvari 56
NITP -- CS3401
DLL - Delete
Delete a node after a specific node in the list

Step 1 - Check whether list is Empty (head == NULL)


Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is
not possible' and terminate the function.
Step 3 - If it is not Empty, then define a Node pointer 'temp' and Delete a node after node data is 20
initialize with head and define another pointer ptr.
Step 4 - Keep moving the temp until it reaches to the location
after a node to be deleted or to the last node.(while
(tempdata != desired && tempnext!= NULL)).
head
Step 5 – If temp points to last node(tempnext== NULL), then
x2000
display 'Deletion is not possible!' and terminate the
function. NULL 20 x3000 x2000 30 x1000 x3000 10 NULL

Step 6 – Set ptr to tempnext; x2000 x3000 x1000


Set tempnext=ptrnext;
temp ptr
ptrnextprev=temp;
free(ptr); tempnext=ptrnext;
head
ptrnextprev=temp;
x2000 Free(ptr)
Time Complexity = Ɵ (n)
NULL 20 x1000 x2000 10 NULL

x2000
Dr Somaraju Suvvari x1000 57
NITP -- CS3401
DLL - Delete
Delete a specific node in the list
Step 8 - If list contains multiple nodes, then check whether
Step 1 - Check whether list is Empty (head == NULL) temp is the first node in the list (temp == head).
Step 2 - If it is Empty then, display 'List is Empty!!! Step 9 - If temp is the first node, then move the head to the
Deletion is not possible' and terminate the function. next node (head = head → next), set head of previous to
Step 3 - If it is not Empty, then define a Node pointer 'temp' NULL (head → previous = NULL) and delete temp,
and initialize with head. terminate the function.
Step 4 - Keep moving the temp until it reaches to the exact Step 10 - If temp is not the first node, then check whether it is
node to be deleted or there are no nodes left (while the last node in the list (temp → next == NULL).
tempdata != desired && temp != NULL). Step 11 - If temp is the last node then set temp of previous of
Step 5 - If the list exhausted, then display 'Given node not next to NULL (temp → previous → next = NULL) and
found in the list! Deletion not possible!!!' and terminate. delete temp (free(temp)), terminate the function.
Step 6 - If it is reached to the exact node which we want to Step 12 - If temp is not the first node and not the last node,
delete, then check whether list is having only one node or then set temp of previous of next to temp of next (temp →
not (tempprev == head and tempnext == NULL). previous → next = temp → next), temp of next of previous
Step 7 - If list has only one node and that is the node which to temp of previous (temp → next → previous = temp →
is to be deleted then set head to NULL and delete temp previous) and delete temp (free(temp)).
(free(temp)), terminate the function.
Time Complexity = Ɵ (n)

Dr Somaraju Suvvari 58
NITP -- CS3401
Display

• Step 1 - Check whether list is Empty (head == NULL)

• Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.

• Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize
with head.

• Step 4 - Keep displaying temp → data until temp reaches to the last node.

Dr Somaraju Suvvari 59
NITP -- CS3401
Circular Doubly Linked List
Node
• Any linked list is called circular doubly linked list if it is a prev Data next
doubly linked list and the first node of the list must be
connect to the last node and last node must be connected to
Points to the previous node Points to the next node
the first node.
• It is a sequence of elements/nodes and each element
consists of three components,
• Data: data / value of an element struct Node
• Next: pointer points to the next node in a list {
• Prev: pointer points to the previous node in a list int data;
struct Node *next;
struct Node *prev;
head };
10 30 20

Dr Somaraju Suvvari 60
NITP -- CS3401
CDLL – Insert_End()
The setup
Initially the node structure will be defined and header node will be created.
typedef struct Node DLL;
struct Node
{
int data;
DLL *prev;
DLL *next;
}*head = NULL;
head
NULL

x100

Dr Somaraju Suvvari 61
NITP -- CS3401
Struct Node *new_node;
CDLL - Insert at beginning new_node = (struct Node*)malloc(sizeof(struct Node));
• Step 1 New_nodeprev=new_nodenext=NULL;
• Create a new_node with given value and new_node → new_node
prev as NULL and new_nodenext as NULL. 1
NULL 10 NULL
• Step 2
x1000
• Check whether list is Empty (head == NULL) 2 head
• Step 3 NULL new_node
• If list is Empty then, assign new_node to head, NULL 10 NULL
new_nodeprev, and to new_nodenext; terminate the head x1000
function. x1000
• Step 4 X1000 10 X1000
3
• If list is not Empty then, assign head to new_node → next, x1000
headprev to new_nodeprev, new_node to
headprevnext, new_node to headprevnext, and
new_node to head. head
x1000
4 X2000 20 X2000
x2000
head x2000
new_node new_node
NULL 10 NULL X2000 10 x1000 20 X1000
x2000
x1000 x1000
Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (1) x2000
Dr Somaraju Suvvari 62
NITP -- CS3401
Struct Node *new_node;
CDLL - Insert at end new= (struct Node*)malloc(sizeof(struct Node
• Step 1 newprev=newnext=NULL;
• Create a new_node with given value and new_node → prev and new_node
new_nodenext as NULL. NULL 10 NULL
• Step 2
x1000
• Check whether list is Empty (head == NULL) head
• Step 3 NULL new_node
NULL 10 NULL
• If list is Empty then, assign new_node to head, new_nodeprev,
and to new_nodenext; terminate the function. x1000
• Step 4
• If list is not Empty, then, define a node pointer temp and initialize
with head. head
• Step 5 x1000
• If the list is not empty, then X1000 10 X1000
• Set head to new_nodenext, headprev to new_nodeprev, x1000
new_node to headprev, new_node to headprevnext.

Time Complexity = Ɵ (1) + Ɵ (1) + Ɵ (1) + Ɵ (1)


Dr Somaraju Suvvari
= Ɵ (1) NITP -- CS3401
63
CDLL - Insert at end
1. new_nodenext = head

2. new_nodeprev = headprev
Insert
3. headprevnext = new_node 10

4. headprev = new_node
new_node
head x2000 x4000 20 x3000 x2000 30 x4000 x3000 40 X2000 NULL 10 NULL
x2000 x3000 x4000 x1000

x4000 X2000 x1000

x1000

Dr Somaraju Suvvari
64
NITP -- CS3401
CDLL - Insert after a specific location
• Step 1
• Create a new_node with given value and initialize its
• Step 6
pointers with NULL. • If temp is equal to head then Display
'Given node is not found in the list!!!
• Step 2
Insertion not possible!!!' and terminate
• Check whether list is Empty (head == NULL) the function.
• Step 3 • Step 7
• If list is Empty then display insertion is not possible,
• new_node→prev=temp,
and return.
new_node → next=tempnext;
• Step 4 tempnext=new_node;
• If list is not Empty then, define a pointer temp and new_node->next->prev=new_node;
initialize temp with head.
• Step 5
• Keep moving the temp until it reaches to the node after
which we want to insert the new_node or the list
exhausted (while( (temp→data!= desired) && temp!
=head) ).
Time Complexity = Ɵ (n)
Dr Somaraju Suvvari
65
NITP -- CS3401
CDLL - Insert after a specific location
new_node Insert
NULL 10 NULL 10 after
30
head
x1000
x2000
X4000 20 x3000 x2000 30 x4000 x3000 40 X2000
head
x2000 x3000 x4000
NULL

head x1000

Insertion is not possible x2000 x1000


X4000 20 x3000 x2000 30 x4000 x3000 40 X2000

1. new_nodeprev= temp; 4
x2000 x3000 3 1new_node 2 x4000
2. new_nodenext= tempnext; temp NULL 10 NULL
3. tempnext=new_node; x1000
x3000
4. new_nodenext->prev= new_node; x4000
Dr Somaraju Suvvari
66
NITP -- CS3401
CDLL - Insert before a specific location
• Step 1
• Step 6
• Create a new_node with given value and
• If list is exhausted then display 'Given node is not
initialize its pointers with NULL.
found in the list!!! Insertion is not
• Step 2 possible!!!' and terminate the function.
• Check whether list is Empty (head == NULL) • Step 7
• Step 3 • new_node→prev=tempprev,
• If list is Empty then Display insertion is not new_nodenext = temp;
possible, return; new_nodeprevnext = new_node;
• Step 4 tempprev =new_node;
• If list is not Empty then, define a // if temp is the previous first node then update head
pointer temp and initialize temp with head.
if(temp == head )
• Step 5
head = new_node;
• Keep moving the temp until it reaches to the
node before which we want to insert the
new_node (while ((temp → data!= desired)
&& temp!=head) ). Time Complexity = Ɵ(n)

Dr Somaraju Suvvari 67
NITP -- CS3401
CDLL - Delete Delete a node from the beginning of the list

Step 1 - Check whether list is Empty (head == NULL) head temp


Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not x2000
possible' and terminate the function. head
Step 3 - If list is not Empty then, define a Node pointer 'temp' and
x2000 20 X2000
NULL
initialize with head.
Step 4 - Check whether list is having only one node (temp → next x2000
== temp && tempprev = temp)
Step 4.1 - If list is having only one node, then set head to NULL and
delete temp (Setting Empty list conditions)
Step 4.2 - If list is with more than one node, then assign temp →
next to head, tempnext to tempprevnext, tempprev to
tempnextprev, and delete temp.
head temp
x2000
X3000 20 x3000 x2000 30 X2000
Time Complexity = Ɵ (1)
x2000 x3000
head
x3000
X3000 30 X3000

x3000
Dr Somaraju Suvvari 68
NITP -- CS3401
CDLL - Delete Delete a node from the end of the list

head temp
Step 1 - Check whether list is Empty (head == NULL)
x2000
Step 2 - If list is Empty, then display 'List is Empty!!! Deletion head
is not possible' and terminate the function. X2000 20 X2000
NULL
Step 3 - If list is not Empty then, define a Node pointer 'temp' x2000
and initialize with headprev.
Step 4 - Check whether list has only one Node (temp → next =
=head)
Step 5 - If list has only one node, then assign NULL to head head temp
and delete temp. Terminate the function. (Setting Empty list x2000
condition)
Step 6 - If list has more than one node, Assign head to temp →
X3000 20 x3000 x2000 30 X2000

previous → next, tempprev to headprev and delete temp. x2000 x3000

Time Complexity = Ɵ (1)

X2000 20 X2000
x2000
head x2000
Dr Somaraju Suvvari 69
NITP -- CS3401
CDLL - Delete
Delete a node after a specific node in the list

Step 1 - Check whether list is Empty (head == NULL)


Step 2 - If list is Empty then, display 'List is Empty!!! Deletion is

not possible' and terminate the function. Delete a node after node data is 20
Step 3 - If list is not Empty, then define a Node pointer 'temp'
and
initialize with head also define a Node pointer ptr.
Step 4 - Keep moving the temp until it reaches to the location
after a node to be deleted or to the last node.(while
(tempdata != desired && tempnext != head)).
Step 5 – If temp points to last node(tempnext== head), then
X1000 20 x3000 x2000 30 x1000 x3000 10 X2000
display 'Deletion is not possible!' and terminate
x2000 the
function. head x2000 x3000 x1000
Step 6 – Set ptr to tempnext;
temp ptr
Set tempnext=ptrnext;
ptrnextprev=temp;
free(ptr); head x2000 X1000 20 x1000 x2000 10 X2000
Time Complexity = Ɵ (n)
x2000 x1000
Dr Somaraju Suvvari
70
NITP -- CS3401
Display

• Step 1 - Check whether list is Empty (head == NULL)

• Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.

• Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize
with head.

• Step 4 - Keep displaying temp → data until temp reaches to the last node.

Dr Somaraju Suvvari 71
NITP -- CS3401
Complexity Analysis
Data
Time Complexity
Structure
Insertion Insertion Insertion Deletion Deletion Deletion
Search at at after/before/at at at after/before/at
Beginning End a specific node Beginning End a specific node
Singly-
Linked List
Θ(n) Θ(1) Θ(n) Θ(n) Θ(1) Θ(n) Θ(n)
Circular
Θ(n) Θ(n) Θ(n)
Θ(n)
Singly- Θ(n) Θ(n) Θ(n)
Linked List

Doubly- Θ(1)
Linked List
Θ(n) Θ(1) Θ(n) Θ(n) Θ(n) Θ(n)

Circular Do Θ(n) Θ(1) Θ(1) Θ(1)


ubly- Θ(n) Θ(1) Θ(n)
Linked List

Dr Somaraju Suvvari 72
NITP -- CS3401
Thank You

Dr Somaraju Suvvari
73
NITP -- CS3401

You might also like