Created
April 3, 2019 19:17
-
-
Save jatinsharrma/aa41fe01f683e7ac6214f63ca6518c0e to your computer and use it in GitHub Desktop.
Queue implementation using array
This file contains hidden or 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
#include<stdio.h> | |
#include<stdlib.h> | |
# define CAPACITY 5 | |
int queue[CAPACITY], top = -1, base = -1; | |
//prototyping | |
void enQueue(int); | |
int isEmpty(void); | |
int isFull(void); | |
void deQueue(void); | |
void traverse(void); | |
void peek(void); | |
//------------------------------------------------------------------------------ | |
int main(){ | |
while(1){ | |
printf("\nSelect your choice\n"); | |
printf(" 1. enQueue \n 2. deQueue \n 3. Traverse \n 4. Peek \n 5. Exit\n"); | |
int option; | |
printf("Enter your choice : "); | |
scanf("%d",&option); | |
switch(option){ | |
case 1 : printf("\nEnter the number you want to enQueue : "); | |
int num; | |
scanf("%d",&num); | |
enQueue(num); | |
break; | |
case 2 : deQueue(); | |
break; | |
case 3 : traverse(); | |
break; | |
case 4 : peek(); | |
break; | |
case 5 : exit(0); | |
break; | |
default : printf("\nInvalid Input\n"); | |
} | |
} | |
} | |
//------------------------------------------------------------------------------ | |
void enQueue (int num){ | |
if (isEmpty()){ | |
base = 0; | |
top = 0; | |
queue[top] = num; | |
} | |
else{ | |
if(isFull()){ | |
printf("\nNo space to enQueue new element\n"); | |
} | |
else{ | |
top++; | |
queue[top] = num; | |
} | |
} | |
} | |
//------------------------------------------------------------------------------ | |
int isEmpty(){ | |
if (base == -1 && top == -1){ | |
return 1; | |
} | |
else { | |
return 0; | |
} | |
} | |
//------------------------------------------------------------------------------ | |
int isFull(){ | |
if (top == CAPACITY-1){ | |
return 1; | |
} | |
else{ | |
return 0; | |
} | |
} | |
//------------------------------------------------------------------------------ | |
void deQueue(){ | |
if (top == -1){ | |
printf("\nNo element to dequeue\n"); | |
} | |
else if (top == 0){ | |
printf("\ndeQueued element : %d\n", queue[top]); | |
base = -1 ; | |
top = -1 ; | |
} | |
else{ | |
printf("\ndeQueued element : %d\n", queue[base]); | |
for (int i=1 ; i<=top ; i++){ | |
queue[i-1] = queue[i]; | |
} | |
top--; | |
} | |
} | |
//------------------------------------------------------------------------------ | |
void traverse(){ | |
if(isEmpty()){ | |
printf("\nQueue is empty.\n"); | |
} | |
else{ | |
printf("\nElements in queue : "); | |
for (int i =0 ; i <= top ; i++){ | |
printf("%d ", queue[i]); | |
} | |
printf("\n\n"); | |
} | |
} | |
//------------------------------------------------------------------------------ | |
void peek(){ | |
if (isEmpty()){ | |
printf("\nQueue is empty.\n"); | |
} | |
else{ | |
printf("\nPeek of queue : %d\n",queue[top]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment