-
Notifications
You must be signed in to change notification settings - Fork 1
/
Queue.hpp
129 lines (114 loc) · 2.54 KB
/
Queue.hpp
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
119
120
121
122
123
124
125
126
127
128
129
/*---------------------------------------------------------------------------------------------
* Copyright (c) Wilsen Hernandez. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
#ifndef _QUEUE_HPP_
#define _QUEUE_HPP_
#include "Node.hpp"
#include <iostream>
template<class T>
class Queue
{
private:
int length;
Node<T> *primero;
Node<T> *ultimo;
public:
Queue() : length( 0 ), primero( NULL ), ultimo( NULL ) { };
Queue(const Queue<T>&);
~Queue();
int size() const { return length; };
bool empty() const { return length == 0; };
void push(T);
void pop();
T front() const { return primero->key(); };
void clear();
};
/**
* Constructor Copia.
* @constructs Cola
* */
template<class T>
Queue<T>::Queue(const Queue<T>& in)
{
if (in.primero != NULL)
{
primero = new Node<T>();
primero->set_key(in.primero->key());
Node<T> *inPivot = in.primero->next();
Node<T> *thisPivot = this->primero;
Node<T> *add;
while (inPivot != NULL)
{
add = new Node<T>();
add->set_key(inPivot->key());
thisPivot->set_next(add);
thisPivot = thisPivot->next();
inPivot = inPivot->next();
}
ultimo = add;
}
this->length = in.length;
}
/**
* Destructor de Cola.
* */
template<class T>
Queue<T>::~Queue()
{
this->clear();
}
/**
* Encolar.
* @param {T} e - Item a encolar.
* */
template<class T>
void Queue<T>::push(T e)
{
Node<T> *nuevo = new Node<T>();
nuevo->set_key(e);
if (empty())
primero = nuevo;
else
ultimo->set_next(nuevo);
ultimo = nuevo;
length++;
}
template<class T>
void Queue<T>::pop()
{
Node<T> *del;
if (empty())
std::cout << "Cola vacia" << std::endl;
else
{
del = primero;
primero = del->next();
delete del;
length--;
}
}
/**
* clear Cola.
* */
template<class T>
void Queue<T>::clear()
{
if (!empty())
{
Node<T> *actual, *next;
actual = primero;
next = actual->next();
for (int i = 1; i < length; i++)
{
delete actual;
actual = next;
next = actual->next();
}
delete actual;
length = 0;
primero = NULL;
ultimo = NULL;
}
}
#endif