| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 844 人关注过本帖
标题:就只有删除头结点有问题啊, 郁闷的.帮我看一下.
只看楼主 加入收藏
自负的蜘蛛
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2008-1-22
收藏
 问题点数:0 回复次数:3 
就只有删除头结点有问题啊, 郁闷的.帮我看一下.
struct node *Del(struct node *head,int date)
{ struct node *p1,*p2;
  if(head==NULL)
  {
      printf("空表"); return head;
  }
  else
  {
    p1=head;
    while(p1!=NULL&&p1->date!=date)
    {
     p2=p1; p1=p1->next;
    }
    if(p1==NULL)
    {
     printf("未找到%d\n",date); return head;
    }
    else if (p1==head)
    {
     head=p1->next; free(p1); return head;
    }
    else if (p1->next!=NULL)
    {
     p2->next=p1->next; free(p1); return head;
    }
    else
    {
     p2->next=NULL; free(p1); return head;
    }
  
  }

}

这是删除单向链表结点的函数, 删除中间和尾部结点都没有问题, 就删除头结点时出问题. 帮我看看有哪里不对的.
下面是整个程序

struct node
{ int date;
  struct node *next;
};
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node *Del(struct node *head,int date);
struct node *Clist1(struct node *);
void Display(struct node *);
void main()
{  int date;
   struct node *head1=NULL;
   head1=Clist1(head1);
   Display(head1);
   printf("输入删除数据\n");
   scanf("%d",&date);
   Del(head1,date);
   Display(head1);
}

/*建立链表函数*/
struct node *Clist1(struct node *head)
{  struct node *p,*p1; int n;
   scanf("%d",&n);
   /*开辟内存空间*/
   p=(struct node *)malloc(sizeof(struct node));
   p->date=n;p->next=NULL;
   /*将head 和 p1 指向 p*/
   head=p;p1=p;
   scanf("%d",&n);
   while(n!=0)/*以0结束*/
   { /*开辟内存空间*/
     p=(struct node *)malloc(sizeof(struct node));
     p->date=n;p->next=NULL;
     p1->next=p;p1=p1->next;
     scanf("%d",&n);
   }
   return head;
}
/*输出链表函数*/
void Display(struct node *head)
{ struct node *p;
  p=head;
  while(p!=NULL)
  { printf("[%d]-->",p->date);
    p=p->next;
  }
  printf("NULL \n");
}

struct node *Del(struct node *head,int date)
{ struct node *p1,*p2;
  if(head==NULL)
  {
      printf("空表"); return head;
  }
  else
  {
    p1=head;
    while(p1!=NULL&&p1->date!=date)
    {
     p2=p1; p1=p1->next;
    }
    if(p1==NULL)
    {
     printf("未找到%d\n",date); return head;
    }
    else if (p1==head)
    {
     head=p1->next; free(p1); return head;
    }
    else if (p1->next!=NULL)
    {
     p2->next=p1->next; free(p1); return head;
    }
    else
    {
     p2->next=NULL; free(p1); return head;
    }
  
  }

}
搜索更多相关主题的帖子: head 结点 next NULL node 
2008-02-01 20:18
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 
head=p1->next; free(p1); return head;
你注意这一句话,head是函数的参数,是一个临时副本,你修改它,函数返回后释放掉,对调用者Del(head1,date);传递的head1没有任何影响。你返回头指针不正是为了解决这个问题吗?
head1=Del(head1,date);
虽然这看上去并不很自然。
在C++中有更好的解决方法。
2008-02-02 00:47
自负的蜘蛛
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2008-1-22
收藏
得分:0 
首先先谢谢你
当时我实在弄不懂了,就看书了,但书上也是那么写的,不过那本书我已经发现有好处错误了,可能在这题方面也有错误吧。

有了你的提示 ,我过会自己在去研究一下
2008-02-02 12:02
自负的蜘蛛
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2008-1-22
收藏
得分:0 
搞定了
是在主函数中出了问题,
写了这个后
head1=Del(head1,date);
问题就解决了
,真的很感谢leeco
2008-02-02 12:26
快速回复:就只有删除头结点有问题啊, 郁闷的.帮我看一下.
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015050 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved