struct student{ int num; struct student *pnext; }; //创建链表 Stu *createLinkedList(Stu *head,int len){ if (head==NULL) { return NULL; } Stu *last=head; //last->num=10; for (int i=1;i<=len;i++) { last->pnext=(Stu*)malloc(sizeof(Stu)); last->pnext->num=i; last=last->pnext; } last->num=len-5; last->pnext=NULL; return head; }
//打印链表 void print_list(Stu *head){ if (head==NULL) { return ; } Stu *last=head->pnext; while (last!=NULL) { printf("%d\t",last->num); last=last->pnext; } }
//从头插入 int addNode(Stu *head,int value){ if (head==NULL) { return -1; } Stu *last=head; Stu *newNode=(Stu*)malloc(sizeof(Stu)); if (newNode==NULL) { return -1; } newNode->num=value; newNode->pnext=head->pnext; head->pnext=newNode; //newNode->pnext=NULL; return 0; }