-
Notifications
You must be signed in to change notification settings - Fork 0
/
single-stack.cpp
147 lines (135 loc) · 2.47 KB
/
single-stack.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <windows.h>
using namespace std;
struct
{
int input, s[100], top, batas;
} tumpukan;
bool isFull ()
{
return tumpukan.top==tumpukan.batas-1;
}
bool isEmpty ()
{
return tumpukan.top==-1;
}
void push()
{
if (isFull())
{
cout <<"Error: Data sudah penuh !"<<endl;
}
else
{
tumpukan.top++;
cout <<">> Input Data = ";
cin>>tumpukan.input;
tumpukan.s[tumpukan.top] = tumpukan.input;
cout <<"Data berhasil dimasukkan !" <<endl;
}
}
void pop()
{
int DATA;
if (isEmpty())
{
cout <<"Data kosong !"<<endl;
}
else
{
cout << "Data " <<tumpukan.s[tumpukan.top]<< " berhasil dihapus !" <<endl;
tumpukan.top--;
}
}
void view()
{
if (isEmpty())
{
cout <<"Data kosong !"<<endl;
}
else
{
cout <<">> Isi Data = " <<endl;
for (int i=tumpukan.top; i>=0; i--)
{
cout << " [" <<tumpukan.s[i] << "]"<<endl; ;
}
}
}
bool peek()
{
if (isEmpty())
{
cout <<"Data kosong !"<<endl;
}
else
{
cout <<">> Data Paling Atas = [" << tumpukan.s[tumpukan.top] << "]" <<endl;
}
}
void clear()
{
if (isEmpty())
{
cout <<"Data kosong !"<<endl;
}
else
{
tumpukan.top=-1;
cout << "Semua Data berhasil dihapus." <<endl;
}
}
int main()
{
tumpukan.top = -1;
int menu;
cout << "Input Batas Data = ";
cin >>tumpukan.batas;
do
{
cout << "==============" <<endl;
cout << "Pilih Menu :" <<endl;
cout << "==============" <<endl;
cout << "1. Push" <<endl;
cout << "2. Pop" <<endl;
cout << "3. View" <<endl;
cout << "4. Peek" <<endl;
cout << "5. Clear" <<endl;
cout << "6. Exit" <<endl;
cout << "\n>> Input Menu = ";
cin>>menu;
system("cls");
if (menu == 1)
{
push();
}
else if (menu == 2)
{
pop();
}
else if (menu == 3)
{
view();
}
else if (menu == 4)
{
peek();
}
else if (menu == 5)
{
clear();
}
else if (menu == 6)
{
exit(0);
}
else
{
cout <<"Input yang benar!"<<endl;
}
cout <<endl;
system("pause");
system("cls");
}
while(true);
}