-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-using-linkedlist.cpp
More file actions
121 lines (98 loc) · 1.95 KB
/
stack-using-linkedlist.cpp
File metadata and controls
121 lines (98 loc) · 1.95 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
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
#include<iostream>
using namespace std;
class Node{
public:
int info;
Node *next;
Node(int val){
info = val;
next = NULL;
}
};
class Stack{
int capacity,len;
Node *top;
public:
Stack(int size){
capacity = size;
len=0;
top = NULL;
}
// ~Stack(){
// delete []arr;
// }
bool isEmptyStack(){
if(len) return false;
else return true;
}
bool isFullStack(){
if(len==capacity) return true;
else return false;
}
int topValue(){
if(isEmptyStack()){
cout<<"Stack is Empty"<<endl;
return 0;
}
return top->info;
}
void push(int val){
if(isFullStack()){
cout<<"Stack is Full"<<endl;
return;
}
Node *n = new Node(val);
if(len)
n->next=top;
top = n;
len++;
}
int pop(){
if(isEmptyStack()){
cout<<"Stack is Empty"<<endl;
return 0;
}
Node *n = top;
int data = n->info;
top = top->next;
delete n;
len--;
return data;
}
void display(){
if(isEmptyStack()){
cout<<"Stack is Empty"<<endl;
return;
}
else{
Node *curr = top;
while(curr){
cout<<curr->info<<"--------";
curr= curr->next;
}
cout<<endl;
}
}
void reverse(Stack &s1){
Stack s2(10);
while(!isEmptyStack()){
s2.push(pop());
}
s1 = s2;
}
};
int main(int argc, char const *argv[])
{
Stack s1(10),s2(10);
s1.push(1);
s1.push(2);
s1.push(3);
s1.push(4);
s1.push(5);
s1.display();
// cout<<s1.pop()<<endl;
// s1.display();
s1.reverse(s1);
s1.display();
return 0;
}