-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2022-05-11.cpp
More file actions
87 lines (79 loc) · 1.6 KB
/
2022-05-11.cpp
File metadata and controls
87 lines (79 loc) · 1.6 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
// push와 pop 함수를 구현하시오
#include <iostream>
using namespace std;
typedef struct NODE {
NODE* next;
int data;
} NODE;
void Push(int _data);
int Pop();
bool isEmpty();
void printData();
void Menu();
NODE* s_top;
int main() {
int data, nKey = 0;
while (true) {
Menu();
cin >> nKey;
cout << "\n";
switch (nKey) {
case 1:
cout << "Push : ";
cin >> data;
Push(data);
break;
case 2:
cout << "Pop : " << Pop() << endl;
break;
case 3:
printData();
break;
default:
cout << "입력 오류" << endl;
break;
}
cout << "\n";
}
return 0;
}
void Push(int _data) {
// 스택이 비어 있을 때와 아닐 를 고려하자
NODE* newNode = new NODE;
newNode->data = _data;
newNode->next = NULL;
if (isEmpty()) {
s_top = newNode;
}
else {
newNode->next = s_top;
s_top = newNode;
}
}
int Pop() {
// 스택 맨 위에 있는 노드를 삭제하고 노드의 데이터 값을 반환하자
int data = s_top->data;
NODE* temp = s_top;
s_top = s_top->next;
delete temp;
return data;
}
void printData() {
if (isEmpty()) {
cout << "Stack is Empty" << endl;
return;
}
NODE* top = s_top;
for (; top != NULL; top = top->next) {
cout << "| " << top->data << " |" << endl;
}
cout << "-----" << endl;
}
bool isEmpty() { return s_top == NULL; }
void Menu()
{
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Show Stack" << endl;
cout << "INPUT : ";
}