-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cc
80 lines (68 loc) · 1.08 KB
/
stack.cc
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include "./head/stack.h"
#include "cudd/bnet.h"
using namespace std;
//Constructor
stack::stack(int length)
{
data = new int[length];
size = length;
len = 0;
}
stack::~stack()
{
delete []data;
}
//Push into the stack
void stack::push(int d)
{
if(len < size)
data[len] = d;
else
{
size = size * 2;
int *new_data = new int[size];
int i = 0;
while(data[i] != '\0')
{
new_data[i] = data[i];
i++;
}
delete []data;
data = new_data;
new_data = NULL;
}
len = len + 1;
}
//Pop from the stack
int stack::pop()
{
if(len <= 0)
return -1;
int pop = data[len-1];
data[len-1] = -1;
len = len - 1;
return pop;
}
//Get the size of the stack
int stack::get_num()
{
return len;
}
//Judge if the stack is empty
bool stack::empty()
{
if(len == 0)
return 1;
return 0;
}
void stack::traverse()
{
int i = 0;
while(data[i] != '\0')
cout << data[i++];
}