Created
January 27, 2016 15:09
-
-
Save 78526Nasir/af2efca6917f4de2f3e1 to your computer and use it in GitHub Desktop.
Queue Implementation In C Using Array !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Queue Implementation In C Using Array ! | |
#include<stdio.h> | |
#include<stdlib.h> // hearder for for using exit and return function | |
#define max 5 // symbolic constant | |
int rear=-1,front=-1; // global variable | |
int queue[max]; | |
void insert(); | |
int delete(); | |
void display(); | |
main() | |
{ | |
int w,num; | |
while(1) | |
{ | |
printf("\n1. Insert"); | |
printf("\n2. Delete"); | |
printf("\n3. Display"); | |
printf("\n4. EXIT"); | |
printf("\nEnter What you want :"); | |
scanf("%d",&w); | |
if(w==1) | |
insert(); | |
else if(w==2) | |
num=delete(); | |
else if(w==3) | |
display(); | |
else if(w==4) | |
exit(1); | |
else printf("\nInvalid Choice!!"); | |
} | |
} | |
void insert() | |
{ | |
int num; | |
if(rear==max-1){ | |
printf("\nQueue is Full !\n"); | |
return; | |
} | |
printf("\nEnter a number for insert :"); | |
scanf("%d",&num); | |
if(front==-1) | |
front=front+1; | |
rear=rear+1; | |
queue[rear]=num; | |
} | |
int delete() | |
{ | |
int num; | |
if(front==-1 || front==rear+1) | |
{ | |
printf("\nQueue is Empty !\n"); | |
return 0; | |
} | |
num=queue[front]; | |
printf("\n%d was deleted !\n",num); | |
front=front+1; | |
} | |
void display() | |
{ | |
int i; | |
if(front==-1 || front==rear+1) | |
{ | |
printf("\nQueue Is Empty ! Nothing To Display !!"); | |
return; | |
} | |
printf("\n\n"); | |
for(i=front;i<=rear;i++) | |
printf("%d\t",queue[i]); | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
easy and clear solution