Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #define MAX 4
- typedef struct
- {
- int q[ MAX ], f, r, flag;
- }queue_t;
- int isEmpty ( queue_t *t )
- {
- return ( !t->flag );
- }
- void insert ( queue_t *t, int ele )
- {
- if ( t->f == ( t->r + 1 ) % MAX && t->flag == 1 )
- {
- printf ( "Queue Overflow\n" );
- return;
- }
- else
- {
- t->r = ( t->r + 1 ) % MAX;
- t->q[ t->r ] = ele;
- t->flag = 1;
- }
- }
- int del( queue_t *t )
- {
- int z;
- if ( isEmpty ( t ) )
- {
- printf ( "Queue Underflow\n" );
- return -1;
- }
- else
- {
- z = t->q[ t->f ];
- t->f = ( t->f + 1 ) % MAX;
- if ( t->f == ( t->r + 1 ) % MAX )
- t->flag = 0;
- return z;
- }
- }
- void display ( queue_t *t )
- {
- int i;
- if ( isEmpty ( t ) )
- {
- printf ( "Queue Empty\n" );
- return;
- }
- i = t->f;
- while ( 1 )
- {
- printf ( "%d\t", t->q[ i ] );
- if ( i == t->r )
- break;
- i = ( i + 1 ) % MAX;
- }
- }
- int queueFront ( queue_t *t )
- {
- return t->q[ t->f ];
- }
- void main ( )
- {
- int choice, ele;
- queue_t queue = { .r = -1, .f = 0, .flag = 0 };
- while ( 1 )
- {
- printf ( "1:\tInsert\n2:\tDelete\n3:\tQueue Front\n4:\tDisplay\n0:\tEXIT\nCHOICE:\t" );
- scanf ( "%d", &choice );
- if ( !choice )
- break;
- switch ( choice )
- {
- case 1:
- printf ( "Enter element to insert:\t" );
- scanf ( "%d", &ele );
- insert ( &queue, ele );
- display ( &queue );
- break;
- case 2:
- if ( isEmpty ( &queue ) )
- {
- printf ( "Queue Underflow.\n" );
- continue;
- }
- printf ( "Deleted element:\t%d\n", del ( &queue ) );
- display ( &queue );
- break;
- case 3:
- if ( isEmpty ( &queue ) )
- {
- printf ( "Queue Empty.\n" );
- continue;
- }
- printf ( "Queue Front:\t%d\n", queueFront ( &queue ) );
- break;
- case 4:
- display ( &queue );
- break;
- default:
- printf ( "Invalid Input.\nTry Again.\n" );
- continue;
- }
- printf ( "\n" );
- }
- display ( &queue );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement