| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 307 人关注过本帖
标题:在学数据结构链式存储的时候遇到一点问题。
只看楼主 加入收藏
风雪归人
Rank: 2
等 级:论坛游民
帖 子:7
专家分:10
注 册:2014-11-1
结帖率:0
收藏
已结贴  问题点数:10 回复次数:1 
在学数据结构链式存储的时候遇到一点问题。
程序代码:
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct Node
{
    ElemType data;
    struct Node *next;
} Node;
typedef struct Node *Linklist;
Linklist Create(Linklist head);

int main(void)
{
    Linklist head;
    Create(head);
    printf("--------------");
    while (head->next != NULL)
        printf("%d.\n", head->data);
    return 0;
}

Linklist Create(Linklist head)
{
    int x, i = 1;
    Linklist p, q;
    q = head;
    printf("Your %d input:(0 to quit)\n", i);
    scanf("%d", &x);
    while (x != 0)
    {
        p = (Node *)malloc(sizeof(Node));
        p->data = x;
        p->next = NULL;
        q->next = p;
        q = p;
        scanf("%d", &x);
    }
    return head;
}

以上是我的代码,每当我输入数据的时候,程序就自动停止了。我到底是什么地方出错了?
还有 typedef struct Node * Linklist
Linklist p 声明的是一个指向Node的指针吗
那Linklist *p 又是什么意思呢?指向指针的指针?
搜索更多相关主题的帖子: Create 
2015-04-07 20:00
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:10 
按照楼主的意图实现的代码
不知道可用不?

程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int data;
    struct node *next;
} ND, *PN;

void create_list(PN head);
void free_list(PN head);

int main(void) {
    ND HEAD;
    PN head = &HEAD, cur = NULL;
    create_list(head);
    puts("--------------");

    for(cur = head; cur; cur = cur->next)
        printf("%d\n", cur->data);

    free_list(head->next);

    return 0;
}

void create_list(PN head) {
    int x, i = 1;
    PN new, cur = head;
    printf("Your %d input:(0 to quit)\n", i);
    scanf("%d", &x);

    while(x != 0) {
        new = malloc(sizeof(ND));
        new->data = x;
        cur->next = new;
        cur = new;
        scanf("%d", &x);
    }
    cur->next = NULL;
}

void free_list(PN head) {
    PN p;

    while(head != NULL) {
        p = head->next;
        free(head);
        head = p;
    }
}

Only the Code Tells the Truth             K.I.S.S
2015-04-07 20:24
快速回复:在学数据结构链式存储的时候遇到一点问题。
数据加载中...
 
   



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

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