Skip to content

Instantly share code, notes, and snippets.

@devils-ey3
Created September 21, 2016 11:10
Show Gist options
  • Save devils-ey3/8522d5f1ad7b89c567345ec66bc6866d to your computer and use it in GitHub Desktop.
Save devils-ey3/8522d5f1ad7b89c567345ec66bc6866d to your computer and use it in GitHub Desktop.
Easiest way to make a queue code by C. It has a bug but working properly
#include <stdio.h>
#define max 5
int rear=-1,front=-1,array[max];
void printInstruction()
{
printf("Press 1 for push\nPress 2 for pop\nPress 3 for display full list\nPress 4 for display top and last\nPress ctrl+z for exit\n\n");
}
void push(int j)
{
int check = ((max-front+rear)%max)+1;
if (check == max)
{
printf("queue overflow\n\n");
}
else
{
if (front==-1)
front=0;
array[++rear]=j;
}
}
void pop()
{
if (front == -1 || front > rear)
{
printf("Nothing to pop\n\n");
return ;
}
else
{
printf("%d is poped \n\n",array[front++]);
}
}
void show_list()
{
int i;
if (front == - 1)
printf("Queue is empty \n\n");
else
{
printf("Queue is : ");
for (i = front; i <= rear; i++)
printf("%d ", array[i]);
printf("\n\n");
}
}
void top_last()
{
printf("Top value %d\n",array[front]);
printf("Last value %d\n",array[rear]);
}
int main()
{
int choise,delement;
printInstruction();
while(scanf("%d",&choise)!=EOF)
{
if (choise==1)
{
int element;
printf("Enter the number for push : ");
scanf("%d",&element);
push(element);
}
else if (choise==2)
{
pop();
}
else if (choise==3)
{
show_list();
}
else if (choise==4)
{
top_last();
}
else
{
system("clear");
printf("Wrong entry\n\n");
}
printInstruction();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment