Appl.stackQ 1
Appl.stackQ 1
This situation also says that Queue is Full and we can not insert the
new element because, 'rear' is still at last position. In above
situation, even though we have empty positions in the queue we can
not make use of them to insert new element. This is the major
problem in normal queue data structure. To overcome this problem
we use circular queue data structure.
Step 1: Include all the header files which are used in the
program and define a constant 'SIZE' with specific value.
Step 2: Declare all user defined functions used in circular
queue implementation.
Step 3: Create a one dimensional array with above defined
SIZE (int cQueue[SIZE])
Step 4: Define two integer variables 'front' and 'rear' and
initialize both with '-1'. (int front = -1, rear = -1)
Step 5: Implement main method by displaying menu of
operations list and make suitable function calls to perform
operation selected by the user on circular queue.
enQueue(value) - Inserting value into the
Circular Queue
In a circular queue, enQueue() is a function which is used to insert
an element into the circular queue. In a circular queue, the new
element is always inserted at rear position. The enQueue() function
takes one integer value as parameter and inserts that value into the
circular queue. We can use the following steps to insert an element
into the circular queue...
void deQueue()
{
if(front == -1 && rear == -1)
printf("\nCircular Queue is Empty!
Deletion is not possible!!!\n");
else{
printf("\nDeleted element : %d\
n",cQueue[front++]);
if(front == SIZE)
front = 0;
if(front-1== rear)
front = rear = -1;
}
}
void display()
{
if(front == -1)
printf("\nCircular Queue is Empty!!!\n");
else
{
int i = front;
printf("\nCircular Queue Elements are : \n");
if(front <= rear)
{
while(i <= rear)
printf("%d\t",cQueue[i++]);
}
Else
{
while(i <= SIZE - 1)
printf("%d\t", cQueue[i++]);
i = 0;
while(i <= rear)
printf("%d\t",cQueue[i++]);
}
}
}
Application of Stack
1. Expression Evolution
2. Expression conversion
1. Infix to Postfix
2. Infix to Prefix
3. Postfix to Infix
4. Prefix to Infix
3. Parsing
4. Simulation of recursion
5. Fuction call
6. Expression Representation
Infix Prefix(polish notation) Postfix(reverse polish)
(a + b) * (c - d) *+ab-cd ab+cd-*
What is an Expression?
In any programming language, if we want to perform any calculation
or to frame a condition etc., we use a set of symbols to perform the
task. These set of symbols makes an expression.
Operands are the values on which the operators can perform the
task. Here operand can be a direct value or variable or address of
memory location.
Expression Types
Based on the operator position, expressions are divided into THREE
types. They are as follows...
1. Infix Expression
2. Postfix Expression
3. Prefix Expression
Infix Expression
In infix expression, operator is used in between operands.
Example
Postfix Expression
In postfix expression, operator is used after operands. We can say
that "Operator follows the Operands".
Example
Prefix Expression
In prefix expression, operator is used before operands. We can say
that "Operands follows the Operator".
Expression Conversion
Any expression can be represented using three types of expressions
(Infix, Postfix and Prefix). We can also convert one type of
expression to another type of expression like Infix to Postfix, Infix to
Prefix, Postfix to Prefix and vice versa.
Example
Consider the following Infix Expression to be converted into Postfix
Expression...
D=A+B*C
Step 1: The Operators in the given Infix Expression : = , + , *
Step 2: The Order of Operators according to their
preference : * , + , =
Step 3: Now, convert the first operator * ----- D = A + B C *
Step 4: Convert the next operator + ----- D = A BC* +
Step 5: Convert the next operator = ----- D ABC*+ =
DABC*+=
Infix to Postfix Conversion using Stack
Data Structure
To convert Infix Expression into Postfix Expression using a stack
data structure, We can use the following steps...
1. Read all the symbols one by one from left to right in the
given Infix Expression.
2. If the reading symbol is operand, then directly print it to
the result (Output).
3. If the reading symbol is left parenthesis '(', then Push it
on to the Stack.
4. If the reading symbol is right parenthesis ')', then Pop all
the contents of stack until respective left parenthesis is
poped and print each poped symbol to the result.
5. If the reading symbol is operator (+ , - , * , / etc.,), then
Push it on to the Stack. However, first pop the
operators which are already on the stack that have
higher or equal precedence than current operator and
print them to the result.
Example
Consider the following Infix Expression...
(A+B)*(C-D)
The given infix expression can be converted into postfix expression
using Stack data Structure as follows...
The final Postfix Expression is as follows...
AB+CD-*
Postfix Expression Evaluation
1. Read all the symbols one by one from left to right in the
given Postfix Expression
2. If the reading symbol is operand, then push it on to the
Stack.
3. If the reading symbol is operator (+ , - , * , / etc.,), then
perform TWO pop operations and store the two popped
oparands in two different variables (operand1 and
operand2). Then perform reading symbol operation
using operand1 and operand2 and push result back on
to the Stack.
4. Finally! perform a pop operation and display the
popped value as final result.
Example
Consider the following Expression...