欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

带头结点的链表

程序员文章站 2024-03-21 19:58:34
...
#include<stdlib.h>

typedef struct student
{
    int num;
    struct student *next;
}Lstudent,*LPstudent;

void InitLink(LPstudent *phead)
{
    Lstudent *ptr;
    *phead=NULL;
    int n;
    printf("please input number of student:\n");
    scanf("%d",&n);
    while(n>0)
    {
        ptr=malloc(sizeof(Lstudent));
        ptr->num=n;
        ptr->next=*phead;
        *phead=ptr;
        printf("please input number of student:\n");
        scanf("%d",&n);
    }
//----------------------------------------------------------------------------
    ptr=malloc(sizeof(Lstudent));
    ptr->next=*phead;
    *phead=ptr;
//----------------------------------------------------------------------------
}

void printf_link(Lstudent *ptri)
{
//----------------------------------------------------------------------------
    ptri=ptri->next;
//----------------------------------------------------------------------------      
    while(ptri!=NULL)
    {
    printf("%d   ",ptri->num);
    ptri=ptri->next;
    }
}

int main()
{
    LPstudent head;
    InitLink(&head);
    printf_link(head);

}