-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueueArray.c
140 lines (135 loc) · 2.36 KB
/
PriorityQueueArray.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//Array Queue
# include <stdio.h>
# include <stdlib.h>
# define max 5
int front = 0, rare = 0,data,stack[max], i,j,temp,delete;
int loc = 0,flag=0;
void Enqueue(void);
void Dequeue(void);
void Display(void);
int Full(void);
int Empty(void);
void initialize(void);
void sort(void);
int search(void);
int main()
{
int ch;
while(1)
{
scanf("%d",&ch);
switch (ch) {
case 1:
Enqueue();
break;
case 2:Dequeue();
break;
case 3:Display();
break;
case 4:initialize();
break;
case 5:exit(1);
break;
default:printf("Invalid option\n");
break;
}
}
return 0;
}
int Full()
{
if(rare == max)
return 1;
else
return 0;
}
int Empty()
{
if(rare == front)
return 1;
else
return 0;
}
void Enqueue()
{
if(Full())
printf("Queue is Full\n");
else
{
printf("Enter Queue data\n");
scanf("%d",&data);
stack[rare] = data;
rare++;
sort();
}
}
void Dequeue()
{
if(Empty())
printf("queue is Empty\n");
else
{
if(search() == 1)
{
printf("Data %d is Deleted\n",delete);
for (int i=loc;i<rare;i++)
{
stack[i] = stack[i+1];
}
rare--;
}
else
{
printf("Entered element %d not found in the list\n",delete);
}
}
}
void Display()
{
if(Empty())
printf("Queue is Empty\n");
else
{
for (int i = 0 ; i<rare ; i++)
{
printf("%d ",stack[i]);
}
}
}
void initialize()
{
front = rare = 0;
}
void sort()
{
for (i = 0 ; i< rare-1 ; i++)
{
for(j=0;j<rare-i-1;j++)
{
if(stack[j] > stack[j+1])
{
temp = stack[j];
stack[j] = stack[j+1];
stack[j+1] = temp;
}
}
}
}
int search()
{
printf("Enter the element to delete\n");
scanf("%d",&delete);
for (i =0 ; i< rare ; i++)
{
if(stack[i] == delete)
{
flag = 1;
break;
}
loc++;
}
if(flag==1)
return 1;
else
return 0;
}