注册 登录
编程论坛 数据结构与算法

刚开始学习不知道哪里出问题了 哪位大神帮看看 谢谢

cclosttemp 发布于 2013-10-20 22:44, 477 次点击
#include<stdio.h>
#include<malloc.h>


typedef struct node
{
   int data;
   struct node *next;
 }node;
 node *head;
 int k;
 node * creates()
 {
  node *p,*s,*h;
  int j=1,x,n;
  p=h=(node*)malloc(sizeof(node));
  h->next=NULL;
  printf("输入线性表长度:");
  scanf("%d,&n");
  printf("输入%d个数字创建表:",n);
  while(j<=n)
   {
     scanf("%d,&x");
s=(node*)malloc(sizeof(node));
s->data=x;
p->next=s;
p=s;
j++;
}
p->next=NULL;
return h;
}

void insertsl(node *head,int k,int i)
{
int j;
node *p,*t;
p=head;
j=0;
while(p&&j<k-1)
{
p=p->next;
j++;
}
if(!p||j>k-1)
printf("插入位置不对\n");
t=(node *)malloc(sizeof(node));
t->data=i;
t->next=p->next;
p->next=t;
}

void deletesl(node *h,int i)
{
node *p,*s,*q;
int j=1;
p=h;
while(p->next!=NULL)
{
q=p->next;
if(q->data==i)
break;
p=p->next;
j++;
}

if(p->next==NULL)
{
printf("找不到你要删除的元素\n");
return;
}
else
{
s=p->next;
p->next=s->next;
free(s);
printf("在位置%d成功删除%d\n",j,i);
}
}

int main()
{
node *p,;
int a,b;
char c;
p=creates();
while(1)
{
printf("\n输入插入元素的位置");
scanf("%d",&b);
printf("\n输入插入的元素");
scanf("%d",&a);
insertsl(p,b,a);
}

printf(p);
printf("\n输入要删除的元素");
scanf("%d",&a);
deletesl(p,a);
printf(p);
}

1 回复
#2
yuccn2013-10-21 16:35
好长,排版不够好。看的好晕
编译问题还是运行问题?
1