0% found this document useful (0 votes)
17 views57 pages

Unit 3

The document provides an overview of data structures and algorithms, focusing on linked lists, stacks, and queues using C and Python. It covers the types of linked lists, their advantages and disadvantages, basic operations, and complexity analysis. Additionally, it includes detailed explanations of insertion and deletion operations within linked lists.

Uploaded by

provokingdiaries
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)
17 views57 pages

Unit 3

The document provides an overview of data structures and algorithms, focusing on linked lists, stacks, and queues using C and Python. It covers the types of linked lists, their advantages and disadvantages, basic operations, and complexity analysis. Additionally, it includes detailed explanations of insertion and deletion operations within linked lists.

Uploaded by

provokingdiaries
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/ 57

DATA STRUCTURES AND ALGORITHMS

Venkata Ramana S
Assistant Professor
CSE,MRECW

MALLA REDDY ENGINEERING COLLEGE FOR


WOMEN (2405ES02) DATA STRUCTURES AND
Data Structures & Algorithms

UNIT-III Linked Lists using C & Python: Singly linked lists: Representation in
memory, Algorithms of several operations: Traversing, Searching, Insertion into,
Deletion from linked list; Linked representation of Stack and Queue. Doubly
Linked List: operations on it and algorithmic analysis. Circular Linked List: all
operations on it. Applications of Linked List.
Data Structures & Algorithms

UNIT-I Introduction: Basic Terminologies: Elementary Data Organizations. Data


Structure Operations: insertion, deletion, traversal etc. Analysis of an Algorithm,
Asymptotic Notations, Time-Space trade off. Searching: Linear Search and Binary
Search Techniques implementation using C & Python and their complexity analysis.
UNIT-II Stacks and Queues using C& Python: ADT Stack and its operations:
Algorithms and their complexity analysis, Applications of Stacks: Expression
Conversion and evaluation – corresponding algorithms and complexity analysis. ADT
Queue: Types of Queue: Simple Queue, Circular Queue, Priority Queue. Double ended
Queue and Operations on each types of Queues and Algorithms. Applications of queues.
What is a Linked List?
Linked Lists
1. A linked list is a linear data structure that stores a collection of data
elements dynamically.
2. Nodes represent those data elements, and links or pointers connect each
node.
3. Each node consists of two fields, the information stored in a linked list and
a pointer that stores the address of its next node.
4. The last node contains null in its second field because it will point to no
node.
5. A linked list can grow and shrink its size, as per the requirement.
6. It does not waste memory space.
5

Uses of Linked List

 The list is not required to be contiguously present in the memory.


 This achieves optimized utilization of space.
 Doesn't need to be declared in advance.
 Empty node can not be present in the linked list.
 We can store values of primitive types or objects in the singly linked list.
Linked List Vs. Array
Array contains following limitations:
1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process. It is almost impossible to expand
the size of the array at run time.
3. All the elements in the array need to be contiguously stored in the memory. Inserting
any element in the array needs shifting of all its predecessors.
Linked list is the data structure which can overcome all the limitations of an array. Using
linked list is useful because,
4. It allocates the memory dynamically. All the nodes of linked list are non-contiguously
stored in the memory and linked together with the help of pointers.
5. Sizing is no longer a problem since we do not need to define its size at the time of
declaration. List grows as per the program's demand and limited to the available
memory space.
7

Advantages of Linked list


The advantages of using the Linked list are given as follows -
Dynamic data structure - The size of the linked list may vary according
to the requirements. Linked list does not have a fixed size.
Insertion and deletion - Unlike arrays, insertion, and deletion in linked
list is easier. Array elements are stored in the consecutive location,
whereas the elements in the linked list are stored at a random location.
To insert or delete an element in an array, we have to shift the elements
for creating the space. Whereas, in linked list, instead of shifting, we just
have to update the address of the pointer of the node.
Memory efficient - The size of a linked list can grow or shrink according
to the requirements, so memory consumption in linked list is efficient.
Implementation - We can implement both stacks and queues using
linked list.
Disadvantages of Linked list
The limitations of using the Linked list are given as follows -
 Memory usage - In linked list, node occupies more memory than array.
