注册 登录
编程论坛 C语言论坛

给位大神你好,我最近在学习数据结构,但是遇到了一些问题,写的创建链表和读链表好像问题,帮忙看一看

天马星空101 发布于 2019-10-22 16:53, 2741 次点击
程序代码:
#include<stdio.h>
#include<stdlib.h>
typedef int elemtype;
typedef struct Node{
    elemtype data;
    struct Node *next;
}Node,*List;

void outputlist(List L);   
Node* createlist(List L,int n);
int main()
{
  List h=NULL;
  printf("请输入5个数:");
  createlist(h,5);
  outputlist(h);
  return  0;
}
   
Node* createlist(List L,int n)
{
        
    L=(struct Node*)malloc(sizeof(Node));
    L->next=NULL;
    Node *r=L;
    int i;
    for(i=0;i<n;i++){
        Node* p=(Node*)malloc(sizeof(Node));
        scanf("%d",&p->data);
        p->next=NULL;
        r->next=p;
        r=p;   
    }
    return L;
}

void outputlist(List L)
{

   
    Node* p;
    p=(Node*)malloc(sizeof(Node));
    p=L->next;
    while(p){
        printf("%d ",p->data);
        p=p->next;
    }
   
}



[此贴子已经被作者于2019-10-27 16:01编辑过]

8 回复
#2
林月儿2019-10-22 17:07
什么问题
#3
天马星空1012019-10-22 17:16
程序运行报错
#4
天马星空1012019-10-22 17:19
只有本站会员才能查看附件,请 登录
#5
纯蓝之刃2019-10-22 18:01
第15行,createlist(h,5); ,是全角的应该用半角的,
第45行,pr6intf("%d ",p->data);中多了个6
#6
林月儿2019-10-22 18:03
26,27行移到16行下面呢
#7
三尺冰2019-10-22 18:14
createList函数有返回值,output里面不需要为p分配内存
#8
c小白23332019-10-23 21:44
问题很多
主要的楼上整理下
1 “,”问题 英文下输入
2 顶七楼 : createList函数有返回值,output里面不需要为p分配内存
3 对与一个结构体
 对其初始化要么定义时为结构体类型 可以初始化的时候用
 定义时为结构体类型指针,在初始化函数中赋值给它
第二种用法 供参考
程序代码:

#include<stdio.h>
#include<stdlib.h>
typedef int elemtype;
typedef struct Node
{
    elemtype data;
    struct Node *next;
}Node,*List;

void outputlist(List L);   
List createlist(int n);
int main()
{
  List h;
  printf("请输入5个数:");
  h = createlist(5);
  outputlist(h);
  return  0;
}
   
List createlist(int n)
{
    List L=(struct Node*)malloc(sizeof(Node));
    L->next=NULL;
    Node *r=L;
    int i;
    for(i=0;i<n;i++){
        Node* p=(Node*)malloc(sizeof(Node));
        scanf("%d",&p->data);
        p->next=NULL;
        r->next=p;
        r=p;   
    }  
    return L;
}

void outputlist(List L)
{
    Node* p;
    p=L->next;
    while(p){
        printf("%d ",p->data);
        p=p->next;
    }
   
}
#9
天马星空1012019-10-27 15:55
虽然这是一个小问题,但解绝了我的疑惑,衷心谢谢各位帮忙

[此贴子已经被作者于2019-10-27 15:57编辑过]

1