| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 507 人关注过本帖
标题:链式栈为什么遍历不出来?
只看楼主 加入收藏
cwl168
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2012-12-14
结帖率:8.33%
收藏
 问题点数:0 回复次数:1 
链式栈为什么遍历不出来?
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct Node
{
     int data;
     struct Node * next;
}Node;
typedef struct Stack
{
    Node * top;
    Node * base;
}Stack;
void Inti(Stack *s)
{
   Node *N=(Node *)malloc(sizeof(Node));
   s->base=s->top=N;
   s->top->next=NULL;//这里必须是s->top->next=NULL;
   printf("Inti Success!\n");

}
void Push(Stack *s,int e)
{
      Node *p=(Node*)malloc(sizeof(Node));
      p->data=e;p->next=s->top->next;
      s->top=p;
      printf("Push Success!\n");
}
void print(Stack *s)
{
     Node * p = s->top;
    if(s->top==s->base)
    {
         printf("The stack is empty!\n");
    }
    while (p !=s->base)
    {
        printf("%d\n", p->data);
        p =p->next;
    }
    printf("\n");
    return;
}

int main()
{   
     Stack *s;
     s=(Stack *)malloc(sizeof(Stack));
     Inti(s);
     Push(s,1);
     Push(s,2);
     Push(s,3);
     Push(s,4);
     Push(s,5);
     print(s);
     return 0;
}
为什么遍历不出来?
搜索更多相关主题的帖子: top next include 
2012-12-21 15:52
crystall
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:184
专家分:809
注 册:2012-12-1
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef struct Node
{
     int data;
     struct Node * next;
}Node;

typedef struct Stack
{
    Node * top;
    Node * base;
}Stack;

void Inti(Stack *s)
{
   //Node *N=(Node *)malloc(sizeof(Node));
   s->base = s->top = NULL;
   //s->top->next = NULL;//这里必须是s->top->next=NULL;
   printf("Inti Success!\n");

}

void Push(Stack *s,int e)
{
    Node *p=(Node*)malloc(sizeof(Node));
    p->data = e;
   
    if((s->top == NULL) && (s->base == NULL))
    {
        s->base = s->top = p;
    }
    else
    {
        s->top->next = p;
        s->top = p;
    }
   
    s->top->next = NULL;
   
    //p->data=e;p->next=s->top->next;
    //s->top=p;
    printf("Push Success!\n");
}

void print(Stack *s)
{
    //以前比如是 目前桟中的元素
    //5
    //4
    //3
    //2
    //1

    //s->base->data = 1;
    //s->base->next = 数据为2的地址
    //s->top->data = 5;
    //s->top->next = ?  s->top 已经是最后栈顶了,你再继续往后遍历是不行的。

    //要遍历输出,按照你的这个程序,只能从 栈底base开始往后遍历。
    Node * p = s->base;
    if(s->top==s->base)
    {
        printf("The stack is empty!\n");
    }
   
    while (p)
    {
        printf("%d\n", p->data);
        p = p->next;
    }
   
    printf("\n");
   
    return;
}

int main()
{   
     Stack *s;
     s=(Stack *)malloc(sizeof(Stack));
     Inti(s);
     Push(s,1);
     Push(s,2);
     Push(s,3);
     Push(s,4);
     Push(s,5);
     print(s);
     return 0;
}
2012-12-22 10:10
快速回复:链式栈为什么遍历不出来?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016263 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved