关于单链表的插入问题,很急!!!
程序代码:
typedef struct Student { char num[15]; char name[10]; char dorm[10]; char major[20]; char tel[12]; }ST; typedef struct ListElmt_ { void *data; //单链表数据域 struct ListElmt_ *next; }ListElmt; typedef struct List_ { int size;; ListElmt *head; ListElmt *tail; }List //插入函数 int list_insert_next(List *list,ListElmt *element,const void *data){ ListElmt *new_element; /*Allocate storage for element */ if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL) return -1; /* Insert the element to the list */ new_element->data = (void *)data; if (element == NULL) {//若element为NULL,则新元素插入链表头部 if (list_size(list) == 0) list->tail = new_element; new_element->next = list->head; list->head = new_element; } else{ if (element->next == NULL) list->tail = new_element; new_element->next = element->next; element->next = new_element; } list->size++; return 0; } 我通过下面的插入方式插入,但是head和tail指向的都是最后一个插入的元素。 struct Student *stu; ListElmt *p; char yes_no = '\0'; if ((stu = (struct Student *)malloc(sizeof(struct Student))) == NULL) return -1; do { printf("学号:"); scanf("%s",&stu->num); printf("姓名:"); scanf("%s",&stu->name); printf("宿舍:"); scanf("%s",&stu->dorm); printf("专业:"); scanf("%s",&stu->major); printf("联系方式:"); scanf("%s",&stu->tel); p = list->tail; if (list_insert_next(list,p,(const void *)stu) == 0){ printf("添加成功,还要继续吗(Y/N)?"); getchar(); scanf("%c",&yes_no); }else{ printf("添加失败,请重试!"); system("pause"); yes_no = 'y'; } } while (yes_no == 'y' || yes_no == 'Y'); 由于先前的一个号找不到了,又重新创了个,就20分,我都给了,希望各位大神能帮帮忙,十分感谢!!