-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkStack.cpp
101 lines (89 loc) · 1.68 KB
/
linkStack.cpp
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
#include <iostream>
#include <string>
#include <stack>
using namespace std;
class linkStack
{
public:
linkStack() : size_(0)
{
head_ = new Node();
}
~linkStack()
{
Node* p = head_;
while(p != nullptr)
{
head_ = head_->next_;
delete p;
p = head_;
}
}
//入栈 O(1)
void push(int val)
{
Node* node = new Node(val);
node->next_ = head_->next_;
head_->next_ = node;
++size_;
}
//出栈 O(1)
void pop()
{
if(head_->next_ == nullptr)
{
throw "stack is empty!";
}
Node* p = head_->next_;
head_->next_ = p->next_;
delete p;
--size_;
}
//获取栈顶元素 O(1)
int top() const
{
if(head_->next_ == nullptr)
{
throw "stack is empty!";
}
return head_->next_->data_;
}
//判断栈是否为空
bool empty() const
{
return head_->next_ == nullptr;
}
//返回栈的元素个数 O(1)
int size() const
{
return size_;
}
private:
struct Node
{
Node(int data = 0) : data_(data), next_(nullptr){}
int data_;
Node* next_;
};
Node* head_;
int size_;
};
int main()
{
int arr[] = {12, 43, 35, 78, 33, 43, 23};
linkStack s;
for(auto v : arr)
{
s.push(v);
}
cout << "stack size: " << s.size() << endl;
cout << "top element: " << s.top() << endl;
while(!s.empty())
{
cout << s.top() << " ";
s.pop();
}
cout << endl;
cout << "stack size: " << s.size() << endl;
return 0;
}