-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority-queue-ssl.c
More file actions
137 lines (131 loc) · 2.5 KB
/
priority-queue-ssl.c
File metadata and controls
137 lines (131 loc) · 2.5 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
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
#include <stdio.h>
#include <stdlib.h>
typedef struct simpul Node;
struct simpul
{
int data, prior;
Node *next;
};
void insert_first();
void insert_last();
void insert_before();
void allocation();
void enqueue();
void dequeue();
void free_node(Node *);
void tampil();
Node *head = NULL , *p, *search, *pbef;
int main()
{
printf("Queue With Priority\n");
printf("1. Push a Value (Enqueue)\n");
printf("2. Pull a Value (Dequeue)\n");
printf("3. Read Whole Queue (LIFO)\n");
int menu;
do
{
printf("\nInsert menu : ");
scanf(" %d", &menu);
fflush(stdin);
switch(menu)
{
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: tampil(); break;
case 4: printf("Bye!!\n"); exit(0); break;
default: printf("Wrong Menu!\n");
}
} while(menu != 4);
return 0;
}
void enqueue()
{
allocation();
search = head;
if(head == NULL)
head = p;
else
{
while(search->prior <= p->prior)
{
if(search->next == NULL)
break;
else{
pbef = search;
search = search->next;
}
}
if(search->next==NULL && search->prior <= p->prior)
insert_akhir();
else
insert_before();
}
}
void allocation()
{
p = (Node *)malloc(sizeof(Node));
if(p == NULL)
exit(0);
printf("Insert value : ");
scanf("%d", &p->data);
fflush(stdin);
printf("Insert priority: ");
scanf("%d", &p->prior);
fflush(stdin);
p->next = NULL;
}
void tampil()
{
if(head==NULL)
printf("Queue is empty!\n");
else
{
Node *baca;
baca = head;
puts("\nData\tPriority");
while(baca != NULL)
{
printf("%d\t%d\n", baca->data, baca->prior);
baca = baca->next;
}
}
}
void insert_first()
{
p->next = head;
head = p;
}
void insert_last()
{
search->next = p ;
search = p;
}
void insert_before()
{
if(search == head)
insert_awal();
else{
p->next = search;
pbef->next = p;
}
}
void dequeue()
{
if(head==NULL)
printf("Queue is empty!\n");
else
{
printf("Dequeue -> %d\n", head->data);
search=head;
if(search->next==NULL)
head=NULL;
else
head=search->next;
free_node(search);
}
}
void free_node(Node *x)
{
free(x);
x = NULL;
}