Unit 2
Unit 2
Stack
• A Stack is a linear data structure that follows a particular order in which
the operations are performed. The order may be LIFO(Last In First
Out) or FILO(First In Last Out). LIFO implies that the element that is
inserted last, comes out first and FILO implies that the element that is
inserted first, comes out last.
Operations on Stack
• Input: A + B * C + D
Output: ABC*+D+
• Input: ((A + B) – C * (D / E)) + F
Output: AB+CDE/*-F+
Why?
• The compiler scans the expression either from left to right or
from right to left.
Consider the expression: a + b * c + d
• The compiler first scans the expression to evaluate the
expression b * c, then again scans the expression to add a to it.
• The result is then added to d after another scan.
• The repeated scanning makes it very inefficient. Infix
expressions are easily readable and solvable by humans
whereas the computer cannot differentiate the operators and
parenthesis easily so, it is better to convert the expression to
postfix(or prefix) form before evaluation.
Below are the steps
1.Scan the infix expression from left to right.
2.If the scanned character is an operand, put it in the postfix expression.
3.Otherwise, do the following
• If the precedence and associativity of the scanned operator are greater than the
precedence and associativity of the operator in the stack [or the stack is empty or the
stack contains a ‘(‘ ], then push it in the stack. [‘^‘ operator is right associative and
other operators like ‘+‘,’–‘,’*‘ and ‘/‘ are left-associative].
o Check especially for a condition when the operator at the top of the stack and the scanned
operator both are ‘^‘. In this condition, the precedence of the scanned operator is higher due to
its right associativity. So it will be pushed into the operator stack.
o In all the other cases when the top of the operator stack is the same as the scanned operator,
then pop the operator from the stack because of left associativity due to which the scanned
operator has less precedence.
• Else, Pop all the operators from the stack which are greater than or equal to in
precedence than that of the scanned operator.
o After doing that Push the scanned operator to the stack. (If you encounter parenthesis while
popping then stop there and push the scanned operator in the stack.)
4.If the scanned character is a ‘(‘, push it to the stack.
5.If the scanned character is a ‘)’, pop the stack and output it
until a ‘(‘ is encountered, and discard both the parenthesis.
6.Repeat steps 2-5 until the infix expression is scanned.
7.Once the scanning is over, Pop the stack and add the operators
in the postfix expression until it is not empty.
8.Finally, print the postfix expression.
• a+b*c+d
• a+b*(c^d-e)^(f+g*h)-i
• K + L - M * N + (O^P) * W/U/V * T + Q
• abcd^e-fgh*+^*+i-
Expression evaluation
• Infix expression : 2 * (5 * (3 + 6)) / 5 - 2
• Postfix expression : 2 5 3 6 + * * 5 / 2 –
• Prefix expression: - / * 2 * 5 + 3 6 5 2
• Reversed prefix expression: 2 5 6 3 + 5 * 2 * / -
Queue Data Structure
• A queue is a linear data structure that follows the First-In-
First-Out (FIFO) principle. It operates like a line where
elements are added at one end (rear) and removed from the
other end (front).
Basic Operations of Queue Data
Structure
• Enqueue (Insert): Adds an element to the rear of the queue.
• Dequeue (Delete): Removes and returns the element from the
front of the queue.
• Peek: Returns the element at the front of the queue without
removing it.
• Empty: Checks if the queue is empty.
• Full: Checks if the queue is full.
Applications of Queue
• Task scheduling in operating systems
• Data transfer in network communication
• Simulation of real-world systems (e.g., waiting lines)
• Priority queues for event processing queues for event
processing
enQueue() and deQueue()
• #include <bits/stdc++.h> • // Create a new LL node
• using namespace std; • QNode* temp = new QNode(x);