Ds ch-2
Ds ch-2
❖ Application of Array:-
Sparse Matrix
❖ It is one of the simplest data structures where each data element can be randomly
accessed by using its index number.
❖ For example, if we want to store the marks of a student in 6 subjects, then we don't
need to define a different variable for the marks in different subjects. Instead, we can
define an array that can store the marks in each subject at the contiguous memory
locations.
Representation of an Array
❖ As per the above illustration, there are some of the following important points -
Index starts with 0.
The array's length is 10, which means we can store 10 elements.
Each element in the array can be accessed via its index.
One - Dimensional Array
❖ Simplest data structure that makes use of computed address to locate its elements is the
one-dimensional array or vector.
❖ Number of memory locations is sequentially allocated to the vector.
❖ A vector size is fixed and therefore requires a fixed number of memory locations.
❖ Vector A with subscript lower bound of “one” is represented as below….
L0 is the address of the first word allocated to the first element of vector A
C words is size of each element or node L
The address of element Ai is
0
Loc(Ai) = L0 + (C*(i-1))
Let’s consider the more general case of a vector A with
L0+(i-1)C
lower bound for it’s subscript is given by some variable b.
The address of element Ai is A[i]
Loc(Ai) = L0 + (C*(i-b))
Calculating the Length of an Array
❖ The length of an array is given by the number of elements stored in it. The general
formula to calculate the length of an array is
Length = upper_bound – lower_bound + 1
❖ where upper_bound is the index of the last element and lower_bound is the index of the
first element in the array.
Lower Bound And Upper Bound In One
Dimensional Array
❖ Let suppose we have 1 dimensional array:
int A[6]={15,7,11,44,93,20};
❖ B = Base Address
❖ W = Storage size of one
element stored in array (in byte)
❖ I = Subscript of element whose
address is to be found
❖ Lb = Lower limit / Lower bound
of subscript, if not specified
Assume 0 (zero).
So, address of A[i] = B + W * ( I – Lb)
Calculating the Address of 1D Array
Elements
❖ The formula to perform this calculation is,
❖ Here, A is the array, k is the index of the element of which we have to calculate the address,
Base_Address is the base address of the array A, and w is the size of one element in
memory, for example, size of int is 2 bytes.
Address calculation in 1d array :
Example
❖ Example 1 : Given an array int marks[] = {99,67,78,56,88,90,34,85}, calculate the
address of marks[4] if the base address = 1000.
We know that storing an integer value requires 2 bytes, therefore, its size is 2 bytes.
Address of A[k] = Base_Address (A) + w(k – Lower_Bound)
marks[4]=1000+2(4–0)
=1000+2(4)
=1008 [Ans]
Address calculation in 1d array :
Example
❖ Example 2 : Base address of an array B[1300 : 1900] as 1020 and size of each element
of array is 2 bytes in the memory. Find the address of B[1700].
The given values are: Base_Address = 1020
Lower_Bound = 1300
w=2
k = 1700
We know that storing an integer value requires 2 bytes, therefore, its size is 2 bytes.
Address of A[k] = Base_Address(A) + w(k – Lower_Bound)
B[1700]=1020+2*(1700–1300)
=1020+2*400
=1020+800
=1820 [Ans]
Two - Dimensional Array
❖ Two dimensional arrays are also called table or matrix.
❖ Two dimensional arrays have two subscripts:-
Column Major Order Matrix
Row Major Order Matrix
❖ Column Major Order Matrix: Two dimensional array in which elements are stored
column by column is called as column major matrix.
❖ Two dimensional array consisting of two rows and four columns is stored sequentially by
columns :
A[1,1],A[2,1], A[1,2],A[2,2], A[1,3],A[2,3], A[1,4],A[2,4]
Calculating the Address of 2D Array
Elements (Column Major Order Matrix)
❖ Formula:-
A[I][J] = B.A. + Size * [(I-Lr) + R* (J-Lc)]
R = Ur – Lr + 1
Here,
B.A. = Base Address
I = Row Index of Element whose address to be calculated
J = Column Index of Element whose address to be calculated
Lr = Lower Bound of Row Index , Lc = Lower Bound of Column Index
Ur = Upper Bound of Row Index , Uc = Upper Bound of Column Index
Size = size (one element in array)
R = Total No. of Rows
Address calculation in 2d array :
Example ( Columns Major Matrix)
❖ Example 1 :Calculate the address of A[6][3] if the given array as A[9] [10] Base
address is 102. The array is floating type array. Here Elements are stored in column
major order.
Solution:-
Base Address = 102, I = 6, J = 3, Given Array A[9] [10] So, R=[9], C=[10]
In Array, A[9] [10] means range B[0…8] [0…9], Size = 4
So, Lr=Lc=0 and Ur = 8, Uc = 9
R = Ur – Lr + 1 = 8-0+1 = 9 So, R = 9
Formula:- A[I] [J] = B.A. + size * [ (I-Lr) + R* (J-Lc) ]
A[6] [3] = 102 + 4 * [ (6-0) + 9 (3-0) ]
= 102 + 4 * (33)
= 234 Answer
Two - Dimensional Array
❖ Row Major Order Matrix: Two dimensional array in which elements are stored row by
row is called as row major matrix.
b2 u2 m = no of rows
n = no of columns
b1
b1 = lower bound subscript of row
u1 = upper bound subscript of row
m = u1 – b1 + 1
u1
mxn b2 = lower bound subscript of column
u2 = upper bound subscript of column
n = u2 – b2 + 1
❖ The address element A [ i , j ] is given by
Loc (A [ i , j ]) = L0 + (i-1)*m + (j – 1)
❖ The address element A [ i , j ] is given by
Loc (A [ i , j ]) = L0 + (i-b1)*(u2-b2+1) + (j – b2)
Calculating the Address of 2D Array
Elements (Row Major Order Matrix)
❖ Example:-
A[I][J] = B.A. + Size * [ C* (I-Lr) + (J-Lc)]
C = Uc – Lc + 1
Here,
B.A. = Base Address
I = Row Index of Element whose address to be calculated
J = Column Index of Element whose address to be calculated
Lr = Lower Bound of Row Index , Lc = Lower Bound of Column Index
Ur = Upper Bound of Row Index , Uc = Upper Bound of Column Index
Size = size (one element in array)
C= Total No. of Columns
Address calculation in 2d array :
Example ( Row Major Matrix)
❖ Example 1 :Calculate the address of B[6][8] if the given array as B[11] [13] Base
address is 1000. The array is floating type array. Here Elements are stored in row major
order.
Solution:-
Base Address = 1000, I = 6, J = 8, Given Array B[11] [13] So, R=[11], C=[13]
In Array, B[11] [13] means range B[0…10] [0…12], Size = 4
So, Lr=Lc=0 and Ur = 10, Uc = 12
C = Uc – Lc + 1 = 12-0+1 = 13 So, C = 13
Formula:- A[I] [J] = B.A. + size * [ C * (I-Lr) + (J-Lc) ]
B[6] [8] = 1000 + 4 * [ 13 (6-0) + (8-0) ]
= 1000 + 4 * (108+8)
= 1344 Answer
Application of an Array
1. Symbol Manipulation (matrix representation of polynomial equation)
2. The array are also used for representing some sequential data structures such as a stack
and queue.
3. Sparse Matrix
2
2X + 5XY + Y 2 X2 + 3XY + Y2+Y-X
1 1 1
5 -1 3
1
Sparse Matrix
❖ An m x n matrix is said to be sparse if “many” of its elements are zero.
❖ A matrix that is not sparse is called a dense matrix.
❖ We can device a simple representation scheme whose space requirement equals the
size of the non-zero elements.
0 1 2 3 4 5 6 7 8
1 1 2 2 2 3 3 4 4
4 7 2 5 8 4 6 2 3
2 1 6 7 3 9 8 4 5
Sparse Matrix
❖ Example:-
Consider a matrix of size 100 X 100 containing only 10 non-zero elements.
In this matrix, only 10 spaces are filled with non-zero values and remaining spaces
of the matrix are filled with zero.
Totally we allocate 100 X 100 X 2 = 20000 bytes of space to store this integer
matrix.
To access these 10 non-zero elements we have to make scanning for 10000 times.
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.
❖ Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes
in the matrix are of no use in most of the cases. So, instead of storing zeroes with
non-zero elements, we only store non-zero elements. This means storing non-zero
elements with triples- (Row, Column, value).
❖ Sparse Matrix Representations can be done in many ways following are two common
representations:
Array representation
Linked list representation
Why Sparse Matrix a better option over
a simple matrix?
❖ Operations of Sparse Matrix
We can perform 3 operations on Sparse Matrix
✔ Add
✔ Multiply
✔ Transpose
❖ To construct matrix structure from liner representation we need to record.
❖ Original row and columns of each non zero entries.
❖ Number of rows and columns in the matrix.
❖ So each element of the array into which the sparse matrix is mapped need to have three
fields: row, column and value.
Sparse Matrix
Linear representation of Matrix
1 3 6
1 5 9
A=
2 1 2
2 4 7
2 5 8
2 7 4
3 1 10
4 3 12
6 4 3
6 7 5
Memory Space required to store
Linear Representation
30 x 2 = 60 bytes
STACK
Outline
❖ STACK
❖ Application of STACK
❖ Operation of STACK
❖ Conversion of Expression :-
Infix to Postfix
Infix to Prefix
❖ Evaluate Expression:-
Postfix Expression
Prefix Expression
Infix Expression
❖ Recursion:-
GCD
Tower of Hanoi
STACK
❖ A linear list which allows insertion and deletion of an element at one end only is called
stack.
❖ The insertion operation is called as PUSH and deletion operation as POP.
❖ The most accessible elements in stack is known as TOP.
❖ The elements can only be removed in the opposite orders from that in which they were
added to the stack.
❖ Such a linear list is referred to as a LIFO (Last In First Out) list or FILO (First In
Last Out).
❖ A pointer TOP keeps track of the top element in the stack.
❖ Initially, when the stack is empty, TOP has a value of “zero”.
❖ Each time a new element is inserted in the stack, the pointer is incremented by “one”
before, the element is placed on the stack.
❖ The pointer is decremented by “one” each time a deletion is made from the stack.
STACK
21 ) ( ABC*D/+E-FG/HI+/+
22 ) ABC*D/+E-FG/HI+/+
Task Examples Solution:-
Task Examples Solution:-
Task Examples Solution:-
Task Examples Solution:-
Task Examples Solution:-
Infix to Prefix Conversion
Infix to Prefix Conversion
Infix to Prefix Conversion:-
❖ Algorithm Approach:-
1. Reverse the infix expression
2. Make every ‘(‘ (Opening bracket) as ‘)’ (closing bracket) and ‘)’ as ‘(‘.
3. Convert the modified expression to postfix.
4. Reverse the postfix expression.
1) GCD ( 64 , 8 ) = 64 % 8 => 0
Ans:- 8
2) GCD ( 64 , 6 ) = 64 % 6 => 4
GCD ( 6 , 4 ) = 6 % 4 => 2
GCD ( 4 , 2 ) = 4 % 2 => 0
Ans:- 2
GCD :- Greatest Common Divisior
Tower of Hanoi
❖ It is a classic problem where you try to move all the disks from one peg to another peg
using only three pegs.
❖ Initially, all of the disks are stacked on top of each other with larger disks under the
smaller disks.
❖ You may move the disks to any of three pegs as you attempt to relocate all of the disks,
but you cannot place the larger disks over smaller disks and only one disk can be
transferred at a time.
Tower of Hanoi
Disk Steps
1 1
2 3
3 7
n 2^n-1
Queue
Outline
❖ Queue
❖ Uses of Queue
❖ Operations of Queue
❖ Circular Queue
❖ Dequeue
❖ Priority Queue
❖ Application of Queue
Queue
❖ Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue
is open at both its ends. One end is always used to insert data (enqueue) and the other is
used to remove data (dequeue).
Queue
❖ Queue: a collection whose elements are added at one end (the rear or tail of the queue)
and removed from the other end (the front or head of the queue)
❖ A queue is a FIFO (first in, first out) data structure
❖ Any waiting line is a queue:
The check-out line at a grocery store
The cars at a stop light
An assembly line
Queue
Queue
Uses of Queues in Computing
❖ For any kind of problem involving FIFO data
❖ Printer queue (e.g. printer in MC 235)
❖ Keyboard input buffer
❖ GUI event queue (click on buttons, menu items)
❖ To encode messages (more on this later)
❖ The linked allocation method of storage can result in both efficient use of computer
storage and computer time.
A linked list is a non-sequential collection of data items.
Each node is divided into two parts, the first part represents the information of
the element and the second part contains the address of the next mode.
The last node of the list does not have successor node, so null value is stored as
the address.
It is possible for a list to have no nodes at all, such a list is called empty list.
Link List
❖ In a single linked list, the address of the first node is always stored in a reference
node known as "front" (Some times it is also known as "head").
❖ Always next part (reference part) of the last node must be NULL.
Operations Link List
❖ In a single linked list we perform the following operations...
Insertion
Deletion
Display
Insertion
❖ In a single linked list, the insertion operation can be performed in three
ways. They are as follows...
❖ 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)
❖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 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
❖ In a single linked list, the deletion operation can be performed in three ways.
They are as follows...
Insertion
Deletion
Display
Insertion
❖ In a double linked list, the insertion operation can be performed in three ways as
follows...
❖ Step 3: If it is Empty then, assign NULL to newNode → next and newNode to head.
❖ Step 4: If it is not Empty then, assign head to newNode → next and newNode to head.
Inserting At End of the list
Inserting At End of the 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 assign NULL to newNode → previous and newNode to
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 in the list (until temp → next is equal to NULL).
❖ Step 6: Assign newNode to temp → next and temp to newNode → previous.
Inserting At Specific location in the list
(After a Node)
Inserting At Specific location in the list
(After a Node)
❖ Step 1: Create a newNode with given value.
❖ Step 2: Check whether list is Empty (head == NULL)
❖ Step 3: If it is Empty then, assign NULL to newNode → previous & newNode →
next and newNode to head.
❖ Step 4: If it is not Empty then, define two node pointers temp1 & temp2 and
initialize temp1 with head.
❖ Step 5: Keep moving the temp1 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 is the node value after which we want to insert the newNode).
Inserting At Specific location in the list
(After a Node)
❖ Step 6: Every time check whether temp1 is reached to the last node. If it is reached to
the last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and 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
❖ In a double linked list, the deletion operation can be performed in three ways as
follows...
❖ 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 → previous is equal to temp →
next)
❖ Step 5: If it is TRUE, then set head to NULL and delete temp (Setting Empty list
conditions)
❖ Step 6: If it is FALSE, then assign temp →next to head, NULL to head → previous and
delete temp.
Deleting from End of the list
Deleting from End of the list
❖ Step 1: Check whether list is Empty (head == NULL)
❖ Step 2: If it is Empty, then display 'List is Empty!!! Deletion is not possible' and
terminate the function.
❖ Step 3: If it is not Empty then, define a Node pointer 'temp' and initialize with head.
❖ Step 4: Check whether list has only one Node (temp →previous and temp → next
both are NULL)
❖ Step 5: If it is TRUE, then assign NULL to head and delete temp. And terminate from
the function. (Setting Empty list condition)
❖ Step 6: If it is FALSE, then keep moving temp until it reaches to the last node in the
list. (until temp → next is equal to NULL)
❖ Step 7: Assign NULL to temp → previous → next and delete temp.
Deleting a Specific Node from the list
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 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 to the last 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 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)).
Deleting a Specific Node from the list
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
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).
Circular Linked List
❖ Circular linked list is a sequence of elements in which every element has link to its
next element in the sequence and the last element has a link to the first element in the
sequence.
Circular Linked List Operations
❖ In a circular linked list, we perform the following operations...
Insertion
Deletion
Display
Insertion Operations
❖ In a circular linked list, the insertion operation can be performed in three ways.
They are as follows...
Head=NULL
Head
Inserting At End of the list
❖ 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 == head).
❖ Step 6: Set temp → next = newNode and newNode → next = head.
Inserting At Specific location in the list
(After a Node)
Inserting At Specific location in the list
(After a Node)
❖ 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 node after
which we want to insert the newNode (until temp1 → data is equal to location,
here location is the node value after which we want to insert the newNode).
Inserting At Specific location in the list
(After a Node)
❖ Step 6: Every time check whether temp is reached to the 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: If temp is reached to the exact node after which we want to insert the
newNode then check whether it is last node (temp → next== head).
❖ Step 8: If temp is last node then set temp → next = newNode and
newNode → next = head.
❖ Step 9: If temp is not last node then set newNode → next = temp → next and
temp → next = newNode.
Deletion
❖ In a circular linked list, the deletion operation can be performed in three
ways those are as follows...
Deleting from Beginning of the list
Deleting from End of the list
Deleting a Specific Node
Deleting from Beginning of the list
Deleting from Beginning of 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 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.
Deleting from End of the list
Deleting from End of 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: Check whether list has only one Node (temp1 →next == head)
❖ Step 5: If it is TRUE. Then, set head = NULL and delete temp1. And terminate
from 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 temp1 reaches to the last node in the list. (until temp1
→ next == head)
❖ Step 7: Set temp2 → next = head and delete temp1.
Deleting a Specific Node from the list
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 or to the
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)).
Deleting a Specific Node from the list
❖ 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 11: 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)).
Displaying a circular 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 head → data.
Search & Join
Split
Linked List using Stack
Push(value) - Inserting an element into
the Stack
❖ Step 1: Create a newNode with given value.
❖ Step 3: If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
❖ 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 it to the next node. Repeat the same until
temp reaches to the first node in the stack (temp → next != NULL).
❖ Step 4: If it is Not Empty then, set rear → next = newNode and rear = newNode.
DeQueue() - Deleting an Element from
Queue
❖ Step 1: Check whether queue is Empty (front == NULL).
❖ 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
❖ 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 same until
'temp' reaches to 'rear' (temp → next != NULL).