| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 400 人关注过本帖
标题:版主,!! 求解释。。简单的栈问题。
只看楼主 加入收藏
dengluoy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:127
专家分:165
注 册:2013-2-5
结帖率:94.44%
收藏
已结贴  问题点数:10 回复次数:6 
版主,!! 求解释。。简单的栈问题。
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1
struct Node                        
{
    struct Node *next;
    int digit;
};
int Pop(struct Node *s,int *i);      //出栈函数
int InitQueue(struct Node *s);       //制造一个空栈
int Push(struct Node *s,int i);      //入栈函数
void conversion(int n,int d);        //十进制转换函数
int EmptyQueue(struct Node *s);      //判断函数
int main(void)
{
    int n = 0;
    int m = 0;
    printf("Enter tows digit:");
    scanf("%d%d",&n,&m);
    conversion(n,m);
    return 0;
}
int InitQueue(struct Node *s)
{
    s = (struct Node *)malloc(sizeof(struct Node));
    if(s == NULL)
        return FALSE;
    s->next = NULL;
    return TRUE;
}
int Push(struct Node *s,int i)
{
    struct Node *p;
    p = (struct Node *)malloc(sizeof(struct Node));
    p->digit = i;
    p->next = s;
    s = p;
    return TRUE;
}
int Pop(struct Node *s,int *i)
{
    struct Node *p;
    p = s;
    if(p == NULL)
        return FALSE;
    *i =p->digit;
    s = p->next;
    free(p);
    return TRUE;
}
void conversion(int n,int d)
{
    struct Node *s =NULL;
    int *i = NULL;
    int x;
    InitQueue(s);
    while(n > 0)
    {
        x = n % d;
        Push(s,x);
        n = n / d;
    }
    while(!EmptyQueue(s))
    {
        Pop(s,i);
        printf("%d",*i);
    }
}
int EmptyQueue(struct Node *s)
{
    if(s == NULL)
        return FALSE;
    else
        return TRUE;
}
程序运行错误。。运行错误的原因是指针S为NULL 。 这是为什么,? 我明明已经申请动态内存成功了。
搜索更多相关主题的帖子: next 
2013-03-14 22:07
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6814
专家分:42393
注 册:2010-12-16
收藏
得分:0 
int InitQueue(struct Node *s)
 {
     s = (struct Node *)malloc(sizeof(struct Node));
     if(s == NULL)
         return FALSE;
     s->next = NULL;
     return TRUE;
 }

