怎么手写stack
1、隐藏部分很少,就两个。
int len;
vector<int>a;
1、void push(int x){
a.push_back(x);
len++;
}//添加

2、void pop(){
len--;
a.pop_back();
}//弹出

3、int top(){
return a[len-1];
}//顶端元素

4、int size(){
return len;
}//长度

5、bool empty(){
return len==0;
}//判空
6、void swap(stac x,stac y){
stac *x1,*x2;
x1=&y;
x2=&x;
stac *x3;
x3=x1;
x1=x2;
x2=x3;
}//指针交换

1、class stac{
public:
void push(int x){
a.push_back(x);
len++;
}
void pop(){
len--;
a.pop_back();
}
int top(){
return a[len-1];
}
int size(){
return len;
}
bool empty(){
return len==0;
}
void swap(stac x,stac y){
stac *x1,*x2;
x1=&y;
x2=&x;
stac *x3;
x3=x1;
x1=x2;
x2=x3;
}
private:
int len;
vector<int>a;
}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:162
阅读量:116
阅读量:152
阅读量:61
阅读量:148