-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQS.cpp
More file actions
88 lines (77 loc) · 1.49 KB
/
QS.cpp
File metadata and controls
88 lines (77 loc) · 1.49 KB
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
#include <iostream>
class Node {
public:
int Data;
Node* Next; // points to next node
Node* Previous; // points to previous point
};
class Queue{
public:
Node* Head; // begining of the list
Node* Tail; // last element
int size;
Queue(){
Head = NULL;
Tail = NULL;
size = 0;
}
Node* Deq(){
Node* p = Head;
if (p==NULL){
std::cout << "Queue is Empty!!!"<<std::endl;
return 0;
}
Head = Head->Next; // goes to the next element
p->Next = NULL;
p->Previous = NULL;
//delete p;
return p; // you can return the node or just delete it here
//return if you are plannig to use that node // for example printing it
}
void Add(int value){
Node* newNode= new Node();
newNode->Data = value;
//first make next and previous pointers null
newNode->Next = NULL;
newNode->Previous = NULL;
// if the list is empty head and tail are the same and point to the new element
if(Head == NULL){
Head = newNode;
Tail = Head;
}else {
newNode->Previous = Tail;
Tail->Next = newNode;
Tail = newNode;
}
size++;
}
void printQ(){
//Node* p = Head;
Node *node = Head;
while (node!=Tail){
std::cout << node->Data << " ";
node= node->Next;
}
}
~Queue(){
Node* p = Head;
Node* temp = p;
while(p!=NULL){
temp = p;
delete p;
p=temp->Next;
delete temp;
}
}
};
int main(){
Queue *q1 = new Queue();
q1->Add(10);
q1->Add(20);
q1->Add(30);
q1->Add(40);
q1->printQ();
Node* p2 = q1->Deq();
q1->printQ();
return 0;
}