-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue using array
More file actions
78 lines (78 loc) · 1.35 KB
/
queue using array
File metadata and controls
78 lines (78 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>
#include<stdlib.h>
void enqueue();
void dequeue();
void display();
int front,rear; //Global Variables
int SIZE,arr[20];
void main()
{
int i;
int choice;
front=rear=-1;
printf("Enter the size of the Queue\n");
scanf("%d",&SIZE);
while(1)
{
printf("\n1.Add Element to the Queue\n");
printf("2.Delete Element from the Queue\n");
printf("3.Display the Queue\n");
printf("4.Exit the Program\n");
printf("-----------------------------------\n");
scanf("%d",&choice);
switch(choice)
{
case 1:enqueue();
break;
case 2:dequeue();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("You have entered a wrong choice\n");
}
}
}
void enqueue()
{
int elem;
if(rear==SIZE-1)
printf("The Queue is already Full\n");
else
{
if(front==-1) // If first element, setting front to 0
front=0;
printf("Enter the element to be inserted\n");
scanf("%d",&elem);
rear++;
arr[rear]=elem; // Inserting Element to the Queue
}
}
void dequeue()
{
if(front==-1)
printf("The Qeueue is Empty\n");
else
{
printf("%d is removed from the queue\n",arr[front]);
if(front==rear) // Checking if its the last element(only element) in the queue
front=rear=-1;
else
front++;
}
}
void display()
{
int i;
if(front==-1)
printf("The Qeueue is Empty\n");
else
{
printf("\nElements of the Queue Are:\n");
for(i=front;i<=rear;i++)
{
printf("%d\t",arr[i]);
}
printf("\nFront= %d, Rear= %d",front,rear);
}
}