数据结构3_顺序栈
顺序栈的实现,包含分配地址空间,出栈,入栈,显示等功能函数
#include<iostream>
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
using namespace std;
class SqlStack
{
char **base;
char **top;
int stacksize;
public:
SqlStack()
{
base=new char*[100]; //这里[]中本来应输入STACK_INIT_SIZE,单是程序始终报错,不知道是不是二重指针的关系,
if(!base)
{
cout<<"OVERFLOW"<<endl;
return;
}
top=base;
stacksize=STACK_INIT_SIZE;
}
void push(char *e)
{
if(top-base>=stacksize)
{
cout<<"OVERFLOW"<<endl;
return;
}
*this->top=new char(sizeof(e)+1);
strcpy(*this->top,e);
top++;
}
void pop(char *&e)
{
if(top==base)
{
cout<<"the stack is empty"<<endl;
return;
}
e=new char(sizeof(*top)+1);
strcpy(e,*--top);
}
void show()
{
while(top!=base)
{
cout<<*--top<<endl;
}
}
};
void main()
{
SqlStack a;
a.push("stack1");
a.push("test");
a.push("lock");
char *out;
a.pop(out);
cout<<out<<endl;
a.push("test123");
a.show();
}
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
using namespace std;
class SqlStack
{
char **base;
char **top;
int stacksize;
public:
SqlStack()
{
base=new char*[100]; //这里[]中本来应输入STACK_INIT_SIZE,单是程序始终报错,不知道是不是二重指针的关系,
if(!base)
{
cout<<"OVERFLOW"<<endl;
return;
}
top=base;
stacksize=STACK_INIT_SIZE;
}
void push(char *e)
{
if(top-base>=stacksize)
{
cout<<"OVERFLOW"<<endl;
return;
}
*this->top=new char(sizeof(e)+1);
strcpy(*this->top,e);
top++;
}
void pop(char *&e)
{
if(top==base)
{
cout<<"the stack is empty"<<endl;
return;
}
e=new char(sizeof(*top)+1);
strcpy(e,*--top);
}
void show()
{
while(top!=base)
{
cout<<*--top<<endl;
}
}
};
void main()
{
SqlStack a;
a.push("stack1");
a.push("test");
a.push("lock");
char *out;
a.pop(out);
cout<<out<<endl;
a.push("test123");
a.show();
}
posted on 2014-05-19 16:53 zhuangwy_CV 阅读(191) 评论(0) 编辑 收藏 举报