-
Notifications
You must be signed in to change notification settings - Fork 5
/
stack.hpp
154 lines (107 loc) · 3.53 KB
/
stack.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
This is stack.cpp
Coxeter version 3.0 Copyright (C) 2002 Fokko du Cloux
See file main.cpp for full copyright notice
*/
#include "error.h"
/****************************************************************************
This module contains an elementary implementation of a pushdown stack,
using our List type, so that the size of the stack is unbounded. In
addition to push and pop, we provide a function top to get the value of
the top element of the stack, without popping it (defined inline.)
It also contains an implementation of a Fifo list, implemented as a
circular list.
****************************************************************************/
/****************************************************************************
Chapter I -- Yhe Fifo class
First-in, first-out lists are implemented here as circular lists. There
are two pointers first and last, for the first and last element in the
list. When last reaches list.size(), it loops to zero; first is increased
on deletion of an element. More room is required when last+1=first mod
size; then more room is obtained in the List, and the part ahead of
first is pushed to the end.
The following functions are defined :
- Fifo() : constructor;
- push(object) : push an object on the list;
- pop() : pop the list;
- top() : peak at the top element (the one that would be popped); (inlined)
- size() : number of elements in list; (inlined)
****************************************************************************/
namespace stack {
template <class T> Fifo<T>::Fifo()
: d_list(0), d_first(0), d_last(~0L), d_size(0)
{}
template <class T> void Fifo<T>::push(const T& object)
/*
Pushes an object on the list, enlarging the list if necessary.
*/
{
d_last++;
if (d_last == d_first) { /* we need more space */
d_list.setSize(d_list.size()+1);
if (d_first < (d_list.size()-1)) { /* move up */
d_list.setData(d_list.ptr()+d_first,d_first+1,d_list.size()-d_first-1);
}
d_first++;
}
else if (d_last == d_list.size()) /* wrap around */
d_last = 0;
d_list[d_last] = object;
d_size++;
return;
}
template <class T> const T& Fifo<T>::pop()
/*
Pops the list; it is assumed that the user has checked for non-emptyness.
*/
{
if (d_first == d_list.size())
d_first = 0;
const T& result = d_list[d_first];
d_size--;
if (d_size == 0) { /* reset an empty list */
d_first = d_list.size();
d_last = ~0L;
}
else
d_first++;
return result;
}
};
/****************************************************************************
Chapter II -- The Stack class.
This section contains the definition of the functions in the Stack class.
The following functions are defined :
- Stack();
- ~Stack();
- push(object) : push an object on the stack, enlarging if necessary;
- pop() : pops the stack, setting an error if empty;
****************************************************************************/
namespace stack {
template <class T> Stack<T>::Stack()
{}
template <class T> Stack<T>::~Stack()
/*
Destructing the components is enough.
*/
{}
template <class T> void Stack<T>::push(const T& object)
/*
Assumes that copy constructor is defined for class T.
*/
{
d_list.append(object);
}
template <class T> const T* Stack<T>::pop()
/*
Pops the stack, returning the address of the corresponding object.
Returns 0 if the stack is empty.
*/
{
if (d_list.size() != 0) {
d_list.setSize(d_list.size()-1);
return &d_list[d_list.size()];
}
return 0;
}
};