-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackwLinkedlist
72 lines (65 loc) · 1.08 KB
/
stackwLinkedlist
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
#include <iostream>
using namespace std;
struct nodes {
int x;
nodes* next;
};
nodes* kok = NULL;
nodes* iter = kok;
void pushStack(int a) {
iter = kok;
if (kok == NULL) {
kok = new nodes;
kok->x = a;
kok->next = NULL;
}
else
{
while (iter->next != NULL) {
iter = iter->next;
}
iter->next = new nodes;
iter->next->x = a;
iter->next->next = NULL;
}
}
void popStack() {
iter = kok;
if (iter == NULL) {
cout << "Stack underflow" << endl;
}
else if (iter->next == NULL) {
cout << "there was only 1 index and it's being deleted" << endl;
delete (iter);
iter->next = NULL;
}
while (iter->next->next != NULL) {
iter = iter->next;
}
delete (iter->next);
iter->next = NULL;
}
void printStack() {
iter = kok;
int cntr = 1;
if (kok == NULL) {
cout << "stack is empty" << endl;
}
while (iter != NULL) {
cout << cntr << ". index is" << iter->x<<endl;
iter = iter->next;
cntr++;
}
}
int main() {
pushStack(20);
pushStack(152);
pushStack(147);
pushStack(1265);
pushStack(1569);
printStack();
popStack();
printStack();
popStack();
printStack();
}