« Queues using LinkedList

1#include <iostream>
2#include <iomanip>
3#include <algorithm>
4#include <string>
5#include <cstring>
6#include <vector>
7#include <cmath>
8#include <map>
9#include <climits>
10// climits for INT_MIN
11#include <unordered_map>
12using namespace std;
13
14template <typename T>
15class Node
16{
17public:
18 int data;
19 Node<T> *next;
20
21 Node(T data)
22 {
23 this->data = data;
24 next = NULL;
25 }
26};
27
28template <typename T>
29class Queue
30{
31 Node<T> *head;
32 Node<T> *tail;
33 int size;
34
35public:
36 Queue()
37 {
38 head = NULL;
39 tail = NULL;
40 size = 0;
41 }
42
43 int getSize() const
44 {
45 return size;
46 }
47
48 bool isEmpty() const { return size == 0; }
49
50 void enqueue(T element)
51 {
52 Node<T> *newNode = new Node<T>(element);
53 if (head == NULL)
54 {
55 head = tail = newNode;
56 }
57 else
58 {
59 tail->next = newNode;
60 tail = newNode;
61 }
62 size++;
63 }
64
65 T front()
66 {
67 if (size == 0)
68 {
69 return 0;
70 }
71 return head->data;
72 }
73
74 T dequeue()
75 {
76 if (size == 0)
77 {
78 return 0;
79 }
80 T ans = head->data;
81 Node<T> *next = head->next;
82 delete[] head;
83 head = next;
84 size--;
85 return ans;
86 }
87};
88
89int main()
90{
91 Queue<int> q;
92 q.enqueue(10);
93 q.enqueue(20);
94 q.enqueue(30);
95 q.enqueue(40);
96 q.enqueue(50);
97 q.enqueue(60);
98 q.enqueue(70);
99
100 cout << q.front() << endl;
101 cout << q.getSize() << endl;
102 q.dequeue();
103 q.dequeue();
104 q.dequeue();
105 cout << q.front() << endl;
106 cout << q.getSize() << endl;
107
108 return 0;
109}