单链表的插入. 谁能帮我看看啊??

来源:百度知道 编辑:UC知道 时间:2024/05/14 04:17:57
#include <stdio.h>
#include <malloc.h>
#include <iostream.h>

struct node
{
char data;
struct node *next;
};

struct node* CreateList(struct node *L,int n)
{
int i;
char ch;
struct node *p;
L=(struct node *) malloc(sizeof(struct node));
L->data=n;
L->next=NULL;
for(i=n;i>0;i--)
{
p=(struct node *) malloc(sizeof(struct node));
scanf("%c",&ch);
p->data=ch;
p->next=L->next;
L->next=p;
}
return L;
}
insert (node *L,int k)
{
node *q,*t;
int j;
char x;
q=L;j=0;
while(q!=NULL&&j<k-1)
{
q=q->next;
j++;
}
if(j!=k-1)
{
printf("插入位置不合理");
return;
}
t=(node *)malloc(sizeof(node));
scanf("%c",&x);
t->data=x;
t->next=q->next;
q->n

LOOK!

insert (node *L,int k)
{
node *q,*t;
int j;
char x;
q=L;j=0;
while(q!=NULL&&j<k-1)
{
q=q->next;
j++;
}
if(j!=k-1)
{
printf("插入位置不合理"); //这句的分号用的中文输入法的,换成英文输入法的
return;
}
还是这个函数,改!
insert (node *L,int k) //这里改为struct node* insert (node *L,int k)
{
node *q,*t;
int j;
char x;
q=L;j=0;
while(q!=NULL&&j<k-1)
{
q=q->next;
j++;
}
if(j!=k-1)
{
printf("插入位置不合理");
return; //这里改为return L
}

程序执行结果MS不对