| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 480 人关注过本帖
标题:链表删除值
只看楼主 加入收藏
mahuaguan
Rank: 1
等 级:新手上路
帖 子:38
专家分:4
注 册:2011-9-13
结帖率:62.5%
收藏
已结贴  问题点数:20 回复次数:2 
链表删除值
//先上代码

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

struct work
{
       int data;
       struct work *next;
};

typedef struct work myfun;


void show(struct work*);
void delet(struct work*,int);

int main()
{
    myfun *head=NULL;
    myfun *current,*prev;
    myfun *change;
    int i=10;
   
    int c=5;
   
   
    while(i<=30)
    {
        current=(struct work*)malloc(sizeof(struct work));
        
        if(head==NULL)
        {
            head=current;
        }
        
        else
        {
            prev->next=current;
        }
        
        current->data=i;
        
        i=i+10;
        
        current->next=NULL;
        
        prev=current;
    }
   
    printf("The original list is: \n");
    show(head);
   
    printf("The list after 5 deleted is: \n");
    delet(head,c);
   
    show(head);
   
   
    system("pause");
    return 0;
}

void show(struct work *current)
{
     while(1)
     {
        printf("%d\n",current->data);
        if(current->next==NULL)
        {
            break;
        }
        current=current->next;
        
    }
}
void delet(struct work *current,int x)
{
     struct work *change;
     while(1)
     {
         if(current->data==x)
         {
             current=current->next;
             break;
         }
         
         else if((current->next)->data==x)
         {
              change=current->next;
              current->next=(current->next)->next;
              free(change);
              break;
         }
         else
         {
             current=current->next;
         }
     }
}

不知道为什就是没有办法删掉第一个,虽然题目要求删的是值为20的,可是就是搞不明白,因为我已经考虑了要删除第一个的情况了,可是就是不行
搜索更多相关主题的帖子: current include change 
2012-01-03 01:30
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:20 
你那里删除第一个?
if(current->data==x)
         {
             current=current->next;
             break;
         }
你改变的不过是current 而不是head
2012-01-03 08:33
mahuaguan
Rank: 1
等 级:新手上路
帖 子:38
专家分:4
注 册:2011-9-13
收藏
得分:0 
回复 2楼 hahayezhe
我已经知道了,谢谢
2012-01-04 19:18
快速回复:链表删除值
数据加载中...
 
   



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

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