forked from JuezUN/opt-cpp-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp-stack.cpp
More file actions
138 lines (107 loc) · 2.7 KB
/
Copy pathcpp-stack.cpp
File metadata and controls
138 lines (107 loc) · 2.7 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// From the test suite of https://github.com/codespecs/daikon
// daikon/tests/kvasir-tests/
// Adapted for Kvasir regression tests by Philip Guo
//: C04:Stack.h
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Nested struct in linked list
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
class Stack {
public:
void push(char* dat);
char* peek();
char* pop();
char* getName();
Stack(char* name);
static int getNumStacksCreated();
static int publicNumLinksCreated;
~Stack();
private:
int numElements;
char* myName;
static int numStacksCreated;
int privateStuff();
struct Link {
char* data;
Link* next;
void initialize(char* dat, Link* nxt);
}* head;
};
int Stack::numStacksCreated;
int Stack::publicNumLinksCreated;
int Stack::getNumStacksCreated() {
return Stack::numStacksCreated;
}
void
Stack::Link::initialize(char* dat, Link* nxt) {
data = dat;
next = nxt;
}
Stack::Stack(char* name) {
myName = strdup(name);
Stack::numStacksCreated++;
head = 0;
numElements = 0;
}
Stack::~Stack() {
free(myName);
}
char* Stack::getName() {
cout << "Private stuff: " << privateStuff() << endl;
return myName;
}
int Stack::privateStuff() {
return 42;
}
void Stack::push(char* dat) {
Link* newLink = new Link;
newLink->initialize(dat, head);
head = newLink;
numElements++;
Stack::publicNumLinksCreated++;
}
char* Stack::peek() {
return head->data;
}
char* Stack::pop() {
if(head == 0) return 0;
char* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
numElements--;
return result;
}
int main() {
Stack first((char*)"My first stack");
first.push((char*)"First line");
first.push((char*)"Second line");
first.push((char*)"Third line");
first.push((char*)"Fourth line");
first.push((char*)"Fifth line");
// Pop the lines from the Stack and print them:
char* s;
cout << first.getName() << ":" << endl;
while((s = first.pop()) != 0) {
cout << s << endl;
}
cout << "numStacksCreated: " << Stack::getNumStacksCreated() << endl;
cout << "publicNumLinksCreated: " << Stack::publicNumLinksCreated << endl;
Stack second((char*)"My second stack");
second.push((char*)"Uno");
second.push((char*)"Dos");
second.push((char*)"Tres");
second.push((char*)"Cuatro");
cout << endl << second.getName() << ":" << endl;
// Pop the lines from the Stack and print them:
while((s = second.pop()) != 0) {
cout << s << endl;
}
cout << "numStacksCreated: " << Stack::getNumStacksCreated() << endl;
cout << "publicNumLinksCreated: " << Stack::publicNumLinksCreated << endl;
}