循环双链表插入一直有问题
#include <stdio.h>#include <string.h>#include <stdlib.h>#define N 100typedef struct student{ int studentID; char name[N]; int age; int sex;}STUDENT;typedef struct score{ int chinese; int math; int english; int average; int total;}SCORE;typedef struct node{ STUDENT stu; SCORE sc; struct node *next; struct node *pione;}NODE;extern NODE * mycreate();extern void myinsert(NODE *,STUDENT *,SCORE *);extern void mycopy(NODE *,STUDENT *,SCORE *);extern void myprint(NODE *);void mycopy(NODE *node,STUDENT *student,SCORE *score){ node->stu.studentID = student->studentID; strcpy(node->stu.name,student->name); node->stu.age = student->age; //printf("age = %d\n",node->stu.age); node->stu.sex = student->sex; node->sc.chinese = score->chinese; node->sc.math = score->math; node->sc.english = score->english; node->sc.average = score->average; node->sc.total = score->total; //return node;}NODE * mycreate(){ NODE *head = (NODE *)malloc(sizeof(NODE *)); head->stu.studentID = 0; head->next = head; head->pione = head; return head;}void myinsert(NODE *head,STUDENT *student,SCORE *score){ //NODE *p = (NODE *)malloc(sizeof(NODE *)); //mycopy(p,student,score); if(head->stu.studentID == 0) { mycopy(head,student,score); myprint(head); } else { NODE *last = head; //NODE *p = (NODE *)malloc(sizeof(NODE *)); //mycopy(p,student,score); printf("bwhile\n"); while(last->next != head) { last = last->next; } printf("awhile\n"); NODE *p = (NODE *)malloc(sizeof(NODE *)); //memset(p,0,sizeof(p)); //last = (NODE *)malloc(sizeof(NODE *)); mycopy(p,student,score); //printf("age--- = %d\n",p->stu.age); head->pione = p; //p->next = head; printf("age1 = %d\n",p->stu.age); last->next = p; //p->pione = last->next; printf("age2 = %d\n",p->stu.age); //p->next = head; //last->next = p; printf("age3 = %d\n",p->stu.age); p->pione = last; p->next = head; //last->next = p; printf("age4 = %d\n",p->stu.age); //last = p; //mycopy(p,student,score); //printf("duanduanduab\n"); //free(p); //p = NULL; } //return head;}void myprint(NODE *head){ //NODE *q = head; NODE *q = (NODE *)malloc(sizeof(q)); q = head; int i = 1; #if 1 while(q->next != head) { printf("No.%d:ID:%d\tName:%s\tAge:%d\tSex:%d\n",i,q->stu.studentID,q->stu.name,q->stu.age,q->stu.sex); printf(" Chinese:%d\tMath:%d\tEnglish:%d\tAverage:%d\tTotal:%d\n",q->sc.chinese,q->sc.math,q->sc.english,q->sc.average,q->sc.total); i++; q = q->next; printf("\n"); } printf("No.%d:ID:%d\tName:%s\tAge:%d\tSex:%d\n",i,q->stu.studentID,q->stu.name,q->stu.age,q->stu.sex); printf(" Chinese:%d\tMath:%d\tEnglish:%d\tAverage:%d\tTotal:%d\n",q->sc.chinese,q->sc.math,q->sc.english,q->sc.average,q->sc.total); #endif}int main(){ //dmes("asdasd"); NODE *head = mycreate(); STUDENT stu1; STUDENT stu2; STUDENT stu3; STUDENT stu4; SCORE sc1,sc2,sc3,sc4; stu1.studentID = 12001; strcpy(stu1.name,"王强"); stu1.age = 19; stu1.sex = 1; sc1.chinese = 90; sc1.math = 91; sc1.english = 92; sc1.average = 91; sc1.total = 273; stu2.studentID = 12002; strcpy(stu2.name,"张芳"); stu2.age = 19; stu2.sex = 0; sc2.chinese = 80; sc2.math = 81; sc2.english = 82; sc2.average = 81; sc2.total = 243; stu3.studentID = 12003; strcpy(stu3.name,"李明"); stu3.age = 19; stu3.sex = 1; sc3.chinese = 70; sc3.math = 71; sc3.english = 72; sc3.average = 71; sc3.total = 213; stu4.studentID = 12004; strcpy(stu4.name,"王小雨"); stu4.age = 20; stu4.sex = 0; sc4.chinese = 60; sc4.math = 61; sc4.english = 62; sc4.average = 61; sc4.total = 183; myinsert(head,&stu1,&sc1); myprint(head); printf("1111111\n"); myinsert(head,&stu2,&sc2); myprint(head); printf("2222222\n"); myinsert(head,&stu3,&sc3); myprint(head); printf("3333333\n"); myinsert(head,&stu4,&sc4); myprint(head); printf("444444444\n"); myprint(head);}