Skip to content

Instantly share code, notes, and snippets.

@AakashCode12
Last active August 26, 2020 16:53
Show Gist options
  • Save AakashCode12/e6b47b5719d4f6bd20bd777508c49614 to your computer and use it in GitHub Desktop.
Save AakashCode12/e6b47b5719d4f6bd20bd777508c49614 to your computer and use it in GitHub Desktop.
//** open in Vs Code with better comments extension for better view
/*
* Queue using Array program Practical-4
*/
// todo standard lib / definitions
#include <stdio.h>
#define N 3
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)
{
if (front == 0)
{
printf("\nthe Queue is filled");
}
else
{
rear = 0;
printf("\nEnter the value you want to enter: ");
scanf("%d", &queue[rear]);
}
}
/*
! 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
{
if (rear == (front - 1))
{
printf("\nthe Queue is filled");
}
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 (front == N - 1)
{
//!iterate to front =0
front = 0;
}
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]);
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 members of queue
printf("\nThe Elements in queue are(front to rear):\n");
if (rear > front)
{
for (int i = front; i <= rear; i++)
{
printf("%d\n", queue[i]);
}
}
else
{
//jab last se aage aajai ham log
for (int i = front; i < N; i++)
{
printf("%d\n", queue[i]);
}
for (int i = 0; i < rear; i++)
{
printf("%d\n", 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