DSA Chapter 1
DSA Chapter 1
LINKED LISTS
DETAILED CONTENT :
➢ Introduction to Data Structures: Linear and Non-Linear Data Structures, Static and
Dynamic Data Structures.
➢ Concept of Stack and Queue. Array Implementation of Stack and Queue, Circular
Queue, Double Ended Queue, Priority Queue.
➢ Concept of Linked Lists. Singly linked lists, doubly linked lists and circular linked lists.
➢ Insertion, deletion, update and copying operations with Singly linked lists, doubly
linked lists and circular linked lists. Reversing a singly linked list.
TT 1
➢ INTRODUCTION TO DATA STRUCTURES
A data structure is basically a group of data elements that are put together under one name, and
which defines a particular way of storing and organizing data in a computer so that it can be
used efficiently.
Data structures are used in almost every program or software system. Some common examples
of data structures are arrays, linked lists, queues, stacks, binary trees, and hash tables. Data
structures are widely applied in the following areas:
• Compiler design • Numerical analysis
• Operating system • Simulation
• Statistical analysis package • Artificial intelligence
• DBMS • Graphics
TT 2
graphs. Non-primitive data structures can further be classified into two categories: linear
and non-linear data structures.
• Linear Data Structure: If the elements of a data structure are stored in a linear or
sequential order, then it is a linear data structure. Examples include arrays, linked lists,
stacks, and queues. Linear data structures can be represented in memory in two different
ways. One way is to have to a linear relationship between elements by means of sequential
memory locations. The other way is to have a linear relationship between elements by
means of links.
• Non – Linear Data Structure: If the elements of a data structure are not stored in a
sequential order, then it is a non-linear data structure. The relationship of adjacency is not
maintained between elements of a non-linear data structure. Examples include trees and
graphs.
TT 3
• Dynamic Data Structure: The size of the structure in not fixed and can be modified
during the operations performed on it. Dynamic data structures are designed to facilitate
change of data structures in the run time. Some advantages of dynamic data structure are
memory allocation is done during program execution, Heap is used, more efficient,
memory reuse.
An abstract data type (ADT) is the way we look at a data structure, focusing on what it does
and ignoring how it does its job. The end-user is not concerned about the details of how the
methods carry out their tasks. They are only aware of the methods that are available to them
and are only concerned about calling those methods and getting the results. They are not
concerned about how they work.
TT 4
➢ ALGORITHM
The typical definition of algorithm is ‘a formally defined procedure for performing some
calculation’. Algorithms are mainly used to achieve software reuse. Once we have an idea or
a blueprint of a solution, we can implement it in any high-level language like C, C++, or Java.
An algorithm is basically a set of instructions that solve a problem. It is not uncommon to have
multiple algorithms to tackle the same problem, but the choice of a particular algorithm must
depend on the time and space complexity of the algorithm.
There are two main approaches to design an algorithm—top-down approach and bottom-up
approach.
• Top Down Approach: A top-down design approach starts by dividing the complex
algorithm into one or more modules. These modules can further be decomposed into one
or more sub-modules, and this process of decomposition is iterated until the desired level
of module complexity is achieved. Top-down design method is a form of stepwise
refinement where we begin with the topmost module and incrementally add modules that
it calls.
• Bottom Up Approach: A bottom-up approach is just the reverse of top-down approach.
In the bottom-up design, we start with designing the most basic or concrete modules and
then proceed towards designing higher level modules. The higher-level modules are
implemented by using the operations performed by lower level modules. Thus, in this
approach sub-modules are grouped together to form a higher-level module. All the higher-
level modules are clubbed together to form even higher-level modules. This process is
repeated until the design of the complete algorithm is obtained
TT 5
1. Big Oh Notation O: The notation Ο(n) is the formal way to express the upper bound of an
algorithm's running time. It measures the worst-case time complexity or the longest amount
of time an algorithm can possibly take to complete.
O(f(n)) = g(n)
2. Omega Notation Ω : The notation Ω(n) is the formal way to express the lower bound of
an algorithm's running time. It measures the best-case time complexity or the best amount
of time an algorithm can possibly take to complete.
3. θ Notation: The notation θ(n) is the formal way to express both the lower bound and the
upper bound of an algorithm's running time. It is represented as follows:
θ (f (n)) = g(n)
TT 6
➢ CONCEPT OF STACKS
Stack is an important data structure which stores its elements in an ordered manner. A stack is
a linear data structure which uses the same principle, i.e., the elements in a stack are added and
removed only from one end, which is called the TOP. Hence, a stack is called a LIFO (Last-
In-First-Out) data structure, as the element that was inserted last is the first one to be taken out.
TT 7
➢ Application of stacks:
➢ Reversing a list
➢ Parentheses checker
➢ Conversion of an infix expression into a postfix expression
➢ Evaluation of a postfix expression
➢ Conversion of an infix expression into a prefix expression
➢ Evaluation of a prefix expression
➢ Recursion
➢ Tower of Hanoi
➢ OPERATIONS ON STACKS
A stack supports three basic operations: push, pop, and peek. The push operation adds an
element to the top of the stack and the pop operation removes the element from the top of the
stack. The peek operation returns the value of the topmost element of the stack.
1. Push Operation: The push operation is used to insert an element into the stack. The new
element is added at the topmost position of the stack. However, before inserting the value, we
must first check if TOP=MAX–1, because if that is the case, then the stack is full and no more
insertions can be done.
To insert an element with value E, we first check if TOP=MAX–1. If the condition is false,
then we increment the value of TOP and store the new element at the position given by stack
[TOP]. Thus, the updated stack becomes as shown below:
TT 8
➢ Algorithm for Push Operation:
Step 1: IF TOP = MAX-1
PRINT “OVERFLOW”
Go to Step 4
[END OF IF]
Step 2: SET TOP = TOP+1
Step 3: SET STACK[TOP] = VALUE
Step 4: END
2. Pop Operation: The pop operation is used to delete the topmost element from the stack.
However, before deleting the value, we must first check if TOP=NULL because if that is the
case, then it means the stack is empty and no more deletions can be done. If an attempt is made
to delete a value from a stack that is already empty, an UNDERFLOW message is printed.
TT 9
➢ Algorithm for Peek Operation:
Step 1: IF TOP = NULL
PRINT STACK IS EMPTY
Go to Step 3
Step 2: RETURN STACK[TOP]
Step 3: END
➢ CONCEPT OF QUEUE
A Queue is a FIFO (First-In, First-Out) data structure in which the element that is inserted first
is the first one to be taken out. The elements in a queue are added at one end called the REAR
and removed from the other end called the FRONT.
➢ Array Representation of Queue: Queues can be easily represented using linear arrays. As
stated earlier, every queue has front and rear variables that point to the position from where
deletions and insertions can be done, respectively.
➢ OPERATIONS ON QUEUE :
The Queues mainly perform two operations such as Insertion of new element at the Queue and
Deletion of element from the Queue.
1. Insertion of new element at the Queue: Suppose we want to add another element with new
value, then REAR would be incremented by 1 and the value would be stored at the position
pointed by REAR. The queue after addition would be as shown in Fig. Here, FRONT = 0 and
REAR = 6. Every time a new element has to be added, we repeat the same procedure.
However, before inserting an element in a queue, we must check for overflow conditions. An
overflow will occur when we try to insert an element into a queue that is already full. When
TT 10
REAR = MAX – 1, where MAX is the size of the queue, we have an overflow condition. Note
that we have written MAX – 1 because the index starts from 0.
Fig.: The Front = 0 and Rear =4 Fig.: Add new element, The Front = 0 and Rear =5
2. Deletion of new element from the Queue: If we want to delete an element from the queue,
then the value of FRONT will be incremented. Deletions are done from only this end of the
queue. Before deleting an element from a queue, we must check for underflow conditions. An
underflow condition occurs when we try to delete an element from a queue that is already
empty. If FRONT = –1 and REAR = –1, it means there is no element in the queue.
Fig.: The Front = 0 and Rear =5 Fig.: The element is deleted, Front = 1 and Rear =5
TT 11
➢ Algorithm to delete element from the Queue
Step 1: IF FRONT = -1 OR FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT+1
[END OF IF]
Step 2: EXIT
➢ TYPES OF QUEUE:
1. Circular Queue: In Circular Queue, all the nodes are represented as circular. It is similar to
the linear Queue except that the last element of the queue is connected to the first element. It
is also known as Ring Buffer, as all the ends are connected to another end. The drawback that
occurs in a linear queue is overcome by using the circular queue. If the empty space is available
in a circular queue, the new element can be added in an empty space by simply incrementing
the value of rear.
2. Dequeue: In Deque or Double Ended Queue, insertion and deletion can be done from both
ends of the queue either from the front or rear. It means that we can insert and delete elements
from both front and rear ends of the queue. Deque can be used as a palindrome checker means
that if we read the string from both ends, then the string would be the same.
TT 12
There are two types of deque that are discussed as follows –
➢ Input restricted deque - As the name implies, in input restricted queue, insertion operation
can be performed at only one end, while deletion can be performed from both ends.
3. Priority Queue: It is a special type of queue in which the elements are arranged based on the
priority. It is a special type of queue data structure in which every element has a priority
associated with it. Suppose some elements occur with the same priority, they will be arranged
according to the FIFO principle. The representation of priority queue is shown in the below
image –
There are two types of priority queue that are discussed as follows –
TT 13
4. Multiple Queue: When we implement a queue using an array, the size of the array must be
known in advance. If the queue is allocated less space, then frequent overflow conditions will
be encountered. To deal with this problem, the code will have to be modified to reallocate more
space for the array. In case we allocate a large amount of space for the queue, it will result in
sheer wastage of the memory. Thus, there lies a tradeoff between the frequency of overflows
and the space allocated. So, a better solution to deal with this problem is to have multiple
queues or to have more than one queue in the same array of sufficient size.
TT 14
➢ CONCEPT OF LINKED LISTS
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 the figure, we can see that the variable START is used to store the address of the first node.
Here, in this example, START = 1, so the first data is stored at address 1, which is H. The
corresponding NEXT stores the address of the next node, which is 4. So, we will look at address
4 to fetch the next data item. The second data element obtained from address 4 is E.
TT 15
➢ Difference Between Array and Linked Lists
Array Linked Lists
An array is a collection of elements of a A linked list is a collection of objects known
similar data type. as a node where node consists of two parts,
i.e., data and address.
Array elements store in a contiguous Linked list elements can be stored anywhere
memory location. in the memory or randomly stored.
Array works with a static memory. Here The Linked list works with dynamic
static memory means that the memory size is memory. Here, dynamic memory means that
fixed and cannot be changed at the run time. the memory size can be changed at the run
time according to our requirements.
Array elements are independent of each Linked list elements are dependent on each
other. other. As each node contains the address of
the next node so to access the next node, we
need to access its previous node.
Array takes more time while performing any Linked list takes less time while performing
operation like insertion, deletion, etc. any operation like insertion, deletion, etc.
In the case of an array, memory is allocated In the case of a linked list, memory is
at compile-time. allocated at run time.
Memory utilization is inefficient in the array. Memory utilization is efficient in the case of
For example, if the size of the array is 6, and a linked list as the memory can be allocated
array consists of 3 elements only then the rest or deallocated at the run time according to
of the space will be unused. our requirement.
Accessing any element in an array is faster Accessing an element in a linked list is
as the element in an array can be directly slower as it starts traversing from the first
accessed through the index. element of the linked list.
TT 16
The operating system does this task of adding the freed memory to the free pool. The operating
system will perform this operation whenever it finds the CPU idle or whenever the programs
are falling short of memory space. The operating system scans through all the memory cells
and marks those cells that are being used by some program. Then it collects all the cells which
are not being used and adds their address to the free pool, so that these cells can be reused by
other programs. This process is called garbage collection.
TT 17
➢ 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
TT 18
➢ Algorithm to Searching for a value in 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 5: EXIT
To insert a new node at the beginning of a 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 a 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. The step 2 & 3 of an algorithm, allocate memory for the new node. In C, there
are functions like malloc(), alloc(), and calloc() which automatically do the memory allocation
on behalf of the user.
TT 19
➢ Algorithm to Insert a new node at the beginning of the Singly linked list
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 7
[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 NEW_NODE -> NEXT = START
Step 6: SET START = NEW_NODE
Step 7: EXIT
To insert a new node at the end of a linked list. In Algorithm Step 6, 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, in Step 9, 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.
TT 20
➢ Algorithm to Insert a new node at the beginning of the Singly linked list
Step 1: IF AVAIL = NULL end
Write OVERFLOW
Go to Step 10
[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 NEW_NODE -> NEXT = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR -> NEXT! =NULL
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: SET PTR -> NEXT = NEW_NODW
Step 10: EXIT
C. Insert a new node before given node at the Singly linked list
In Step 5, 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. 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, in Steps 10 and 11, we change the NEXT pointers
in such a way that the new node is inserted before the desired node.
TT 21
➢ Algorithm to Insert a new node before given node at the Singly linked list
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 12
[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: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PTR -> DATA! =NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 10: PREPTR -> NEXT = NEW_NODE
Step 11: SET NEW_NODE -> NEXT = PTR
Step 12: EXIT
D. Insert a new node before given node at the Singly linked list
In Step 5, 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.
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 after this node.
Once we reach this node, in Steps 10 and 11, we change the NEXT pointers in such a way that
new node is inserted after the desired node.
TT 22
➢ Algorithm to Insert a new node after given node at the Singly linked list
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 12
[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: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PREPTR -> DATA! =NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 10: PREPTR -> NEXT = NEW_NODE
Step 11: SET NEW_NODE -> NEXT = PTR
Step 12: EXIT
TT 23
➢ Algorithm to Delete a First node from the Singly Linked List
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START -> NEXT
Step 4: FREE PTR
Step 5: EXIT
TT 24
➢ Algorithm to Delete a Last node from the Singly Linked List
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 and 5
While PTR -> NEXT! = NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
➢ Algorithm to Delete a node after a given node from the Singly Linked List
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 10
[END OF IF]
Step 2: SET PTR = START
TT 25
Step 3: SET PREPTR = PTR
Step 4: Repeat Steps 5 and 6
while PREPTR -> DATA! =NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 7: SET TEMP = PTR
Step 8: SET PREPTR -> NEXT = PTR -> NEXT
Step 9: FREE TEMP
Step 10: EXIT
TT 26
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. While inserting a node in a circular linked list, we have to use a while loop to
traverse to the last node of the list. 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.
➢ Algorithm to Insert new node at the beginning of the Circular Linked List
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 Step 7 while PTR -> NEXT! =START
Step 7: PTR = PTR -> NEXT
[END OF LOOP]
Step 8: SET NEW_NODE -> NEXT = START
Step 9: SET PTR -> NEXT= NEW_NODE
Step 10: SET START = NEW_NODE
Step 11: EXIT
TT 27
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.
➢ Algorithm to Insert new node at the end of the Circular Linked List
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 10
[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 NEW_NODE -> NEXT = START
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR -> NEXT! = START
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: SET PTR -> NEXT = NEW_NODE
Step 10: EXIT
➢ Deletion of node from the Circular Linked List
A. Deletion of node from the beginning of the Circular Linked List
In Step 5, 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, in Step 7, the
second node now becomes the first node of the list and its address is stored in the pointer
variable START.
TT 28
➢ Algorithm to Delete node from beginning of the Circular Linked List
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR NEXT! =START
Step 4: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 5: SET PTR -> NEXT = START -> NEXT
Step 6: FREE START
Step 7: SET START = PTR -> NEXT
Step 8: EXIT
TT 29
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = START
Step 7: FREE PTR
Step 8: EXIT
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. Therefore, it consists
of three parts—data, a pointer to the next node, and a pointer to the previous node.
TT 30
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 insertion of new node at the beginning of the Doubly Linked List
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 9
[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 NEW_NODE -> PREV = NULL
Step 6: SET NEW_NODE -> NEXT = START
Step 7: SET START -> PREV = NEW_NODE
Step 8: SET START = NEW_NODE
Step 9: EXIT
TT 31
➢ Algorithm to insertion of new node at the end of the Doubly Linked List
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 NEW_NODE -> NEXT= NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR -> NEXT!=NULL
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: SET PTR -> NEXT = NEW_NODE
Step 10: SET NEW_NODE -> PREV = PTR
Step 11: EXIT
➢ Insertion of new node before the given value of the Doubly Linked List
In Step 5, 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.
➢ Algorithm to insert new node before the given value at Doubly Linked List
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
TT 32
Step 5: SET PTR = START
Step 6: Repeat Step 7 and 8 while PTR -> DATA != NUM
Step 7: SET PREPTR = PTR
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: SET NEW_NODE -> NEXT = PREPTR - > NEXT
Step 10: SET NEW_NODE -> PREV = PTR -> PREV
Step 11: SET PREPTR -> NEXT = NEW_NODE
Step 12: SET PTR -> PREV = NEW_NODE
Step 13: EXIT
➢ Insertion of new node after the given value of the Doubly Linked List
In Step 5, we take a pointer 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 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 for Insertion of new node after the given value of the Doubly Linked List
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 and 8 while PREPTR -> DATA != NUM
Step 7: SET PREPTR = PTR
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
TT 33
Step 9: SET NEW_NODE -> NEXT = PREPTR - > NEXT
Step 10: SET NEW_NODE -> PREV = PTR -> PREV
Step 11: SET PREPTR -> NEXT = NEW_NODE
Step 12: SET PTR -> PREV = NEW_NODE
Step 13: EXIT
➢ Algorithm for Deletion of node from the beginning of the Doubly Linked List
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 6 [END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START -> NEXT
Step 4: SET START -> PREV = NULL
Step 5: FREE PTR
Step 6: EXIT
TT 34
➢ Algorithm for Deletion of node from the end of the Doubly Linked List
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 and 5 while PTR -> NEXT !=NULL
Step 4: PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = NULL
Step 7: Free PTR
Step 8: Exit
➢ Deletion of node from before given value of the Doubly Linked List
In Step 2, 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. The while loop traverses through the linked
list to reach the given node. 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 field of the succeeding node. Finally, the
memory of the node succeeding the given node is freed and returned to the free pool.
TT 35
➢ Algorithm for Deletion of node from before given value of the Doubly Linked List
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 and 5 while PTR -> DATA != VAL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PTR -> PREV = PREPTR -> PREV
Step 7: SET PREPTR -> PREV -> NEXT = PTR
Step 8: Free PREPTR
Step 9: EXIT
➢ Deletion of node from after given value of the Doubly Linked List
In Step 2, we take a pointer variable PTR and initialize it with START. That is, PTR now
points to the first node of the linked list. The while loop traverses through the linked list to
reach the desired node. 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.
➢ Algorithm for Deletion of node from After given value of the Doubly Linked List
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 and 5 while PREPTR -> DATA != VAL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
TT 36
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = PTR -> NEXT
Step 7: SET PTR -> NEXT -> PREV = PREPTR
Step 8: Free PTR
Step 9: EXIT
TT 37
➢ Algorithm to Update Value at a Linked List
TT 38