UNIT-III DSUC-UG
UNIT-III DSUC-UG
USING
Satcks:C Introduction to stacks, Stack as an Abstract Data
Type, Representation of stacks through Arrays,
Representation of Stacks through Linked Lists, Applications
of Stacks, Stacks and Recursion
Queues: Introduction, Queue as an Abstract data Type,
Representation of Queues, circular Queues, Double-ended
queues-Deques, Priority queues, Application of Queues
Read Data i.e 1 and push the data into the stack. Check the top
value before pushing the data into the stack. If top is equal to
Size-1 Print ”Stack is Full”. If top is not equal to Size-1 then
increment the top value and insert data into the stack at top value.
Read Data i.e 2 and push the data into the stack. Check the top
value before pushing the data into the stack. If top is equal to
Size-1 Print ―Stack is Full‖. If top is not equal to Size-1 then
increment the top value and insert data into the stack at top value.
Read Data i.e 3 and push the data into the stack. Check the top
value before pushing the data into the stack. As top is equal to
Size-1 Print ―Stack is Full‖ and data 3 is not pushed onto the stack.
Remove Data i.e 2 from the stack. Check the top value before
deleting the data from the stack. If top is equal to -1 Print ―Stack is
Empty‖. If top is not equal to - 1 then get the value at the top into
Remove Data i.e. 3 from the stack. Check the top value before deleting
the data from the stack. As top is equal to -1 Print ―Stack is Empty‖.
Check the top value before displaying the data from the stack. If top is
equal to
-1 Print ”Stack is Empty”. If top is not equal to -1 then initialize I with
top values and print the value at the I position and then decrement
the I value.
The elements in STACK are as
follows Top-->2
If I is greater than or equal to 0 then print the value at the I position
and then decrement the I value.
The elements in STACK are as
follows Top-->2
1
If I is not greater than or equal to 0 then print
”End”
The elements in STACK are as follows
Top-->2
1
End
Checking whether the stack is empty:
Algorithm:
isempty() Step 1:
If(Top==-1)
Return
1 Else
Return 0
Checking whether the stack is full:
Algorithm: isfull()
Step 1: If(Top==Size-1)
Return
1 Else
Return 0
Read Data i.e 1 and insert the data into the queue. Check the Rear
value before inserting the data into the queue. If Rear is equal to Size-
1 Print ”Queue is Full”. If Rear is not equal to Size-1 then increment
the Rear value and insert data into the queue at rear. Check the Front
value after inserting the data into the queue. If Front is equal to zero
then assign the Rear value to the front.
Read Data i.e 1 and insert the data into the queue. Check the Rear
value before inserting the data into the queue. If Rear is equal to Size-
1 Print “Queue is Full”. If Rear is not equal to Size-1 then increment
the Rear value and insert data into the queue at rear. Check the Front
value after inserting the data into the queue. If Front is equal to zero
then assign the Rear value to the front.
Read Data i.e 3 and insert the data into the queue. Check the Rear
value before inserting the data into the queue. As Rear is equal to
DATA STRUCTURES UNIT: III
USING
Size-1 C ”Queue is Full” and data 3 is not pushed onto the queue.
Print
DATA STRUCTURES UNIT: III
USING C
Delete an element from Front position of the queue:
Algorithm:
delete() Step 1:
If(Front==-1)
Print ”Queue is
Empty” Else
{
Num=Q[Fro
nt] If
(Front=Rear
)
Rear=Front
=-1 Else
Front=Front
+1 Return
Num
}
Procedure to delete data 1,2 from the queue:
Initially the queue contains two elements i.e Front=0 and
Rear=1.
Remove Data i.e 1 from the queue. Check the Front value before
deleting the data from the Queue. If Front is equal to -1 Print ―Queue
is Empty‖. If Front is not equal to -1 then get the value at the Front into
num and then decrement the Front value. If Front value equal to -1
then assign the Front value to Rear.
Remove Data i.e 2 from the queue. Check the Front value before
deleting the data from the Queue. If Front is equal to -1 Print ―Queue
is Empty‖. If Front is not equal to -1 then get the value at the Front into
num and then decrement the Front value. If Front value equal to -1 then
DATA STRUCTURES UNIT: III
USING
assign theCFront value to Rear.
Remove Data i.e. 3 from the Queue. Check the Front value before
deleting the
DATA STRUCTURES UNIT: III
USING
data from Cthe Queue. As Front is equal to -1, Print ”Queue is Empty”.
Check the Front value before displaying the data from the stack.If Front
is equal to -1, Print ”Queue is Empty”. If Front is not equal to -1, then
initialize I with Front values and print the value at the I position and
then increment the I value.
The elements in Queue are as
follows Front-->1
If I is less than or equal to Rear then print the value at the I position and
then increment the I value.
The elements in Queue are as
follows Front-->1-->2
If I is less than or equal to Rear then print
”Rear”
The elements in Queue are as follows
Front-->1-->2-->Rear
All nodes are connected to each other in a sequential manner. The pointer
of the
DATA STRUCTURES UNIT: III
USING
first nodeCpoints to the value of the second and so on. The first
node has no pointer pointing towards it whereas the last node has
no pointer pointing out from it.
That is items can be added or deleted from the front or rear end, but
no changes can be made elsewhere in the list.
There are two types of deque. They are
1. Input Restricted Deque
2. Output Restricted Deque
DATA STRUCTURES UNIT: III
USINGRestricted
Input C Deque: Input Restricted Deque is a deque which
allows the insertions at one end of the queue but delete operations
can be done at both the ends of the queue.