0% found this document useful (0 votes)
2 views14 pages

Appl.stackQ 1

The document explains the concept of a circular queue, which is a linear data structure that connects the last position back to the first to efficiently utilize space. It details the implementation and functions such as enQueue, deQueue, and display, along with their respective steps and conditions. Additionally, it discusses expression types and conversions, particularly focusing on infix, postfix, and prefix expressions, including methods for converting and evaluating these expressions using stack data structures.

Uploaded by

pespsyco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Appl.stackQ 1

The document explains the concept of a circular queue, which is a linear data structure that connects the last position back to the first to efficiently utilize space. It details the implementation and functions such as enQueue, deQueue, and display, along with their respective steps and conditions. Additionally, it discusses expression types and conversions, particularly focusing on infix, postfix, and prefix expressions, including methods for converting and evaluating these expressions using stack data structures.

Uploaded by

pespsyco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Circular Queue

In a normal Queue Data Structure, we can insert elements until


queue becomes full. But once if queue becomes full, we can not
insert the next element until all the elements are deleted from the
queue. For example consider the queue below...

After inserting all the elements into the queue.

Now consider the following situation after deleting three elements


from the queue...

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.

What is Circular Queue?


A Circular Queue can be defined as follows...
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.

Graphical representation of a circular queue is as follows...

Implementation of Circular Queue


To implement a circular queue data structure using array, we first
perform the following steps before we implement actual operations.

 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...

 Step 1: Check whether queue is FULL. ((rear == SIZE-1 &&


front == 0) || (front == rear+1))
 Step 2: If it is FULL, then display "Queue is FULL!!!
Insertion is not possible!!!" and terminate the function.
 Step 3: If it is NOT FULL, then check rear == SIZE - 1 &&
front != 0 if it is TRUE, then set rear = -1.
 Step 4: Increment rear value by one (rear++),
set queue[rear] = value and check 'front == -1' if it
is TRUE, then set front = 0.

deQueue() - Deleting a value from the


Circular Queue
In a circular queue, deQueue() is a function used to delete an
element from the circular queue. In a circular queue, the element is
always deleted from front position. The deQueue() function doesn't
take any value as parameter. We can use the following steps to
delete an element from the circular queue...

 Step 1: Check whether queue is EMPTY. (front == -1 &&


rear == -1)
 Step 2: If it is EMPTY, then display "Queue is EMPTY!!!
Deletion is not possible!!!" and terminate the function.
 Step 3: If it is NOT EMPTY, then display queue[front] as
deleted element and increment the front value by one (front
++). Then check whether front == SIZE, if it is TRUE, then
set front = 0. Then check whether both front -
1 and rear are equal (front -1 == rear), if it TRUE, then set
both front and rear to '-1' (front = rear = -1).

display() - Displays the elements of a Circular


Queue
We can use the following steps to display the elements of a circular
queue...

 Step 1: Check whether queue is EMPTY. (front == -1)


 Step 2: If it is EMPTY, then display "Queue is
EMPTY!!!" and terminate the function.
 Step 3: If it is NOT EMPTY, then define an integer variable 'i'
and set 'i = front'.
 Step 4: Check whether 'front <= rear', if it is TRUE, then
display 'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until 'i <= rear' becomes FALSE.
 Step 5: If 'front <= rear' is FALSE, then display 'queue[i]'
value and increment 'i' value by one (i++). Repeat the same
until'i <= SIZE - 1' becomes FALSE.
 Step 6: Set i to 0.
 Step 7: Again display 'cQueue[i]' value and increment i value
by one (i++). Repeat the same until 'i <= rear'
becomes FALSE.

 void enQueue(int value)


 {
 if((front == 0 && rear == SIZE - 1)
 || (front == rear+1))
 printf("\nCircular Queue
 is Full!Insertion not possible!!!\n");
 Else
 {
 if(rear == SIZE-1 && front != 0)
 rear = -1;
 cQueue[++rear] = value;
 printf("\nInsertion Success!!!\n");
 if(front == -1)
 front = 0;
 }
 }

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 +ab ab+

a+b*c +a*bc abc*+

(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.

An expression can be defined as follows...

An expression is a collection of operators and operands


that represents a specific value.
In above definition, operator is a symbol which performs a
particular task like arithmetic operation or logical operation or
conditional operation etc.,

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.

The general structure of an Infix expression is as follows...

Operand1 Operator Operand2

Example
Postfix Expression
In postfix expression, operator is used after operands. We can say
that "Operator follows the Operands".

The general structure of Postfix expression is as follows...

Operand1 Operand2 Operator

Example

Prefix Expression
In prefix expression, operator is used before operands. We can say
that "Operands follows the Operator".

The general structure of Prefix expression is as follows...

Operator Operand1 Operand2


Example

Any expression can be represented using the above three different


types of expressions. And we can convert an expression from one
form to another form like Infix to Postfix, Infix to Prefix, Prefix
to Postfix and vice versa.

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.

To convert any Infix expression into Postfix or Prefix expression we


can use the following procedure...

1. Find all the operators in the given Infix Expression.


2. Find the order of operators evaluated according to their
Operator precedence.
3. Convert each operator into required type of expression (Postfix
or Prefix) in the same order.

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*+ =

Finally, given Infix Expression is converted into Postfix Expression as


follows...

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

A postfix expression is a collection of operators and operands in


which the operator is placed after the operands. That means, in a
postfix expression the operator follows the operands.

Postfix Expression has following general structure...

Operand1 Operand2 Operator


Example

Postfix Expression Evaluation using


Stack Data Structure
A postfix expression can be evaluated using the Stack data
structure. To evaluate a postfix expression using Stack data
structure we can use the following steps...

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...

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy