-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedstack.cpp
More file actions
96 lines (85 loc) · 1.76 KB
/
linkedstack.cpp
File metadata and controls
96 lines (85 loc) · 1.76 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
#include<iostream>
using namespace std;
class node{
private: int info;
node * next;
public: friend class stack;
node(int &x){
this->info = x;
next = NULL;
}
};
class stack {
private:
node * top;
int size;
public:
stack(){
top = NULL;
size = 0;
}
bool isempty();
void push(int &x);
int pop();
int stacksize();
int peek();
};
bool stack::isempty(){
return(size==0);
}
void stack::push(int &x){
auto new_node = new node(x);
new_node->next = top;
top = new_node;
size++;
}
int stack::pop(){
int x = -1;
if(isempty())
return x;
else{
x = top->info;
node * temp;
temp = top ;
top = top->next;
temp ->next = NULL;
delete temp;
size--;
}
return x;
}
int stack::stacksize(){
return size;
}
int stack::peek(){
if(isempty()){
std::cout<<"The stack is empty cannot access the top element\n";
return -1;}
else{
return top->info;
}
}
signed main(){
class stack st;
int x;
std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
for(int i = 0; i < 10; i++){
std::cout<<"Enter the element to be pushed\n";
std::cin>>x;
st.push(x);
// cerr<<st.pop()<<endl;
}
for(int i = 9; i>0 ; i++){
if(st.isempty()){
std::cout<<"stack underflow\n"<<std::endl;
break;
}
std::cout<<"ELement Popped "<<st.pop()<<std::endl;
std::cout<<"The element at the top is "<<st.peek()<<std::endl;
}
return 0;
}