링크 길이 계산

1490 단어 데이터 구조
#include
#include

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:
"); 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:
"); scanf("%d",&n); } } void printf_link(Lstudent *ptri) { ptri=ptri->next;///////////////////////////////////////////////// while(ptri!=NULL) { printf("%d ",ptri->num); ptri=ptri->next; } } unsigned int ListLength(Lstudent *ptr) { unsigned int l=0; ptr=ptr->next;// while(ptr!=NULL) { l++; ptr=ptr->next; } return l; } int main() { LPstudent head; InitLink(&head); printf_link(head); unsigned int a=ListLength(head); printf("%u
",a); }

좋은 웹페이지 즐겨찾기