| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1035 人关注过本帖
标题:关于删除链表中结点
只看楼主 加入收藏
高威
Rank: 1
等 级:新手上路
帖 子:18
专家分:7
注 册:2016-6-5
结帖率:66.67%
收藏
 问题点数:0 回复次数:2 
关于删除链表中结点
生成两个链表a,b,包括学号,姓名。将a中学号与b中重合的删除。
 我编了个程序,分为三部分,主函数int main(),生成函数creat(),删除函数delet(),输出函数print().
但执行时删除函数总是出错,找不到错在哪儿,希望指教。
#include<stdio.h>
#include<malloc.h>
#define N 5
struct student
{
    int num;
    char name[8];
    struct student *next;
};
int main()
{
    struct student *creat();
    struct student *delet(struct student *p1,struct student *p2);
    void print(struct student *p);
    struct student *p1, *p2, *p3;
    printf("please enter the first:\n");
    p1 = creat();
    printf("please enter the second:\n");
    p2 = creat();
    printf("The deleted:\n");
    p3=delet(p1, p2);
    print(p3);
    return 0;
}
struct student *creat()
{
    struct student *p1, *p2, *head;
    int i = 1;
    p1 = (struct student *)malloc(sizeof(struct student));
    scanf("%d %s", &p1->num, p1->name);
    head = p2 = p1;
    while(i !=N)
    {
        p1 = (struct student *)malloc(sizeof(struct student));
        scanf("%d %s", &p1->num, p1->name);
        p2->next = p1;
        p2 = p1;
        i++;
    }
    p2->next = NULL;
    return head;
}
struct student *delet(struct student *p1, struct student *p2)
{
    struct student *st1, *st2, *temp;
    st1 = p1;
    st2 = p2;
    while (st1!=NULL)
    {
        st2 = p2;
        while ((st1->num != st2->num)&&(st2!=NULL))
        {
            st2 = st2->next;
        }
        if (st1->num == st2->num)
        {
            if (st1 == p1)
            {
                p1 = st1->next;
            }
            else
            {
                temp->next = st1->next;
                st1 = st1->next;
            }
        }
        else
        {   
            temp = st1;
            st1 = st1->next;
        }
    }
    return p1;
}
void print(struct student *p)
{
    while (p != NULL)
    {
        printf("%d %s\n", p->num, p->name);
        p = p->next;
    }
}
搜索更多相关主题的帖子: 姓名 include 中学 
2016-10-09 22:01
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
改了一下,加入了free释放节点,还有就是st2!=NULL的顺序。因为先要确认st2!=NULL后面才能进行st1->num != st2->num的比较。你试试,现在可以了。
struct student *delet(struct student *p1, struct student *p2)
 {
     struct student *st1, *st2, *temp,*temp1;
     st1 = p1;
     st2 = p2;
     while (st1!=NULL)
     {
         st2 = p2;
         while ((st2!=NULL)&&(st1->num != st2->num))
         {
            
             st2 = st2->next;
         }
          if(st2==NULL)
         {   
             temp = st1;
             st1 = st1->next;
         }
         else if (st2!=NULL&&st1->num==st2->num)
         {
             if(st1==p1)
             {
                 temp=p1;
                st1=p1=st1->next;
                free(temp);
             }
             else
             {
                 temp->next=st1->next;
                 temp1=st1;
                 st1=st1->next;
                 free(temp1);
             }

         }

         
         
     }
     return p1;
 }
2016-10-09 23:40
高威
Rank: 1
等 级:新手上路
帖 子:18
专家分:7
注 册:2016-6-5
收藏
得分:0 
回复 2楼 ehszt
谢谢
2016-10-10 10:26
快速回复:关于删除链表中结点
数据加载中...
 
   



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

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