-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
82 lines (75 loc) · 1.2 KB
/
stack.c
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>
#include<string>
#define STACK
using namespace std;
template <class T>
class stack{
int top;
public:
T *mystack;
stack<T>(){
mystack= new T[5];
top =-1;
}
T pop();
T peek();
void push(T item);
int getTop();
private:
void resize();
int size();
};
template <class T>
T stack<T>::pop(){
T temp =mystack[top];
//mystack[top]=NULL;
top--;
return temp;
}
template <class T>
T stack<T>::peek(){
return mystack[top];
}
template <class T>
int stack<T>::getTop(){
return top;
}
template <class T>
void stack<T>::push(T thi){
if(top==size()-1)
resize();
top+=1;
mystack[top]=thi;
}
template <class T>
void stack<T>::resize(){
T *temp= new T[size()+10];
for(int k=0;k<=top;k++)
temp[k]=mystack[k];
mystack=temp;
}
template<class T>
int stack<T>::size(){
return( sizeof mystack/ (sizeof mystack[0]));
}
template<class T>
std::ostream &operator<< (ostream &strm, stack<T> h){
if(h.getTop()>-1)
{
strm<<"["<<h.mystack[0];
for(int k=1;k<=h.getTop();k++)
strm<<", "<<h.mystack[k];
strm<<"]";
}
else
strm<<"[]"<<"\0";
}
/*int main(){
stack<string> fish;
fish.push("5");
cout<<fish.pop();
fish.push("65");
fish.push("54");
cout<< fish <<endl;
return 0;
}*/