Last active
August 26, 2020 13:47
-
-
Save AakashCode12/7070f6363a61adbba99297e594c6b19e to your computer and use it in GitHub Desktop.
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
//** open in Vs Code with better comments extension for better view | |
/* | |
* Queue using Array program Practical-5 | |
*/ | |
// todo standard lib / definitions | |
#include<stdio.h> | |
#define N 5 | |
int queue[N]; | |
int front =-1; | |
int rear=-1; | |
// todo The prototypes of functions | |
void enqueue(); | |
void dequeue(); | |
void peek(); | |
void display(); | |
// todo enqueue Function | |
void enqueue(){ | |
//! we are adding elements in queue, | |
//! so we need to check is the array full first | |
if(rear==N-1){ | |
printf("\n Queue Overflow"); | |
} | |
/* | |
! now we have 2 conditions for filling, | |
! one for filling first time and one for rest of the time. | |
! because for first time, we even need to even move the front counter | |
*/ | |
else if(rear==-1 && front==-1 ){ | |
front++; | |
rear++; | |
printf("\nEnter the value you want to enter: "); | |
scanf("%d",&queue[rear]); | |
} | |
else{ | |
rear++; | |
printf("\nEnter the value you want to enter: "); | |
scanf("%d", &queue[rear]); | |
} | |
} | |
// todo dequeue Function | |
void dequeue(){ | |
//! we are removing elements in queue, | |
//! so we need to check is the array empty first | |
if (rear == - 1 && front == -1) | |
{ | |
printf("\n Queue Underflow"); | |
} | |
/* | |
! now we have 2 conditions for removing, | |
! one for removing when f,r are same and one for rest of the time. | |
*/ | |
else if (rear == front) | |
{ | |
printf("\nThe Value you removed is: %d",queue[front]); | |
front = -1; | |
rear = -1; | |
} | |
else | |
{ | |
printf("\nThe Value you removed is: %d", queue[front]); | |
queue[front]=0; | |
front++; | |
} | |
} | |
// todo peek Function | |
void peek(){ | |
if(front==-1){ | |
printf("\nThe queue is empty"); | |
} | |
else{ | |
printf("\nThe element at the front of queue is: %d",queue[front]); | |
} | |
} | |
// todo display Function | |
void display(){ | |
//? prints all the elements of the array | |
for(int i=0;i<N;i++){ | |
printf("\n%d",queue[i]); | |
} | |
} | |
// todo main Functions | |
void main(){ | |
int option; | |
//? loop for the main menu | |
do | |
{ | |
printf("\n **-------Main Menu-------**"); | |
printf("\n 1. Enqueue"); | |
printf("\n 2. Dequeue"); | |
printf("\n 3. Peek"); | |
printf("\n 4. Display"); | |
printf("\n 5. Exit"); | |
printf("\n Enter your option: "); | |
scanf("%d", &option); | |
//? switch case for options | |
switch (option) | |
{ | |
case 1: enqueue(); | |
break; | |
case 2: dequeue(); | |
break; | |
case 3: peek(); | |
break; | |
case 4: display(); | |
break; | |
} | |
} while (option != 5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment