Stack ADT-Operations
Stack ADT-Operations
Stack ADT-Operations:
What is Stack?
A stack is a linear data structure in which the insertion of a new
element and removal of an existing element takes place at the same end
represented as the top of the stack.
To implement the stack, it is required to maintain the pointer to the top of the
stack, which is the last element to be inserted because we can access the
elements only on the top of the stack.
Examples:
Input: A + B * C + D
Output: ABC*+D+
Input: ((A + B) – C * (D / E)) + F
Output: AB+CDE/*-F+
3rd Step: Now i = 2 and exp[i] = ‘b’ i.e., an operand. So add this in the postfix
expression. postfix = “ab” and stack = {+}.
4th Step: Now i = 3 and exp[i] = ‘*’ i.e., an operator. Push this into the
stack. postfix = “ab” and stack = {+, *}.
Push ‘*’ in the stack
5th Step: Now i = 4 and exp[i] = ‘c’ i.e., an operand. Add this in the postfix
expression. postfix = “abc” and stack = {+, *}.
6th Step: Now i = 5 and exp[i] = ‘+’ i.e., an operator. The topmost element of
the stack has higher precedence. So pop until the stack becomes empty or the
top element has less precedence. ‘*’ is popped and added in postfix. So postfix
= “abc*” and stack = {+}.
Pop ‘*’ and add in postfix
Now top element is ‘+‘ that also doesn’t have less precedence. Pop it. postfix =
“abc*+”.
7th Step: Now i = 6 and exp[i] = ‘d’ i.e., an operand. Add this in the postfix
expression. postfix = “abc*+d”.
Final Step: Now no element is left. So empty the stack and add it in the postfix
expression. postfix = “abc*+d+”.
Pop ‘+’ and add it in postfix
Queue ADT:
What is Queue Data Structure?
A Queue is defined as a linear data structure that is open at both ends and the
operations are performed in First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at one
end, and all deletions from the list are made at the other end. The element
which is first pushed into the order, the operation is first performed on that.
FIFO Principle of Queue:
A Queue is like a line waiting to purchase tickets, where the first person in
line is the first person served. (i.e. First come first serve).
Position of the entry in a queue ready to be served, that is, the first entry that
will be removed from the queue, is called the front of the
queue(sometimes, head of the queue), similarly, the position of the last entry
in the queue, that is, the one most recently added, is called the rear (or
the tail) of the queue. See the below figure.
Characteristics of Queue:
Queue can handle multiple data.
We can access both ends.
They are fast and flexible.
Types of Queues:
There are five different types of queues that are used in different
scenarios. They are:
1. Input Restricted Queue (this is a Simple Queue)
2. Output Restricted Queue (this is also a Simple Queue)
3. Circular Queue
4. Double Ended Queue (Deque)
5. Priority Queue
Ascending Priority Queue
Descending Priority Queue
1. Circular Queue: Circular Queue is a linear data structure in which the
operations are performed based on FIFO (First In First Out) principle and the
last position is connected back to the first position to make a circle. It is also
called ‘Ring Buffer’. This queue is primarily used in the following cases:
1. Memory Management: The unused memory locations in the case of
ordinary queues can be utilized in circular queues.
2. Traffic system: In a computer-controlled traffic system, circular queues are
used to switch on the traffic lights one by one repeatedly as per the time set.
3. CPU Scheduling: Operating systems often maintain a queue of processes
that are ready to execute or that are waiting for a particular event to occur.
The time complexity for the circular Queue is O(1).
2. Input restricted Queue: In this type of Queue, the input can be taken from
one side only(rear) and deletion of elements can be done from both sides(front
and rear). This kind of Queue does not follow FIFO(first in first out). This queue
is used in cases where the consumption of the data needs to be in FIFO order
but if there is a need to remove the recently inserted data for some reason and
one such case can be irrelevant data, performance issue, etc.
4. Double ended Queue: Double Ended Queue is also a Queue data structure
in which the insertion and deletion operations are performed at both the ends
(front and rear). That means, we can insert at both front and rear positions and
can delete from both front and rear positions. Since Deque supports both stack
and queue operations, it can be used as both. The Deque data structure
supports clockwise and anticlockwise rotations in O(1) time which can be useful
in certain applications. Also, the problems where elements need to be removed
and or added both ends can be efficiently solved using Deque.