Each node of the linked list occupies two types of variables, i.e., one is a
simple variable, and another one is the pointer variable.
 Traversal - Traversal is not easy in the linked list. If we have to access
an element in the linked list, we cannot access it randomly, while in case
of array we can randomly access it by index. For example, if we want to
access the 3rd node, then we need to traverse all the nodes before it.
So, the time required to access a particular node is large.
 Reverse traversing - Backtracking or reverse traversing is difficult in a
linked list. In a doubly-linked list, it is easier but requires more memory
to store the back pointer.
9

Applications of Stacks
The applications of the Linked list are given as follows -
 With the help of a linked list, the polynomials can be represented as well
as we can perform the operations on the polynomial.
 A linked list can be used to represent the sparse matrix.
 The various operations like student's details, employee's details, or
product details can be implemented using the linked list as the linked
list uses the structure data type that can hold different data types.
 Using linked list, we can implement stack, queue, tree, and other
various data structures.
 The graph is a collection of edges and vertices, and the graph can be
represented as an adjacency matrix and adjacency list. If we want to
represent the graph as an adjacency matrix, then it can be implemented
as an array. If we want to represent the graph as an adjacency list, then
it can be implemented as a linked list.
 A linked list can be used to implement dynamic memory allocation. The
dynamic memory allocation is the memory allocation done at the run-
10

Basic Operations in Linked List

The basic operations in the linked lists are


 Insertion − Adds an element at the beginning of the list.
 Deletion − Deletes an element at the beginning of the list.
 Display − Displays the complete list.
 Search − Searches an element using the given key.
 Delete − Deletes an element using the given key.
Complexity

ata Time Complexity Space


Structure Compl
eity

Average Worst Worst

Access Search Insertio Deletion Access Search Insertio Deletion


n n

Singly Linked θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
List
Types of Linked list
The following are the types of linked list:

1. Singly Linked list


2. Doubly Linked list
3. Circular Linked list
4. Doubly Circular Linked list
1. Singly Linked list
1. It is the commonly used linked
list in programs.
2. If we are talking about the linked
list, it means it is a singly linked
list.
3. The singly linked list contains two
parts, i.e., one is the data part,
and the other one is the address
part, which contains the address
of the next or the successor
node. The address part in a node
is also known as a pointer.
1. Singly Linked list

The first node contains the address of the next node, i.e., 200,
the second node contains the address of the last node, i.e.,
300, and the third node contains the NULL value in its address
part as it does not point to any node.
1. Need Self 2. Structure
Referential Structure of Node

3. One Pointer 4.
allow Example
For Connecting Third nodes ,We have Two
methods
Linked list implementation of stack

Before we implement actual operations, first we need to set up


an empty list. First, perform the following steps before
implementing actual operations.
 Step 1 - Include all the header files which are used in the program.
 Step 2 - Declare all the user defined functions.
 Step 3 - Define a Node structure with two members data and next
 Step 4 - Define a Node pointer 'head' and set it to NULL.
 Step 5 - Implement the main method by displaying operations
menu and make suitable function calls in the main method to
perform user selected operation.
Basic Operations in Single Linked List

The basic operations in the Single linked lists are

1. Insertion − Adds an element at the beginning of the list.


2. Deletion − Deletes an element at the beginning of the list.
3. Display − Displays the complete list.
4. Search − Searches an element using the given key.
5. Delete − Deletes an element using the given key.
Inserting
Insertion in linked list can be done in three different ways. They
are explained as follows –
 Insertion at Beginning
 Insertion at Ending
 Insertion at a Given Position
Inserting at Beginning of the list
We can use the following steps to insert a new node at
beginning of the single linked list.
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, set newNode→next = NULL and
head = newNode.
Step 4 - If it is Not Empty then, set newNode→next = head
and head = newNode.
Insertion at Ending

We can use the following steps to insert a new node at


end of the single linked list.
Step 1 - Create a newNode with given value and newNode →
next as NULL.
Step 2 - Check whether list is Empty (head == NULL).
Step 3 - If it is Empty then, set head = newNode.
Step 4 - If it is Not Empty then, define a node pointer temp and
initialize with head.
Step 5 - 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 6 - Set temp → next = newNode.
Inserting At Specific location in the list (After a Node)
 In this operation, we are adding an element at any position within the list. To insert a new
node at a specific position, we need to traverse the list to position – 1.
 If the position is valid, we adjust the pointers similarly such that the next pointer of the new
node points to the next of current nod and next pointer of current node points to
the new node.
We can use the following steps to insert a new node after a node in the single
linked list.
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL).
Step 3 - If it is Empty then, set newNode → next = NULL and head = newNode.
Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the node after which we
want to insert the newNode (untiltemp1 → data is equal to location, here relocation is the
node value after which we want to insert the newNode).
Step 6 - Every time check whether temp is reached to last node or not. If it is reached to last
node then display 'Given node is not found in the list!!! Insertion not possible!!!' and
terminate the function. Otherwise move the temp to next node.
Step 7 - Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'.
Deletion

 Deletion in linked list can be done in three different ways. They are explained as
follows –
1. Deletion at Beginning We can use the following steps to delete a node from
2. Deletion at Ending beginning of the single linked list.
3. Deletion at Given Position 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 initialize with head.
Step 4 - Check whether list is having only one node (temp → next ==
NULL)
Step 5- If it is TRUE then set head = NULL and delete temp (Setting
Empty list conditions)
Step 6 - If it is FALSE then set head = temp → next, and delete temp.
Data Structures & Algorithms

Deletion in singly linked list at the end


There are two scenarios in which, a node is deleted from the end of the linked list.
1. There is only one node in the list and that needs to be deleted.
2. There are more than one node in the list and the last node of the list will be deleted.

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 two Node pointers
'temp1' and 'temp2' and initialize 'temp1' with head.
Step 4 - Check whether list has only one Node (temp1 → next == NULL)
Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the
function. (Setting Empty list condition)
Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next
node. Repeat the same until it reaches to the last node in the list. (Until temp1 →
next == NULL)
Step 7 - Finally, Set temp2 → next = NULL and delete temp1
Deletion in singly linked list after the specified node
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 8 - If list contains multiple nodes, then check whether temp1 is
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' the first node in the list (temp1 == head).
and initialize 'temp1' with head Step 9 - If temp1 isthe first node then move the head to the next node
Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted (head = head → next) and delete temp1.
or to the last node. And every time set 'temp2 = temp1' before moving the Step 10 - If temp1 is not first node then check whether it is last node
'temp1' to its next node. in the list (temp1 → next == NULL).
Step 5 - If it is reached to the last node then display 'Given node not found in Step11 – If temp1 is last node then set temp2 → next = NULL and
the list! Deletion not possible!!!'. And terminate the function. delete temp1 (free (temp1)).
Step 6 - If it is reached to the exact node which we want to delete, then check Step 12 - If temp1 is not first node and not last node then set temp2
whether list is having only one node or not → next = temp1 → next and delete temp1 (free (temp1)).
Step 7 - If list has only one node and that is the node to be deleted, then set
head = NULL and delete temp1 (free (temp1)).
Traversing/ Displaying a Single Linked List

We can use the following steps to display the elements of a single linked list...
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 with an arrow (--->) until temp reaches to
the last node .
Step 5 - Finally display temp → data with arrow pointing to NULL (temp → data ---
> NULL).
Single Link List Using C
Data Structures & Algorithms
Stack Using Linked List

 The major problem with the stack implemented using an array is, it works only for affixed number of data
values. That means the amount of data must be specified at the beginning of the implementation itself.
 above example, the last
 Stack implemented using an array is not suitable, when we don't know the size of data which we are going to
inserted node is 99 and
use. A stack data structure can be implemented by using a linked list data structure.
the first inserted node
is 25.The order of  The stack implemented using linked list can work for an unlimited number of values. That means, stack In the
elements inserted is 25,
32, 50 and 99.
implemented using linked list works for the variable size of data. So, there is no need to fix the size at the
beginning of the implementation.
 The Stack implemented using linked list can organize as many data values as we want. In linked list
implementation of a stack, every new element is inserted as 'top ‘element. That means every newly inserted
element is pointed by 'top'.
 Whenever we want to remove an element from the stack, simply remove the node which is pointedly 'top' by
moving 'top' to its previous node in the list. The next field of the first element must be always NULL.
Data Structures & Algorithms

Stack Operations using Linked List


To implement a stack using a linked list, we need to set the following things
before implementing actual operations.
Step 1 - Include all the header files which are used in the program. And declare
all the user defined functions.
Step 2 - Define a 'Node ‘structure with two members data and next.
Step 3 - Define a Node pointer 'top' and set it to NULL.
Step 4 - Implement the main method by displaying Menu with list of operations
and make suitable function calls in the main method
Data Structures & Algorithms

push(value) - Inserting an element into the pop() - Deleting an Element from a Stack
Stack We can use the following steps to delete a node from the

We can use the following steps to insert a new node stack...

into the stack... Step 1 - Check whether stack is Empty (top == NULL).

Step 1 - Create a newNode with given value. Step 2 - If it is Empty, then display "Stack is Empty!!!

Step 2 - Check whetherstack is Empty (top == NULL) Deletion is notpossible!!!" and terminate the function

Step 3 - If it is Empty, then set newNode → next Step 3 - If it is Not Empty, then define a Node pointer

=NULL. 'temp' and set it to 'top'.

Step 4 - If it is Not Empty, then set newNode → next Step 4 - Then set 'top = top → next'.

= top. Step 5 - Finally, delete 'temp'. (free(temp)).

Step 5 - Finally, set top = newNode.


Data Structures & Algorithms

Display () - Displaying stack of elements


We can use the following steps to display the elements (nodes) of a stack...
Step 1 - Check whether stack is Empty (top == NULL).
Step 2 - If it is Empty, then display 'Stack is Empty!!!' and terminate the
function.
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and initialize
with top.
Step 4 - Display 'temp → data --->' and move itto the next node. Repeat the
sameuntil temp reaches to the first node in the stack. (temp → next != NULL).
Step 5 - Finally! Display 'temp → data --->NULL'.
Stack Using Linked List in
C
Data Structures & Algorithms

Queue Using Linked List


1. The major problem with the queue implemented using an array is, It will work for
an only fixed number of data values. That means, the amount of data must be
specified at the beginning itself.
2. Queue using an array is not suitable when we don't know the size of data which we
are going to use.
3. The queue which is implemented using a linked list can work for an unlimited
number of values. That means, queue using linked list can work for the variable
size of data (No need to fix the size at the beginning of the implementation).
4. The Queue implemented using linked list can organize as many data values as we
want. In linked list implementation of a queue, the last inserted node is always
pointed by 'rear' and the first node is always pointed by 'front'.
Data Structures & Algorithms

Operations
To implement queue using linked list, we need to set the following things before implementing actual operations.
Step 1 - Include all the header files which are used in the program. And declare all the user defined functions.
Step 2 - Define a 'Node ‘structure with two members data and next.
Step 3 - Define two Node pointers 'front' and 'rear' and set both to NULL.
Step 4 - Implement the main method by displaying Menu of list of operations and make suitable function
calls in the main method to perform user selected operation.

enQueue(value) - Inserting an element into the Queue


We can use the following steps to insert a new node into the queue...
Step 1 - Create a newNode with given value and set 'newNode → next' to NULL.
Step 2 - Check whether queue is Empty (rear == NULL)
Step 3 - If it is Empty then, set front = newNode and rear = newNode.
Step 4 - If it is Not Empty then, set rear → next = newNode and rear = newNode.deQueue().
Data Structures & Algorithms

Deleting an Element from Queue


We can use the following steps to delete a node from the queue...
Step 1 - Check whether queue is Empty (front == NULL).
Step 2 - If it is Empty, then display "Queue is Empty!!! Deletion is notpossible!!!" and terminate from the function
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
Step 4 - Then set 'front = front → next' and delete 'temp' (free(temp)).

display() - Displaying the elements of Queue


We can use the following steps to display the elements (nodes) of a queue...
Step 1 - Check whether queue is Empty (front == NULL).
Step 2 - If it is Empty then, display 'Queue is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with front.
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the sameuntil 'temp' reaches to
'rear' (temp → next != NULL).
Step 5 - Finally! Display 'temp → data --->NULL'.
Queue Using Linked List Using C
Double Linked List
What is Double Linked List?
1. In a single linked list, every node has a link to its next node in the sequence.
2. So, we can traverse from one node to another node only in one direction and we cannot traverse
back.
3. We can solve this kind of problem by using a double linked list.
Double Linked List
1. In a double linked list, every node has a link to its previous node and next
node.
2. So, we can traverse forward by using the next field and can traverse
backward by using the previous field.
3. Every node in a double linked list contains three fields.
4. Here, 'link1' field is used to store the address of the previous node in the
sequence, 'link2' field is used to store the address of the next node in the
sequence and 'data' field is used to store the actual value of that node.
Operations on Double Linked List
Operations on Double Linked List
In a double linked list, we perform the following operations... Inserting At Beginning of the list
1. Insertion
2. Deletion
3. Display We can use the following steps to insert a new
Insertion node at beginning of the double linked list...
In a double linked list, the insertion operation can be performed in three ways as
follows... Step 1 - Create a newNode with given value and
1. Inserting At Beginning of the list
newNode → previous as NULL. Step 2 - Check
2. Inserting At End of the list
3. Inserting At Specific location in the list whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to
Deletion
1. Deleting from Beginning of the list newNode → next and newNode to head.
2. Deleting from End of the list
3. Deleting a Specific Node
Step 4 - If it is not Empty then, assign head to
newNode → next and newNode to head.
Insertion Operations on Double Linked List
Inserting At Specific location in the list (After a
Inserting At End of the list Node)
We can use the following steps to insert a new node at We can use the following steps to insert a new node after a node
end of the double linked list... in the double linked list...
 Step 1 - Create a newNode with given value and Step 1 - Create a newNode with given value.
newNode → next as NULL. Step 2 - Check whether list is Empty (head == NULL)
 Step 2 - Check whether list is Empty (head == Step 3 - If it is Empty then, assign NULL to both newNode →
NULL) previous & newNode → next and set newNode to head.
 Step 3- If it is Empty, then assign NULL to Step 4 - If it is noEmpty then, define two node pointers temp1
newNode→previous and newNode to head. & temp2 and initialize temp1 with head.
Step 5 - Keep moving the temp1 to its next node until it
 Step 4 - If it is not Empty, then, define a node reaches to the node after which we want to insert the newNode
pointer temp and initialize with head. (until temp1 → data is equal to location, here relocation is the
 Step 5 - Keep moving the temp to its next node until node value after which we want to insert the newNode).
it reaches to the last node in the list (until temp → Step 6 - Every time check whether temp1 is reached to the last
next is equal to NULL). node. If it is reached to the last node then display 'Given node
 Step 6 - Assign newNode to temp → next and is not found in the list!!! Insertion not possible!!!' and
temp to newNode → previous. terminate the function. Otherwise move the temp1 to next
node.
Step 7 - Assign temp1 → next to temp2, newNode to temp1
→ next, temp1 to newNode→ previous, temp2 to newNode
→ next and newNode to temp2 → previous.
Deletion on Double Linked List
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from End of the list


