-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackWithLinlist.cpp
77 lines (68 loc) · 1.1 KB
/
stackWithLinlist.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
#include<iostream>
#include<vector>
using namespace std;
class Link {
public:
int data;
Link * next;
Link(int value) {
data = value;
next = NULL;
}
};
class stackADT {
public:
Link *head;
stackADT() {
head = NULL;
}
void push(int value) {
Link * newLink = new Link(value);
if (head == NULL) {
head = newLink;
} else {
Link * current = head;
while (current->next != NULL){
current = current->next;
}
current->next = newLink;
}
}
void pop() {
if (head == NULL) {
cout << "stack is empty";
} else {
Link * current = head;
Link *prev;
while (current->next != NULL) {
prev = current;
current = current->next;
}
prev->next = NULL;
delete (current);
}
}
void display() {
Link *current = head;
while (current != NULL) {
cout << current->data << "-->";
current = current->next;
}
}
};
int main() {
stackADT stack;
stack.push(12);
stack.push(13);
stack.push(14);
stack.push(15);
stack.push(16);
stack.push(17);
stack.push(18);
stack.display();
stack.pop();
stack.pop();
cout<<"after pop-->>";
stack.display();
return 0;
}