| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1736 人关注过本帖
标题:【求助】用链表进行电话本查找删除的程序
只看楼主 加入收藏
mozarella
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2016-5-11
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:9 
【求助】用链表进行电话本查找删除的程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct link
{
    char name[20];
    char num[50];
    struct link *next;
};

struct link *Insert(struct link *head)          //插入输入的节点
{
    char name2[20];
    struct link *pr = head, *p = head;
    p = (struct link *)malloc(sizeof(struct link));
    p ->next = NULL;
    scanf("nsert %s %s behind %s",p->name,p->num,name2);
    int q;
    q = strcmp(name2,"Nobody");
    if (q == 0) head = p;
    else
    {
        q=strcmp(pr->name,name2);
        while (q!=0 && pr->next != NULL)
        {
            pr = pr->next;
            q=strcmp(pr->name,name2);
        }
        if (q == 0)
        {
            struct link *temp = NULL;
            temp = pr->next;
            pr->next = p;
            p->next = temp;
        }
    }
    return head;
}
struct link *Delete(struct link *head)   //删除节点
{
    char name2[20];
    scanf("elete %s",name2);
    struct link *p = head, *pr = head;
    int m;
    m=strcmp(p->name,name2);
    while(m != 0 && p->next != NULL);
    {
        pr = p;
        p = p->next;
        m = strcmp(p->name,name2);
    }
    if (m == 0)
    {
        if (p == head)
        {
            head = p->next;
        }
        else
        {
            pr->next = p->next;
        }
        free(p);
    }
    return head;
}
void Print(struct link *head)            //显示电话本
{
    struct link *p = head;
    while (p != NULL)
    {
        printf("%s ",p->name);
        printf("%s",p->num);
        printf("\n");
        p = p->next;
    }
    fflush(stdin);
}
void DELETE(struct link *head)              //清空内存
{
    struct link *p = head,  *pr = NULL;
    while (p != NULL)
    {
        pr = p;
        p = p->next;
        free(pr);
    }
}


int main()
{
    struct link *head = NULL;
    char order;
    scanf("%c",&order);
    while(order != 'E')
    {
        if (order == 'I')
        {
            head = Insert(head);
        }
        else
        {
            if (order == 'D')
            {
                head = Delete(head);
            }
            else
            {
                if( order == 'P')
                {
                    Print(head);
                }
            }
        }
        order = '\0';
        scanf("%c",&order);
    }
    DELETE(head);
    return 0;
}

用例为
Input:

Insert LiLei 14068888 behind Nobody
Insert Joker 04517999 behind LiLei
Insert Healer 987213 behind Joker
Print
Delete LiLei
Print
End

Output:

LiLei 14068888
Joker 04517999
Healer 987213
Joker 04517999
Healer 987213

2016-05-11 17:33
mozarella
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2016-5-11
收藏
得分:0 
用Delete函数的之后,程序就不能再往下进行了。
调试的时候提示运行时错误。
2016-05-11 17:35
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:20 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct link
{
    char name[20];
    char num[50];
    struct link *next;
};

struct link *Insert(struct link *head)          //插入输入的节点
{
    char name2[20];
    struct link *pr = head, *p = head;
    p = (struct link *)malloc(sizeof(struct link));
    p->next = NULL;
    scanf("nsert %s %s behind %s", p->name, p->num, name2);
    int q;
    q = strcmp(name2, "Nobody");
    if (q == 0) head = p;
    else
    {
        q = strcmp(pr->name, name2);
        while (q != 0 && pr->next != NULL)
        {
            pr = pr->next;
            q = strcmp(pr->name, name2);
        }
        if (q == 0)
        {
            struct link *temp = NULL;
            temp = pr->next;
            pr->next = p;
            p->next = temp;
        }
    }
    return head;
}
struct link *Delete(struct link *head)   //删除节点
{
    char name2[20];
    scanf("elete %s", name2);
    struct link *p = head, *pr = head;
    int m;
    m = strcmp(p->name, name2);
    while (m != 0 && p->next != NULL);
    {
        pr = p;
        p = p->next;
        m = strcmp(p->name, name2);
    }
    if (m == 0)
    {
        if (p == head)
        {
            head = p->next;
        }
        else
        {
            pr->next = p->next;
        }
        free(p);
    }
    return head;
}
void Print(struct link *head)            //显示电话本
{
    struct link *p = head;
    while (p != NULL)
    {
        printf("%s ", p->name);
        printf("%s", p->num);
        printf("\n");
        p = p->next;
    }
    fflush(stdin);
}
void DELETE(struct link *head)              //清空内存
{
    struct link *p = head, *pr = NULL;
    while (p != NULL)
    {
        pr = p;
        p = p->next;
        free(pr);
    }
}


int main()
{
    struct link *head = NULL;
    char order;
    scanf("%c", &order);
    while (order != 'E')
    {
        if (order == 'I')
        {
            head = Insert(head);
        }
        else
        {
            if (order == 'D')
            {
                head = Delete(head);
            }
            else
            {
                if (order == 'P')
                {
                    Print(head);
                }
            }
        }
        order = '\0';
        scanf(" %c", &order);//这里会收到缓冲区的回车,所以需要改一下
    }
    DELETE(head);
    return 0;
}
2016-05-11 17:41
mozarella
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2016-5-11
收藏
得分:0 
回复 3楼 grmmylbs
加了fflush(stdin)之后还是不行…
2016-05-11 17:45
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
Delete失效的问题:
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct link
{
    char name[20];
    char num[50];
    struct link *next;
};

