| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 275 人关注过本帖
标题:删除链表中输入的值
只看楼主 加入收藏
烟雾中的迷茫
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:621
专家分:1069
注 册:2011-2-9
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
删除链表中输入的值
程序代码:
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)

struct student
{
    float num;
    char name[10];
    char sex[10];
    int age;
    struct student *next;
};

int main()
{

    struct student *delete(int age);
    struct student *creat(void);

    struct student *head;
    int age;

    printf("please input the basic date\n");
    creat();

    printf("please input the age that you want to delege\n");
    scanf("%d",&age);
    head=delete(age);

    printf("follows are the link have delete the age you input\n");
    do
    {
        printf("%f\t%s\t%s\t%d\n",head->num,head->name,head->sex,head->age);
        head=head->next;
    }while(head!=NULL);
return 0;
}

struct student *creat(void)
{
    struct student *head,*p[2];
    int n=0;

    printf("please input the first node date\n");
    head=(struct student *)malloc(LEN);
    scanf("%f%s%s%d",&p[0]->num,p[0]->name,p[0]->sex,&p[0]->age);
    p[0]=head;

    while(p[0]->next!=NULL)
    {
       if(p[0]->num!=0)
        {
            p[1]=p[0];
            printf("please input the %d node\n",++n);
            p[0]=(struct student *)malloc(LEN);
            scanf("%f%s%s%d",&p[0]->num,p[0]->name,p[0]->sex,&p[0]->age);
            p[1]->next=p[0];
        }
        else p[0]->next=NULL;
        p[1]->next=NULL;
    }
  return head;
}

struct student *delete(int age)
{
    struct student *head,*p[2];
    p[0]=creat();
    head=creat();

     while(p[0]->next!=NULL)
     {
         if(p[0]->age==age)
         {
             if(p[0]==head)
                 head=p[0]->next;
            else
                p[1]->next=p[0]->next;
         }
          else  p[1]=p[0]->next;
          p[0]=p[0]->next;
     }
     if(p[0]->next==NULL)
         if(p[0]->age==age)
         head=NULL;
         return head;
}
这个链表编译链接无措 就是在执行时 输入第二个值得时候程序出错 无法执行
首先申明 编译器没问题 麻烦看下 哪出问题了
搜索更多相关主题的帖子: next 
2011-05-26 18:27
jacksongod
Rank: 2
等 级:论坛游民
帖 子:32
专家分:82
注 册:2011-5-18
收藏
得分:9 
struct student *creat(void)
{
    struct student *head,*p[2];
    int n=0;

    printf("please input the first node date\n");
    head=(struct student *)malloc(LEN);
    scanf("%f%s%s%d",&p[0]->num,p[0]->name,p[0]->sex,&p[0]->age);
    p[0]=head;

    while(p[0]->next!=NULL)         //p[0]->next 有初始值吗
    {
       if(p[0]->num!=0)
        {
            p[1]=p[0];
            printf("please input the %d node\n",++n);
            p[0]=(struct student *)malloc(LEN);
            scanf("%f%s%s%d",&p[0]->num,p[0]->name,p[0]->sex,&p[0]->age);
            p[1]->next=p[0];      //这句和下面第3行的矛盾  你是反向插链表的吧后输入的会到链表前面
        }
        else p[0]->next=NULL;
        p[1]->next=NULL;        //怎么插好了 ->next 又变成NULL了
    }
  return head;      //head 一直是P[0],错了。 你新的head应该是最后输入那个node上面说了。。你在反向插链表


还有你main的前两行是daclaration吧应该在main外面。。既然你说编译没错那算了。。

还有。。你单词打错好多
}
2011-05-26 18:45
烟雾中的迷茫
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:621
专家分:1069
注 册:2011-2-9
收藏
得分:0 
好的谢谢 指教
2011-05-26 22:34
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:1 
看不懂。

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-05-26 22:38
快速回复:删除链表中输入的值
数据加载中...
 
   



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

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