-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack using LL.cpp
81 lines (77 loc) · 1.26 KB
/
stack using LL.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
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int item;
node *next;
};
class stack{
private:
node *top;
public:
stack();
void push(int );
int peek();
bool isEmpty();
void pop();
~stack();
void reverseStack();
};
void stack::reverseStack(){
node *t2,*t1;
if(top && top->next){
t1=NULL;
do
{
t2=top;
top=top->next;
t2->next=t1;
t1=t2;
} while (top!=NULL);
top->next=t2;
}
}
stack::~stack(){
while(top)
pop();
}
bool stack::isEmpty(){
return top==NULL;
}
stack::stack(){
top=NULL;
}
void stack::push(int data){
node *n;
n=new node;
n->item=data;
n->next=top;
top=n;
}
int stack::peek(){
if(isEmpty()){
cout<<"Stack is Empty UNDERFLOW\n";
return 0;}
return top->item;
}
void stack::pop(){
node *t;
t=top;
if(isEmpty())
{cout<<"array is empty underflow\n";}
else{
cout<<peek()<<"element deleted\n";
top=top->next;
delete t;
}
}
int main(){
stack obj;
obj.push(10);
obj.push(20);
obj.push(30);
obj.pop();
cout<< obj.peek();
return 0;
}