struct link *Insert(struct link *head)          //插入输入的节点
{
    char name2[20];
    struct link *pr = head, *p = head;
    p = (struct link *)malloc(sizeof(struct link));
    p->next = NULL;
    scanf("nsert %s %s behind %s", p->name, p->num, name2);
    int q;
    q = strcmp(name2, "Nobody");
    if (q == 0) head = p;
    else
    {
        q = strcmp(pr->name, name2);
        while (q != 0 && pr->next != NULL)
        {
            pr = pr->next;
            q = strcmp(pr->name, name2);
        }
        if (q == 0)
        {
            struct link *temp = NULL;
            temp = pr->next;
            pr->next = p;
            p->next = temp;
        }
    }
    return head;
}
struct link *Delete(struct link *head)   //删除节点
{
    char name2[20];
    scanf("elete %s", name2);
    struct link *p = head, *pr = head;
    int m;
    m = strcmp(p->name, name2);
    while (m != 0)
    {
        pr = p;
        p = p->next;
        m = strcmp(p->name, name2);
        if (p->next == NULL)
        {
            break;
        }
    }
    if (m == 0)
    {
        if (p == head)
        {
            head = p->next;
        }
        else
        {
            pr->next = p->next;
        }
        free(p);
    }
    return head;
}
void Print(struct link *head)            //显示电话本
{
    struct link *p = head;
    while (p != NULL)
    {
        printf("%s ", p->name);
        printf("%s", p->num);
        printf("\n");
        p = p->next;
    }
    fflush(stdin);
}
void DELETE(struct link *head)              //清空内存
{
    struct link *p = head, *pr = NULL;
    while (p != NULL)
    {
        pr = p;
        p = p->next;
        free(pr);
    }
}


int main()
{
    struct link *head = NULL;
    char order;
    scanf("%c", &order);
    while (order != 'E')
    {
        if (order == 'I')
        {
            head = Insert(head);
        }
        else
        {
            if (order == 'D')
            {
                head = Delete(head);
            }
            else
            {
                if (order == 'P')
                {
                    Print(head);
                }
            }
        }
        order = '\0';
        scanf(" %c", &order);
    }
    DELETE(head);
    return 0;
}
2016-05-11 17:49
mozarella
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2016-5-11
收藏
得分:0 
回复 5楼 grmmylbs
谢谢大神!我用code blocks自己编译通过了,但是测试代码的系统编译器好像不太一样,一直显示运行时错误,请问可能是什么原因呢?
2016-05-11 18:01
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
测试的用例是什么?我试试。我只是用你之前给的用例试了。
2016-05-11 18:03
mozarella
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2016-5-11
收藏
得分:0 
回复 7楼 grmmylbs
我也不知道用例是什么老师不告诉……
2016-05-11 18:06
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
有没有可能
Insert LiLei 14068888 behind Nobody
 Delete LiLei
 Insert Joker 04517999 behind LiLei
这样的话判断起来比较麻烦了。各种条件纠缠在一起

程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct link
{
    char name[20];
    char num[50];
    struct link *next;
};

struct link *Insert(struct link *head)          //插入输入的节点
{
    char name2[20];
    struct link *pr = head, *p = head;
    p = (struct link *)malloc(sizeof(struct link));
    p->next = NULL;
    scanf("nsert %s %s behind %s", p->name, p->num, name2);
    int q;
    q = strcmp(name2, "Nobody");
    if (q == 0) head = p;
    else
    {
        q = strcmp(pr->name, name2);
        while (q != 0 && pr->next != NULL)
        {
            pr = pr->next;
            q = strcmp(pr->name, name2);
        }
        if (q == 0)
        {
            struct link *temp = NULL;
            temp = pr->next;
            pr->next = p;
            p->next = temp;
        }
    }
    return head;
}
struct link *Delete(struct link *head)   //删除节点
{
    char name2[20];
    scanf("elete %s", name2);
    struct link *p = head, *pr = head;
    int m;
    if (p == NULL)
    {
        return head;
    }
    m = strcmp(p->name, name2);
    while (m != 0)
    {
        pr = p;
        p = p->next;
        m = strcmp(p->name, name2);
        if (p->next == NULL)
        {
            break;
        }
    }
    if (m == 0)
    {
        if (p == head)
        {
            head = p->next;
        }
        else
        {
            pr->next = p->next;
        }
        free(p);
    }
    return head;
}
void Print(struct link *head)            //显示电话本
{
    struct link *p = head;
    while (p != NULL)
    {
        printf("%s ", p->name);
        printf("%s", p->num);
        printf("\n");
        p = p->next;
    }
    fflush(stdin);
}
void DELETE(struct link *head)              //清空内存
{
    struct link *p = head, *pr = NULL;
    while (p != NULL)
    {
        pr = p;
        p = p->next;
        free(pr);
    }
}


int main()
{
    struct link *head = NULL;
    char order;
    scanf("%c", &order);
    while (order != 'E')
    {
        if (order == 'I')
        {
            head = Insert(head);
        }
        else
        {
            if (order == 'D')
            {
                head = Delete(head);
            }
            else
            {
                if (order == 'P')
                {
                    Print(head);
                }
            }
        }
        order = '\0';
        scanf(" %c", &order);
    }
    DELETE(head);
    return 0;
}
2016-05-11 18:19
mozarella
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2016-5-11
收藏
得分:0 
回复 8楼 mozarella
系统提示我的是 程序执行了非法操作。一般是试图访问不可访问的内存,或试图执行无权执行的指令
2016-05-11 18:28
快速回复:【求助】用链表进行电话本查找删除的程序
数据加载中...
 
   



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

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