-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
82 lines (73 loc) · 1.08 KB
/
stack.cpp
File metadata and controls
82 lines (73 loc) · 1.08 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
#include<iostream>
#define MAX 10
using namespace std;
struct node
{
int data;
node *next;
};
node *top=NULL, *temp;
int count=0;
void push(int x)
{
if(count==MAX)
{
cout<<"Stack is full!";
return;
}
else
{
node *temp=new node;
count++;
if(top==NULL)
{
temp->data=x;
temp->next=NULL;
top=temp;
}
else
{
temp->data=x;
temp->next=top;
top=temp;
}
}
}
void pop()
{
if(count==0)
{
cout<<"Underflow!";
return;
}
else
{
count--;
temp=top;
top=top->next;
temp->next=NULL;
delete temp;
}
}
void display()
{
temp=top;
while(temp!=NULL)
{
cout<<temp->data<<"\n";
temp=temp->next;
}
}
int main()
{
push(1);
push(2);
push(3);
push(1);
pop();
push(2);
push(3);
pop();
display();
return 0;
}