-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.h
74 lines (69 loc) · 827 Bytes
/
stack.h
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
using namespace std;
template<class S>
struct snode{
S val;
snode* next;
};
template<class T>
class stack{
snode<T>* top;
int size;
public:
stack()
{
top=0;
size=0;
}
int getsize()
{
return size;
}
void push(T val)
{
snode<T>* temp=new snode<T>;
temp->val=val;
temp->next=NULL;
if(top==NULL)
{
top=temp;
size++;
}
else
{
temp->next=top;
top=temp;
size++;
}
}
T pop()
{
snode<T>* p=top;
if(top!=NULL)
top=top->next;
T val= p->val;
if(p!=NULL)
delete p;
size--;
return val;
}
void display()
{
snode<T>* p=top;
while(p!=NULL)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
}
T operator[](int index)
{
snode<T>* p=top;
for(int i=0;i<index;i++)
{
if(p!=NULL)
p=p->next;
}
return p->val;
}
};