| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 440 人关注过本帖
标题:求助!为什么两边的数字删除不了???
只看楼主 加入收藏
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
结帖率:83.33%
收藏
已结贴  问题点数:10 回复次数:4 
求助!为什么两边的数字删除不了???
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
    int num;
   
    struct student *link;
}stud;

 stud* creat(int n)
{
    stud *h,*p;
    int i;
    h=(stud *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
    for(i=0;i<=n;i++)
    {
        
        
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)
            
            
            
        {
            printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        
        p->link=h;
        h=p;
        
    }
    return(h);
}
   
  void search(stud *h)
  {
    stud *p;
    int m;
    printf("请输入需查找数据!");
    scanf("%d",&m);
    p=h;
    while(p!=NULL)
    {
   
        if(m==p->num)
        {printf("%d\n",m);
        
         break;
        }
        else
         p=p->link;
        
    }
         if(p==NULL)
            printf("无此数!");
   
   
   
  }
  stud *delet(stud *h)
  {
      stud *q,*p;
      p=h;
      int n;
      q=h->link;
      printf("请输入要删除的数字!");
      scanf("%d",&n);
      if(n==p->num)
      h=p->link;
      else if(n==q->num)
          p->link=q->link;
      else if(n!=p->num&&n!=q->num)
         while(q->link!=NULL&&p->link!=NULL)
         {
              if(n==q->num)
              p->link=q->link;
              else
              p=p->link;
              q=q->link;
         
         }
         
         
      return(h);
  }
 void print(stud *h)
 {
     stud *p;
     p=h;
     while(p!=NULL)
     {
         printf("%3d",p->num);
         p=p->link;
     }
 }
void main()
{
    struct student *h;
   
    h=(struct student *)creat(3);
   
    search(h);
    delet(h);
    print(h);
}

搜索更多相关主题的帖子: include memory return 
2011-09-26 18:26
伍初
Rank: 2
等 级:论坛游民
帖 子:16
专家分:22
注 册:2011-5-15
收藏
得分:10 
stud *delet(stud *h)
  {
      stud *q,*p;
      int n;
      p=h;
      q=h->link;
      printf("请输入要删除的数字!");
      scanf("%d",&n);
      
      while(n!=p->num&&p->link!=NULL)
      {  
          q=p;
          p=p->link;      
    }
if(p->link==NULL&&n!=p->num)
{
printf("无此数\n");
return h;
}
if(p==h)
h=p->link;
else
if(p->link==NULL)
q->link=NULL;
else
q->link=p->link;         
      return(h);
  }
在主函数中 delet(h);
应写h=delet(h);
删除链表分三种情况:头,中,尾。
2011-09-26 20:16
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
为什么要这样写啊???
2011-09-26 20:55
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
第一个数字删不掉了
2011-09-26 21:09
伍初
Rank: 2
等 级:论坛游民
帖 子:16
专家分:22
注 册:2011-5-15
收藏
得分:0 
第一个数字可以删掉啊,你也可以不这样写啊; while(n!=p->num&&p->link!=NULL)
      {  
          q=p;
          p=p->link;      
    }
先找出要删除的节点;
if(p->link==NULL&&n!=p->num)
{
printf("无此数\n");
return h;
}
判断删除的数有不有,没有折返回 h;
if(p==h)
h=p->link;
如果要删除的数是头节点,那么把头节点去掉
if(p->link==NULL)
q->link=NULL;
如果要删除的数是尾节点,那么把尾节点去掉
else
q->link=p->link;
如果是中间的节点,就把中间的节点去点啊;
好好看下书,慢慢体会,当然还有很都删除数的写法哦;加油!!
2011-09-27 16:33
快速回复:求助!为什么两边的数字删除不了???
数据加载中...
 
   



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

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