Deleting from Beginning of the list
We can use the following stepsto delete a node from end ofthe double
We can use the following steps to delete a node from beginning of the
linked list... Step 1 - Check whether list is Empty (head == NULL)
double linked list...
Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible'
Step 1 - Check whether list is Empty (head == NULL)
and terminate the function.
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible'
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize
and terminate the function.
with head.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize
Step 4 - Check whether list has only one Node (temp → previous and temp
with head.
→ next both are NULL)
Step 4 - Check whether list is having only one node (temp → previous is
Step 5 - If it is TRUE, then assign NULL to head and delete temp. And
equal to temp→ next)
terminate from the function. (Setting Empty list condition)
Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty
Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node
list conditions)
in the list. (until temp → next is equal to NULL)
Step 6- If it is FALSE, then assign temp→next to head, NULL to head→
Step 7 - Assign NULL to temp → previous → next and delete temp.
previous and delete temp.
Deleting a Specific Node from the list (Double Linked List)
We can use the following steps to delete a specific node from the double linked 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 initialize with head.
 Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or tothe last node.
 Step 5 - If it is reached to the last node, then display 'Given node not found in thelist! Deletion not
possible!!!' and terminate the function.
 Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one
node or not
 Step 7 - If list has only one node and that is the node which is to be deleted then set head to NULL and delete
temp (free(temp)).
 Step 8 - If list contains multiple nodes, then check whether temp is the first node in the list (temp == head).
 Step 9 - If temp is the first node, then move the head to the next node (head = head → next), set head of
previous to NULL (head → previous = NULL) and delete temp.
 Step 10 - If temp is not the first node, then check whether it is the last node in the list (temp → next ==
NULL).
 Step 11 - If temp is the last node then set temp of previous of next to NULL (temp → previous → next =
NULL) and delete temp (free(temp)).
 Step 12 - If temp is not the first node and not the last node, then set temp of previous of next to temp of
next (temp → previous → next = temp → next), temp of next of previous to temp of previous (temp →
next → previous = temp→ previous) and delete temp (free(temp)).
Displaying a Double Linked List
We can use the following steps to display the elements of a double linked
list... 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 - Display 'NULL <--- '.
 Step 5 - Keep displaying temp → data with an arrow (<===>) until
temp reaches to the last node
 Step 6 - Finally, display temp → data with arrow pointing to NULL
(temp → data ---> NULL).
Implementation of Double Linked List using C

OUTPUT
Circular Linked List

1. In single linked list, every node points to its next node in the sequence and the last node points NULL.
2. But in circular linked list, every node points to its next node in the sequence but the last node points to the first
node in the list.
3. That means circular linked list is similar to the single linked list except that the last node points to the first node
in the list
Operations In a circular linked list

1. Insertion Insertion
2. Deletion In a circular linked list, the insertion operation can be performed in three ways. They are as follows...
1. Inserting At Beginning of the list
3. Display
2. Inserting At End of the list
3. Inserting At Specific location in the list

Deletion
In a circular linked list, the deletion operation can be performed in three ways those are as follows...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node
Before we implement actual operations, first we need to setup empty
list. First perform the following steps before implementing actual
operations.
 Step 1 - Include all the header files which are used in the program.
 Step 2 - Declare all the user defined functions.
 Step 3 - Define a Node structure with two members data and next
 Step 4 - Define a Node pointer 'head' and set it to NULL.
 Step 5 - Implement the main method by displaying operations menu
and make suitable function calls in the main method to perform user
selected operation.
Insertion in Circular Linked List

1. Inserting At Beginning of the list


