/顺序栈
#include
#include
#include
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
typedef int ElemType;
int InitStack(SqStack &S) //为栈S分配存储空间,并置S为空栈
{
int size = STACK_INIT_SIZE;
S。
base=(int *)malloc(size*sizeof(ElemType));
if(!S。base)
return 0;
S。top=S。base; //置栈S为空栈
S。
stacksize=STACK_INIT_SIZE;
return 1;
}
int GetTop(SqStack S,int &e) //若栈不空,则用e返回S的栈顶元素
{
if(S。
top==S。base) return 0;
e=*(S。top-1);
return 1;
}
int Push(SqStack &S, int e) /*进栈函数,将e插入栈S中,并使之成为栈顶元素*/
{ if(S。
top-S。base>=S。stacksize) /*栈满,追加存储空间*/
{
int stackinvrement = STACKINCREMENT;
S。base=(ElemType *) realloc(S。
base,(S。stacksize stackinvrement)*sizeof(ElemType));
if(!S。base)
return 0; /*存储分配失败*/
S。stacksize =STACKINCREMENT;
}
*S。
top =e;
return 1;
}
int Pop(SqStack &S,int &e)/*出栈函数,若栈S不空,则删除S的栈顶元素,用e返回其值*/
{ if(S。
top==S。base) return 0;
e=*--S。top;
return 1;
}
void OutputStack(SqStack &S)
{int *q;
q=S。
top-1;
for(int i=0;i
#include
typedef struct SNode
{
int data;
struct SNode *next;
}SNode,*LinkStack;
LinkStack top;
LinkStack PushStack(LinkStack top,int x) //入栈
{
LinkStack s;
s=(LinkStack)malloc(sizeof(SNode));
s->data=x;
s->next=top;
top=s;
return top;
}
LinkStack PopStack(LinkStack top) //退栈
{
LinkStack p;
if(top!=NULL)
{
p=top;
top=top->next;
free(p);
printf("退栈已完成
");
return top;
}
else printf("栈是空的,无法退栈!
"); return 0;
}
int GetStackTop(LinkStack top) //取栈顶元素
{
return top->data;
}
bool IsEmpty()//bool取值false和true,是0和1的区别,bool只有一个字节,BOOL为int型,bool为布尔型
{
return top==NULL ? true:false;
}
void Print()
{
SNode *p;
p=top;
if(IsEmpty())
{
printf("The stack is empty!
");
return;
}
while(p)
{
printf("%d ", p->data);
p=p->next;
}
printf("
");
}
void main()
{
int x,a,b;
char m;
do { printf("
");
printf("###############链栈的基本操作##################
");
printf("××××××××1。
置空栈××××××××××
");
printf("××××××××2。进栈×××××××××××
");
printf("××××××××3。退栈×××××××××××
");
printf("××××××××4。
取栈顶元素××××××××
");
printf("××××××××5。
退出程序×××××××××
");
printf("##############################################
");
printf("
请选择一个字符:");
scanf("%c",&m);
switch(m){
case '1':
top=NULL;
printf("
栈已置空!");
break;
case '2':
printf("
请输入要进栈的元素个数是:");
scanf("%d",&a);
printf("
请输入要进栈的%d个元素:",a);
for(b=0;b。
全部