include iostream include cassert using namespace std class Stack publi

 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
#include <iostream>
#include <cassert>
using namespace std;
class Stack
{
public:
Stack();
~Stack();
int top() const;
void push(const int v);
void pop();
bool isEmpty() const;
private:
struct Node;
struct Node
{
Node* pNext_;
int data_;
};
Node* pHead_ = nullptr;
};
int main()
{
Stack a;
cout << a.isEmpty() << endl;
a.push(3);
cout << a.isEmpty() << endl;
a.pop();
cout << a.isEmpty() << endl;
a.push(3);
a.push(3);
};
Stack::Stack() = default;
Stack::~Stack()
{
while (!isEmpty())
{
pop();
}
}
void
Stack::push(const int v)
{
Node* pNew = new Node;
pNew->pNext_ = pHead_;
pNew->data_ = v;
pHead_ = pNew;
}
bool
Stack::isEmpty() const
{
return (nullptr == pHead_);
}
void
Stack::pop()
{
if (!isEmpty())
{
Node* pDeleted{ pHead_ };
pHead_ = pDeleted->pNext_;
delete pDeleted;
}
}