-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.myStack栈.cpp
More file actions
65 lines (63 loc) · 959 Bytes
/
21.myStack栈.cpp
File metadata and controls
65 lines (63 loc) · 959 Bytes
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
/*
Author: vant
Date: 05/02/17 19:22
Description: 我的stack
*/
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Stack {
private:
struct Node {
int data;
Node *next;
Node(int data_ = 0, Node *next_ = NULL) {
data = data_;
next = next_;
}
};
Node *top, *tmp;
public:
Stack();
~Stack();
int pop();
void push(int data_);
bool empty() {
return top->next == NULL;
};
void clear();
};
Stack::Stack() {
top = new Node;
}
Stack::~Stack() {
clear();
delete top;
}
void Stack::clear() {
while(!empty())pop();
}
void Stack::push(int data_){
tmp = new Node(data_, top->next);
top->next = tmp;
}
int Stack::pop(){
int tt = top->next->data;
tmp = top->next;
top->next = top->next->next;
delete tmp;
return tt;
}
int main() {
Stack s;
s.push(5);
s.push(6);
s.push(7);
s.push(8);
while(!s.empty()){
cout<<s.pop()<<endl;
}
s.clear();
}