主题:栈的基本运算
hanghaizhe
[专家分:0] 发布于 2007-06-06 19:59:00
我写了一个程序,可是调试不出来,希望精通数据结构的大侠们帮我找找错误,本人不胜感谢
#define MAX 100
typedef char datatype;
typedef struct
{datatype data[MAX];
int top;
}Sqstack;
Sqstack *S;
InitSqstack(Sqstack &S)
{S->top=0;}
int isempty(Sqstack *S)
{return (S->top==0);}
int isfull(Sqstack *S)
{return (S->top==MAX);}
int push(Sqstack *S,datatype e)
{if(S->top==MAX)
{printf("\nstack is full");return 0;}
S->data[S->top++]=e;
}
int pop(Sqstack *S,datatype *e)
{if(S->top==0) return 0;
*e=S->data[--S->top];
return 1;
}
main()
{datatype *e;
char ch;int x;
InitSqstack(&S);
printf("\nplease input a data");
ch=getchar();
x=push(S,ch);
printf("x=%d",x);
x=pop(S,e);
printf("%d",x);
}
回复列表 (共2个回复)
沙发
朋友,你还好吗? [专家分:40] 发布于 2007-06-08 11:28:00
InitSqstack(Sqstack &S)
{S->top=0;}
int isempty(Sqstack *S)
{return (S->top==0);}
int isfull(Sqstack *S)
{return (S->top==MAX);}
int push(Sqstack *S,datatype e)
{if(S->top==MAX)
[color=FF0000]好好看看书吧!你的函数实现过程 非那里取乐?[/color]
板凳
朋友,你还好吗? [专家分:40] 发布于 2007-06-10 23:34:00
我刚写#include<stdio.h>
#include<malloc.h>
#include<process.h>
typedef int elemtype ;
typedef int status;
struct stack
{
elemtype *top;
elemtype *base;
int stacksize;
};
typedef struct stack stack;
#define Stack_Size 100
#define Sizeincrese 10
#define ok 1
#define error 0
#define overflow 0
//全部用线性表实现
//***********************************************************************************************
//空栈
status makestack(stack *s)
{
s->base=(elemtype*)malloc(Stack_Size * sizeof(elemtype));
if(!s->base)
exit(overflow);
s->top = s->base ;
s->stacksize=Stack_Size ;
return ok;
}
//************************************************************************************************
//插入一个元素为新的栈顶元素
status push(stack *s, elemtype e)
{
if(s->top - s->base >= s->stacksize)
{
s->base=(elemtype*)realloc(s->base, (Stack_Size+Sizeincrese)*sizeof(elemtype));
if(!s->base)
exit(overflow);
s->top=s->base+Stack_Size;
s->stacksize += Sizeincrese;
}
*(s->top++)=e;
return ok;
}
//***********************************************************************************************
//弹出一个栈顶元素,并用e返回其值
status pop(stack *s,elemtype *e)
{
if(s->base==s->top)
puts("此栈为空");
return error;
*e=*(--s->top);
return ok;
}
//************************************************************************************************
//若栈不为空,返回栈顶元素
status gettop(stack *s,elemtype *e)
{
if(s->base == s->top)
{
puts("此栈为空");
return error;
}
*e=*(s->top - 1);
return ok;
}
//*************************************************************************************************
//检测栈是否为空
status stackempty(stack *s)
{
if(s->base == s->top)
return ok;
else
return error;了给你 不过可能有错 ,好好
我来回复