Inserting At Beginning of the list
2. Inserting At End of the list
We can use the following steps to insert a new node at beginning of the circular linked
3. Inserting At Specific location in the list list..
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, set head = newNode and newNode→next = head.
Step 4 - If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'.
Step 5 - Keep moving the 'temp' to its next node until it reaches to the last node (until
'temp → next == head').
Step 6 - Set 'newNode → next =head', 'head = newNode' and 'temp → next = head'.
Insertion in Circular Linked List

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the circular linked
Inserting At End of the list list...
We can use the following steps to insert a new node at end of the Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL)
circular linked list...
Step 3 - If it is Empty then, set head = newNode and newNode → next = head.
Step 1 - Create a newNode with given value. Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
Step 2 - Check whether list is Empty (head == NULL). Step 5 - Keep moving the temp to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → data is equal to location, here location
Step 3 - If it is Empty then, set head = newNode and newNode →
is the node value after which we want to insert the newNode).
next = head. Step 6 - Every time check whether temp is reached to the last node or not. If it is
Step 4 - If it is Not Empty then, define a node pointer temp and reached to last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp to next node.
initialize with head.
Step 7 - If temp is reached to the exact node after which we want to insert the
Step 5 - Keep moving the temp to its next node until it reaches to newNode then check whether it is last node (temp → next == head).
the last node in the list (until temp → next == head). Step 8 - If temp is last node then set temp → next = newNode and newNode → next =
head.
Step 6 - Set temp → next = newNode and newNode → next =
Step 9- If temp is not last node then set newNode → next = temp → next and temp →
head. next = newNode.
Deletion in Circular Linked List

1. Deleting from Beginning of the list Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the circular linked list...
2. Deleting from End of the list
Step 1 - Check whether list is Empty (head == NULL)
3. Deleting a Specific Node
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 two Node pointers 'temp1' and 'temp2' and initialize
both 'temp1' and 'temp2' with head.
Step 4 - Check whether list is having only one node (temp1 → next == head)
Step 5 - If it is TRUE then set head = NULL and delete temp1 (Setting Empty list conditions)
Step 6 - If it is FALSE move the temp1 until it reaches to the last node. (until temp1 → next
== head)
Step 7 - Then set head = temp2 → next, temp1 → next = head and delete temp2.
Deletion in Circular Linked List
Deleting from End of the list
Displaying a circular Linked List
We can use the following steps to delete a node from end of the circular
We can use the following steps to display the elements of a
linked list...
circular linked 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' Step 1 - Check whether list is Empty (head == NULL)
and terminate the function. Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and the function.
'temp2' and initialize 'temp1' with head. Step 3 - If it is Not Empty then, define a Node pointer 'temp' and
Step 4 - Check whether list has only one Node (temp1 → next == head)
initialize with head.
Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And
Step 4 - Keep displaying temp → data with an arrow (--->) until
terminate from the function. (Setting Empty list condition)
temp reaches to the last node
Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its
next node. Repeat the same until temp1 reaches to the last node in the list. Step 5 - Finally display temp → data with arrow pointing to head
(until temp1 → next == head) → data.
Step 7 - Set temp2 → next = head and delete temp1.
Deleting a Specific Node from 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 two Node pointers 'temp1' and 'temp2' and initialize 'temp1' with head.

 Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted rotate last node. And every time set 'temp2 = temp1' before moving the
'temp1' to its next node.

 Step 5 - If it is reached to the last node then display 'Given node not found in the list! Deletion not possible!!!'. And terminate the function.

 Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node (temp1 → next == head)

 Step 7 - If list has only one node and that is the node to be deleted then set head = NULL and delete temp1 (free(temp1)).

 Step 8 - If list contains multiple nodes then check whether temp1 is the first node in the list (temp1 == head).

 Step 9 - If temp1 is the first node then set temp2 = head and keep moving temp2 to its next node until temp2 reaches to the last node. Then set head = head
→ next, temp2 → next = head and delete temp1.

 Step 10 - If temp1 is not first node then check whether it is last node in the list(temp1 → next == head).

 Step 1 1- If temp1 is last node then set temp2 → next = head and delete temp1 (free(temp1)).

 Step 12 - If temp1 is not first node and not last node then set temp2 → next = temp1 → next and delete temp1 (free(temp1)).
Implementation of Circular Linked list in C

Output:-
Unit – 3 Completed

You might also like