Stack Queue
Stack Queue
When the new element is pushed in a stack, first, the value of the
top gets incremented, i.e., top=top+1, and the element will be
placed at the new position of the top.
The elements will be inserted until we reach the max size of the
stack.
POP operation
Increment the variable Top so that it can now refer to the next
memory location.
{
if (top == n )
printf("\n Overflow");
else
{
top = top +1;
stack[top] = val;
}
}
Deletion of an element from a stack (Pop operation)
int pop ()
{
if(top == -1)
{
printf ("Underflow");
return 0;
}
else
{
return stack[top - - ];
}
}
Infix expression: The expression of the form “a
operator b” (a + b) i.e., when an operator is in-
between every pair of operands.
Examples:
Input: A + B * C + D
Output: ABC*+D+
Why postfix representation of the expression?
The compiler scans the expression either from left to right or from right to
left.
Consider the expression: a + b * c + d
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.
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.
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.
Illustration:
Follow the below illustration for a better understanding
Approach:
The idea is to create an empty stack and push all the characters
from the string into it. Then pop each character one by one from
the stack and put them back into the input string starting from
the 0’th index. As we all know, stacks work on the principle of
first in, last out. After popping all the elements and placing them
back to string, the formed string would be reversed.
Convert the following Infix Notations into Postfix:
1.Input: A*B+C
2.Input: (A+B)*(C/D)
3.Input: A*(B*C+D*E)+F
4.Input: (A+B)*C+(D-E)/F+G
1. Output: AB*C+
2. Output: AB+CD/*
3. Output: ABC*DE*+*F+
4. Output: AB+C*DE-F/+G+
Convert the following Infix expression to
Postfix form using a stack
x + y * z + (p * q + r) * s,
One by one pop all characters from stack and put them back
to string.
Evaluation of postfix expression
Algorithm
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 b
Characteristics of Queue:
Stack is used in solving problems works Queue is used in solving problems having
on recursion. sequential processing.
There are four different types of queue that are listed as follows -
Simple Queue or Linear Queue
In Linear Queue, an insertion takes place from one end while the deletion
occurs from another end. The end at which the insertion takes place is
known as the rear end, and the end at which the deletion takes place is
known as front end. It strictly follows the FIFO rule.
The major drawback of using a linear Queue is that insertion is done only
from the rear end. If the first three elements are deleted from the Queue, we
cannot insert more elements even though the space is available in a Linear
Queue. In this case, the linear Queue shows the overflow condition as the
rear is pointing to the last element of the Queue.
Circular Queue
In Circular Queue, all the nodes are represented as circular. It is similar to the linear Queue
except that the last element of the queue is connected to the first element. It is also known
as Ring Buffer, as all the ends are connected to another end. The representation of circular
queue is shown in the below image -
The drawback that occurs in a linear queue is overcome by using the circular queue. If the
empty space is available in a circular queue, the new element can be added in an empty
space by simply incrementing the value of rear. The main advantage of using the circular
queue is better memory utilization.
Priority Queue
In Deque or Double Ended Queue, insertion and deletion can be done from both
ends of the queue either from the front or rear. It means that we can insert and
delete elements from both front and rear ends of the queue. Deque can be used as
a palindrome checker means that if we read the string from both ends, then the
string would be the same.
Deque can be used both as stack and queue as it allows the insertion and deletion
operations on both ends. Deque can be considered as stack because stack follows
the LIFO (Last In First Out) principle in which insertion and deletion both can be
performed only from one end. And in deque, it is possible to perform both
insertion and deletion from one end, and Deque does not follow the FIFO principle.
The fundamental operations that can be performed on queue are listed
as follows
Peek: This is the third operation that returns the element, which is
pointed by the front pointer in the queue but does not delete it.
We can easily represent queue by using linear arrays. There are two
variables i.e. front and rear, that are implemented in the case of every
queue. Front and rear variables point to the position from where insertions
and deletions are performed in a queue. Initially, the value of front and
queue is -1 which represents an empty queue. Array representation of a
queue containing 5 elements along with the respective values of front and
rear, is shown in the following figure.
Insertion operation: enqueue()
Algorithm
1 – START
2 − Check if the queue is empty.
3 − If the queue is empty, produce underflow error and exit.
4 − If the queue is not empty, access the data where front is
pointing.
5 − Increment front pointer to point to the next available data
element.
6 − Return success.
7 – END