-
Notifications
You must be signed in to change notification settings - Fork 495
/
queue.cpp
138 lines (123 loc) · 2.45 KB
/
queue.cpp
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
//@ raunak kumar jaiswal
// function You can use is defined below:
// 1. push() - to insert a new data in queue
// 2. pop() - to delete a data from a queue
// 3. size() - to get the
// 4. front() - to get the front data stored in queue
// 5. back() - to get the last stored data in queue
// 6. isEmpty()- to check whether queue is empty or not
#include <iostream>
#include <cstdlib>
using namespace std;
template <class T1, class T2>
class Pair
{
public:
T1 first;
T2 second;
Pair()
{
}
Pair(T1 x, T2 y)
{
first = x;
second = y;
}
};
template <class T>
class Node
{
public:
T data;
Node<T> *next;
Node(T d)
{
data = d;
next = NULL;
}
};
template <class T>
class Queue
{
int sz;
Node<T> *rear;
Node<T> *fron;
public:
Queue()
{
fron = rear = NULL;
sz = 0;
}
void push(T item)
{
Node<T> *temp = new Node<T>(item);
++sz;
if (rear == NULL)
{
rear = temp;
fron = temp;
return;
}
rear->next = temp;
rear = temp;
}
void pop()
{
if (fron == NULL)
return;
Node<T> *temp = fron;
fron = fron->next;
delete (temp);
--sz;
if (fron == NULL)
{
sz = 0;
rear = NULL;
}
}
T front()
{
if (fron == NULL)
{
exit(0);
}
return fron->data;
}
int size()
{
return sz;
}
T back()
{
if (rear == NULL)
{
exit(0);
}
return rear->data;
}
bool isEmpty()
{
return fron == NULL;
}
};
int main()
{
Queue<Pair<Pair<int, string>, int>> qq;
qq.push({{5, "data"}, 6});
qq.push({{6, "structure"}, 7});
qq.push({{7, "fcfs"}, 8});
qq.push({{8, "queue"}, 9});
qq.push({{9, "stl"}, 10});
qq.pop();
cout << qq.front().first.first << " " << qq.front().first.second << " " << qq.front().second << endl;
cout << qq.back().first.first << " " << qq.back().first.second << " " << qq.back().second << endl;
//while loop iterating over all elements and deleting
cout << "\n--- iterating over all elements -- \n"
<< endl;
while (!qq.isEmpty())
{
cout << qq.front().first.first << " " << qq.front().first.second << " " << qq.front().second << endl;
qq.pop();
}
return 0;
}