Upload
ssankett-negi
View
1.751
Download
2
Embed Size (px)
DESCRIPTION
Circular queues
Citation preview
Infix, prefix and postfix notation
• A + B Infix
• + A B Prefix
• A B + Postfix
A + (B*C) A+(BC*) A(BC*)+ ABC*+
(A + B)*C (AB+)*C (AB+)C* AB+C*
Basic operations
• Five basic operations are there– Addition +– Subtraction -– Multiplication *– Division /– Exponentiation $– Now following is the order of precedence
• Exponentiation• Multiplication/Division• Addition/Subtraction
Some Rules
• When unparenthesized operators of the same precedence are scanned the order is assumed to be left to right.
• But in case of exponentiation the order is assumed to be from right to left.
• E.g. A+B+C (A+B)+C
• And A$B$C A$(B$C)
Infix Postfix
A + B AB+
A+B-C AB+C-
(A+B)*(C-D) AB+CD-*
A$B*C-D+E/F/(G+H) AB$C*D-EF/GH+/+
((A+B)*C-(D-E))$(F+G) AB+C*DE—FG+$
A-B/(C*D$E) ABCDE$*/-
Infix Prefix
A + B +AB
A+B+C -+ABC
(A+B)*(C-D) *+AB-CD
A$B*C-D+E/F/(G+H) +-*$ABCD//EF+GH
((A+B)*C-(D-E))$(F+G) $-*+ABC-DE+FG
A-B/(C*D$E) -A/B*C$DE
Infix to Prefix notation (Example)
1. A + B + A B
2. A + B - C
+ A B - C
- + A B C
3. (A + B) * (C – D)
(+ A B) * (- C D)
*(+ A B)(-C D)
*+A B – C D
4. A-B/(C*D$E)
A-B/(C*$DE)
A-B/(*C$DE)
A-/B(*C$DE)
-A/B(*C$DE)
Example (Cont…)A$B*C–D+E/F/(G+H)
A$B*C–D+E/F/(+G H)
$AB*C–D+E/F/(+GH)
*$ABC–D+/EF/(+GH)
-*$ABCD+//EF(+GH)
+-$ABCD+//EF+GH
Example (Cont…)
((A+B)*C-(D-E))$(F+G)
((+AB)*C-(-DE))$(+FG)
$(*(+AB)C-(-DE))(+FG)
$(-*(+AB)C(-DE))(+FG)
$-*+ABC-DE+FG
Evaluating a postfix Expression (Algo)
Opndstk = the empty stack/* scan the input string reading one */
While(not end of input){
symb = next input character;if(symb is an operand)
push(opndstk, symb);else{
opnd2 = pop(opndstk);opnd1 = pop(opndstk);value = result of applying symb to opnd1 and opnd2;push(opndstk, value);
}}
Evaluate :- 6 2 3 + - 3 8 2 / + * 2 $ 3 +Symb opnd1 opnd2 value Opndstk
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
$ 7 2 49 49
3 7 2 49 49,3
+ 49 3 52 52
Circular Queues
1. It is a ring showing, conceptually, a circular buffer.2. This visually shows that the buffer has no real end and it can
loop around the buffer.3. However, since memory is never physically created as a ring, a
linear representation is generally used as is done below.4. A circular buffer or ring buffer is a data structure that uses a
single, fixed-size buffer as if it were connected end-to-end. 5. This structure lends itself easily to buffering data streams.
Circular Queue (Insertion)void insert(int val)
{ if((front==0 && rear==MAX-1) || (rear+1==front))
printf(" Circular Queue is Full"); else { if(rear==MAX-1)
rear=0; else
rear++; a[rear]=val; } if(front==-1) front=0;
}
55
44
33
22
11
22
11
55
44
11
55
44
33
22
4
3
2
1
0
1
0
4
3
0
4
3
2
1
Rear
Front
Front = 0
Rear = max – 1
Queue is full
Rear
Front
Rear
Front
Front != 0 &
Rear = max-1
Rear = 0
A[rear] = val
33 2
Front !=0 &
Rear != max-1
Rear ++
A[rear] = val
Inserting an element into queue (all conditions)
Since if your queue is empty means (front=-1) then make front =0 & rear = 0 (by rear++)
It will become 0 means(-1+1=0)
Circular Queue (Deletion)int deletion()
{ int k; if(front==-1)
printf("Circular Queue is Empty"); else {
k=a[front];if(front==rear) front=rear=-1;else{ if(front==MAX-1)
front=0; else
front++;}
} return k;
}
11
44
33
22
11
11
55
44
33
220
3
2
1
0
0
4
3
2
1
Rear
Front
Front = Rear
Front = Rear = -1
Rear
Front
Rear
Front
If Front != max-1 &
Front !=-1
Front ++
55 4
Deleting an element into queue (all conditions)
If (front=-1) then
Queue is empty
If Front == max -1
Then
Front = 0
void display() {
int i; if(front==-1)
printf("Circular Queue is Empty"); else {
if(rear < front) {
for(i=front;i<=MAX-1;i++) printf("%d ",a[i]);for(i=0;i<=rear;i++) printf("%d ",a[i]);
} else {
for(i=front;i<=rear;i++) printf("%d \n ",a[i]);
} }
}
Converting an Expression from Infix to Postfix
Opstk = the empty stack;While(not end of input){
symb = next input character;if(symb is an operand)
add symb to the postfix stringelse{
while(!empty(opstk) && prcd(stacktop(opstk), symb)) { topsymb = pop(opstk); add topsymb to the postfix string;}push(opstk, symb);
}}While(!empty(opstk){ topsymb = pop(opstk);
add topsymb to the postfix string;}
Example – A + B * C
Symb
1 A A
2 + A +
3 B AB +
4 * AB + *
5 C ABC + *
6 ABC* +
7 ABC*+
Example 2 (A+B) * C
symb Postfix string opstk
( (
A A (
+ A ( +
B AB ( +
) AB+
* AB+ *
C AB + C *
AB + C*
Example 3 ((A – (B + C)) * D) $ (E + F)( A (
( A ((
A A ((
- AB ((-
( AB ((-(
B ABC ((-(
+ ABC + ((-( +
C ABC + - ((-( +
) ABC + - ((-
) ABC + - D (
* ABC + - D (
D ABC + - D (*
) ABC + - D * (*
$ ABC + - D *
( ABC + - D * $
E ABC + - D * E $(
+ ABC + - D * E $(
F ABC + - D * EF $( +
) ABC + - D * EF + $
ABC + - D * EF + $