forked from Aniket965/Hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stacks.cpp
57 lines (48 loc) · 860 Bytes
/
Stacks.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
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
class stack
{
public:
node* top;
stack() {
top=NULL;
}
void push(int x) {
node* ptr=new node;
ptr->data=x;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
}
int pop() {
node* temp;
if(top==NULL) {
cout<<"Stack Underflow";
return 0;
}
temp=top;
top=top->next;
return temp->data;
}
void show() {
while (top!=NULL) {
cout<<top->data<<">>";
top=top->next;
}
}
};
int main() {
stack s;
s.push(1);
s.push(2);
s.push(3);
s.show();
// cout<< "Element is "<<s.pop()<<endl;
//cout<< "Element is "<<s.pop()<<endl;
}