c语言链表的建立和顺序访问各节点的数据域

发布网友

我来回答

1个回答

热心网友

#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
    int score;
    struct student *next;
}student;

student *creatlist()
{
    int i=0;
    student *head,*p,*q;
    head=(student*)malloc(sizeof(student));
    p=head;
    scanf("%d",&i);
    while(i!=-1)
    {
        q=(student*)malloc(sizeof(student));
        q->score=i;
        p->next=q;
        p=q;
        scanf("%d",&i);
    }
    p->next=NULL;
    return head;
}
void print(student *head)
{
    if(!head) return;
    student *p = head->next;
    while(p)
    {
        printf("%d ",p->score);
        p=p->next;
    }
}
int main()
{
    student *head;
    head=creatlist();
    print(head);
    system("pause");
    return 0;
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com