往链表中插入新元素的bug
#include<stdio.h>#include <stdlib.h>
typedef struct node{
char name[20];
struct node *next;
}NODE;
void creat(NODE *head, int n){
NODE *p;
for (; n > 0; n--){
p = (NODE *)malloc(sizeof(NODE));
gets(p->name);
p->next = head->next;
head->next = p;
}
}
void output(NODE *head){
NODE *p;
p = head->next;
while (p != NULL){
puts(p->name);
p = p->next;
}
}
void insert_note(NODE *head, NODE *p, int i){
NODE *q;
int n = 0;
for (q = head; n < i && p->next != NULL; ++n)
q = q->next;
p->next = q->next;
q->next = p;
}
void main(){
NODE *head;
int n;
head = (NODE *)malloc(sizeof(NODE));
head->next = NULL;
printf("结点个数为:");
scanf("%d", &n);
getchar();
creat(head, n);
printf("\n\n");
printf("输出;\n");
output(head);
NODE *p;
p =(NODE *)malloc(sizeof(NODE));
int i;
printf("请输入要插入的位置");
scanf("%d", &i);
printf("请输入要插入的数据");
gets(p->name);
getchar();
insert_note(head, p, i);
printf("输出新链表;\n");
output(head);
}
程序哪里出错了????相当于只插入了空结点,而没存入数据呀。,,,,求解决,,,,