Write a program to implement the Simple Queue using Array.
/*
File: DSPrgm08.c
Author: Aditya Saini
Date: Nov 12, 2019
Description: Program to implement the Simple Queue using Array.
*/
#include <stdio.h>
#define SIZE 20
//Declare an array for Queue
int queue[SIZE];
int count = 0;
void enqueue (void);
void dequeue (void);
void display (void);
//Main function
int main (void)
{
int choice;
//Menu
while (1)
{
printf ("\t***Menu***\n");
printf ("1. Enqueue\n");
printf ("2. Dequeue\n");
printf ("3. Display\n");
printf ("4. Exit\n");
printf ("\nChoice: ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
enqueue ( );
break;
case 2:
dequeue ( );
break;
case 3:
display ( );
break;
case 4:
return 0;
default:
printf ("\nError! Wrong Choice. \n");
}
}
return 0;
};
//Function to enqueue
void enqueue (void)
{
int element;
if (count >= SIZE)
{
printf ("Error! Queue Full\n\n");
return;
}
printf ("Input element to insert: ");
scanf ("%d", &element);
queue[count++] = element;
printf ("\n");
};
//Function to dequeue
void dequeue (void)
{
int i;
if (count <= 0)
{
printf ("Error! Empty Queue\n\n");
return;
}
printf ("%d deleted\n\n", queue[0]);
for (i = 0; i <= count - 1; i++)
{
queue[i] = queue[i + 1];
}
count--;
};
//Function to display the elements in the queue
void display (void)
{
int i;
if (count <= 0)
{
printf ("Error! Empty Queue\n\n");
return;
}
printf ("Queue: ");
for (i = 0; i <= count - 1; i++)
{
printf (" %d ", queue[i]);
}
printf ("\n\n");
};
File: DSPrgm08.c
Author: Aditya Saini
Date: Nov 12, 2019
Description: Program to implement the Simple Queue using Array.
*/
#include <stdio.h>
#define SIZE 20
//Declare an array for Queue
int queue[SIZE];
int count = 0;
void enqueue (void);
void dequeue (void);
void display (void);
//Main function
int main (void)
{
int choice;
//Menu
while (1)
{
printf ("\t***Menu***\n");
printf ("1. Enqueue\n");
printf ("2. Dequeue\n");
printf ("3. Display\n");
printf ("4. Exit\n");
printf ("\nChoice: ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
enqueue ( );
break;
case 2:
dequeue ( );
break;
case 3:
display ( );
break;
case 4:
return 0;
default:
printf ("\nError! Wrong Choice. \n");
}
}
return 0;
};
//Function to enqueue
void enqueue (void)
{
int element;
if (count >= SIZE)
{
printf ("Error! Queue Full\n\n");
return;
}
printf ("Input element to insert: ");
scanf ("%d", &element);
queue[count++] = element;
printf ("\n");
};
//Function to dequeue
void dequeue (void)
{
int i;
if (count <= 0)
{
printf ("Error! Empty Queue\n\n");
return;
}
printf ("%d deleted\n\n", queue[0]);
for (i = 0; i <= count - 1; i++)
{
queue[i] = queue[i + 1];
}
count--;
};
//Function to display the elements in the queue
void display (void)
{
int i;
if (count <= 0)
{
printf ("Error! Empty Queue\n\n");
return;
}
printf ("Queue: ");
for (i = 0; i <= count - 1; i++)
{
printf (" %d ", queue[i]);
}
printf ("\n\n");
};
Output
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 9
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 2
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Queue: 9 2 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
9 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
2 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Input element to insert: 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 4
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 9
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 2
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Queue: 9 2 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
9 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
2 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Input element to insert: 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 4
No comments:
Post a Comment
Please do not post spam links.