SOS

zitiger 2003-10-01 01:58:24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 5
#define STACKINCREMENT 1
typedef char SElemType;
typedef int Status;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base=(SElemType *)malloc (sizeof(SElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Gettop(SqStack S,SElemType &e)
{
if(S.top==S.base)return ERROR;
e=*(S.top-1);
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)return ERROR;
e=*S.top--;
return OK;
}

Status Push(SqStack &S,SElemType e)
{

if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType *)realloc (S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)exit (OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}

Status DestroyStack(SqStack &S)
{
S.top=S.base;
return OK;
}
Status ClearStack(SqStack &S)
{
while (S.top!=S.base)
S.top=S.top--;
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.top==S.base)return ERROR;
return FALSE;
}
int Stacklength(SqStack S)
{
if(S.top==S.base)return ERROR;
int length;
length=S.top-S.base;
return (length);
}
Status shuju(SqStack &S)
{
char e,i=1;
while(i<=S.stacksize)
{ e=getchar();
Push(S,e);
i++;
}
return OK;
}
void print(SqStack S)
{
char e;
if(S.top==S.base )printf("栈S出错。");
while (S.top!=S.base)
{ Pop(S,e);
printf("%c ",e);
}

printf("%c",*S.top);
printf("\n");
}
void main()
{
SqStack S;
SElemType e;
printf("初使化栈S并判断此栈是否为空:");
InitStack(S);
printf("%d\n",StackEmpty(S));
printf("输入5个字母:");
shuju(S);
print(S);
printf("取出栈顶元素并用e返回:");
Gettop(S,e);
printf("%c\n",e);
printf("将e中元素放入栈顶:");
// Pop(S,e);
Push(S,'d');
print(S);
printf("删除栈顶元素并用e返回:");
Pop(S,e);
printf("%c\n",e);
print(S);
}



...全文
35 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zitiger 2003-10-01
  • 打赏
  • 举报
回复
Push(S,'d');之后数据变"?"了
zitiger 2003-10-01
  • 打赏
  • 举报
回复
还是不行呀,使用Push之后出现了四个"?"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 5
#define STACKINCREMENT 1
typedef char SElemType;
typedef int Status;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base=(SElemType *)malloc (sizeof(SElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Gettop(SqStack S,SElemType &e)
{
if(S.top==S.base)return ERROR;
e=*(S.top-1);
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)return ERROR;
e=*--S.top;//e=*S.top--;
return OK;
}

Status Push(SqStack &S,SElemType e)
{

if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType *)realloc (S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)exit (OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}

Status DestroyStack(SqStack &S)
{
S.top=S.base;
return OK;
}
Status ClearStack(SqStack &S)
{
while (S.top!=S.base)
S.top=S.top--;
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.top==S.base)return ERROR;
return FALSE;
}
int Stacklength(SqStack S)
{
if(S.top==S.base)return ERROR;
int length;
length=S.top-S.base;
return (length);
}
Status shuju(SqStack &S)
{
char e,i=1;
while(i<=S.stacksize)
{ e=getchar();
Push(S,e);
i++;
}
return OK;
}
void print(SqStack S)
{
char e;
if(S.top==S.base )printf("栈S出错。");
while (S.top!=S.base)
{ Pop(S,e);
printf("%c ",e);
}

// printf("%c",*S.top);
printf("\n");
}
void main()
{
SqStack S;
SElemType e;
printf("初使化栈S并判断此栈是否为空:");
InitStack(S);
printf("%d\n",StackEmpty(S));
printf("输入5个字母:");
shuju(S);
print(S);
printf("取出栈顶元素并用e返回:");
Gettop(S,e);
printf("%c\n",e);
printf("将e中元素放入栈顶:");
Push(S,e);
print(S);
printf("删除栈顶元素并用e返回:");
Pop(S,e);
printf("%c\n",e);
print(S);
}
zhhuang2002 2003-10-01
  • 打赏
  • 举报
回复
出啥事了?

33,028

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