C语言 链表问题啊 这个插最后一个 插不进去 求高手改改?
提示: 作者被禁止或删除 内容自动屏蔽
#include <stdio.h> #include <stdlib.h> #define Max 10 struct list { int number; int total; struct list *Next; }; typedef struct list Node; typedef Node *link; int data[2][Max] = { 1,3,5,7,2,4,6,8,9,0,15,35,10,67,25,65,38,70,30,20 };//输入数据 link Delete_list(link head,int key) { link pointer; link back; pointer = head;//pointer设为首节点 while(1) { if( pointer->Next == NULL) { printf("Not Found!!\n"); break; } if( head->number == key)//删除节点 { head = pointer->Next;//指向 free(pointer); break; } back = pointer;//?? pointer = pointer->Next; if( pointer->number == key)//插入链表的中部或尾端 { back->Next = pointer->Next;//改接 free(pointer); break; } } return head; } //插入节点至链表内 link Insert_list(link head, link new, link key) { link pointer;//节点的声明 pointer = head;//pointer设为首节点 while(1) { if( pointer == NULL)//插入在首节点前 { new -> Next = head; head = new;//将new->Next尾端指向head break; } if( pointer ->number == key )//插入链表中部或尾端 { new -> Next = pointer -> Next; pointer -> Next = new; break; } pointer = pointer->Next;//前往下一节点 } return head; } //输出链表数据 void Print_list(link head) { link pointer; pointer = head;//pointer设为首节点 while(pointer != NULL) { printf("[%d,%d]",pointer->number,pointer->total); pointer = pointer->Next; } printf(" \n "); } //释放链表 void Free_list(link head) { link pointer; while(head != NULL) { pointer = head; head = head->Next; free(pointer); } } //建立链表 link Create_list(link head) { link pointer; link new; int i; head = (link)malloc(sizeof(Node)); if( head == NULL) { printf("Memory allocate failure!! \n"); } else { head ->number = data[0][0]; head ->total = data[1][0]; head ->Next = NULL;//数据的初始化 pointer = head;//pointer节点设为首节点 for (i=0; i <= Max; i++) { new = (link)malloc(sizeof(Node)); new -> number = data[0][i]; new -> total = data[1][i]; new -> Next = NULL; pointer -> Next = new;//将新链表窜连在原链表尾部 pointer = new;//列表尾端节点为新节点 } } return head; } //主程序 void main() { link head; link new; int key; int n; head = Create_list(head);//调用建立链表 if( head != NULL) { Print_list(head); printf("选择\n1 delete 2 insert \n"); scanf("%d",&n); switch(n) { case 1: printf("delete"); while(1) { printf("Input 0 to EXIT\n"); new = (link)malloc(sizeof(Node)); printf("Please input the data number : "); scanf("%d",&new->number); if(new->number == 0) break; printf("Please input the data total :"); scanf("%d",&new -> total); printf("Please input the data number for insert : "); scanf("%d",&key); head = Insert_list( head,new, key); Print_list(head); } break; case 2: printf("insert"); while(1) { printf("Input 0 to EXIT!\n"); printf("Please input the data to delete : "); scanf("%d",&key); if( key == 0) break; head = Delete_list(head,key);//调用插入节点 Print_list(head);//调出链表数据 } break; default : printf("error!\n"); break; } Free_list(head); } }