-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.h
92 lines (88 loc) · 1.79 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
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
#include "nodeestruc.h"
//first in first out
using namespace std;
template <typename T>
class Queue{
private:
Nodeestruc<T>* head;
Nodeestruc<T>* tail;
int nodes;
public:
Queue(){
head= nullptr;
tail= nullptr;
nodes=0;
};
T get(){
if(!head){
cout<<"cola vacia"<<endl;
throw "Lista Vacia";
} else{
return head->data;
}
};
void push(T value){//back
auto* temp= new Nodeestruc<T>{value, nullptr};
if (head==nullptr) {
head=temp;
tail=temp;
}else{
tail->next=temp;
tail=temp;
}
nodes++;
};
void pop(){ //front
auto* temp=head;
if(!head)
throw "Lista vacia";
if(nodes==1)
head=tail=nullptr;
else{
head=temp->next;
}
delete temp;
nodes--;
};
T get(int position){
if(!head){
cout<<"cola vacia"<<endl;
throw "Vacio";
}
auto* temp=head;
for (int i = 1; i < position; i++) {
temp=temp->next;
}
return temp->data;
};
void print(){
Nodeestruc<T>* actual=head;
while(actual!=tail){
cout<<actual->data->par[0]<<","<<actual->data->par[1]<<endl;
actual=actual->next;
}
cout<<tail->data->getdata()<<endl;
};
bool empty(){
return head == nullptr;
};
int size(){
return nodes;
};
void clear(){
head=nullptr;
tail=nullptr;
nodes=0;
};
~Queue(){
if(head){
head->killSelf();
}
//delete head;
// delete tail;
};
};
#endif