请教数据结构
#include<stdio.h>#include<stdlib.h>
#define stackmaxsize 100
#define stackincert 10
typedef struct
{
char *top;
char *base;
int stacksize;
}Stack;
int Stackinit(Stack *s)
{
s->base=(char*)malloc(stackmaxsize*sizeof(char));
if(s->base==NULL) exit(1);
s->top=s->base;
s->stacksize=stackmaxsize;
return 0;
}
int push(Stack *s,char ch)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(char*)realloc(s->base,(s->stacksize+stackincert)*sizeof(char));
if(s->base==NULL) return 1;
s->top=s->base+s->stacksize;
s->stacksize+=stackincert;
}
*(s->top++)=ch;
return 0;
}
int pop(Stack *s,char *ch)
{
if(s->top==s->base) return 1;
*ch=*(--s->top);
return 0;
}
int empty(Stack s)
{
if(s.top==s.base) return 0;
else return 1;
}
int Stackclear(Stack *s)
{
s->top=s->base;
*(s->top)=NULL;
return 0;
}
int destroystack(Stack *s)
{
free(s->base);
*(s->base)=NULL;
return 0;
}
int main()
{
Stack s;
char ch1,ch2;
Stackinit(&s);
ch1=getchar();
push(&s,ch1);
ch1=getchar();
while(ch1!='-')
{
switch(ch1)
{
case '#': pop(&s,&ch2);break;
case '@': Stackclear(&s);break;
default:push(&s,ch1);break;
}
ch1=getchar();
}
/*while(empty(s))
{
pop(&s,&ch3);
putchar(ch3);
}*/
while(s.base<s.top)
putchar(*(s.base++));
// destroystack(&s);
return 0;
}
栈的清空与销毁时这样用吗?还有我的*(s->base)=NULL出现警告,怎么改一下?
//************************
[ 本帖最后由 xdh0817 于 2011-12-29 16:44 编辑 ]