1
+
2
+ // @ raunak kumar jaiswal
3
+
4
+ // function You can use is defined below:
5
+ // 1. push() - to insert a new data in queue
6
+ // 2. pop() - to delete a data from a queue
7
+ // 3. size() - to get the
8
+ // 4. front() - to get the front data stored in queue
9
+ // 5. back() - to get the last stored data in queue
10
+ // 6. isEmpty()- to check whether queue is empty or not
11
+
12
+ #include < iostream>
13
+ #include < cstdlib>
14
+ using namespace std ;
15
+ template <class T1 , class T2 >
16
+
17
+ class Pair
18
+ {
19
+ public:
20
+ T1 first;
21
+ T2 second;
22
+ Pair ()
23
+ {
24
+ }
25
+ Pair (T1 x, T2 y)
26
+ {
27
+ first = x;
28
+ second = y;
29
+ }
30
+ };
31
+ template <class T >
32
+ class Node
33
+ {
34
+ public:
35
+ T data;
36
+ Node<T> *next;
37
+ Node (T d)
38
+ {
39
+ data = d;
40
+ next = NULL ;
41
+ }
42
+ };
43
+ template <class T >
44
+ class Queue
45
+ {
46
+ int sz;
47
+ Node<T> *rear;
48
+ Node<T> *fron;
49
+
50
+ public:
51
+ Queue ()
52
+ {
53
+ fron = rear = NULL ;
54
+ sz = 0 ;
55
+ }
56
+
57
+ void push (T item)
58
+ {
59
+ Node<T> *temp = new Node<T>(item);
60
+ ++sz;
61
+ if (rear == NULL )
62
+ {
63
+ rear = temp;
64
+ fron = temp;
65
+ return ;
66
+ }
67
+ rear->next = temp;
68
+ rear = temp;
69
+ }
70
+
71
+ void pop ()
72
+ {
73
+ if (fron == NULL )
74
+ return ;
75
+
76
+ Node<T> *temp = fron;
77
+ fron = fron->next ;
78
+
79
+ delete (temp);
80
+ --sz;
81
+ if (fron == NULL )
82
+ {
83
+ sz = 0 ;
84
+ rear = NULL ;
85
+ }
86
+ }
87
+ T front ()
88
+ {
89
+ if (fron == NULL )
90
+ {
91
+ exit (0 );
92
+ }
93
+ return fron->data ;
94
+ }
95
+ int size ()
96
+ {
97
+ return sz;
98
+ }
99
+ T back ()
100
+ {
101
+ if (rear == NULL )
102
+ {
103
+ exit (0 );
104
+ }
105
+ return rear->data ;
106
+ }
107
+ bool isEmpty ()
108
+ {
109
+ return fron == NULL ;
110
+ }
111
+ };
112
+
113
+ int main ()
114
+ {
115
+
116
+ Queue<Pair<Pair<int , string>, int >> qq;
117
+
118
+ qq.push ({{5 , " data" }, 6 });
119
+ qq.push ({{6 , " structure" }, 7 });
120
+ qq.push ({{7 , " fcfs" }, 8 });
121
+ qq.push ({{8 , " queue" }, 9 });
122
+ qq.push ({{9 , " stl" }, 10 });
123
+ qq.pop ();
124
+
125
+ cout << qq.front ().first .first << " " << qq.front ().first .second << " " << qq.front ().second << endl;
126
+
127
+ cout << qq.back ().first .first << " " << qq.back ().first .second << " " << qq.back ().second << endl;
128
+
129
+ // while loop iterating over all elements and deleting
130
+ cout << " \n --- iterating over all elements -- \n "
131
+ << endl;
132
+ while (!qq.isEmpty ())
133
+ {
134
+ cout << qq.front ().first .first << " " << qq.front ().first .second << " " << qq.front ().second << endl;
135
+ qq.pop ();
136
+ }
137
+ return 0 ;
138
+ }
0 commit comments