Advertisement
DevilDaga

Circular Queue

Sep 2nd, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. #define MAX 4
  4.  
  5. typedef struct
  6. {
  7.     int q[ MAX ], f, r, flag;
  8. }queue_t;
  9.  
  10. int isEmpty ( queue_t *t )
  11. {
  12.     return ( !t->flag );
  13. }
  14.  
  15. void insert ( queue_t *t, int ele )
  16. {
  17.     if ( t->f == ( t->r + 1 ) % MAX && t->flag == 1 )
  18.     {
  19.         printf ( "Queue Overflow\n" );
  20.         return;
  21.     }
  22.     else
  23.     {
  24.         t->r = ( t->r + 1 ) % MAX;
  25.         t->q[ t->r ] = ele;
  26.         t->flag = 1;
  27.     }
  28. }
  29.  
  30. int del( queue_t *t )
  31. {
  32.     int z;
  33.     if ( isEmpty ( t ) )
  34.     {
  35.         printf ( "Queue Underflow\n" );
  36.         return -1;
  37.     }
  38.     else
  39.     {
  40.         z = t->q[ t->f ];
  41.         t->f = ( t->f + 1 ) % MAX;
  42.         if ( t->f == ( t->r + 1 ) % MAX )
  43.             t->flag = 0;
  44.         return z;
  45.     }
  46. }
  47.  
  48. void display ( queue_t *t )
  49. {
  50.     int i;
  51.     if ( isEmpty ( t ) )
  52.     {
  53.         printf ( "Queue Empty\n" );
  54.         return;
  55.     }
  56.     i = t->f;
  57.     while ( 1 )
  58.     {
  59.         printf ( "%d\t", t->q[ i ] );
  60.         if ( i == t->r )
  61.             break;
  62.         i = ( i + 1 ) % MAX;
  63.     }
  64. }
  65.  
  66. int queueFront ( queue_t *t )
  67. {
  68.     return t->q[ t->f ];
  69. }
  70.  
  71. void main ( )
  72. {
  73.     int choice, ele;
  74.     queue_t queue = { .r = -1, .f = 0, .flag = 0 };
  75.     while ( 1 )
  76.     {
  77.         printf ( "1:\tInsert\n2:\tDelete\n3:\tQueue Front\n4:\tDisplay\n0:\tEXIT\nCHOICE:\t" );
  78.         scanf ( "%d", &choice );
  79.         if ( !choice )
  80.             break;
  81.         switch ( choice )
  82.         {
  83.             case 1:
  84.                 printf ( "Enter element to insert:\t" );
  85.                 scanf ( "%d", &ele );
  86.                 insert ( &queue, ele );
  87.                 display ( &queue );
  88.                 break;
  89.             case 2:
  90.                 if ( isEmpty ( &queue ) )
  91.                 {
  92.                     printf ( "Queue Underflow.\n" );
  93.                     continue;
  94.                 }
  95.                 printf ( "Deleted element:\t%d\n", del ( &queue ) );
  96.                 display ( &queue );
  97.                 break;
  98.             case 3:
  99.                 if ( isEmpty ( &queue ) )
  100.                 {
  101.                     printf ( "Queue Empty.\n" );
  102.                     continue;
  103.                 }
  104.                 printf ( "Queue Front:\t%d\n", queueFront ( &queue ) );
  105.                 break;
  106.             case 4:
  107.                 display ( &queue );
  108.                 break;
  109.             default:
  110.                 printf ( "Invalid Input.\nTry Again.\n" );
  111.                 continue;
  112.         }
  113.         printf ( "\n" );
  114.     }
  115.     display ( &queue );
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement