关于栈的链式结构输出问题

weixin_37969367 2017-03-19 01:23:20

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NULL 0
typedef int ElemType;
typedef struct Node
{
ElemType date;
struct Node *next;
}StackNode;
//初始化
StackNode *InitStack(StackNode *top)
{
// top=(StackNode *)malloc(sizeof (StackNode));
// if(!top) exit (OVERFLOW);
top=NULL;
return top;
}
//判空栈
int EmptyStack(StackNode *top)
{
if(top==NULL) return OK;
else return ERROR;
}
//压栈
StackNode *PushStack(StackNode *top,ElemType e)
{
StackNode *p=(StackNode *)malloc(sizeof (StackNode));
p->date=e;
p->next=top;
top=p;
return top;
}
//弹栈
StackNode *PopStack(StackNode *top,ElemType &e)
{
StackNode *p=NULL;
if(EmptyStack(top))
{
printf("Stack Underflow!");
return p;
}
e=top->date;
p=top;
top=top->next;
free(p);
return top;
}
//取栈顶
ElemType GetStack(StackNode *top)
{
if(EmptyStack(top))
{
printf("Stack Underflow!");
return OVERFLOW;
}
return(top->date);
}
//输入
StackNode *CreatStack(StackNode *top)
{
ElemType e;
scanf("%d",&e);
while(e)
{
top=PushStack(top,e);
scanf("%d",&e);
}
return top;
}
//输出
void print(StackNode *top)
{

StackNode *p;
p=top;
ElemType e;
while(!EmptyStack(p))
{
// p=PopStack(p,e);
e=p->date;
p=p->next;
printf("%d\t",e);
}
printf("\n");
}
void main()
{
ElemType e;
StackNode *top;
top=InitStack(top);
printf("请输入一组数字(以0结束):\n");
top=CreatStack(top);
printf("堆栈为:\n");
print(top);
top=PopStack(top,e);
printf("删除栈顶元素%d:\n",e);
printf("新的堆栈为:\n");
print(top);
e=GetStack(top);
e=top->date;
printf("现在栈顶元素为:%d\n",e);
printf("堆栈为:\n");
print(top);
}

输出部分里为什么不能用pop函数作循环呢?(用了pop函数之后,print函数第一次用是对的,之后貌似top就被改了,后面的都是错的了)
...全文
140 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2017-03-20
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int ElemType;

typedef struct Node
{
    ElemType data;
    struct Node *next;
}StackNode;

//初始化
StackNode *InitStack(StackNode *top)
{
    top = (StackNode *)malloc(sizeof (StackNode));
    if(!top)
        exit (OVERFLOW);
    top->next = NULL;

    return top;
}
//判空栈
int EmptyStack(StackNode *top)
{
    if(!top->next)
        return OK;
    else
        return ERROR;
}
//压栈
StackNode *PushStack(StackNode *top,ElemType e)
{
    StackNode *p = (StackNode *)malloc(sizeof (StackNode));
    if (!p) {
        fprintf(stderr, "malloc error!\n");
        exit(-1);
    }
    p->data = e;

    p->next = top->next;
    top->next = p;

    return top;
}

//弹栈
StackNode *PopStack(StackNode *top, ElemType *e)
{
    StackNode *p = NULL;
    if(EmptyStack(top)) {
        printf("Stack Underflow!");
        return p;
    }
    p = top->next;
    *e = p->data;

    top->next = p->next;
    free(p);

    return top;
}
//取栈顶
ElemType GetStack(StackNode *top)
{
    if(EmptyStack(top)) {
        printf("Stack Underflow!");
        return OVERFLOW;
    }
    return(top->next->data);
}
//输入
StackNode *CreatStack(StackNode *top)
{
    ElemType e;

    scanf("%d", &e);
    while (e) {
        top = PushStack(top, e);
        scanf("%d", &e);
    }
    return top;
}
//输出
void print(StackNode *top)
{

    StackNode *p;
    p = top->next;
    ElemType e;

    while (p) {
        e = p->data;
        printf("%d\t", e);
        p = p->next;
    }
    printf("\n");
}

void reinit_stack(StackNode *stack)
{
    StackNode *p = stack;
    ElemType e;

    fprintf(stdout, "Output from stack: \n");
    while (!EmptyStack(p)) {
        p = PopStack(p, &e);
        printf("%d\t", e);
    }
    putchar(10);
    free(stack);
}

int main(void)
{
    ElemType e;
    StackNode *top = NULL;
    top = InitStack(top);

    printf("请输入一组数字(以0结束):\n");
    top = CreatStack(top);
    printf("堆栈为:\n");
    print(top);
    top = PopStack(top, &e);
    printf("删除栈顶元素: %d\n", e);
    printf("新的堆栈为:\n");
    print(top);

    e = GetStack(top);
    printf("现在栈顶元素为:%d\n", e);

    printf("堆栈为:\n");
    print(top);

    reinit_stack(top);
    return 0;
}
在你的基础上修改的,参考一下吧; 链表的使用是一个比较难的地方,也是很容易检查C语言的精髓之处学习的如何。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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