-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 9.cpp
More file actions
137 lines (134 loc) · 1.94 KB
/
Assignment 9.cpp
File metadata and controls
137 lines (134 loc) · 1.94 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
//============================================================================
// Name : Assignment 9.cpp
// Author : 21258
// Version :
// Copyright : Your copyright notice
// Description : Priority queue
//============================================================================
#include <iostream>
using namespace std;
class Node
{
public:
string name;
int priority;
Node *next;
Node()
{
name='\0';
priority=0;
next=NULL;
}
};
class Queue
{
public:
Node *front,*rear;
Queue()
{
front=NULL;
rear=NULL;
}
bool isempty()
{
if(front==NULL)
return 1;
else
return 0;
}
void push(string c, int a)
{
if(!isempty())
{
Node *t=new Node();
t->name=c;
t->priority=a;
if(t->priority>front->priority)
{
t->next=front;
front=t;
}
else if(t->priority<rear->priority)
{
rear->next=t;
rear=t;
}
else
{
Node *temp=front->next;
Node *m=front;
while(temp!=NULL)
{
if(temp->priority<t->priority)
{
t->next=m->next;
m->next=t;
break;
}
else
{
temp=temp->next;
m=m->next;
}
}
}
}
else
{
front=new Node();
front->name=c;
front->priority=a;
rear=front;
}
}
void pop()
{
Node *t=front;
front=front->next;
delete t;
}
void display()
{
Node *t=front;
while(t!=rear)
{
cout<<t->name<<endl;
t=t->next;
}
cout<<rear->name<<endl;
}
};
int main() {
Queue q;
string h;
int t,c;
char e;
do
{
cout<<"Enter your choice : \n1.Add a job\n2.Delete a job\n3.Display jobs according to priority\n";
cin>>c;
switch(c)
{
case 1:
cout<<"Enter job name : ";
cin>>h;
cout<<"Enter priority : ";
cin>>t;
q.push(h,t);
break;
case 2:
q.pop();
break;
case 3:
q.display();
break;
default:
cout<<"Invalid choice"<<endl;
}
cout<<"Would you like to continue? (Y/N) ";
cin>>e;
}
while(e=='Y'||e=='y');
cout<<"Thank you!";
return 0;
}