-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueue.h
102 lines (90 loc) · 1.63 KB
/
Queue.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
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
#pragma once
#include <iostream>
using namespace std;
template <typename T>
struct Node
{
T data;
Node<T>* prev=NULL;
Node<T> *next=NULL;
};
template <typename T>
class Queue
{
private:
Node<T> *head;
Node<T> *tail;
int length;
bool isEmpty()
{
return length == 0;
}
public:
Queue()
{
head = new Node<T>;
tail= new Node<T>;
length = 0;
}
void enqueue(T& x)
{
Node<T>* temp = new Node<T>;
temp->data = x;
if(isEmpty())
{
head->next = temp;
temp->prev = head;
temp->next = tail;
tail->prev = temp;
length++;
}
else
{
tail->prev->next = temp;
temp->prev = tail->prev;
temp->next = tail;
tail->prev = temp;
length++;
}
}
void dequeue(T& input)
{
Node<T>* temp = head;
head = head->next;
input = head->data;
length--;
delete temp;
}
// void find(int x)
// {
// int i;
// for(i=1, temp = head;temp->next != NULL && temp->data != x;temp = temp->next, i++);
// if(temp->data == x)
// {
// cout << "Found at position:" << i << endl;
// }
// else if(temp->next == NULL)
// {
// cout << "Error: Number Not found..." << endl;
// }
// }
int getLength(){
return length;
}
void MakeEmpty(){
Node<T>* temp = head;
Node<T>* t = temp;
while (temp->next!=tail)
{
t = temp;
temp = temp->next;
delete t;
if (temp == NULL) break;
}
delete temp;
delete tail;
tail = new Node<T>;
head = new Node<T>;
length = 0;
}
};