Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Circular queues

3,773 views

Published on

Circular queues

Published in: Technology, Business

Circular queues

  1. 1. Infix, prefix and postfix notation <ul><li>A + B Infix </li></ul><ul><li>+ A B Prefix </li></ul><ul><li>A B + Postfix </li></ul><ul><li>A + (B*C)  A+(BC*)  A(BC*)+  ABC*+ </li></ul><ul><li>(A + B)*C  (AB+)*C  (AB+)C*  AB+C* </li></ul>
  2. 2. Basic operations <ul><li>Five basic operations are there </li></ul><ul><ul><li>Addition + </li></ul></ul><ul><ul><li>Subtraction - </li></ul></ul><ul><ul><li>Multiplication * </li></ul></ul><ul><ul><li>Division / </li></ul></ul><ul><ul><li>Exponentiation $ </li></ul></ul><ul><ul><li>Now following is the order of precedence </li></ul></ul><ul><ul><ul><li>Exponentiation </li></ul></ul></ul><ul><ul><ul><li>Multiplication/Division </li></ul></ul></ul><ul><ul><ul><li>Addition/Subtraction </li></ul></ul></ul>
  3. 3. Some Rules <ul><li>When unparenthesized operators of the same precedence are scanned the order is assumed to be left to right. </li></ul><ul><li>But in case of exponentiation the order is assumed to be from right to left. </li></ul><ul><li>E.g. A+B+C  (A+B)+C </li></ul><ul><li>And A$B$C  A$(B$C) </li></ul>
  4. 4. 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$*/-
  5. 5. 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
  6. 6. Infix to Prefix notation (Example) <ul><li>A + B  + A B </li></ul><ul><li>2. A + B - C </li></ul><ul><li> + A B - C </li></ul><ul><li> - + A B C </li></ul><ul><li>(A + B) * (C – D) </li></ul><ul><li> (+ A B) * (- C D) </li></ul><ul><li> *(+ A B)(-C D) </li></ul><ul><li> *+A B – C D </li></ul>4. A-B/(C*D$E) A-B/(C*$DE) A-B/(*C$DE) A-/B(*C$DE) -A/B(*C$DE)
  7. 7. Example (Cont…) <ul><li>A$B*C–D+E/F/(G+H) </li></ul><ul><li>A$B*C–D+E/F/(+G H) </li></ul><ul><li>$AB*C–D+E/F/(+GH) </li></ul><ul><li>*$ABC–D+/EF/(+GH) </li></ul><ul><li>-*$ABCD+//EF(+GH) </li></ul><ul><li>+-$ABCD+//EF+GH </li></ul>
  8. 8. Example (Cont…) <ul><li>((A+B)*C-(D-E))$(F+G) </li></ul><ul><li>((+AB)*C-(-DE))$(+FG) </li></ul><ul><li>$(*(+AB)C-(-DE))(+FG) </li></ul><ul><li>$(-*(+AB)C(-DE))(+FG) </li></ul><ul><li>$-*+ABC-DE+FG </li></ul>
  9. 9. Evaluating a postfix Expression (Algo) <ul><li>Opndstk = the empty stack </li></ul><ul><li>/* scan the input string reading one */ </li></ul><ul><li>While(not end of input) </li></ul><ul><li>{ </li></ul><ul><li>symb = next input character; </li></ul><ul><li>if(symb is an operand) </li></ul><ul><li>push(opndstk, symb); </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>opnd2 = pop(opndstk); </li></ul><ul><li>opnd1 = pop(opndstk); </li></ul><ul><li>value = result of applying symb to opnd1 and opnd2; </li></ul><ul><li>push(opndstk, value); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  10. 10. 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
  11. 11. Circular Queues <ul><li>It is a ring showing, conceptually, a circular buffer. </li></ul><ul><li>This visually shows that the buffer has no real end and it can loop around the buffer. </li></ul><ul><li>However, since memory is never physically created as a ring, a linear representation is generally used as is done below. </li></ul><ul><li>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. </li></ul><ul><li>This structure lends itself easily to buffering data streams . </li></ul>
  12. 12. Circular Queue (Insertion) <ul><li>void insert(int val) </li></ul><ul><li> { </li></ul><ul><li> if((front==0 && rear==MAX-1) || (rear+1==front)) </li></ul><ul><li> printf(&quot; Circular Queue is Full&quot;); </li></ul><ul><li> else </li></ul><ul><li> { </li></ul><ul><li> if(rear==MAX-1) </li></ul><ul><li> rear=0; </li></ul><ul><li> else </li></ul><ul><li> rear++; </li></ul><ul><li> a[rear]=val; </li></ul><ul><li> } </li></ul><ul><li> if(front==-1) </li></ul><ul><li> front=0; </li></ul><ul><li> } </li></ul>
  13. 13. 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)
  14. 14. Circular Queue (Deletion) <ul><li>int deletion() </li></ul><ul><li> { </li></ul><ul><li> int k; </li></ul><ul><li> if(front==-1) </li></ul><ul><li>printf(&quot;Circular Queue is Empty&quot;); </li></ul><ul><li> else </li></ul><ul><li> { </li></ul><ul><li>k=a[front]; </li></ul><ul><li>if(front==rear) </li></ul><ul><li> front=rear=-1; </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li> if(front==MAX-1) </li></ul><ul><li> front=0; </li></ul><ul><li> else </li></ul><ul><li> front++; </li></ul><ul><li>} </li></ul><ul><li> } </li></ul><ul><li> return k; </li></ul><ul><li> } </li></ul>
  15. 15. 11 44 33 22 11 11 55 44 33 22 0 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
  16. 16. <ul><li>void display() </li></ul><ul><li> { </li></ul><ul><li> int i; </li></ul><ul><li> if(front==-1) </li></ul><ul><li> printf(&quot;Circular Queue is Empty&quot;); </li></ul><ul><li> else </li></ul><ul><li> { </li></ul><ul><li> if(rear < front) </li></ul><ul><li> { </li></ul><ul><li>for(i=front;i<=MAX-1;i++) </li></ul><ul><li> printf(&quot;%d &quot;,a[i]); </li></ul><ul><li>for(i=0;i<=rear;i++) </li></ul><ul><li> printf(&quot;%d &quot;,a[i]); </li></ul><ul><li> } </li></ul><ul><li> else </li></ul><ul><li> { </li></ul><ul><li>for(i=front;i<=rear;i++) </li></ul><ul><li> printf(&quot;%d n &quot;,a[i]); </li></ul><ul><li> } </li></ul><ul><li> } </li></ul><ul><li> } </li></ul>
  17. 17. Converting an Expression from Infix to Postfix <ul><li>Opstk = the empty stack; </li></ul><ul><li>While(not end of input) </li></ul><ul><li>{ </li></ul><ul><li>symb = next input character; </li></ul><ul><li>if(symb is an operand) </li></ul><ul><li>add symb to the postfix string </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>while(!empty(opstk) && prcd(stacktop(opstk), symb)) </li></ul><ul><li>{ </li></ul><ul><li> topsymb = pop(opstk); </li></ul><ul><li> add topsymb to the postfix string; </li></ul><ul><li>} </li></ul><ul><li>push(opstk, symb); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>While(!empty(opstk) </li></ul><ul><li>{ </li></ul><ul><li>topsymb = pop(opstk); </li></ul><ul><li>add topsymb to the postfix string; </li></ul><ul><li>} </li></ul>
  18. 18. Example – A + B * C Symb 1 A A 2 + A + 3 B AB + 4 * AB + * 5 C ABC + * 6 ABC* + 7 ABC*+
  19. 19. Example 2 (A+B) * C symb Postfix string opstk ( ( A A ( + A ( + B AB ( + ) AB+ * AB+ * C AB + C * AB + C*
  20. 20. 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 + $

×