-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack2.cpp
86 lines (83 loc) · 1.18 KB
/
stack2.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
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;
class Stack
{
Stack *root=NULL;
int val;
Stack *next=NULL;
int sz=0;
void pr(Stack *curr)
{
if(curr!=NULL)
{
pr(curr->next);
printf("%d ",curr->val);
}
}
public:
void push(int n)
{
sz++;
if(root==NULL)
{
root= new Stack;
root->val=n;
}
else
{
Stack *ne= new Stack;
ne->val=n;
ne->next=root;
root=ne;
}
}
int size(){return sz;}
void pop()
{
if(root==NULL)return ;
sz--;
if(root->next==NULL)
{
root=NULL;
return;
}
Stack *cu=root->next;
delete (root);
root=cu;
}
void print()
{
pr(root);
if(sz)printf("\n");
}
int top()
{
if(root==NULL)return 1/0;
return root->val;
}
bool empty(){return root==NULL;}
};
int main()
{
Stack v;
v.push(10);
v.pop();
// cout<<v.top()<<endl;
cout<<v.size()<<endl;
v.print();
v.pop();
cout<<v.size()<<endl;
cout<<v.empty()<<endl;
v.print();
v.push(10);
v.push(20);
v.push(100);
v.push(90);
v.push(101);
v.pop();
v.push(102);
v.print();
cout<<endl;
cout<<v.top()<<endl;
cout<<v.size()<<endl;
}