Unit 3
Unit 3
Venkata Ramana S
Assistant Professor
CSE,MRECW
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
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
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:
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
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)
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
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
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
Step 4 - If it is Not Empty, then set newNode → next Step 4 - Then set 'top = top → next'.
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.
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 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 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