主题:高手看一下单链表的插入哈
#include<stdio.h>
#include<stdlib.h>
#define error 0
#define ok 1
typedef int elemtype;
typedef struct node
{
int data;
struct node * next;
}node;
creatclist(node * clist,int n)
{node *p;
int j;
clist=(node *)malloc(sizeof(node)); /*tou jie dian*/
clist->next=NULL;
for (j=n;j>0;--j)
{
p=(node *)malloc(sizeof(node)); /*jian li lian biao*/
scanf("%d",&(p->data));
p->next=clist->next;clist->next=p;
printf("%d\n",p->data);
}
}
insertclist(node *clist,int i,elemtype e)
{node * q,*s;
int j;
q=(node *)malloc(sizeof(node));
q=clist;j=0;
while (q&&j<i-1)
{
q=q->next;
j++;
}
if (!q||j>i-1) return error;
s=(node *)malloc(sizeof(node));
s->data=e;s->next=q->next;
q->next=s;
return ok;
printf("%d",s->data);
printf("%d",s->next->data);
}
void main()
{node clist;
int n,e,i;
scanf("%d",&n);
creatclist(&clist,n);
printf("please input the number which you want to insert:\n");
scanf("%d",&e);
printf("the direction which you want to insert:\n");
scanf("%d",&i);
insertclist(&clist,i,e);
}
高帮我看一下insertclist()函数这里有错没有;我想看一下S到底插入成功没有,于是想看一下S后面这个数是不是S插入后紧接它后面的数!请高手看一下哈.C是面编译通过,但看S后面的数输出的都是0哈.请师兄帮忙!
#include<stdlib.h>
#define error 0
#define ok 1
typedef int elemtype;
typedef struct node
{
int data;
struct node * next;
}node;
creatclist(node * clist,int n)
{node *p;
int j;
clist=(node *)malloc(sizeof(node)); /*tou jie dian*/
clist->next=NULL;
for (j=n;j>0;--j)
{
p=(node *)malloc(sizeof(node)); /*jian li lian biao*/
scanf("%d",&(p->data));
p->next=clist->next;clist->next=p;
printf("%d\n",p->data);
}
}
insertclist(node *clist,int i,elemtype e)
{node * q,*s;
int j;
q=(node *)malloc(sizeof(node));
q=clist;j=0;
while (q&&j<i-1)
{
q=q->next;
j++;
}
if (!q||j>i-1) return error;
s=(node *)malloc(sizeof(node));
s->data=e;s->next=q->next;
q->next=s;
return ok;
printf("%d",s->data);
printf("%d",s->next->data);
}
void main()
{node clist;
int n,e,i;
scanf("%d",&n);
creatclist(&clist,n);
printf("please input the number which you want to insert:\n");
scanf("%d",&e);
printf("the direction which you want to insert:\n");
scanf("%d",&i);
insertclist(&clist,i,e);
}
高帮我看一下insertclist()函数这里有错没有;我想看一下S到底插入成功没有,于是想看一下S后面这个数是不是S插入后紧接它后面的数!请高手看一下哈.C是面编译通过,但看S后面的数输出的都是0哈.请师兄帮忙!