-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.hpp
More file actions
114 lines (97 loc) · 2.22 KB
/
Stack.hpp
File metadata and controls
114 lines (97 loc) · 2.22 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
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Wilsen Hernandez. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
#ifndef _STACK_HPP_
#define _STACK_HPP_
#include <iostream>
#include "Node.hpp"
template<class T>
class Stack
{
private:
int length;
Node<T> *primero;
public:
Stack() : length (0), primero(NULL) { };
Stack(const Stack<T>&);
~Stack() { this->clear(); };
int size() const { return length; };
bool empty() const { return length == 0; };
void push(T);
void pop();
T top() const { return primero->key(); };
void clear();
};
/**
* Constructor copia de Pila.
* @constructs Pila.
* */
template<class T>
Stack<T>::Stack(const Stack<T>& in)
{
if (in.primero != NULL)
{
primero = new Node<T>();
primero->set_key(in.primero->key());
Node<T> *inaux = in.primero->next();
Node<T> *thisaux = this->primero;
while (inaux != NULL)
{
Node<T> *nuevo = new Node<T>();
nuevo->set_key(inaux->key());
thisaux->set_next(nuevo);
thisaux = thisaux->next();
inaux = inaux->next();
}
}
this->length = in.length;
}
/**
* Apilar.
* @param {T} e - T a apilar
* */
template<class T>
void Stack<T>::push(T e)
{
Node<T> *nuevo;
nuevo = new Node<T>();
nuevo->set_key(e);
nuevo->set_next(primero);
primero = nuevo;
length++;
}
/**
* Desapilar.
* */
template<class T>
void Stack<T>::pop()
{
Node<T> *del;
del = primero;
primero = primero->next();
delete del;
}
/**
* clear Pila
* */
template<class T>
void Stack<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;
}
}
#endif