链表插入时出错,大家看看哪里错了....
[size=5]请教个问题:该程序先创建一个链表,然后在表中插入元素,
运行的时候显示在插入元素的时候(ListInsert)出了问题,大家帮忙看看是哪里错了...size]
程序代码:
#include "stdio.h" #define LEN sizeof(LNode) #define error -1 typedef struct LNode { int data; struct LNode *next;[local]1[/local] }LNode,*LinkList; main() { LinkList creat(int); void print(LinkList); LinkList ListInsert(LinkList,int,int); LinkList head; int n,Insp,Insdata; printf("input the number of node:\n"); scanf("%d",&n); head=creat(n); print(head); printf("input the position and digit tobe insert:"); scanf("%d,%d",&Insp,&Insdata); head=ListInsert(head,Insp,Insdata); printf("the list after inserted"); print(head); } LinkList creat(int n) { LinkList L,p; int i; L=(LinkList)malloc(LEN); L->next=NULL; for(i=n;i>0;i--) { p=(LinkList)malloc(LEN); printf("input %dth node data:",i); scanf("%d",&p->data); p->next=L->next; L->next=p; } return L; } void print(LinkList head) { LinkList p; int n=1; for(p=head->next;p!=NULL;p=p->next) { printf("point %d=%d\n",n++,p->data); } } LinkList ListInsert(LinkList head,int Insp,int Insdata) { /*在链表第Insp个元素之前插入元素Insdata*/ LinkList p,ins; int i=1; ins=(LinkList)malloc(LEN); ins->data=Insdata; p=head; while(i!=Insp); { p=p->next; ++i; } ins->next=p->next; p->next=ins; return head; }
question.rar
(996 Bytes)