当前位置: 代码迷 >> C语言 >> [求助]一个关于stack的问题
  详细解决方案

[求助]一个关于stack的问题

热度:173   发布时间:2006-04-09 10:58:00.0
[求助]一个关于stack的问题

我最近学数据结构,在栈这遇到了问题
#include<stdio.h>
#include<stdlib.h>

#define EMPTY 0
#define FULL 1000

typedef char data;
typedef enum{false, true} boolean;

typedef struct elem{ /* an element on the stack */
data d;
struct elem *next;
}elem;

typedef struct stack{
int cnt;
elem *top;
}stack;

void initialize(stack *stk);
void push(data a, stack *stk);
data pop(stack *stk);
data top(stack *stk);
boolean empty(const stack *stk);
boolean full(const stack *stk);

int main(void)
{
char str[] ="My name is Joanna Kelley!";
int i;
stack s;

initialize(&s); /*initialize the stack*/
printf(" In the string: %s\n",str);
for(i = 0; str[i] != '\0'; ++i)
if(!full(&s))
push(str[i], &s); /*push a char on the stack*/
printf("From the stack: ");
while(!empty(&s));
putchar(pop(&s)); /*pop a char off the stack*/
putchar('\n');
return 0;
}

void initialize(stack *stk)
{
stk -> cnt = 0;
stk -> top = NULL;
}

void push(data d,stack *stk)
{
elem *p;

p = malloc(sizeof(elem));
p -> d = d;
p -> next = stk -> top ;
stk -> top = p;
stk -> cnt++;

}

data pop(stack *stk)
{
data d;
elem *p;

d = stk -> top -> d;
p = stk -> top;
stk -> top = stk -> top -> next;
stk -> cnt--;
free(p);
return d;
}

data top(stack *stk)
{
return (stk -> top -> d);
}

boolean empty(const stack *stk)
{
return ((boolean)(stk -> cnt == EMPTY));
}

boolean full(const stack *stk)
{
return ((boolean) (stk -> cnt == FULL));
}
这个程序是我直接从课本抄来的,理论上的执行结果,应该是:
In the string : My name is Joanna Klley!
From the stack : !yellek annaoJ si eman yM
但是我在执行时只到了"From the stack :"就停了,各位高手请帮我看看到底是怎么回事,小弟在此先谢了!
还有,谁能告诉我怎么学数据结构啊~~好难~~~

搜索更多相关的解决方案: stack  data  typedef  elem  struct  

----------------解决方案--------------------------------------------------------
C里面有这个东西吗boolean)
----------------解决方案--------------------------------------------------------
好象只有bool
----------------解决方案--------------------------------------------------------
我用了typedef啊
----------------解决方案--------------------------------------------------------
还有,我在LINUX上只到"In the string : My name is Joanna Klley!"就停了

----------------解决方案--------------------------------------------------------

谁能给我几个关于栈的完整可执行程序吗?


----------------解决方案--------------------------------------------------------

栈不是你哪个样子的,看看严蔚敏的数据结构,上面有详细的说明,你的栈定义不标准


----------------解决方案--------------------------------------------------------
我们老师叫我们不要看严蔚敏的书
我们用的都是英文的教材

----------------解决方案--------------------------------------------------------
叼,你们老师太牛B了
----------------解决方案--------------------------------------------------------
是啊,他从美国回来的


----------------解决方案--------------------------------------------------------
  相关解决方案