« 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_MIN11#include <unordered_map>12using namespace std;1314template <typename T>15class Node16{17public:18 int data;19 Node<T> *next;2021 Node(T data)22 {23 this->data = data;24 next = NULL;25 }26};2728template <typename T>29class Queue30{31 Node<T> *head;32 Node<T> *tail;33 int size;3435public:36 Queue()37 {38 head = NULL;39 tail = NULL;40 size = 0;41 }4243 int getSize() const44 {45 return size;46 }4748 bool isEmpty() const { return size == 0; }4950 void enqueue(T element)51 {52 Node<T> *newNode = new Node<T>(element);53 if (head == NULL)54 {55 head = tail = newNode;56 }57 else58 {59 tail->next = newNode;60 tail = newNode;61 }62 size++;63 }6465 T front()66 {67 if (size == 0)68 {69 return 0;70 }71 return head->data;72 }7374 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};8889int 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);99100 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;107108 return 0;109}