Skip to content

Instantly share code, notes, and snippets.

@priyadarshitathagat
Created September 17, 2016 16:50
Show Gist options
  • Save priyadarshitathagat/fd7ba43518bd75a5fc41054e311948e6 to your computer and use it in GitHub Desktop.
Save priyadarshitathagat/fd7ba43518bd75a5fc41054e311948e6 to your computer and use it in GitHub Desktop.
Queue Implementation in c
#include<stdio.h>
main()
{ int a[20],front=-1,rear=-1,x,n;
printf("enter size of queue\n");
scanf("%d",&n); n=n-1;
while(1)
{ printf("Enter 1 to push,2 to pop and 3 to display:\n");
int c; scanf("%d",&c);
switch(c)
{ case 1:{rear=push(&a,rear,n); break;}
case 2:{front=pop(&a,front,n); break;}
case 3:{disp(&a,front,rear); break;}
default:printf("wrong option chosen\n");
}
printf("to continue press 0, else press 1\n");
scanf("%d",&x);
if(x==1)
break;
}
//disp(&a,front,rear);
}
int push(int *p,int y,int n)
{ if(y>=n)
{printf("overflow\n"); return(y);}
printf("enter a value\n");
int t; scanf("%d",&t);
*(p+(++y))=t;
return(y);
}
int pop(int *p,int y,int n)
{ if(y>=n)
{printf("underflow\n"); return(y);}
printf("The popped out value is: %d\n",*(p+(++y)));
return(y);
}
disp(int *p,int y,int x)
{ int i=0;
if(x==-1)
{printf("The Queue is empty.\n");return;}
printf("The Queue contains:\n");
while(y<x)
{
printf("%d\n",*(p+(++y)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment