#include <stdio.h>
#include <stdlib.h>
struct MyQueue
{
int max;
int front;
int rear;
int *Base;
} queue;
struct Node
{
int data;
struct Node *next;
};
struct Node *front = NULL;
struct Node *rear = NULL;
void setArray(struct MyQueue *a, int tsize)
{
(*a).max = tsize;
(*a).front = (*a).rear = -1;
(*a).Base = (int *)malloc(sizeof(int) * tsize);
}
void enqueueArray(struct MyQueue *a)
{
if ((*a).rear == (*a).max - 1)
printf("\t\t\tQueue is full, enqueue is not possible...\n");
else
{
if ((*a).front == -1)
(*a).front++;
(*a).rear++;
printf("\t\tEnter the element to be pushed: ");
scanf("%d", &(*a).Base[(*a).rear]);
printf("\t\tValue is pushed successfully...");
}
}
void dequeueArray(struct MyQueue *a)
{
if ((*a).front == -1)
printf("\t\t\tQueue is empty, dequeue is not possible...\n");
else
{
printf("\t\tPopped value: %d\n", (*a).Base[(*a).front]);
if ((*a).front == (*a).rear)
(*a).front = (*a).rear = -1;
else
(*a).front++;
}
}
void displayArray(struct MyQueue *a)
{
if ((*a).front == -1)
printf("\t\t\tQueue is empty, no element to show...\n");
else
{
printf("\t\tPrinting the elements in queue...\n\t\t");
for (int i = (*a).front; i <= (*a).rear; i++)
printf("%d\t", (*a).Base[i]);
}
}
void enqueueLinkedList()
{
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
if (node == NULL)
printf("\t\t\tEnqueue is not possible...");
else
{
printf("\t\tEnter the value to be pushed: ");
scanf("%d", &(*node).data);
(*node).next = NULL;
if (front == NULL)
front = node;
else
(*rear).next = node;
rear = node;
printf("\t\tValue is pushed successfully...");
}
}
void dequeueLinkedList()
{
int item;
if (front == NULL)
printf("\t\t\tQueue is empty, dequeue is not possible...");
else
{
printf("\t\tPopped value: %d\n", (*front).data);
if (front == rear)
front = rear = NULL;
else
front = (*front).next;
}
}
void displayLinkedList()
{
struct Node *ptr = front;
if (front == NULL)
printf("\t\t\tQueue is empty, so there is no element to show...");
else
{
printf("\t\tPrinting the elements in queue\n\t\t");
while (ptr != NULL)
{
printf("%d\t", (*ptr).data);
ptr = (*ptr).next;
}
}
}
void star()
{
int j = 0;
printf("\n\n\t\t");
while (j <= 5)
{
printf("================");
j++;
}
printf("\n\n");
}
void main()
{
int item, choice;
setArray(&queue, 10);
while (1)
{
star();
printf("\t\t\t\t\tQUEUE IMPLEMENTATION\n\n\t\t\tPress '1' to perform ENQUEUE in array\n\t\t\t
Press '2' to perform DEQUEUE in array\n\t\t\tPress '3' to SHOW the queue in array\n\n\t\t\t
Press '4' to ENQUEUE an element in LINKED LIST\n\t\t\tPress '5' to DEQUEUE an element in LINKED LIST\n\t\t\t
Press '6' to SHOW the elements in LINKED LIST\n\t\t\tPress 'any other key' to EXIT\n\t\t\tEnter your choice: ");
scanf("%d", &choice);
star();
if (choice == 1)
{
enqueueArray(&queue);
star();
}
else if (choice == 2)
{
dequeueArray(&queue);
star();
}
else if (choice == 3)
{
displayArray(&queue);
star();
}
else if (choice == 4)
{
enqueueLinkedList();
star();
}
else if (choice == 5)
{
dequeueLinkedList();
star();
}
else if (choice == 6)
{
displayLinkedList();
star();
}
else
{
printf("\t\t\tExiting...");
star();
exit(0);
}
}
}
Output :
================================================================================================
QUEUE IMPLEMENTATION
Press '1' to perform ENQUEUE in array
Press '2' to perform DEQUEUE in array
Press '3' to SHOW the queue in array
Press '4' to ENQUEUE an element in LINKED LIST
Press '5' to DEQUEUE an element in LINKED LIST
Press '6' to SHOW the elements in LINKED LIST
Press 'any other key' to EXIT
Enter your choice: 1
================================================================================================
Enter the element to be pushed: 1
Value is pushed successfully...
================================================================================================
================================================================================================
QUEUE IMPLEMENTATION
Press '1' to perform ENQUEUE in array
Press '2' to perform DEQUEUE in array
Press '3' to SHOW the queue in array
Press '4' to ENQUEUE an element in LINKED LIST
Press '5' to DEQUEUE an element in LINKED LIST
Press '6' to SHOW the elements in LINKED LIST
Press 'any other key' to EXIT
Enter your choice: 1
================================================================================================
Enter the element to be pushed: 2
Value is pushed successfully...
================================================================================================
================================================================================================
QUEUE IMPLEMENTATION
Press '1' to perform ENQUEUE in array
Press '2' to perform DEQUEUE in array
Press '3' to SHOW the queue in array
Press '4' to ENQUEUE an element in LINKED LIST
Press '5' to DEQUEUE an element in LINKED LIST
Press '6' to SHOW the elements in LINKED LIST
Press 'any other key' to EXIT
Enter your choice: 1
================================================================================================
Enter the element to be pushed: 3
Value is pushed successfully...
================================================================================================
================================================================================================
QUEUE IMPLEMENTATION
Press '1' to perform ENQUEUE in array
Press '2' to perform DEQUEUE in array
Press '3' to SHOW the queue in array
Press '4' to ENQUEUE an element in LINKED LIST
Press '5' to DEQUEUE an element in LINKED LIST
Press '6' to SHOW the elements in LINKED LIST
Press 'any other key' to EXIT
Enter your choice: 3
================================================================================================
Printing the elements in queue...
1 2 3
================================================================================================
================================================================================================
QUEUE IMPLEMENTATION
Press '1' to perform ENQUEUE in array
Press '2' to perform DEQUEUE in array
Press '3' to SHOW the queue in array
Press '4' to ENQUEUE an element in LINKED LIST
Press '5' to DEQUEUE an element in LINKED LIST
Press '6' to SHOW the elements in LINKED LIST
Press 'any other key' to EXIT
Enter your choice: 2
================================================================================================
Popped value: 1
================================================================================================
================================================================================================
QUEUE IMPLEMENTATION
Press '1' to perform ENQUEUE in array
Press '2' to perform DEQUEUE in array
Press '3' to SHOW the queue in array
Press '4' to ENQUEUE an element in LINKED LIST
Press '5' to DEQUEUE an element in LINKED LIST
Press '6' to SHOW the elements in LINKED LIST
Press 'any other key' to EXIT
Enter your choice: 4
================================================================================================
Enter the value to be pushed: 4
Value is pushed successfully...
================================================================================================
================================================================================================
QUEUE IMPLEMENTATION
Press '1' to perform ENQUEUE in array
Press '2' to perform DEQUEUE in array
Press '3' to SHOW the queue in array
Press '4' to ENQUEUE an element in LINKED LIST
Press '5' to DEQUEUE an element in LINKED LIST
Press '6' to SHOW the elements in LINKED LIST
Press 'any other key' to EXIT
Enter your choice: 4
================================================================================================
Enter the value to be pushed: 5
Value is pushed successfully...
================================================================================================
================================================================================================
QUEUE IMPLEMENTATION
Press '1' to perform ENQUEUE in array
Press '2' to perform DEQUEUE in array
Press '3' to SHOW the queue in array
Press '4' to ENQUEUE an element in LINKED LIST
Press '5' to DEQUEUE an element in LINKED LIST
Press '6' to SHOW the elements in LINKED LIST
Press 'any other key' to EXIT
Enter your choice: 4
================================================================================================
Enter the value to be pushed: 6
Value is pushed successfully...
================================================================================================
================================================================================================
QUEUE IMPLEMENTATION
Press '1' to perform ENQUEUE in array
Press '2' to perform DEQUEUE in array
Press '3' to SHOW the queue in array
Press '4' to ENQUEUE an element in LINKED LIST
Press '5' to DEQUEUE an element in LINKED LIST
Press '6' to SHOW the elements in LINKED LIST
Press 'any other key' to EXIT
Enter your choice: 6
================================================================================================
Printing the elements in queue
4 5 6
================================================================================================
================================================================================================
QUEUE IMPLEMENTATION
Press '1' to perform ENQUEUE in array
Press '2' to perform DEQUEUE in array
Press '3' to SHOW the queue in array
Press '4' to ENQUEUE an element in LINKED LIST
Press '5' to DEQUEUE an element in LINKED LIST
Press '6' to SHOW the elements in LINKED LIST
Press 'any other key' to EXIT
Enter your choice: 5
================================================================================================
Popped value: 4
================================================================================================
================================================================================================
QUEUE IMPLEMENTATION
Press '1' to perform ENQUEUE in array
Press '2' to perform DEQUEUE in array
Press '3' to SHOW the queue in array
Press '4' to ENQUEUE an element in LINKED LIST
Press '5' to DEQUEUE an element in LINKED LIST
Press '6' to SHOW the elements in LINKED LIST
Press 'any other key' to EXIT
Enter your choice: 7
================================================================================================
Exiting...
================================================================================================
Comments
Post a Comment