Stack_Queue
Stack_Queue
Algorithm Example
• Step 3 − If the stack is not full, increment top to point next empty
space.
• if stack is full
• return null
• endif
• top ← top + 1
• stack[top] ← data
• end procedure
Example
• void push(int data)
• {
• if(!isFull())
• {
• top = top + 1;
• stack[top] = data;
• }
• else
• {
• printf("Could not insert data, Stack is full.\n");
• }
• }
Pop Operation
• Accessing the content while removing it from the
stack, is known as a Pop Operation.
• if stack is empty
• return null
• endif
• data ← stack[top]
• top ← top - 1
• return data
• end procedure
Example
• int pop(int data)
• {
• if(!isempty())
• {
• data = stack[top];
• top = top - 1;
• return data;
• }
• else
• {
• printf("Could not retrieve data, Stack is empty.\n");
• }
• }
QUEUES
Queue
• Queue is an abstract data structure, somewhat similar
to Stacks.
Insertion Deletion
peek(): This function helps to see the data at
the front of the queue.
Algorithm Example
• if queue is full
• return overflow
• End if
• rear ← rear + 1
• queue[rear] ← data
• return true
• end procedure
Example
• int enqueue(int data)
• if(isfull())
• return 0;
• rear = rear + 1;
• queue[rear] = data;
• return 1;
• end procedure
Dequeue Operation
• Accessing data from the queue is a process of two tasks −
access the data where front is pointing and remove the data
after access.
• if queue is empty
• return underflow
• end if
• data = queue[front]
• front ← front + 1
• return true
• end procedure
Example
• int dequeue()
• {
• if(isempty())
• return 0;
• front = front + 1;
• return data;
• }
circular queue
• A circular queue is a variation of the regular queue in which the last
position is connected back to the first position to form a circle. It
addresses several limitations of a linear queue
• Efficient Use of Space:
•Problem with Linear Queue: In a linear queue, once the queue
reaches its maximum size and elements are dequeued from the
front, the space is wasted. Even though there are free spaces at the
front, the queue cannot reuse them, leading to inefficient use of
memory.
•Solution in Circular Queue: In a circular queue, the last position is
connected back to the first position. So, when the rear pointer
reaches the end of the queue and there are empty spaces at the
front (due to dequeue operations), the rear pointer can wrap around
and start filling these spaces. This makes better use of available
space.
Working of Circular Queue
• Initial state:
Dequeue 10:
• [ _ , _ , _ , _ , _ ] (Empty queue) [ _, 20, 30, _ , _ ]
^front ^rear
• Enqueue 10:
• [10, _ , _ , _ , _ ] Enqueue 40, 50:
• [ _, 20, 30, 40, 50]
^front, ^rear
^front ^rear
Dequeue 20:
• Enqueue 30: [60, _, 30, 40, 50]
• [10, 20, 30, _ , _ ] ^rear ^front
• ^front, ^rear
Basic Operation
isFull:
•The queue is full if the rear is right behind the front,
which happens either when front == 0 && rear ==
SIZE - 1 (queue wraps around), or front == rear + 1
(queue wraps around).
•isEmpty:
•The queue is empty when both front and rear are set
to -1
•enqueue:
•Adds an element at the rear. If the queue is full, the
operation is denied. The rear is incremented circularly by
using the modulo operation rear= (rear + 1) % SIZE.
Operation
•dequeue:
•Removes the element at the front. If the
queue is empty, the operation is denied. The
front is incremented circularly by front= (front
+ 1) % SIZE.
•display:
•Displays all elements from the front to the
rear in circular order, considering the circular
nature of the queue.
Arithmetic Expression Evaluation
The stack organization is very effective in evaluating
arithmetic expressions. Expressions are usually represented
in what is known as Infix notation, in which each operator
is written between two operands (i.e., A + B). With this
notation, we must distinguish between ( A + B )*C and A +
( B * C ) by using either parentheses or some operator-
precedence convention. Thus, the order of operators and
operands in an arithmetic expression does not uniquely
determine the order in which the operations are to be
performed.
• 1. Polish notation (prefix notation) –
It refers to the notation in which the operator is placed before its two
operands. Here no parentheses are required, i.e.,
• +AB
Postfix
• 2. Reverse Polish notation(postfix notation) –
It refers to the analogous notation in which the
operator is placed after its two operands. Again, no
parentheses is required in Reverse Polish notation,
i.e.,
• AB+ Stack-organized computers are better suited for
post-fix notation than the traditional infix notation.
Thus, the infix notation must be converted to the
postfix notation. The conversion from infix notation
to postfix notation must take into consideration the
operational hierarchy.
There are 3 levels of precedence for 5 binary operators as given
below:
H
Operand → Add
F ABCD^E-F ['+', '*', '^', '(']
to output
Operator → Push
+ ABCD^E-F ['+', '*', '^', '(', '+']
to stack
Operand → Add
G ABCD^E-FG ['+', '*', '^', '(', '+']
to output
Operator → Push ['+', '*', '^', '(', '+',
* ABCD^E-FG
to stack '*']
Operand → Add ['+', '*', '^', '(', '+',
H ABCD^E-FGH
to output '*']
Right parenthesis
ABCD^E-FGH
) → Pop to output ['+', '*', '^']
*+
until (
Operator → Pop ^ A B C D ^ E - F G H
- ['-']
and *, then push - * + ^ *
Operand → Add ABCD^E-FGH
I ['-']
to output *+^*I
Pop remaining
ABCD^E-FGH
(end) operators to []
*+^*I-
output
Q:A+(B*C-(D/E^F)*G)*H
Book:Data Structure By Seymour Lipschutz
Example: 6.6
Ans.A B C * D E F ^ / G * - H * +
Infix to prefix
• The idea is to first reverse the given infix
expression while swapping ‘(‘ with ‘)’ and vice
versa, then convert this modified expression to
postfix notation using a stack-based approach
that follows operator precedence and associativity
rules, and finally reverse the obtained postfix
expression to get the prefix notation.
Step by step approach: