链表的问题
程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct node { char name[10]; int age; struct node *next; }NODE; NODE *create() { NODE *head,*tail,*p; head=tail=NULL; p=(NODE *)malloc(sizeof(NODE)); printf("please input a name.\n"); scanf("%s",p->name); while(strcmp(p->name,"quit")!=0) { printf("please input age:\n"); scanf("%d",&p->age); if(NULL==head) head=tail=p; else { tail->next=p; tail=p; } p=(NODE *)malloc(sizeof(NODE)); printf("please input a name.\n"); scanf("%s",p->name); } return head; } int getlenth(NODE *head) { NODE *p; p=head; int n=0; while(p!=NULL) { p=p->next; ++n; } return n; } void print(NODE *head) { NODE *p; p=head; while(p!=NULL) { printf("name is %s age is %d\n",p->name,p->age); p=p->next; } } NODE* clear(NODE *head) { NODE *p,*q; p=head; while(p) { q=p->next; free(p); p=q; } return p; } NODE * insert(NODE *head,int n) { int j=0; NODE *p,*q; p=head; while((p!=NULL)&&(j<n)) { p=p->next; j++; } if(p==NULL) printf("no node.\n"); else { q=(NODE *)malloc(sizeof(NODE)); printf("please inout the insert name:\n"); scanf("%s",q->name); setbuf(stdin,NULL); printf("please input the insert age:\n"); scanf("'%d",q->age); p->next=q; q->next=p->next; } return p; } int main(void) { int n; int lenth; NODE *p; do { printf(" -------------1: create link:--------------\n"); printf("|||||---------2: getlenth:-----------|||||\n"); printf("|||||---------3:print:---------------|||||\n"); printf("|||||---------4:clear:---------------|||||\n"); printf("|||||---------5:insert:--------------|||||\n"); printf("|||||---------6:search:--------------|||||\n"); printf("--please input the number for what do you want to do.---\n"); scanf("%d",&n); switch(n) { case 1:p=create(); break; case 2: lenth=getlenth(p); printf("the link lenth is %d\n",lenth); break; case 3: print(p); break; case 4: p=clear(p); case 5: printf("please input where do you want wo insert the numer:\n"); scanf("%d",&n); p=insert(p,n); break; case 6: printf("please input the number which you want to serch:\n"); scanf("%d",&n); default:break; } }while(1); free(p); }插入新节点后 在重新打印 停不下来了 该怎么修改?还是我插入方法不对?