Question:
Exercise 4:
CopyQueue
Write a program that copies the content one queue to another
Input Data Output Data
Q1: 1 2 3 4 5 Q2: 1 2 3 4 5
Soultion:
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
#define SIZE 100
unsigned int size = 0;
int front = 0;
int rear = 0;
int queue[SIZE];
void enqueue();
void dequeue();
void displayQueue(int que[]);
void sizeOfQueue();
void copyQueue();
int main()
{
int choice;
int queue2[SIZE];
while(1)
{
printf("--------------------------------------------------------\n");
printf(" Queue \n");
printf("--------------------------------------------------------\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Items in the queue\n");
printf("4. Size of the queue\n");
printf("5. Copy Queue To Another\n");
printf("0. Exit\n");
printf("--------------------------------------------------------\n");
printf("Select your option: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
displayQueue(queue);
break;
case 4:
sizeOfQueue();
break;
case 5:
copyQueue();
break;
case 0:
printf("\nExit...!!!\n");
exit(0);
default:
printf("Invalid choice!!! Please set your choice between (1-5)!!!");
break;
}
printf("\n\n");
}
}
void enqueue()
{
int item;
if(rear == SIZE)
{
printf("\nQueue is Full!!!\n");
}
else if(front == 0)
{
printf("\nEnqueue Element: ");
scanf("%d", &item);
printf("\nItem Enqueued To The Queue!!!");
queue[rear] = item;
rear = rear + 1; //rear++
}
}
void dequeue()
{
if(front == rear)
{
printf("\nQueue Is Empty!!!\n");
}
else
{
printf("\nItem Dequeued From The Queue Is: %d\n\n", queue[front]);
front = front + 1;
}
}
void copyQueue(int queue2[])
{
for (int i=front; i<rear; i++)
{
queue2[i] = queue[i];
}
printf("\nCopying Queue is Done!!!\n");
displayQueue(queue2);
}
void displayQueue(int que[])
{
if(front == rear)
{
printf("\nQueue is Empty!!!\n");
}
else
{
printf("\nItems In The Queue... ");
for(int i=front; i<rear; i++)
{
printf("%d ", que[i]);
}
printf("\n\n");
}
}
void sizeOfQueue()
{
if (front == rear)
{
printf("\nQueue is Empty!!!\n");
}
else
{
printf("\nQueue Size: %d", rear);
}
}