这个 s指针不能吧内存带出去的,要用指针的指针
int InitQueue(struct Node **s)
 {
     *s = (struct Node *)malloc(sizeof(struct Node));
……

这样试试

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-03-14 22:18
dengluoy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:127
专家分:165
注 册:2013-2-5
收藏
得分:0 
回复 2楼 yuccn
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1
struct Node                        
{
    struct Node *next;
    int digit;
};
int Pop(struct Node *s,int *i);      //出栈函数
int InitQueue(struct Node **s);       //制造一个空栈
int Push(struct Node *s,int i);      //入栈函数
void conversion(int n,int d);        //十进制转换函数
int EmptyQueue(struct Node *s);      //判断函数
int main(void)
{
    int n = 0;
    int m = 0;
    printf("Enter tows digit:");
    scanf("%d%d",&n,&m);
    conversion(n,m);
    return 0;
}
int InitQueue(struct Node **s)
{
    *s = (struct Node *)malloc(sizeof(struct Node));
    if(*s == NULL)
        return FALSE;
    (*s)->next = NULL;
    return TRUE;
}
int Push(struct Node *s,int i)
{
    struct Node *p;
    p = (struct Node *)malloc(sizeof(struct Node));
    p->digit = i;
    p->next = s;
    s = p;
    return TRUE;
}
int Pop(struct Node *s,int *i)
{
    struct Node *p;
    p = s;
    if(p == NULL)
        return FALSE;
    *i = p->digit;
    s = p->next;
    free(p);
    return TRUE;
}
void conversion(int n,int d)
{
    struct Node *s =NULL;
    int *i = NULL;
    int x;
    InitQueue(&s);
    while(n > 0)
    {
        x = n % d;
        Push(s,x);
        n = n / d;
    }
    while(!EmptyQueue(s))
    {
        Pop(s,i);
        printf("%d",*i);
    }
}
int EmptyQueue(struct Node *s)
{
    if(s == NULL)
        return FALSE;
    else
        return TRUE;
}
好像还是不正确呀,版主。。。。现在没有运行错误了。但是我调试了。s的值还是如此。。

一同学习, 一同进步
2013-03-14 22:35
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:0 
push和pop也改成 2级指针


[fly]存在即是合理[/fly]
2013-03-14 22:42
dengluoy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:127
专家分:165
注 册:2013-2-5
收藏
得分:0 
回复 4楼 azzbcc
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1
struct Node                        
{
    struct Node *next;
    int digit;
};
int Pop(struct Node **s,int **i);     //出栈函数
int InitQueue(struct Node **s);       //制造一个空栈
int Push(struct Node **s,int i);      //入栈函数
void conversion(int n,int d);        //十进制转换函数
int EmptyQueue(struct Node **s);      //判断函数
int main(void)
{
    int n = 0;
    int m = 0;
    printf("Enter tows digit:");
    scanf("%d%d",&n,&m);
    conversion(n,m);
    return 0;
}
int InitQueue(struct Node **s)
{
    *s = (struct Node *)malloc(sizeof(struct Node));
    if(*s == NULL)
        return FALSE;
    (*s)->next = NULL;
    return TRUE;
}
int Push(struct Node **s,int i)
{
    struct Node *p;
    p = (struct Node *)malloc(sizeof(struct Node));
    p->digit = i;
    p->next = *s;
    *s = p;
    return TRUE;
}
int Pop(struct Node **s,int **i)
{
    struct Node *p;
    p = *s;
    if(p == NULL)
        return FALSE;
    **i = p->digit;
    *s = p->next;
    free(p);
    return TRUE;
}
void conversion(int n,int d)
{
    struct Node *s =NULL;
    int *i = NULL;
    int x;
    InitQueue( &s);
    while(n > 0)
    {
        x = n % d;
        Push(&s,x);
        n = n / d;
    }
    while(!EmptyQueue(&s))
    {
        Pop(&s,&i);
        printf("%d",*i);
    }
}
int EmptyQueue(struct Node **s)
{
    if(*s == NULL)
        return FALSE;
    else
        return TRUE;
}

还是错误。。。我要疯了。。

一同学习, 一同进步
2013-03-14 22:57
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:10 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1
struct Node                        
{
    struct Node *next;
    int digit;
};
int Pop(struct Node **s,int **i);     //出栈函数
int InitQueue(struct Node **s);       //制造一个空栈
int Push(struct Node **s,int i);      //入栈函数
void conversion(int n,int d);        //十进制转换函数
int EmptyQueue(struct Node *s);      //判断函数
int main(void)
{
    int n = 0;
    int m = 0;
    printf("Enter tows digit:\n");
    scanf("%d%d",&n,&m);
    conversion(n,m);
    return 0;
}
int InitQueue(struct Node **s)
{
    *s = (struct Node *)malloc(sizeof(struct Node));
    if(*s == NULL)
        return FALSE;
    (*s)->next = NULL;
    return TRUE;
}
int Push(struct Node **s,int i)
{
    struct Node *p;
    p = (struct Node *)malloc(sizeof(struct Node));
    p->digit = i;
    p->next = *s;
    *s = p;
    return TRUE;
}
int Pop(struct Node **s,int *i)
{
    struct Node *p;
    p = *s;
    if(p->next == NULL)
    {free(p);return FALSE;}        //你的栈多了一个节点
    *i = p->digit;
    *s = p->next;
    free(p);
    return TRUE;
}
void conversion(int n,int d)
{
    struct Node *s =NULL;
    int i = 0;                //用变量接收
    int x;
    InitQueue( &s);
    while(n > 0)
    {
        x = n % d;
        Push(&s, x);
        n = n / d;
    }
    while(EmptyQueue(s))        //是下面反了,还是这里?
    {
        if (!Pop(&s, &i))break;    //栈空时,i仍有值,需要直接跳出
        printf("%d", i);
    }
    puts("");
}
int EmptyQueue(struct Node *s)
{
    if(s == NULL)
        return FALSE;
    else
        return TRUE;
}


[fly]存在即是合理[/fly]
2013-03-14 23:28
dengluoy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:127
专家分:165
注 册:2013-2-5
收藏
得分:0 
十分感谢。。

一同学习, 一同进步
2013-03-15 15:39
快速回复:版主,!! 求解释。。简单的栈问题。
数据加载中...
 
   



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

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