-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriorityq.c
235 lines (210 loc) · 7.33 KB
/
priorityq.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "priorityq.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
list* newlist(int d, int p)
{
list* temp = (list*)malloc(sizeof(list));
temp->data = d;
temp->priority = p;
temp->next = NULL;
return temp;
}
int peek(list** head)
{
return (*head)->data;
}
int pop(list** head)
{
int retValue = (*head)->data;
list* temp = *head;
(*head) = (*head)->next;
free(temp);
return retValue;
}
void push(list** head, int d, int p)
{
list* start = (*head);
list* temp = newlist(d, p);
if ((*head)->priority > p)
{
temp->next = *head;
(*head) = temp;
}
else
{
while (start->next != NULL && start->next->priority <= p)
{
start = start->next;
}
temp->next = start->next;
start->next = temp;
}
}
void destroy(list **head)
{
list *temp;
while(*head!=NULL);
{
temp=*head;
*head=(*head)->next;
free(temp);
}
}
void deleteList(list **head)
{
list *current = *head;
list *next;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
*head = NULL;
}
int isEmptys(list** head)
{
return (*head) == NULL;
}
//int main()
//{
// int inData, inPriority, length, input,checker=-1;
/*
printf("Prioritetine eile, tai algoritmas leidziantis pagal prioriteta surikiuoti narius");
while(input!=8)
{
printf("\nIveskite skaiciu nuo 1 iki 6:\n1) Sukuria sarasa\n2) Push pagal prioritetine eile\n3) Pop pirmaji nari eileje\n4) Peek pagal prioritetine eile(parodo i pirmaji nari)\n5) Patikrina ar sarasas yra tuscias\n6) Patikrina ar sarasas pilnas\n7) Panaikina sarasa\n8) Isjungti programa\n\n");
scanf("%d",&input);
switch (input)
{
case 1:
if(checker==-1)
{
list* priorityQ = NULL;
printf("Iveskite maximalu saraso ilgi:");
scanf("%d",&length);
checker=0;
break;
}
else
{
printf("Neimanoma sukurti saraso, nes sarasas jau yra sukurtas\n");
break;
}
case 2:
if(checker==0)
{
printf("Iveskite reiksme:\n");
scanf("%d",&inData);
printf("Iveskite prioriteta:\n");
scanf("%d",&inPriority);
priorityQ = newlist(inData,inPriority,&checker);
break;
}
if((checker>0)&&(checker<length))
{
printf("Iveskite reiksme:\n");
scanf("%d",&inData);
printf("Iveskite prioriteta:\n");
scanf("%d",&inPriority);
push(&priorityQ,inData,inPriority,&checker);
break;
}
if (checker<0)
{
printf("Neimanoma ipushinti reiksmes, nes sarasas nera sukurtas\n");
break;
}
if (checker==0)
{
printf("Neimanoma ipushinti reiksmes, nes sarasas tuscias\n");
break;
}
if (checker>=length)
{
printf("Neimanoma ipushinti reiksmes, nes sarasas jau pilnas\n");
break;
}
case 3:
if(checker>0)
{
pop(&priorityQ);
checker--;
break;
}
if(checker==-1)
{
printf("Neimanoma ispopinti reiksmes, nes sarasas nera sukurtas\n");
break;
}
if(checker==0)
{
printf("Neimanoma ispopinti reiksmes, nes sarasas tuscias\n");
break;
}
case 4:
if(checker>0)
{
printf("Pirmojo elemento reiksme: %d\n",peek(&priorityQ));
break;
}
if(checker==-1)
{
printf("Neimanoma parodyti pirmojo elemento reiksmes, nes sarasas nera sukurtas\n");
break;
}
if(checker==0)
{
printf("Neimanoma parodyti pirmojo elemento reiksmes, nes sarasas tuscias\n");
break;
}
case 5:
if(checker>0)
{
printf("Sarasas netuscias");
break;
}
if(checker==-1)
{
printf("Sarasas nera sukurtas\n");
break;
}
if(checker==0)
{
printf("Sarasas tuscias\n");
break;
}
case 6:
if(length==checker)
{
printf("Sarasas pilnas");
break;
}
if(checker==-1)
{
printf("Sarasas nera sukurtas\n");
break;
}
else
{
printf("Sarasas netuscias\n");
break;
}
case 7:
if(checker>-1)
{
deleteList(&priorityQ);
checker = -1;
break;
}
if(checker==-1)
{
printf("Sarasas nera sukurtas\n");
break;
}
}
}
return 0;
}
*/