-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStack.cpp
More file actions
46 lines (44 loc) · 762 Bytes
/
MyStack.cpp
File metadata and controls
46 lines (44 loc) · 762 Bytes
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
#include<iostream>
#include<stack>
using namespace std;
class MyStack{
int currsize,maxsize;
int* arr;
public:
MyStack()
{
arr=new int[1];
maxsize=1;
currsize=0;
}
void push(int item){
if (currsize==maxsize){
int* temp = new int[2*maxsize];
for (int i=0;i<maxsize;i++){
temp[i]=arr[i];
}
delete[] arr;
maxsize=maxsize*2;
arr=temp;
}
arr[currsize]=item;
currsize++;
}
int pop(){
int x;
x=arr[currsize-1];
currsize--;
return x;
}
int peek(){
return arr[currsize-1];
}
bool empty(){
if(currsize==0){
return true;
}
else{
return false;
}
}
};