有关单链表删除, 不知错在哪,大虾帮忙看看??
//单链
# include <stdio.h>
# include <stdlib.h>
typedef struct link
{
char data;
struct link *next;
}node;
//==================
node * create_(node *head)
{
char x;
node *p,*s;
head=(node *)malloc (sizeof (node));
p=head;
printf("单链表建立如下:");
while (1)
{
scanf("%c",&x);
if (x=='!')
break;
else
{
s=(node *)malloc (sizeof (node));
s->data=x;
p->next=s;
p=s;
}
}
p->next=NULL;
return head;
}
//======================
void print_(node *head)
{
node *p;
p=head;
printf("打印单链表:");
while (p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
//===========================
void del(node *head,char x) //
{
node *p,*t;
if (head==NULL)
printf("表空!");
if (head->data=x)
{
p=head;
head=head->next;
free(p);
}
else
{
t=head;
p=head->next;
while (p!=NULL && p->data!=x)
{
if (p->data!=x)
{
t=p;
p=p->next;
}
}
if (p!=NULL)
{
t->next=p->next;
free(p);
}
else
{
printf("末找到删除结点!");
}
}
}
//==========================
void main()
{
int n;
char x='B';
node *w=NULL;
w=create_(w);
print_(w);
//----------------
del(w,x);
print_(w);
//--------------
printf("\n");
return;
}
问题点数:0、回复次数:4Top
1 楼wcq8303(pope)回复于 2004-01-04 17:24:57 得分 0
void del(node *head,char x) //
{
node *p,*t;
if (head==NULL){
printf("表空!");
return;}
if (head->data==x) // == not =
{
p=head;
head=head->next;
free(p);
}
else
{
t=head;
p=head->next;
while (p!=NULL && p->data!=x)
{
if (p->data!=x)
{
t=p;
p=p->next;
}
}
if (p!=NULL)
{
t->next=p->next; //你这样做,会删除很多节点的
free(p);
}
else
{
printf("末找到删除结点!");
}
}
}
Top
2 楼wcq8303(pope)回复于 2004-01-04 17:26:55 得分 0
long cuo le ,bu hui shanchu hen duo dianTop
3 楼com8888(com)回复于 2004-01-04 18:25:54 得分 0
t->next=p->next;
不是删除了P结点么?
如你所说,该怎改??请贴上来!Top
4 楼wcq8303(pope)回复于 2004-01-04 20:26:39 得分 0
我不是那个意思,刚才那句“你这样做,会删除很多节点的”是错写的。
void del(node *head,char x)
{
node *p,*t;
if (head==NULL){
printf("表空!");
return;//这里应该加上这句,不然,如果head为空时,执行下面的语还是会如错的}
if (head->data==x) // 你在这里写成了=,应该是==
{
p=head;
head=head->next;
free(p);
}
else
{
t=head;
p=head->next;
while (p!=NULL && p->data!=x)
{
if (p->data!=x)
{
t=p;
p=p->next;
}
}
if (p!=NULL)
{
t->next=p->next;
free(p);
}
else
{
printf("末找到删除结点!");
}
}
}
Top
相关问题
- 添加删除数据出乱子,不知道几时在删除,几时在添加
- 不知道该怎么说?(删除不存在的纪录)-在线
- datagrid删除数据出错!!在线等!!!!!!!!!!!!!!!
- DataGrid分页删除出错(在线等)
- 不知错在哪?
- 在删除的时候,再次决定是否要删除,免得删除错了,怎么编写啊?
- ADO 编程删除SQL server中记录存在物理删除和标记删除吗?如果有,该如何物理删除。ACCESS里是有这个问题的,不知道SQL server如何
- 我的WIN2000系统,不知道设置了什么地方,现在删除文件时没有确认提示了!直接就删除了!
- 不知道谁在客户端,操作数据库删除了些东西,怎样才能知道哪台机器操作数据库删除的? 谢谢了!
- access2000 的删除问题,我的朋友在单纯的access环境中做个小的程序不知道如何删除无用的数据




