-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyqueue.h
56 lines (56 loc) · 951 Bytes
/
myqueue.h
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
#pragma once
#include<iostream>
using namespace std;
template<class T>
class queue{
class node{
public:
T data;
node* next;
node(){ next = NULL; }
node(T ele){
data = ele;
next = NULL;
}
};
node* head;
node* tail;
int size;
public:
queue(){ head = tail = NULL; size = 0; }
bool isEmpty(){ return size == 0; }
void push(T ele){
node* temp = new node(ele);
if (isEmpty())
head = tail = temp;
else{
tail->next = temp;
tail = temp;
}
size++;
//print();
}
void pop(){
node* temp = head;
if (isEmpty())
cout << "Empty Queue\n";
else{
head = head->next;
delete temp;
size--;
}
//print();
}
void print(){
node *p = head;
while (p){
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int Size(){ return size; }
T front(){ return head->data; }
T rear(){ return tail->data; }
~queue(){while(head) pop();}
};