-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueueLinkedList.cpp
118 lines (112 loc) · 2.7 KB
/
queueLinkedList.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <stack>
using namespace std;
template<typename type>
class queueLinkedList{
private:
int size;
class node{
public:
type element;
node* next;
node* prev;
};
node* head;
node* tail;
public:
queueLinkedList(){
head = tail = NULL;
size = 0;
}
void enqueue(type ele){
node* newNode = new node();
newNode->element = ele;
if(head == NULL){
head = tail = newNode;
tail->next = NULL;
head->prev = NULL;
}else{
tail->next = newNode;
newNode->prev = tail;
tail->next = NULL;
tail = newNode;
}
size++;
}
type first(){
// cout << "first : " << head->element << endl;
// cout << "inside : " << endl;
// print();
if(head == NULL){
cout << "\n\t\t\t*** This queue is empty ***\n" << endl;
return head->element;
}else{
return head->element;
}
}
type dequeue(){
// cout << "enter 1" << endl;
if(head == NULL || size == 0){
cout << "\n\t\t\t*** This queue is empty ***\n";
return head->element;
}else{
// Node* current = first;
// first = first->next;
// first->prev = NULL;
// delete current;
type e = head->element;
node* ptr = head;
head = head->next;
head->prev = NULL;
delete ptr;
size--;
cout << "inside : " << endl;
print();
return e;
}
}
bool isEmpty(){
return (size == 0 || head == NULL);
}
int queueSize(){
return size;
}
void clear(){
size = 0;
head = NULL;
tail = NULL;
}
void print(){
if(head == NULL){
cout << "\n\t\t\t*** This queue is empty ***\n" << endl;
return;
}
node* p = tail;
while(p->prev != NULL){
cout << p->element << " -> ";
p = p->prev;
}
cout << p->element << endl;
}
};
int main(){
queueLinkedList<string>q;
// cout << q.dequeue() << endl;
q.enqueue("dfdf");
q.enqueue("er");
q.enqueue("ryty");
q.enqueue("adsgg");
q.print();
// system("pause");
// cout << "size : " << q.queueSize() << endl;
system("pause");
cout << q.dequeue() << endl;
// cout << "size : " << q.queueSize() << endl;
// system("pause");
// cout << q.first() << endl;
// system("pause");
// cout << "size : " << q.queueSize() << endl;
// system("pause");
// system("pause");
// q.print();
}