-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_link.cpp
More file actions
67 lines (62 loc) · 1.22 KB
/
Stack_link.cpp
File metadata and controls
67 lines (62 loc) · 1.22 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
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
// struct ListNode {
// int val;
// ListNode *next;
// ListNode(int x) : val(x), next(NULL) {}
// }
struct ListNode{
int val;
ListNode* next;
ListNode(int x): val(x), next(NULL){};
};
class MyStack {
public:
ListNode* head = new ListNode(0);
ListNode* curr = head;
MyStack() {
}
void push(int x) {
curr -> next = new ListNode(x);
curr = curr -> next;
}
void pop() {
ListNode* temp = head;
while(temp -> next){
if(temp -> next == curr){
temp -> next = NULL;
curr = temp;
}
else{
temp = temp -> next;
}
}
}
int top() {
if(head == curr){
return -1;
}
return curr -> val;
}
bool empty() {
if(head == curr){
return true;
}
else{
return false;
}
}
};
int main(){
MyStack A;
//A.MyStack();
A.push(1);
A.push(2);
cout << A.top() << endl;
A.pop();
cout << A.empty() << endl;
cout << A.top() << endl;
return 0;
}