求助帖 C语言链表输出

liyamin_ 2018-03-18 11:45:05
#include <stdio.h>
#include <stdlib.h>
struct student
{
long no;
char name[20];
char add[30];
struct student *next;
};
typedef struct student LIST;
LIST *CreateList();
void DispList(LIST *h);
void main()
{
LIST *head;
head=CreateList();
printf("输入 学号 姓名 地址\n");
DispList(head);

}
LIST* CreateList()
{
LIST *h,*prev,*cur;
int i,n;
h=NULL;
printf("输入节点个数:\n");
scanf("%d",&n);
for(i=0; i<n; i++) {
cur=(LIST*)malloc(sizeof(LIST));
cur->next=NULL;
if(h=NULL)
h=cur;
else
prev->next=cur;
scanf("%d %s %s",&cur->no,cur->name,cur->add);
prev=cur;
}
return h;
}
void DispList(LIST *h) {
LIST *p=h;
while(p!=NULL) {
printf("%d %s %s\n",p->no,p->name,p->add);
p=p->next;
}
链表不输出!
...全文
350 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2018-03-19
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

struct student
{
    long no;
    char name[20];
    char add[30];
    struct student *next;
};

typedef struct student LIST;
LIST *CreateList();
void DispList(LIST *h);

int main()
{
    LIST *head;
    head=CreateList();
    DispList(head);

}

LIST* CreateList()
{
    LIST *h,*prev,*cur;
    int i,n;
    h=NULL;
    printf("输入节点个数:\n");
    scanf("%d",&n);

    for(i=0; i<n; i++) {
        cur = (LIST*)malloc(sizeof(LIST));
        cur->next = NULL;
        if(h == NULL) {
            h = cur;
            prev = h;
        } else {
            prev->next = cur;
            prev=cur;
        }
        printf("输入 学号  姓名 地址\n");
        scanf("%ld %s %s",&cur->no,cur->name,cur->add);
    }

    return h;
}

void DispList(LIST *h)
{
    LIST *p=h;

    printf("学号  姓名 地址\n");
    while(p) {
        printf("%ld  %s  %s\n",p->no,p->name,p->add);
        p = p->next;
    }
}
参考一下吧 注意链表创建时的逻辑。否则会出现段错误
赵4老师 2018-03-19
  • 打赏
  • 举报
回复
数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633
应战者 2018-03-18
  • 打赏
  • 举报
回复
void DispList(LIST *h) { 后面就没大括号了。
FoolCarpe 2018-03-18
  • 打赏
  • 举报
回复
if (h = NULL)
改成
if (h == NULL)

69,377

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