当前位置: 代码迷 >> C语言 >> [求助] 关于链表
  详细解决方案

[求助] 关于链表

热度:145   发布时间:2006-05-29 19:29:00.0
[求助] 关于链表

大家请看,我这个程序错在哪儿啦?
#include<stdio.h>
#include<stdlib.h>

struct Node
{
int date;
struct Node *next;
};


struct Node *creat()
{
struct Node *head,*tail,*p;
int x;
head=tail=NULL;
printf("Input the number:");
while(scanf("%d",&x)==1) /*输入一个字母或符号结束输入*/
{
p= (struct Node *) malloc (sizeof(struct Node));
p->date=x;
p->next=NULL;
if(head==NULL)
head=tail=p;
else
tail=tail->next;
tail->next=p;
}
return(head);
}

struct Node *del(struct Node *head)
{
struct Node *p,*q;
int i,x;
p=head;
printf("Input the number you want to del:");
scanf("%d",&x);
while(1)
{
if(x==1)
{
head=head->next;
free(p);
break;
}
else
{
for(i=1;i<x;i++)
{
q=p;
p=p->next;
}
q=p->next;
free(p);
break;
}
}
return(head);
}


main()
{
struct Node *head,*p,*q,*r,*s;
int i,x;
head=creat();
p=r=head;
while(p)
{
printf("%d ",p->date);
q=p->next;
free(p);
p=q;
}
scanf("%d",&x);
r=del(r);
}
就是不能删除,不知问题在哪儿。

搜索更多相关的解决方案: 链表  

----------------解决方案--------------------------------------------------------

写的太乱啦!!
----------------解决方案--------------------------------------------------------

#include<stdio.h>
#include<stdlib.h>

struct Node
{
int date;
struct Node *next;
};


struct Node *creat()
{
struct Node *head,*tail,*p;
int x;
head=tail=NULL;
printf("Input the number:");
while(scanf("%d",&x)==1) /*输入一个字母或符号结束输入*/
{
p= (struct Node *) malloc (sizeof(struct Node));
p->date=x;
p->next=NULL;
if(head==NULL)
head=tail=p;
else
tail=tail->next;
tail->next=p;
}
return(head);
}

void del(struct Node *head,int x)
{
struct Node *p,*q;
int i;
p=head;

if(x==1)
{
head=head->next;
free(p);

}
else
{
for(i=1;i<x;i++)
{
q=p;
p=p->next;
}

q->next=p->next;
free (p);

}

}

main()
{
struct Node *head,*p,*r,*s;
int i,x;
head=creat();
p=r=head;
while(p)
{
printf("%d ",p->date);
p=p->next;

}
printf("Input the number you want to del:");
fflush(stdin);
scanf("%d",&x);
del(r,x);
p=head;
while(p)
{
printf("%d ",p->date);
p=p->next;

}
getch();
}



请自己对照,我自己也忘了都改了哪些,确实比较乱,楼主您的代码风格不好,


----------------解决方案--------------------------------------------------------
fflush(stdin);???

什么意思啊?

----------------解决方案--------------------------------------------------------

存盘,这里用来清空键盘最后输入的那个字符.


----------------解决方案--------------------------------------------------------
哎,不懂的太多啦!
----------------解决方案--------------------------------------------------------

为什么不用fflush就出不了结果呢


----------------解决方案--------------------------------------------------------
回复:(zhujie)为什么不用fflush就出不了结果呢?
以下是引用zhujie在2006-5-30 13:57:00的发言:

为什么不用fflush就出不了结果呢

因为前一次的scanf()语句没有将你键盘输入的所有字符用完,这种“残留”往往会坏事儿。所以要用fflush()把它们清洗一下,就像上顿用过的餐具要洗涮干净才能吃下顿一样。这种很有实用价值的东西在正规教材上往往不讲。


----------------解决方案--------------------------------------------------------
今天刚学这个东西,有点困难发现错误!!~`~
----------------解决方案--------------------------------------------------------
有实用价值而正规教材不大讲的还有:随机数rand( )
实际上很多程序的调试为了避免程序员先入为主的非典型测试数据,可以广泛使用随机数来进行测试,较容易暴露逻辑漏洞。
----------------解决方案--------------------------------------------------------
  相关解决方案