0% found this document useful (0 votes)
5 views37 pages

DS Unit-2 (R23)

The document provides an overview of linked lists, a fundamental data structure consisting of nodes that store data and pointers to the next node. It discusses the differences between linked lists and arrays, operations on singly linked lists including traversal, counting nodes, searching, inserting, and deleting nodes, as well as applications like polynomial expression representation. The document includes algorithms for various operations on linked lists and emphasizes their advantages over arrays.

Uploaded by

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

DS Unit-2 (R23)

The document provides an overview of linked lists, a fundamental data structure consisting of nodes that store data and pointers to the next node. It discusses the differences between linked lists and arrays, operations on singly linked lists including traversal, counting nodes, searching, inserting, and deleting nodes, as well as applications like polynomial expression representation. The document includes algorithms for various operations on linked lists and emphasizes their advantages over arrays.

Uploaded by

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

UNIT II

1. LINKED LISTS
Basic Terminologies

A linked list, in simple terms, is a linear collection of data elements. These data elements are
called nodes. Linked list is a data structure which in turn can be used to implement other data
structures. Thus, it acts as a building block to implement data structures such as stacks, queues,
and their variations. A linked list can be perceived as a train or a sequence of nodes in which
each node contains one or more data fields and a pointer to the next node.

In above Fig., we can see a linked list in which every node contains two parts, an integer and a
pointer to the next node. The left part of the node which contains data may include a simple data
type, an array, or a structure. The right part of the node contains a pointer to the next node (or
address of the next node in sequence). The last node will have no next node connected to it, so it
will store a special value called NULL. In above Fig, the NULL pointer is represented by X.
While programming, we usually define NULL as –1. Hence, a NULL pointer denotes the end of
the list. Since in a linked list, every node contains a pointer to another node which is of the same
type, it is also called a self-referential data type.

Linked lists contain a pointer variable START that stores the address of the first node in the list.
We can traverse the entire list using START which contains the address of the first node; the
next part of the first node in turn stores the address of its succeeding node. Using this technique,
the individual nodes of the list will form a chain of nodes. If START = NULL, then the linked
list is empty and contains no nodes.

In C, we can implement a linked list using the following code:


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

DATA STRUCTURES – UNIT 2[R23] Page 1


Linked Lists versus Arrays

 Both arrays and linked lists are a linear collection of data elements. But unlike an array, a
linked list does not store its nodes in consecutive memory locations.
 Another point of difference between an array and a linked list is that a linked list does not
allow random access of data. Nodes in a linked list can be accessed only in a sequential
manner. But like an array, insertions and deletions can be done at any point in the list in a
constant time.
 Another advantage of a linked list over an array is that we can add any number of
elements in the list. This is not possible in case of an array. For example, if we declare an
array as int marks[20], then the array can store a maximum of 20 data elements only.
There is no such restriction in case of a linked list.

2. SINGLY LINKED LISTS

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. By saying that the node contains a pointer to the
next node, we mean that the node stores the address of the next node in sequence. A singly
linked list allows traversal of data only in one way. Figure below shows a singly linked list.

Example

DATA STRUCTURES – UNIT 2[R23] Page 2


3. OPERATION ON SINGLE LINKED LIST

I. Traversing a Linked List

Traversing a linked list means accessing the nodes of the list in order to perform some processing
on them. Remember a linked list always contains a pointer variable START which stores the
address of the first node of the list. End of the list is marked by storing NULL or –1 in the NEXT
field of the last node. For traversing the linked list, we also make use of another pointer variable
PTR which points to the node that is currently being accessed.

Algorithm for traversing a linked list


Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 and 4 while PTR! = NULL
Step 3: Apply Process to PTR DATA
Step 4: SET PTR = PTR NEXT
[END OF LOOP]
Step 5: EXIT

II. Count the number of nodes in a linked list.

To count the number of nodes in a linked list, we will traverse each and every node of the list
and while traversing every individual node, we will increment the counter by 1. Once we reach
NULL, that is, when all the nodes of the linked list have been traversed, the final value of the
counter will be displayed.

Algorithm to print the number of nodes in a linked list


Step 1: [INITIALIZE] SET COUNT = 0
Step 2: [INITIALIZE] SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR != NULL
Step 4: SET COUNT = COUNT +1
Step 5: SET PTR = PTR NEXT
[END OF LOOP]
Step 6: Write COUNT
Step 7: EXIT

III. Searching for a Value in a Linked List

Searching a linked list means to find a particular element in the linked list. As already discussed,
a linked list consists of nodes which are divided into two parts, the information part and the next
part. So searching means finding whether a given value is present in the information part of the
node or not. If it is present, the algorithm returns the address of the node that contains the value.

DATA STRUCTURES – UNIT 2[R23] Page 3


Example:- we want to find 4 is available in the list or not the process is

Algorithm to search a linked list

Step 1: [INITIALIZE] SET PTR = START


Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = PTR DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL
Step 5: EXIT

IV. Inserting a New Node in a Linked List


we will see how a new node is added into an already existing linked list. We will take four cases
and then see how insertion is done in each case.
Case 1: The new node is inserted at the beginning.
Case 2: The new node is inserted at the end.
Case 3: The new node is inserted after a given node.
Case 4: The new node is inserted before a given node.

DATA STRUCTURES – UNIT 2[R23] Page 4


a. Inserting a Node at the Beginning of a Linked List

Consider the linked list shown in Fig. Suppose we want to add a new node with data 9 and add it
as the first node of the list. Then the following changes will be done in the linked list.

Algorithm to insert a new node at the beginning

b. Inserting a Node at the End of a Linked List

Consider the linked list and suppose we want to add a new node with data 9 as the last node of
the list. Then the following changes will be done in the linked list. First we insert a new node at
the end of a linked list. We take a pointer variable PTR and initialize it with START. That is,
PTR now points to the first node of the linked list. We traverse PTR through the linked list to
reach the last node. Once we reach the last node, we change the NEXT pointer of the last node to
store the address of the new node. Remember that the NEXT field of the new node contains
NULL, which signifies the end of the linked list.

DATA STRUCTURES – UNIT 2[R23] Page 5


Algorithm to insert a new node at the end

DATA STRUCTURES – UNIT 2[R23] Page 6


c. Inserting a Node After a Given Node in a Linked List

We take a pointer variable PTR and initialize it with START. That is, PTR now points to the first
node of the linked list. Then we take another pointer variable PREPTR which will be used to
store the address of the node preceding PTR. Initially, PREPTR is initialized to PTR. So now,
PTR, PREPTR, and START are all pointing to the first node of the linked list.

We traverse through the linked list to reach the node that has its value equal to NUM. We need to
reach this node because the new node will be inserted after this node. Once we reach this node,
we change the NEXT pointers in such a way that new node is inserted after the desired node.

DATA STRUCTURES – UNIT 2[R23] Page 7


Algorithm to insert a new node after a node that has value NUM

Step 1: IF AVAIL = NULL


Write OVERFLOW
Go to Step 11
[END OF IF]

Step 2: SET NEW_NODE = AVAIL


Step 3: SET AVAIL = AVAIL → NEXT
Step 4: SET NEW_NODE → DATA = VAL

Step 5: SET PTR = START

Step 6: Repeat Steps 7 and 8 while PTR ≠ NULL AND PTR → DATA ≠ NUM
Step 7: SET PTR = PTR → NEXT
[END OF LOOP]

Step 8: IF PTR = NULL // If NUM is not found


Write "Element not found"
Go to Step 11
[END OF IF]

Step 9: SET NEW_NODE → NEXT = PTR → NEXT


Step 10: SET PTR → NEXT = NEW_NODE

Step 11: EXIT

d. Inserting a Node Before a Given Node in a Single Linked List

Suppose we want to add a new node, we take a pointer variable PTR and initialize it with
START. That is, PTR now points to the first node of the linked list. Then, we take another
pointer variable PREPTR and initialize it with PTR. So now, PTR, PREPTR, and START are all
pointing to the first node of the linked list. We traverse through the linked list to reach the node
that has its value equal to NUM. We need to reach this node because the new node will be
inserted before this node. Once we reach this node, we change the NEXT pointers in such a way
that the new node is inserted before the desired node.

DATA STRUCTURES – UNIT 2[R23] Page 8


Algorithm to insert a new node before a node that has value NUM

Step 1: IF AVAIL = NULL


Write OVERFLOW
Go to Step 14
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL → NEXT
Step 4: SET NEW_NODE → DATA = VAL
Step 5: IF START = NULL
SET START = NEW_NODE
SET NEW_NODE → NEXT = NULL
Go to Step 14
[END OF IF]
Step 6: IF START → DATA = NUM // Special case: Insert at the beginning
SET NEW_NODE → NEXT = START
SET START = NEW_NODE
Go to Step 14
[END OF IF]

DATA STRUCTURES – UNIT 2[R23] Page 9


Step 7: SET PTR = START
Step 8: SET PREPTR = NULL
Step 9: Repeat Steps 10 and 11 while PTR ≠ NULL AND PTR → DATA ≠ NUM
Step 10: SET PREPTR = PTR
Step 11: SET PTR = PTR → NEXT
[END OF LOOP]

Step 12: IF PTR = NULL // If NUM is not found


Write "Element not found"
Go to Step 14
[END OF IF]

Step 13: SET NEW_NODE → NEXT = PTR


SET PREPTR → NEXT = NEW_NODE
Step 14: EXIT

V. Deleting a Node from a Linked List

If we want to delete node in already existing list consider three cases and then see how deletion
is done in each case.
Case 1: The first node is deleted.
Case 2: The last node is deleted.
Case 3: The node after a given node is deleted.

let us first discuss an important term called UNDERFLOW. Underflow is a condition that occurs
when we try to delete a node from a linked list that is empty. This happens when START =
NULL or when there are no more nodes to delete. Note that when we delete a node from a linked
list, we actually have to free the memory occupied by that node. The memory is returned to the
free pool so that it can be used to store other programs and data. Whatever be the case of
deletion, we always change the AVAIL pointer so that it points to the address that has been
recently vacated.

a. Deleting the First Node from a Linked List

When we want to delete a node from the beginning of the list, then the following changes will be
done in the linked list. We check if the linked list exists or not. If START = NULL, then it
signifies that there are no nodes in the list and exit.

However, if there are nodes in the linked list, then we use a pointer variable PTR that is set to
point to the first node of the list. For this, we initialize PTR with START that stores the address
of the first node of the list. START is made to point to the next node in sequence and finally the
memory occupied by the node pointed by PTR (initially the first node of the list) is freed and
returned to the free pool.
DATA STRUCTURES – UNIT 2[R23] Page 10
Algorithm to delete the first node

b. Deleting the Last Node from a Linked List

Suppose we want to delete the last node from the linked list, then the following changes will be
done in the linked list. We take a pointer variable PTR and initialize it with START. That is,
PTR now points to the first node of the linked list. We take another pointer variable PREPTR
such that it always points to one node before the PTR. Once we reach the last node and the
second last node, we set the NEXT pointer of the second last node to NULL, so that it now
becomes the (new) last node of the linked list. The memory of the previous last node is freed and
returned back to the free pool.

DATA STRUCTURES – UNIT 2[R23] Page 11


Algorithm to delete the last node

c. Deleting the Node After a Given Node in a Linked List

Suppose we want to delete the node that succeeds the node which contains data value 4. Then the
following changes will be done in the linked list. We take a pointer variable PTR and initialize it
with START. That is, PTR now points to the first node of the linked list. In the while loop, we
take another pointer variable PREPTR such that it always points to one node before the PTR.
Once we reach the node containing VAL and the node succeeding it, we set the next pointer of
the node containing VAL to the address contained in next field of the node succeeding it. The
memory of the node succeeding the given node is freed and returned back to the free pool.

DATA STRUCTURES – UNIT 2[R23] Page 12


Algorithm to delete the node after a given node
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 11
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Steps 5 and 6 while PTR ≠ NULL AND PTR → DATA ≠ NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR → NEXT
[END OF LOOP]
Step 7: IF PTR = NULL // If NUM not found
Write "Element not found"
Go to Step 11
[END OF IF]
Step 8: SET TEMP = PTR
Step 9: SET PREPTR → NEXT = PTR → NEXT
Step 10: FREE TEMP
Step 11: EXIT

4. APPLICATIONS ON SINGLE LINKED LIST

i. Polynomial Expression Representation

WHAT IS POLYNOMIAL

A polynomial p(x) is the expression in variable x which is in the form (axn + bxn-1 + …. + jx+ k),
where a, b, c …., k fall in the category of real numbers and 'n' is non negative integer, which is
called the degree of polynomial. An essential characteristic of the polynomial is that each term in
the polynomial expression consists of two parts:

 one is the coefficient


 other is the exponent
Example:
10x2 + 26x, here 10 and 26 are coefficients and 2, 1 is its exponential value.

The polynomial can be represented in linked list by using the nodes. Each node can consist of
coefficient, power and address of the next node.

DATA STRUCTURES – UNIT 2[R23] Page 13


For example the polynomial 4x3+6x2+10x+6 is represented in linked list is

Polynomial can be represented using structure is


struct node
{
int num;
int coeff;
struct node *next;
};

Adding two polynomials

If we want to add two polynomials first we can compare the exponentiation part
a. If the exponentiations of the two terms are equal then the coefficients are added and put
the same expression.
b. If the exponentiation part of the items is greater than the other simply write the greater
exponentiation term of coefficient and exponentiation.

Example:-

A = 5x5 + 6x3 + 3x2 + 1


B = 6x4 + 2x3 + 2
C= A + B then
C = 5x5 + 6x4 + 8x3 + 3x2 + 3

DATA STRUCTURES – UNIT 2[R23] Page 14


Algorithm

Algorithm PolynomialAdd_SL
Input: Two polynomials P and Q whose header pointers are PHEADER and QHEADER.
Output: A polynomial R is the sum of P and Q having the header RHEADER.

Pptr = PHEADER, Qptr = QHEADER //Get a header node for the resultant polynomial//
Rptr = RHEADER
While (Pptr ≠ NULL) and (Qptr ≠ NULL) do
CASE: Pptr →EXP = Qptr→ EXP
New= GetNode (NODE)
Rptr→ LINK= new, Rptr = new
Rptr →COEFF = Pptr→COEFF + Qptr→COEFF
Rptr→ EXP = Pptr→ EXP
Rptr →LINK = NULL
Pptr = Pptr→ LINK, Qptr = Qptr→LINK

CASE: Pptr →EXP > Qptr→EXP


new =GetNode (NODE)
Rptr→ LINK = new, Rptr = new
Rptr →COEFF = Pptr →COEFF
Rptr→ EXP = Pptr→EXP
Rptr→ LINK= NULL
Pptr= Pptr →LINK

CASE: Pptr →EXP < Qptr→EXP


new= GetNode (NODE)

DATA STRUCTURES – UNIT 2[R23] Page 15


Rptr →LINK= new, Rptr = new
Rptr→ COEFF = Qptr→COEFF
Rptr →EXP =Qptr→EXP
Rptr→ LINK= NULL
Qptr=Qptr→ LINK
End While

If (Pptr ≠ NULL) and (Qptr = NULL) then


While (Pptr ≠ NULL) do
new =GetNode(NODE)

Rptr →LINK =new, Rptr = new


Rptr→ COEFF = Pptr→COEFF
Rptr →EXP = Pptr →Exp
Rptr→ LINK= NULL
Pptr= Pptr→ LINK
End While
EndIf

If (Pptr = NULL) and (Qptr ≠NULL) then


While (Qptr ≠NULL) do
new =GetNode(NODE)
Rptr→ LINK= new, Rptr = new
Rptr →COEFF= Qptr→COEFF
Rptr →EXP=Qptr→EXP
Rptr→ LINK = NULL
Qptr=Qptr→ LINK
End While
EndIf
Return(RHEADER)
Stop

Multiplication of two polynomials using Linked list

Given two polynomials in the form of linked list. The task is to find the multiplication of both
polynomials.

Approach:
1. In this approach we will multiply the 2nd polynomial with each term of 1st polynomial.
2. Store the multiplied value in a new linked list.

DATA STRUCTURES – UNIT 2[R23] Page 16


3. Then we will add the coefficients of elements having the same power in resultant
polynomial.

Examples:

Input: Poly1: 3x^2 + 5x^1 + 6, Poly2: 6x^1 + 8


Output: 18x^3 + 54x^2 + 76x^1 + 48

On multiplying each element of 1st polynomial with elements of 2nd polynomial, we get
18x^3 + 24x^2 + 30x^2 + 40x^1 + 36x^1 + 48
On adding values with same power of x,
18x^3 + 54x^2 + 76x^1 + 48

Algorithm Polynomial Multiply_LL


Input: Two polynomials P and Q having their HEADERs as PHEADER, QHEADER
Output: A polynomial R storing the result of multiplication of P and Q
Data structure: Single Linked list structure for representing a term in a single variable
polynomial.
BEGIN
Pptr= PHEADER, Qptr= QHEADER
RHEADER= GetNode(NODE) // Get a node for the HEADER of R
RHEADER NEXT= NULL, RHEADER  COEFF = NULL, RHEADER  EXP = NULL
If (Pptr = NULL) or (Qptr = NULL) then // No valid operation possible

DATA STRUCTURES – UNIT 2[R23] Page 17


Exit
EndIf
While (Pptr ≠ NULL) do //as long as polynomial P has terms
While (Qptr ≠ NULL) do //as long as polynomial Q has terms
C=Pptr COEFF X Qptr COEFF // Multiply coefficients
X=Ppt EXP + Qptr  EXP //Add exponents
Rptr= RHEADER
If(Rptr=NULL) then // If so far no terms in Resultant polynomial
temp =GetNode(NODE)
temp→COEFF = C, temp→EXP = X
temp→NEXT = NULL
Rptr  NEXT= temp
Else
// Search for the equal exponent value in R
While (Rptr ≠ NULL) and (Rptr  EXP>= X) do
Rptr1 = Rptr
Rptr = Rptr→NEXT
If (Rptr→EXP = X) then
Rptr COEFF = Rptr→COEFF + C
Else // Add a temp node at the correct position in R
temp =GetNode(NODE)
temp→COEFF = C, temp→EXP = X
tempNEXT=Rptr
Rptr1→NEXT= temp
EndIf
EndWhile
If( Rptr = NULL)
temp =GetNode(NODE)
temp→COEFF = C, temp→EXP = X
temp→NEXT = NULL
Rptr  NEXT= temp
Endif
Endif
Qptr = Qptr NEXT
EndWhile
Pptr= Pptr→NEXT
EndWhile
Return(HEADER) //if both the polynomials have no terms
STOP

DATA STRUCTURES – UNIT 2[R23] Page 18


SPARSE MATRIX

A matrix is a two-dimensional data object made of m rows and n columns, therefore having total
m x n values. If most of the elements of the matrix have 0 values, then it is called a sparse
matrix.
Why to use Sparse Matrix instead of simple matrix?
 Storage: There are lesser non-zero elements than zeros and thus lesser memory can be
used to store only those elements.
 Computing time: Computing time can be saved by logically designing a data structure
traversing only non-zero elements..

Example:-

00304
00570
00000
02600
This concept is essential as the matrix can be designed to:

1. Save Space: The sparse matrix is represented using forms where only the NON-ZERO
elements and their locations are stored. This saves space over a simple matrix where
ZERO elements would also consume memory.
2. Save Computing Time: The sparse matrix can be stored in a way that accessing the
elements becomes efficient.

Linked Representation`

A linked list may be used to store a sparse matrix by representing each NON-ZERO value as a
node and linking this Node in a specific way such that it represents the position in the original
array. To represent sparse matrix in linked list we can use three types of nodes called as head
node, row node and column node.

Head Node:-

In the head node the node contains 4-parts. In that node we can represents the size of the matrix
(row and column) and total non – zero values and pointer.

DATA STRUCTURES – UNIT 2[R23] Page 19


Row node:-

In the row node contain 3-parts. The 1st one is row number, 2nd one is pointer for next row and
3rd one is pointer for column.

Column Node:-
In the column node contains 3- parts. They are column number, value and pointer for next value
in the same row.

Example: - Given that the sparse matrix is

0 2 0 0
4 0 3 0
0 1 0 1
0 2 0 0
We can represent the given sparse matrix in linked list as

DATA STRUCTURES – UNIT 2[R23] Page 20


SINGLY LINKED LIST
*ADVANTAGE:-
1) Insertion and Deletions can be done easily.
2) It does not need movement of elements for insertion and deletion.
3) It space is not wasted as we can get space according to our requirements.
4) Its size is not fixed.
5) It can be extended or reduced according to requirements.
6) Elements may or may not be stored in consecutive memory available; even then we can store
the data in computer.
7) It is less expensive.

*DISADVANTAGE:-
1) It requires more space as pointers are also stored with information.
2) Different amount of time is required to access each element.
3) If we have to go to a particular element then we have to go through all those elements that
come before that element.
4) We cannot traverse it from last & only from the beginning.
5) It is not easy to sort the elements stored in the linear linked list.

5. DOUBLY LINKED LISTS

A doubly linked list or a two-way linked list is a more complex type of linked list which contains
a pointer to the next as well as the previous node in the sequence. Doubly Linked List is a
variation of Linked list in which navigation is possible in both ways, either forward or backward

DATA STRUCTURES – UNIT 2[R23] Page 21


easily as compared to Single Linked List. Therefore, it consists of three parts—data, a pointer to
the next node, and a pointer to the previous node as shown in Fig.

Example

Important Points to be Remembered


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.

In C, the structure of a doubly linked list can be given as,


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

Inserting a New Node in a Doubly Linked List


In this section, we will discuss how a new node is added into an already existing doubly linked
list. We will take four cases and then see how insertion is done in each case.
Case 1: The new node is inserted at the beginning.
Case 2: The new node is inserted at the end.
Case 3: The new node is inserted after a given node.
Case 4: The new node is inserted before a given node.

DATA STRUCTURES – UNIT 2[R23] Page 22


a. Inserting a Node at the Beginning of a Doubly Linked List

Consider the doubly linked list shown in Fig. Suppose we want to add a new node with data 9 as
the first node of the list. To insert a new node at the beginning of a doubly linked list, we first
check whether memory is available for the new node. If the free memory has exhausted, then an
OVERFLOW message is printed. Otherwise, if free memory cell is available, then we allocate
space for the new node. Set its DATA part with the given VAL and the NEXT part is initialized
with the address of the first node of the list, which is stored in START. Now, since the new node
is added as the first node of the list, it will now be known as the START node, that is, the
START pointer variable will now hold the address of NEW_NODE.

Algorithm to insert a new node at the beginning

b. Inserting a Node at the End end of a Doubly Linked List

Consider the doubly linked list shown in Fig. Suppose we want to add a new node with data 9 as
the last node of the list. To insert a new node at the end of a doubly linked list, we take a pointer
variable PTR and initialize it with START. In the while loop, we traverse through the linked list

DATA STRUCTURES – UNIT 2[R23] Page 23


to reach the last node. Once we reach the last node, we change the NEXT pointer of the last node
to store the address of the new node. Remember that the NEXT field of the new node contains
NULL which signifies the end of the linked list. The PREV field of the NEW_NODE will be set
so that it points to the node pointed by PTR (now the second last node of the list).

Algorithm to insert a new node at the end

DATA STRUCTURES – UNIT 2[R23] Page 24


c. Inserting a Node after a Given Node in a Doubly Linked List
Consider the doubly linked list shown in Fig. Suppose we want to add a new node with value 9
after the node containing 3. To insert a new node after a given node in a doubly linked list, we
take a pointer PTR and initialize it with START. That is, PTR now points to the first node of the
linked list. We traverse through the linked list to reach the node that has its value equal to NUM.
We need to reach this node because the new node will be inserted after this node. Once we reach
this node, we change the NEXT and PREV fields in such a way that the new node is inserted
after the desired node.

Algorithm to insert a new node after a given node

Step 1: IF AVAIL = NULL


Write "OVERFLOW"
Go to Step 15
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL → NEXT
Step 4: SET NEW_NODE → DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 7 while PTR ≠ NULL AND PTR → DATA ≠ NUM
Step 7: SET PTR = PTR → NEXT
[END OF LOOP]

DATA STRUCTURES – UNIT 2[R23] Page 25


Step 8: IF PTR = NULL
Write "ELEMENT NOT FOUND"
Go to Step 15
[END OF IF]
Step 9: SET NEW_NODE → PREV = PTR
Step 10: IF PTR → NEXT = NULL
SET NEW_NODE → NEXT = NULL // Explicitly set for last node insertion
SET PTR → NEXT = NEW_NODE
Write "INSERTION SUCCESSFUL"
Go to Step 15
[END OF IF]
Step 11: SET NEW_NODE → NEXT = PTR → NEXT
Step 12: SET PTR → NEXT → PREV = NEW_NODE
Step 13: SET PTR → NEXT = NEW_NODE
Step 14: Write "INSERTION SUCCESSFUL"
Step 15: EXIT

d. Inserting a Node before a Given Node in a Doubly Linked List

Consider the doubly linked list shown in Fig. Suppose we want to add a new node with value 9
before the node containing 3. We first check whether memory is available for the new node.
Then, we take a pointer variable PTR and initialize it with START. That is, PTR now points to
the first node of the linked list. In the while loop, we traverse through the linked list to reach the
node that has its value equal to NUM. We need to reach this node because the new node will be
inserted before this node. Once we reach this node, we change the NEXT and PREV fields in
such a way that the new node is inserted before the desired node.

DATA STRUCTURES – UNIT 2[R23] Page 26


Algorithm to insert a new node before a given node
Step 1: IF AVAIL = NULL
Write "OVERFLOW"
Go to Step 13
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL → NEXT
Step 4: SET NEW_NODE → DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 7 while PTR ≠ NULL AND PTR → DATA ≠ NUM
Step 7: SET PTR = PTR → NEXT
[END OF LOOP]

Step 8: IF PTR = NULL


Write "ELEMENT NOT FOUND"
Go to Step 13
[END OF IF]

Step 9: SET NEW_NODE → NEXT = PTR

Step 10: IF PTR → PREV ≠ NULL


SET NEW_NODE → PREV = PTR → PREV
SET PTR → PREV → NEXT = NEW_NODE
ELSE
SET START = NEW_NODE // Updating START for first node insertion
SET NEW_NODE → PREV = NULL // Explicitly setting PREV to NULL for first node
[END OF IF]

Step 11: SET PTR → PREV = NEW_NODE


Step 12: Write "INSERTION SUCCESSFUL"
Step 13: EXIT

Deleting a Node from a Doubly Linked List

In this section, we will see how a node is deleted from an already existing doubly linked list. We
will take four cases and then see how deletion is done in each case.
Case 1: The first node is deleted.
Case 2: The last node is deleted.
Case 3: The node after a given node is deleted.
Case 4: The node before a given node is deleted.

DATA STRUCTURES – UNIT 2[R23] Page 27


a. Deleting the First Node from a Doubly Linked List

Consider the doubly linked list shown in Fig. When we want to delete a node from the beginning
of the list, we check if the linked list exists or not. If START is NULL, then it signifies that there
are no nodes in the list and the control is transferred to the last statement of the algorithm.
However, if there are nodes in the linked list, then we use a temporary pointer variable PTR that
is set to point to the first node of the list. For this, we initialize PTR with START that stores the
address of the first node of the list. START is made to point to the next node in sequence and
finally the memory occupied by PTR (initially the first node of the list) is freed and returned to
the free pool.

Algorithm to delete the first node

b. Deleting the Last Node from a Doubly Linked List

Consider the doubly linked list shown in Fig. Suppose we want to delete the last node from the
linked list, we take a pointer variable PTR and initialize it with START. That is, PTR now points
to the first node of the linked list. Once we reach the last node, we can also access the second last

DATA STRUCTURES – UNIT 2[R23] Page 28


node by taking its address from the PREV field of the last node. To delete the last node, we
simply have to set the next field of second last node to NULL, so that it now becomes the (new)
last node of the linked list. The memory of the previous last node is freed and returned to the free
pool.

Algorithm to delete the last node

c. Deleting the Node After a Given Node in a Doubly Linked List

Consider the doubly linked list shown in Fig. Suppose we want to delete the node that succeeds
the node which contains data value 4. We take a pointer variable PTR and initialize it with
START. That is, PTR now points to the first node of the doubly linked list. Once we reach the
node containing VAL, the node succeeding it can be easily accessed by using the address stored
in its NEXT field. The NEXT field of the given node is set to contain the contents in the NEXT

DATA STRUCTURES – UNIT 2[R23] Page 29


field of the succeeding node. Finally, the memory of the node succeeding the given node is freed
and returned to the free pool.

Algorithm to delete a node after a given node

Step 1: IF START = NULL


Write "UNDERFLOW"
Go to Step 10
[END OF IF]

Step 2: SET PTR = START

Step 3: Repeat Step 4 while PTR != NULL AND PTR->DATA != NUM


Step 4: SET PTR = PTR->NEXT
[END OF LOOP]

Step 5: IF PTR = NULL OR PTR->NEXT = NULL


Write "NODE NOT FOUND OR NO NODE TO DELETE AFTER NUM"
Go to Step 10
[END OF IF]

Step 6: SET TEMP = PTR->NEXT

Step 7: IF TEMP->NEXT = NULL


SET PTR->NEXT = NULL

DATA STRUCTURES – UNIT 2[R23] Page 30


ELSE
SET PTR->NEXT = TEMP->NEXT
SET TEMP->NEXT->PREV = PTR
[END OF IF]

Step 8: FREE TEMP

Step 9: Print "Node Deleted Successfully"

Step 10: EXIT

Deleting the Node before a Given Node in a Doubly Linked List

Consider the doubly linked list shown in Fig. Suppose we want to delete the node preceding the
node with value 4. We take a pointer variable PTR and initialize it with START. That is, PTR
now points to the first node of the linked list. Once we reach the node containing VAL, the
PREV field of PTR is set to contain the address of the node preceding the node which comes
before PTR. The memory of the node preceding PTR is freed and returned to the free pool.
Hence, we see that we can insert or delete a node in a constant number of operations given only
that node’s address. Note that this is not possible in the case of a singly linked list which requires
the previous node’s address also to perform the same operation.

DATA STRUCTURES – UNIT 2[R23] Page 31


Algorithm to delete a node before a given node

Step 1: IF START = NULL OR START->NEXT = NULL


Write "UNDERFLOW"
Go to Step 11
[END OF IF]

Step 2: SET PTR = START


Step 3: Repeat Step 4 while PTR->DATA != NUM
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: IF PTR = NULL OR PTR->PREV = NULL
Write "NODE NOT FOUND OR NO NODE TO DELETE BEFORE NUM"
Go to Step 11
[END OF IF]
Step 6: SET TEMP = PTR->PREV
Step 7: IF TEMP->PREV = NULL
SET START = PTR
SET PTR->PREV = NULL
ELSE
SET TEMP->PREV->NEXT = PTR
SET PTR->PREV = TEMP->PREV
[END OF IF]
Step 8: FREE TEMP
Step 9: Print "Node Deleted Successfully"
Step 10: EXIT

CIRCULAR LINKED LISTs

In a circular linked list, the last node contains a pointer to the first node of the list. We can have a
circular singly linked list as well as a circular doubly linked list. While traversing a circular
linked list, we can begin at any node and traverse the list in any direction, forward or backward,
until we reach the same node where we started. Thus, a circular linked list has no beginning and
no ending. Figure shows a circular linked list.

DATA STRUCTURES – UNIT 2[R23] Page 32


The only downside of a circular linked list is the complexity of iteration. Note that there are no
NULL values in the NEXT part of any of the nodes of list. Circular linked lists are widely used
in operating systems for task maintenance.

Inserting a New Node in a Circular Linked List

In this section, we will see how a new node is added into an already existing linked list. We will
take two cases and then see how insertion is done in each case.
Case 1: The new node is inserted at the beginning of the circular linked list.
Case 2: The new node is inserted at the end of the circular linked list.

a. Inserting a Node at the Beginning of a Circular Linked List

Consider the linked list shown in Fig. Suppose we want to add a new node with data 9 as the first
node of the list. We first check whether memory is available for the new node. If the free
memory has exhausted, then an OVERFLOW message is printed. Otherwise, if free memory cell
is available, then we allocate space for the new node. Set its DATA part with the given VAL and
the NEXT part is initialized with the address of the first node of the list, which is stored in
START. Now, since the new node is added as the first node of the list, it will now be known as
the START node, that is, the START pointer variable will now hold the address of the
NEW_NODE. Because the last node contains a pointer to START, its NEXT field is updated so
that after insertion it points to the new node which will be now known as START.

DATA STRUCTURES – UNIT 2[R23] Page 33


Algorithm to insert a new node at the beginning

b. Inserting a Node at the End of a Circular Linked List

Consider the linked list shown in Fig. Suppose we want to add a new node with data 9 as the last
node of the list. We take a pointer variable PTR and initialize it with START. That is, PTR now
points to the first node of the linked list. In the while loop, we traverse through the linked list to
reach the last node. Once we reach the last node, we change the NEXT pointer of the last node to
store the address of the new node. Remember that the NEXT field of the new node contains the
address of the first node which is denoted by START.

DATA STRUCTURES – UNIT 2[R23] Page 34


Algorithm to insert a new node at the end

Deleting a Node from a Circular Linked List

In this section, we will discuss how a node is deleted from an already existing circular linked list.
We will take two cases and then see how deletion is done in each case. Rest of the cases of
deletion is same as that given for singly linked lists.
Case 1: The first node is deleted.
Case 2: The last node is deleted

a. Deleting the First Node from a Circular Linked List

Consider the circular linked list shown in Fig. When we want to delete a node from the
beginning of the list, we check if the linked list exists or not. If START = NULL, then it signifies
that there are no nodes in the list and the control is transferred to the last statement of the
algorithm. However, if there are nodes in the linked list, then we use a pointer variable PTR
which will be used to traverse the list to ultimately reach the last node. We change the next
pointer of the last node to point to the second node of the circular linked list. In Step 6, the
memory occupied by the first node is freed. Finally, the second node now becomes the first node
of the list and its address is stored in the pointer variable START.

DATA STRUCTURES – UNIT 2[R23] Page 35


Algorithm to delete the first node

b. Deleting the Last Node from a Circular Linked List

Consider the circular linked list shown in Fig. Suppose we want to delete the last node from the
linked list, we take a pointer variable PTR and initialize it with START. That is, PTR now points
to the first node of the linked list. We take another pointer variable PREPTR such that PREPTR
always points to one node before PTR. Once we reach the last node and the second last node, we
set the next pointer of the second last node to START, so that it now becomes the (new) last
node of the linked list. The memory of the previous last node is freed and returned to the free
pool.

DATA STRUCTURES – UNIT 2[R23] Page 36


Algorithm to delete the last node

Advantage of circular linked list

 Entire list can be traversed from any node of the list.


 It saves time when we have to go to the first node from the last node.
 It is used for the implementation of queue.
 Reference to previous node can easily be found.
 When we want a list to be accessed in a circle or loop then circular linked list is used.

Disadvantage of circular linked list

 Circular list are complex as compared to singly linked lists.


 Reversing of circular list is a complex as compared to singly or doubly lists.
 If not traversed carefully, then we could end up in an infinite loop.
 Like singly and doubly lists circular linked lists also doesn’t support direct accessing of
elements.

Applications of Circular Linked List


A Circular Linked List can be used for the following –

 It is used in the Operating system to share time for different users, generally uses a
Round-Robin time-sharing mechanism.
 Multiplayer games use a circular list to swap between players in a loop.
 Implementation of Advanced data structures like Fibonacci Heap
 The browser cache which allows you to hit the BACK button
 Undo functionality in Photoshop or Word

DATA STRUCTURES – UNIT 2[R23] Page 37

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy