Preeti S. Patil Introduction to Data structure MPSTME, SHIRPUR Stacks & Queues
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 1/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Stacks & Queues
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 2/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Stack• Non-primitive linear data structure.
• Ordered list where addition and deletion of
an existing data item is done at one end
called top.• Last-in first-out (LIFO) data structure.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 3/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Stack Creation,Insertion & Deletion
• Stack has five elements capacity. size=5
topStack empty
[just created]
top AInsert A
A
Insert B
Btop
A
Insert C
B
topC
A
Insert D
B
C
Dtop
A
Insert E
Stack full
B
C
D
Etop
ADelete
B
C
Dtop
ADelete
B
Ctop
ADelete
Btop
ADelete
top
Delete
Stack empty
top
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 4/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
• If attempted to add new element beyond themaximum size, stack full condition isencountered.
• Stack empty condition is reached ,if tried toremove the elements beyond the base of thestack
• PUSH: to add an element in stack
• POP: to delete an element from stack
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 5/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Representation of Stacks• Represented using
– Static [Using Arrays]
– Dynamic [Using Linked List]
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 6/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Static Representation• Created as
int stack[ MAXSIZE ]
Where MAXSIZE is the maximum number of elements.
• Define macro,MAXSIZE with appropriate value
as
#define MAXSIZE 10
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 7/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Operations on stack
Operations Description Restriction
1. PUSH This adds(pushes) a new
element
The number of data items
on the stack should not
exceed MAXSIZE
2. POP This deletes the data item
from the top of the stack.
The number of data item
on the stack should not
go beyond Zero.
3. TOP It returns the value of theitem at the top of the stack. -
4.stack_empty It returns true if the stack is
empty. Otherwise it returns
false
-
5. Stack_full It returns true if the stack is
full. Otherwise it returns
false.
-
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 8/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Push OperationPush (int stack[],int MAXSIZE,int top)
{
int item;if(top==(MAXSIZE-1)) /*Check for overflow*/
{
printf(“ stack is full”);
return;
}else
{ printf(“ Enter the element to be inserted”);
scanf(“%d”,&item);
top=top+1; /* increment top*/
stack[top] = item; /* insert item*/}
}
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 9/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Pop Operationpop (int stack[ ], int top)
{
int item;if(top == -1) /* underflow */
{
printf(“stack is empty”);
return;
}else
{
item= stack[top]; /* delete item*/
top= top-1;
}
return(item);
}
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 10/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Applications of Stack• To keep track of function calls.
• In conversion of arithmetic expressions
into any form.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 11/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Illustration of Function calls• Consider following c program
#include<stdio.h>
main()
{
int i=10, j; j= funct1(i);
printf(“ %d”,j);
} /* End of Main*/
funct1(int k)
{
int m=20, n;
n= funct2(m);printf(“%d”,n);
} /* End of funct1()*/
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 12/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
funct2(int x)
{int y,z;;
y= x+10;
z= funct3(y);
return(z);} /* End of funct2()*/
funct3( int a)
{int b;
b=a*5;
return(b);
} /*End of funct3*/
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 13/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Illustration of function call
Initial stack
Main()
Funct1()
called
Main()
Funct2()
called
Funct3()
called
Main()
Main() Main()
Funct1()
Funct1() Funct1()
Funct2()
Funct3()
completesFunct2()
completes
Funct1()
completes
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 14/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Arithmetic Expression• Expression which involves addition,
subtraction, multiplication and divisionoperations.
• There are three ways of writing arithmeticexpressions.
– Infix
– Postfix – Prefix
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 15/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Infix Expression• Arithmetic operator exists in between two
operands.
• Usual notation of writing mathematicalexpressions.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 16/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Example: Infix ExpressionEg 1: let A & B are two operands then infix form of expression for
arithmetic operators +, -, * & / are as
A + B
A - B A * B
A / B
Eg 2: if A , B & C are three operands, then we can write the infix form
as
(A + B) – C
(A + (B * C)
(A / B) + C
Note: In infix form of expression, parenthesis impose a precedence(priority) of operation.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 17/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Postfix• It is the form of an arithmetic expression in
which, arithmetic operator is fixed (place)after (post) its operands.
• Called as suffix notation.
• Referred even as reverse polish notation.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 18/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Eg 1: let A & B are two operands then postfix form of expression for
arithmetic operators +, -, * & / are as
Eg 2: if A , B & C are three operands, then we can write the postfixform as
INFIX FORM POSTFIX FORM
A + B * C ABC*+
A + B – C AB + C-
(A + B) * C AB + C*
INFIX FORM A + B
A - B
A * B
A / B
POSTFIX FORM AB+
AB-
AB*
AB/
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 19/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Rules that govern the evaluation of Arithmetic
expression
Precedence of arithmetic expression
Operation Operators Precedence
Exponential ^ Highest
Multiplication
/ Division
*, / Next
Addition /
Subtraction
+ , - Last
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 20/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Rules that govern the evaluation of
Arithmetic expression contd
• Is evaluated from left to right.
• Exponential operation is evaluated from right to
left.
• In C, We have no exclusive exponential
operator. We use ^ as an exponential operators
• Parenthesized expression is evaluated FIRST.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 21/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Examples.Eg 1: Consider
A + B * C
Steps to convert to postfix form.
• A + (B C *)
• A (B C *) +
• ABC*+
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 22/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Ex: 5+(4-3)*8
1. Convert 4-3 into postfix and store it in TT= 4 3-
2. Then the expression becomes
5 + T * 8
3. Convert T* 8 into postfix and store it in SS= T8*
4. Then the expression becomes
5 + S
5. Now convert the expression 5+S into postfix and substitute the
value for S5(T8*)+
6. Substitute the value of T in the expression and the result will beas
5((43-)8*)+7. Finally the resultant postfix
543-8*+
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 23/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Prefix Notation
• It is form of arithmetic expression, in which
we fix (place) the arithmetic operator
before (pre) its operands.
• Also called polish notation.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 24/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
ExamplesEg 1: let A & B are two operands then postfix form of expression for
arithmetic operators +, -, * & / are as
Eg 2: if A , B & C are three operands, then we can write the postfixform as
INFIX FORM PREFIX FORM
A + B * C +A*BC
A + B – C -+ABC(3 + 4) * (8 - 9) *+34-89
X / Y ^Z +A +/X ^ YZA
INFIX FORM
A + B
A - B
A * B
A / B
A^B
PREFIX FORM
+AB
-AB
*AB
/AB
^AB
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 25/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Convert infix to prefix.• Procedure
1. Apply the rules that govern the evaluation of
arithmetic expression.2. Go on placing the corresponding operator
before its two operands.
Ex : Convert the infix expression to prefix.
A * B + C / D
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 26/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Steps to convert infix to prefix
Given Infix Expression
A * B + C / D
• Convert A * B to prefix form and store it in T. i.e., T = *AB• Then, The expression becomes
T + C/D
convert C/D to prefix form and store it in S
i.e. S = /CD
3. Then, the Expression becomes
T + S
Now, convert this expression to prefix form and store it in R
i.e. R = +TS
4. So, R will contain the result, that is.R = +((*AB)(/CD)) (Parenthesis not convenience)
= +*AB/CD (After removing parenthesis)
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 27/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Conversion from Infix to Postfix• Assumptions:
– Only single character (letter or digit) operands areallowed.
– Only +, -, * ,^ and / operators are considered. Unaryminus is excluded.
– Expression is error-free.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 28/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Algorithm
Infix to postfix1. Scan the infix expression from left to right.
2. a) if the scanned symbol is left parentheses, push it onto the stack.
b) if the scanned symbol is an operand, then place
directly in the postfix expression.
c) if the scanned symbol is a right parentheses, then
go on popping all the items from the stack and placethem in postfix expression till we get the matching
left parentheses.
d) if scanned symbol is an operator, then go on removing all theoperators from the stack and place them in the postfix expression, if and only if the precedence of the operator which is on the top of thestack is greater than or equal to the precedence of the scannedoperator. Otherwise, push the scanned operator onto the stack.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 29/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Consider infix expression A+B*C
Steps of evaluation to postfix as follows
Action Symbol Stack Postfixexpn
Description
Scan A A Empty A Place it on the postfix
Scan + + + A Push + onto the stack
Scan B B + AB Place it on the postfix
Scan * * +* AB *has precedence over + sopush onto the top of the stack.
Scan C C +* ABC Straight away place C in the
postfix expression.
Allsymbols
are over
Nil Nil ABC*+ popout all operators from thestack and place them in the
postfix.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 30/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Program for infix to postfix conversion
#include<stdio.h>
#include<string.h>
int index=0, pos=0,top=-1, length;
char symbol, temp;
char infix[20],postfix[20],stack[20];
void main()
{
clrscr();
printf(“Enter the infix expression”);scanf(“%s”,infix);
Infix_to_postfix(infix,postfix);
printf(“Infix expression is =%s\n”,infix);
printf(“postfix expression= %s \n”,postfix);
getch();
}
infix to postfix (char infix[ ] char postfix[ ])
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 31/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
infix_to_postfix (char infix[ ], char postfix[ ])
{
length = strlen(infix);
push(‘#’);while (index < length)
{
symbol = infix[index];
switch (symbol)
{
case ‘(‘ : push (symbol);
break;
case ‘)’: temp=pop();
while( temp != ‘(‘){
postfix[pos]=temp;
pos++;
temp=pop();}
break;
case ‘+’:
case ’-’:
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 32/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
case ‘*’:
case’/’:
case’^’: while (preced (stack[top])>=preced(symbol))
{
temp=pop();
postfix [pos] =temp;
pos++;
}
push (symbol);
break;
default : postfix[pos++]=symbol;
break;
}
index++;
}
while top>0)
{
temp=pop();
postfix [pos++]=temp;
}
return;
}
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 33/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
push (char symb)
{
top=top+1;
stack[top]=symb;
return;
}
pop()
{
char symb;
symb=stack[top];
top=top-1;
return (symb);
}
i t d ( h b)
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 34/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
int preced (char symb)
{
int p;
switch(symb)
{
case ‘^’ : p=3;
break;
case ‘*’ :case ‘/’ : p=2;
break;
case ‘+’:
case ’-’ : p=1;
break;
case ‘)’:
case’(‘ : p=0;
break;
case ‘#’: p=-1;break;
return(p);
}
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 35/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Output for the program
Enter the infix expression:
A + B * C
Infix expression is = A + B * C
The postfix expression is ABC*+
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 36/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Convert these infix expression to
postfix form
• (A*B)+(C-D)
• ((A/B)/C)+D
• (A+B)*(C-D)/E-F
• (A-B)/C*((C-D/C+D))• A^B^C*D
• (A+B)*D+E/(F+G+D)
• A+B^C/D
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 37/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Evaluation of postfix expression
1. Store the numeric values corresponding
to each operand, in an array.
2. Scan the given postfix expression from LEFTto RIGHT
a. If the scanned symbol is an operand (variable
name), then push its value onto the stack.b. If the scanned symbol is an operator, then pop out
two values from the stack and assign them
respectively to operand2 and operand 1.
3. Then perform the required arithmetic
operation.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 38/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Algorithm contdcase operator of
* : result = operand 1 * operand 2
/ : result = operand 1 / operand 2
+ : result = operand 1 + operand 2
- : result = operand 1 + operand 2
end case
4. Push the result onto the stack5. Repeat steps 1 to 4 until all the symbols in the postfix
expression are over.
Al ith i l t ffi i
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 39/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Algorithm in c evaluate suffix expression
float eval_suffix (char suffix[],float data[])
{
int i=0;
float op1,op2,res;
char ch;
while(suffix[i] != ‘\0’)
{
ch= suffix[i];
if(isalpha (suffix[i]))push (data[i]);
else
{
op2= pop();op1= pop();
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 41/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Ex: Infix: A+B*C
Postfix: ABC*+
Values: A=2.0,B=3.0, and C=1.0
A ti S b l St k O 1 O 2 R lt D i ti
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 42/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Action Symbol Stack Oper 1 Oper 2 Result Description
Scan A A - - - Push A’svalue into
the stack
Scan B B - - - Push B’s value
into the stack
Scan C C - - - Push C’s
value into the
stack
Scan * * 3.0 1.0 3.0 Pop 2 immediate
values and assign
them to operand 2
and operand 1 and
evaluate.then push
result back
Onto the stack
Scan + + 2.0 3.0 5.0 Pop 2 immediate
values and assign
them to operand 2
and operand 1 and
perform addition
then store result
back onto the stack
2.0
2.0 3.0
2.03.0
2.0 3.0
5.0
1.0
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 43/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
• Pop out the stack content, value 5.0 is the
result.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 44/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
End of Topic
Stacks
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 45/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Queues• A queue is a non primitive linear data
structure.
• Ordered homogeneous group of elements
in which elements are added at one endcalled rear.
• Deletion is done from other end calledfront.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 46/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
• First element added to the queue is thefirst one to be removed from the queue.
• Often called First In First Out (FIFO) data
structure.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 47/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
• Capacity =6, size=6.
• Element 10 is the first element to be removed
• Element 60 is last element to be deleted.
10 20 30 40 50 60Front end Rear end
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 48/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Representation of a Queue• Static representation using arrays.
• Dynamic representation using linked list.
• Array representation of a queue needs two
indices. FRONT and REAR.
• Array declaration in C is as followsint queue [MAXSIZE];
C diti t b d
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 49/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Conditions to be assumed
FRONT=REAR, if the queue is empty.
Therefore the initial condition of a queue is
FRONT=REAR=-1
Operations on a Queue
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 50/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Operations on a Queue
Operation Description Restriction1. Create Queue It creates a new empty queue.
This operation must be carried
out in order to make the queue
logically accessible
-
2. Qinsert It inserts a new data item at the
rear of the queue
Queue must not
be full
3. Qdelete It deletes and then return the
data item at the front of the
queue.
Queue must not
be empty
4. Queue full It returns true if the queue is full.
Otherwise it returns false. -
5. Queue Empty It returns true if the queue isempty. Otherwise it returns
false.
Algorithms for Queue operations
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 51/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Algorithms for Queue operationsFront and rear are assigned value -1.
1. Qinsert
Inserts a new data item at the rear end of a queue.
Qinsert(int Q[ ],int MAXSIZE, int FRONT, int item){
if (REAR>=MAXSIZE-1) /* overflow condition*/
{
printf(“Queue is full”);
return;
}
REAR=REAR+1; /* increment rear pointer */
Q[REAR]=item; /* insert data item */
if(FRONT==-1) /* check for proper setting of front pointer*/FRONT=0;
}
Algorithms contd
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 52/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Algorithms contd
• QdeleteThis operation deletes and returns the data item at the front of
the queue.
Qdelete(int Q[ ], int FRONT,int REAR, int item)
{if(FRONT==-1)
{ printf(“Queue is empty”); /* underflow condition*/
return;
}
item=Q [FRONT];
if(FRONT=REAR)
FRONT=REAR=-1;
else
FRONT=FRONT+1;return (item);
}
• Consider a queue which can hold maximum 4 elements initially it is
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 53/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Consider a queue which can hold maximum 4 elements, initially it is
empty.
F R
Empty QueueFRONT=REAR=-1
Insert 10 to the queue. Then queue status will be
F R
10 R=R+1
Next, Insert 20 to the queue. Then queue status will be
F R
10 20 R=R+1
F=0
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 54/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Again, Insert another element 30 to the queue. The queue status is
F R
10 20 R=R+130
Let us delete an element. Remember that the element at the FRONT is
the candidate for removal. So, the status of queue would be
F R
20 F=F+130
A i d l t l t Th l t t b d l t d i l i t d
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 55/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Again, delete an element.The element to be deleted is always pointed
by FRONT pointer. So,, 20 is deleted. therefore, the status of queue
would be
F R
F=F+130
Now, Insert a new element 40 in the queue. Then queue status will be
R=R+130 40
F R
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 56/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
• Next insert another element,say,50 to the queue. You
cannot add 50 to the queue as the rear crossed the
maximum size of a queue (i.e 4) there will be Queue fullsignal. Thus the status is shown below.
R=R+130 40
F R
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 57/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
DeQue• Homogeneous list of elements in which
insertion and deletion operations areperformed from both the ends.
• Called as double ended queue.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 58/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
Types of Deque.• Types are due to the restriction put to perform either the
insertions or deletions only at one end.
• Two Types
– Input-restricted deque.
– Output-restricted deque.
Deque with 5 elements.
30 4020 5010
FRONT REAR
Deletion
Insertion
Insertion
Deletion
Dq[0] Dq[1] Dq[2] Dq[3] Dq[4]
Algorithms to perform operations on Deque
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 59/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
• Operations on DeQue1. Insertion of an element at the REAR end of
the queue.
2. Deletion of an element from the FRONT endof the queue.
3. Insertion of an element at the FRONT end of
the queue.4. Deletion of an element from the REAR end
of the queue.
Note: 2,3, & 4 are valid operations for input restricted deque.
1,2,& 3 are valid operations for output restricted deque.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 60/74
• An input-restricted deque is one where
deletion can be made from both ends, butinsertion can only be made at one end.
• An output-restricted deque is one where
insertion can be made at both ends, butdeletion can be made from one end only.
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
1 I ti f l t t th d f th
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 61/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
1. Insertion of an element at the rear end of the queue.
DQinsert_Rear(int Q[ ],int FRONT,int REAR, int item, int MAXSIZE)
{
if(REAR==(MAXSIZE -1)) /*overflow*/
{
printf(“Queue is full”);
return;
}REAR=REAR+1; /* increment rear by 1*/
Q[REAR] = item; /* insert element into Q*/
}
2. Deletion of an element from the FRONT end of the
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 62/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
queue
DQdelete_Front(int Q[ ], int FRONT, int REAR, int item)
{
if(FRONT==REAR) /* is Q Empty*/
{
printf(“Queue is empty”);
return;
}
item= Q[FRONT]; /*delete element into Q*/
FRONT=FRONT+1; /*increment FRONT*/
}
3. Insertion of an element at the Front end of the queue.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 63/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
DQInsert_Front(int Q[ ], int FRONT, int REAR,int item)
{
if(FRONT== 0)
{
printf(“Queue is full”);
return;
}
FRONT=FRONT-1; /* decrement FRONT*/
Q[FRONT]=item; /*Insert element*/
}
4. Deletion of an element from the REAR end of the queue.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 64/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
DQdelete_Rear(int Q[ ],int FRONT, int REAR,int item){
if(FRONT==REAR==-1)
{
printf(Queue is empty”);
return;
}
item=Q[item]; /*Delete element*/REAR=REAR-1; /*Decrement Rear*/
}
5. Algorithm to display the elements of Queue.
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 65/74
Preeti S. Patil Introduction to Data structureMPSTME, SHIRPUR
void DQ_display(int Q[ ],int FRONT, int REAR)
{
if(FRONT <= REAR)
{
printf(“status of the queue\n”);
for(i=FRONT;i<=REAR; i++)
{
printf(“%d”,DQ[i]);}
else
printf(“Queue is empty”);}
}
Circular queue
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 66/74
Circular queue
• There are some problems associated with
the ordinary queues – Time consuming
– Signaling queue even if the queue is actually
empty.
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 67/74
• Now if we delete all the 6 elements from the
queue, at the end of those deletions the value of
front would become 6.
• In this situation, if we attempt to insert a new
data item to the queue, we would receive queuefull message, even though queue is not full.
• This problem is overcome by implementing
Circular queue.
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
10 20 30 40 50 60Front end Rear end
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 68/74
• In circular queue, if we reach the end whileinserting elements to it, it is possible to insert
new elements if the slots at the beginning of the
circular queue are empty.• FRONT and REAR are used to keep track of the
elements to be deleted and inserted
respectively.• Each time the new element is inserted into the
queue the variable REAR is incremented by one.
• Similarly each time an item is deleted from thequeue, the variable front is incremented by one.
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Front Rear
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 69/74
• insert an element 32.so the status would be
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
5 10 25
CQ[0] CQ[1] CQ[2] CQ[3] CQ[4]
5 10 25 32
Front Rear
CQ[0] CQ[1] CQ[2] CQ[3] CQ[4]
REAR=(REAR+1)%5
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 70/74
• Again insert element 50, so the rear is incremented by 1and 50 is put in that position. Then the status of the
queue will be.
• If we insert a new element to the queue . The next
position of the FRONT where the element is going to be
inserted is computed using modulus operator.
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
5 10 25 32 50
Front Rear
CQ[0] CQ[1] CQ[2] CQ[3] CQ[4]
REAR=(REAR+1)%5
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 71/74
• Here Rear=4REAR=(REAR+1)%5
=0
Therefore, REAR will be pointing to CQ[0].
And, FRONT is also pointing to CQ[0]. Since
FRONT=REAR, the queue is full. So we cannot
add the element.
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Algorithm to insert an element into a circular queue
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 72/74
Algorithm to insert an element into a circular queue
Void Cqinsert(int Q, int FRONT, int REAR, int Item, int
MAXSIZE)
{ if (FRONT==(REAR+1)%MAXSIZE) /* queue overflow*/
printf(“queue is full”);
return;
}
if(FRONT==-1)
FRONT=REAR=0;
else
REAR=(REAR+1)%MAXSIZE; /* insert item*/
Q[REAR]=item;
}Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Algorithm to delete an item from circular queue
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 73/74
Int CQDelete(int Q[], int FRONT, int REAR, int item,){
if(FRONT==-1) /* under flow condition*/
{ printf(“queue is Empty”);
return;
}
item=Q[FRONT];
If(FRONT==REAR) /* is queue empty*/
FRONT= REAR=-1;
else
FRONT=(FRONT+1)%MAXSIZE;
}
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Priority Queues
7/30/2019 Unit 1 - Stacks & Queues
http://slidepdf.com/reader/full/unit-1-stacks-queues 74/74
Preeti S. Patil Introduction to Data structure
MPSTME, SHIRPUR
Priority Queues
• Collection of elements such that each element
has been assigned a priority
• Elements are deleted and processed based on
following – An element of higher priority is processed before any
element of lower priority.
– Two elements with the same priority are processed
according to the order in which they were added to
the queue.