-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgdoublestack.cc
More file actions
72 lines (65 loc) · 1.33 KB
/
Copy pathgdoublestack.cc
File metadata and controls
72 lines (65 loc) · 1.33 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
# include <gdoublestack.h>
# include <stdlib.h>
# include <math.h>
# include <isinf.h>
GDoubleStack::GDoubleStack()
{
/* The default constructor of the class.
* It allocates 512 doubles to the stack,
* in order to accelarate the processes
* of push and pop.
* */
data = (double*)malloc(512*sizeof(double));
counter =0;
}
void GDoubleStack::clear()
{
/* Set the counter of elements to zero.
* The stack may be considered as empty.
* */
counter = 0;
}
int GDoubleStack::size() const
{
/* Return the amount of elements on
* the stack.
* */
return counter;
}
void GDoubleStack::push(double x)
{
/* Add the element x at the end of
* the stack. If we need more space
* from the stack, we allocate it.
* */
if(isnan(x) || isinf(x)) {x=1e+8;}
if(counter>=512)
{
data=(double*)realloc(data,(counter+1)*sizeof(double));
}
data[counter]=x;
counter++;
}
double GDoubleStack::top() const
{
/* Return the top most element of the stack.
* If the stack is empty return -1.
* */
return (counter!=0)?data[counter-1]:-1;
}
double GDoubleStack::pop()
{
/* Remove and return the top most element of
* the stack. If the stack is empty return -1.
* */
if(!counter) return -1;
double t=data[counter-1];
counter--;
return t;
}
GDoubleStack::~GDoubleStack()
{
/* Deallocate the memory of the stack.
* */
free(data);
}