-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityQueue.c
More file actions
101 lines (93 loc) · 2.43 KB
/
priorityQueue.c
File metadata and controls
101 lines (93 loc) · 2.43 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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data, p;
struct node* next;
};
typedef struct{
struct node* front;
}queue;
struct node* create(int val, int pr){
struct node* new = (struct node*)malloc(sizeof(struct node));
new->data = val; new->p = pr; new->next = NULL;
return new;
}
queue* insert(queue* q,int item, int priority)
{
struct node *tmp, *q_;
tmp = create(item,priority);
if (q->front == NULL || priority < q->front->p)
{
tmp->next = q->front;
q->front = tmp;
}
else
{
q_ = q->front;
while (q_->next != NULL && q_->next->p <= priority)
q_=q_->next;
tmp->next = q_->next;
q_->next = tmp;
}
return q;
}
void del(queue* q)
{
struct node *tmp;
if(q->front == NULL)
printf("Queue Underflow\n");
else
{
tmp = q->front;
printf("Deleted item is: %d\n",tmp->data);
q->front = q->front->next;
free(tmp);
}
}
void display(queue *q)
{
struct node *ptr;
ptr = q->front;
if (q->front == NULL)
printf("Queue is empty\n");
else
{ printf("Queue is :\n");
printf("Priority Item\n");
while(ptr != NULL)
{
printf("%d %d\n",ptr->p,ptr->data);
ptr = ptr->next;
}
}
}
int main(){
int choice, item, priority;
queue *q =(queue*)malloc(sizeof(queue));
do
{
printf("1.Insert\n2.Delete\n3.Display\n4.Quit\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input the item value to be added in the queue : ");
scanf("%d",&item);
printf("Enter its priority : ");
scanf("%d",&priority);
q=insert(q,item, priority);
break;
case 2:
del(q);
break;
case 3:
display(q);
break;
case 4:
break;
default :
printf("Wrong choice\n");
}
}
while(choice != 4);
return 0;
}