#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int num;
struct student *next;
}Lstudent,*LPstudent;
void InitLink(LPstudent *phead)
{
Lstudent *ptr,*r;
*phead=NULL;
//-----------------------------------------------
ptr=malloc(sizeof(Lstudent));
*phead=ptr;
ptr->next=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=NULL;
if((*phead)->next==NULL)///
(*phead)->next=ptr;//
else
r->next=ptr;
r=ptr;
printf("please input number of student:\n");
scanf("%d",&n);
}
}
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);
}
带头节点的后插法链表
最新推荐文章于 2023-08-31 19:29:34 发布