我最近学数据结构,在栈这遇到了问题
#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 :"就停了,各位高手请帮我看看到底是怎么回事,小弟在此先谢了!
还有,谁能告诉我怎么学数据结构啊~~好难~~~