| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 412 人关注过本帖
标题:帮我看下我这个栈有什么问题啊
只看楼主 加入收藏
denylee
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-4-2
结帖率:0
收藏
已结贴  问题点数:10 回复次数:1 
帮我看下我这个栈有什么问题啊
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define                SIZE            4
#define                NUM                4   

typedef struct stack{
    int             date;
    struct stack    *next;
}S;
int push(S *s, int con)
{
    S *p;
    p=(S *)malloc(sizeof(S));
    if(!p){
        return -1;
    }
    p->date=con;
    p->next=s;
    s=p;
    return 0;
}   

int pop(S *s, int *con)
{
    S *p;
    int cont;
    if( s==    NULL){
        return -1;
    }
    printf("%d\n",s->date);
    p=s;
    *con=p->date;
    s=p->next;
    free(p);
    return 0;
}
int main(void)
{   

     S *s;
    char buf[SIZE];
    int i=0, dat, cout;
    while((i++) < NUM){
        fgets(buf, SIZE, stdin);
        dat=atoi(buf);
        push(s, dat);
    }
    printf("%d\n",s->date);
    while(--i){
            if(!pop(s, &cout)){
                printf("%d\n",cout);
            }
    }
    return 0;
}
搜索更多相关主题的帖子: next include return 
2010-05-14 11:56
hzh512
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:234
专家分:1333
注 册:2009-6-5
收藏
得分:10 
指针问题,你在调用push函数时,传递的是地址的值,分配了空间之后,没有传回来,造成main函数中s为野指针。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 4
#define NUM 4   

typedef struct stack{
    int             date;
    struct stack    *next;
}S;
int push(S **s, int con)
{
    S *p;
    p=(S *)malloc(sizeof(S));
    if(!p){
        return -1;
    }
    p->date=con;
    p->next=*s;
    *s=p;
    return 0;
}   

int pop(S *s, int *con)
{
    S *p;
    int cont;
    if( s==    NULL){
        return -1;
    }
    printf("%d\n",s->date);
    p=s;
    *con=p->date;
    s=p->next;
    free(p);
    return 0;
}
int main(void)
{   
   
    S *s;
    char buf[SIZE];
    int i=0, dat, cout;
    while((i++) < NUM)
    {
        fgets(buf, SIZE, stdin);
        dat=atoi(buf);
        push(&s, dat);
    }
    printf("%d\n",s->date);
    while(--i){
        if(!pop(s, &cout)){
            printf("%d\n",cout);
        }
    }
    return 0;
}

编程=用几种语言在某个或几个平台上通过抽象思维运用一系列算法来解决现实中问题的手段
2010-05-14 12:45
快速回复:帮我看下我这个栈有什么问题啊
数据加载中...
 
   



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

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