| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1655 人关注过本帖
标题:关于删除链表中结点
只看楼主 加入收藏
高威
Rank: 1
等 级:新手上路
帖 子:18
专家分:7
注 册:2016-6-5
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:1 
关于删除链表中结点
生成两个链表a,b,包括学号,姓名。将a中学号与b中重合的删除。
我编了个程序,分为三部分,主函数int main(),生成函数creat(),删除函数del(),输出函数print().
但执行时删除函数总是出错,找不到错在哪儿,希望指教。
#include<stdio.h>
#include<malloc.h>
#define N 5
struct student
{
    int num;
    char name[20];
    struct student *next;
};
int main()
{
    struct student *creat();
    struct student *delete(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 = delete(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 *delete(struct student *p1, struct student *p2)
{
    struct student *st1, *st2,*temp;
    st1 = p1;
    st2 = p2;
    temp = p1;
    while (st1!=NULL)
    {
        st2 = p2;
        while ((st1->num != st2->num)&&(st2!=NULL))
        {
            st2 = st2->next;
        }
        if (st1->num == st2->num)
        {
            if (st1 == p1)
            {
                st1 = st1->next;
                p1 = st1;
            }
            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-09-29 23:54
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:20 
//  st1->num != NULL->num && NULL != NULL
while ((st1->num != st2->num)&&(st2!=NULL))
{
  st2 = st2->next;
}

// 上面的循环结束后,st2 = NULL
if (st1->num == st2->num)
      

—>〉Sun〈<—
2016-09-30 03:58
快速回复:关于删除链表中结点
数据加载中...
 
   



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

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