| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 357 人关注过本帖
标题:链表排序出错了 谁帮看一下 多谢!
只看楼主 加入收藏
迷城
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-3-15
收藏
 问题点数:0 回复次数:0 
链表排序出错了 谁帮看一下 多谢!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define LEN sizeof(struct person)
struct person
{   
    char name[10];
    char position[10];
    char positionname[10];
    char telephone[10];
    char Email[10];
    struct person *next;
};
static int sum=0;
int print()
{
    int i;
    printf("1   输入联系人\n");
    printf("2   删除联系人\n");
    printf("3   查找联系人\n");
    printf("4   修改联系人\n");
    printf("5   排序输出联系人\n");
    printf("6   退出\n");
    printf("请选择:");
    scanf("%d",&i);
    return i;
}
int print2()
{
    int i;
    printf("1 按姓名查找\n");
    printf("2 按职务查找\n");
    printf("3 按职称查找\n");
    printf("4 按电话查找\n");
    printf("5 按Email查找\n");
    printf("6 返回\n");
    printf("请选择:");
    scanf("%d",&i);
    return i;
}
struct person *input(struct person *head)
{
    int n;
    struct person *p1,*p2;
    p1=(struct person *)malloc(LEN);
    p2=head;
    while(true)
    {
        if(head==NULL)
        {
            head=p2=p1;
            break;
        }
        else if(p2->next!=NULL)
        {
            p2=p2->next;
            if(p2->next==NULL)
            {
                p2->next=p1;
                p2=p1;
                break;
            }
        }
        else
        {
            p2->next=p1;
            p2=p1;
            break;
        }
    }
    while(true)
    {
        printf("请输入联系人基本信息:\n");
        printf("姓名:");
        scanf("%s",p1->name);
        printf("职务:");
        scanf("%s",p1->position);
        printf("职称:");
        scanf("%s",p1->positionname);
        printf("电话:");
        scanf("%s",p1->telephone);
        printf("电子邮件:");
        scanf("%s",p1->Email);
        sum=sum+1;
        printf("已存储人数为:%d\n",sum);
        printf("是否继续添加\n 1 是 2 否 ");
        scanf("%d",&n);
        if(n==2)
        {
            p1->next=NULL;
            printf("输入完毕!\n");
            break;
        }
        p1=(struct person *)malloc(LEN);
        p2->next=p1;
        p2=p1;
    }
    p2->next=NULL;
    p1=p2=NULL;
    printf("任意键返回");
    getch();
    return head;
}
struct person *del(struct person *head)
{
    struct person *p1,*p2;
    char name[10];
    int a=0;
    p1=p2=head;
    if(head==NULL)  printf("存储人数为零\n");
    else
    {
        printf("请输入将要删除联系人的姓名:");
        scanf("%s",name);
        if(strcmp(p1->name,name)==0)
        {
            a=1;
            if(p1->next!=NULL)
            {
                head=p1->next;
                printf("已删除\n");
                sum=sum-1;
                printf("已存储人数为:%d\n",sum);
            }
            else
            {
                head=NULL;
                printf("已删除,目前存储人数为零\n");
            }
        }
        else
        {      
            while(p1->next!=NULL)
            {
                p2=p1->next;
                if(strcmp(p2->name,name)==0)
                {
                    a=1;
                    if(p2->next!=NULL) p1->next=p2->next;
                    else p1->next=NULL;   
                    printf("已删除\n");
                    sum=sum-1;
                    printf("已存储人数为:%d\n",sum);
                    break;
                }
                if(p2->next!=NULL)
                {
                    p1=p2->next;
                    if(strcmp(p1->name,name)==0)
                    {
                        a=1;
                        if(p1->next!=NULL) p2->next=p1->next;
                        else p2->next=NULL;
                        sum=sum-1;
                        printf("已存储人数为:%d\n",sum);
                        break;
                    }
                }
                else break;
            }
        }
        if(a==0) printf("未找到\n");
    }
    p1=p2=NULL;
    printf("任意键返回");
    getch();
    return head;
}
void chazhao1(struct person *head)
{
    char name[10];
    int a=0;
    struct person *p;
    printf("请输入您要查找的联系人姓名:");
    scanf("%s",name);
    for(p=head;;p=p->next)
    {
        if(strcmp(p->name,name)==0)
        {
            a=a+1;
            printf("姓名:%s\n",p->name);
            printf("职务:%s\n",p->position);
            printf("职称:%s\n",p->positionname);
            printf("电话:%s\n",p->telephone);
            printf("Email:%s\n",p->Email);
            break;
        }
        if(p->next==NULL) break;
    }
    if(a==0)  printf("未找到联系人信息\n");
    printf("任意键返回");
    getch();
}
void chazhao2(struct person *head)
{   
    char position[10];
    int a=0;
    struct person *p;
    printf("请输入您要查找的联系人职务:");
    scanf("%s",position);
    for(p=head;;p=p->next)
    {
        if(strcmp(p->position,position)==0)
        {
            a=a+1;
            printf("姓名:%s\n",p->name);
            printf("职务:%s\n",p->position);
            printf("职称:%s\n",p->positionname);
            printf("电话:%s\n",p->telephone);
            printf("Email:%s\n",p->Email);
            break;
        }
        if(p->next==NULL) break;
    }
    if(a==0)  printf("未找到联系人信息\n");
    head=p=NULL;
    printf("任意键返回");
    getch();
}
void chazhao3(struct person *head)
{
    char positionname[10];
    int a=0;
    struct person *p;
    printf("请输入您要查找的联系人职称:");
    scanf("%s",positionname);
    for(p=head;;p=p->next)
    {
        if(strcmp(p->positionname,positionname)==0)
        {
            a=a+1;
            printf("姓名:%s\n",p->name);
            printf("职务:%s\n",p->position);
            printf("职称:%s\n",p->positionname);
            printf("电话:%s\n",p->telephone);
            printf("Email:%s\n",p->Email);
            break;
        }
        if(p->next==NULL) break;   
    }
    if(a==0)  printf("未找到联系人信息\n");
    head=p=NULL;
    printf("任意键返回");
    getch();
}
void chazhao4(struct person *head)
{
    char telephone[10];
    int a=0;
    struct person *p;
    printf("请输入您要查找的联系人电话:");
    scanf("%s",telephone);
    for(p=head;;p=p->next)
    {
        if(strcmp(p->telephone,telephone)==0)
        {
            a=a+1;
            printf("姓名:%s\n",p->name);
            printf("职务:%s\n",p->position);
            printf("职称:%s\n",p->positionname);
            printf("电话:%s\n",p->telephone);
            printf("Email:%s\n",p->Email);
            break;
        }
        if(p->next==NULL) break;
    }
    if(a==0)  printf("未找到联系人信息\n");
    head=p=NULL;
    printf("任意键返回");
    getch();
}
void chazhao5(struct person *head)
{
    char Email[10];
    int a=0;
    struct person *p;
    p=head;
    printf("请输入您要查找的联系人Email:");
    scanf("%s",Email);
    for(;;p=p->next)
    {
        if(strcmp(p->Email,Email)==0)
        {
            a=a+1;                             
            printf("姓名:%s\n",p->name);                     
            printf("职务:%s\n",p->position);
            printf("职称:%s\n",p->positionname);
            printf("电话:%s\n",p->telephone);
            printf("Email:%s\n",p->Email);
            break;
        }
        if(p->next==NULL) break;
    }      
    if(a==0)  printf("未找到联系人信息\n");
    head=p=NULL;
    printf("任意键返回");
    getch();
}
void chazhao(struct person *head)
{
    int i=0;
    if(head==NULL)  printf("存储人数为零\n");
    else
    {
        while(i!=6)
        {
            i=print2();
            switch(i)
            {
            case 1:system("cls");chazhao1(head);system("cls");break;                                                           
            case 2:system("cls");chazhao2(head);system("cls");break;
            case 3:system("cls");chazhao3(head);system("cls");break;
            case 4:system("cls");chazhao4(head);system("cls");break;
            case 5:system("cls");chazhao5(head);system("cls");break;
            case 6:break;
            }
        }
    }
    printf("任意键返回");
    getch();
}
void change(struct person *head)
{
    char name[10];
    int  a=0;
    struct person *p;
    if(head==NULL)  printf("存储人数为零\n");
    else
    {
        printf("请输入修改人姓名:\n");
        scanf("%s",name);
        for(p=head;;p=p->next)
        {
            if(strcmp(p->name,name)==0)  
            {
                a=1;
                printf("请重新输入联系人信息:\n");
                printf("姓名:");
                scanf("%s",p->name);
                printf("职务:");
                scanf("%s",p->position);
                printf("职称:");
                scanf("%s",p->positionname);
                printf("电话:");
                scanf("%s",p->telephone);
                printf("电子邮件:");
                scanf("%s",p->Email);
                printf("已修改\n");                                                            
                break;               
            }
            if(p->next==NULL) break;
        }               
        if(a==0)  printf("未找到联系人!");   
    }
    p=NULL;            
    printf("任意键返回");               
    getch();        
}                          
struct person *output(struct person *head)
{                              
    struct person *p,*min,*head1,*p1,*p2;
    int n=0,m;
    m=sum;
    if(m==0)  
    {
        head1=NULL;
        printf("存储人数为零\n");
        printf("任意键返回");
        getch();
        return head1;
    }
    else
    {
        if(m==1)  
        {
            head1=head;
            p=head1;
        }
        else   
        {
            for(;;)
            {
                min=head;
                if(m==1)  
                {
                    p->next=head;
                    p=head;
                    break;
                }
                for(p1=head,p2=head;;)
                {  
                    if(strcmp(p1->next->name,min->name)<0)
                    {
                        p2=p1;
                        min=p2->next;   
                        if(p1->next->next==NULL)  break;
                        p1=p1->next;
                    }
                    else
                    {
                        if(p1->next->next==NULL)  break;
                        p2=p1;
                        p1=p1->next;
                    }
                }
                n=n+1;
                m=m-1;
                if(n==1)
                {
                    head1=min;
                    p=min;
                }
                else
                {
                    p->next=min;
                    p=p->next;
                }
                if(min==head) head=head->next;
                else if(min->next==NULL) p2->next=NULL;
                else p2->next=min->next;   
            }
        }
        p->next=NULL;
        p=head1;
        printf("姓名      职务      职称      电话      电子邮件\n");
        printf("%-10s%-10s%-10s%-10s%-10s\n",p->name,p->position,p->positionname,p->telephone,p->Email);
        while(p->next!=NULL)
        {
            p=p->next;
            printf("%-10s%-10s%-10s%-10s%-10s\n",p->name,p->position,p->positionname,p->telephone,p->Email);
        }
        printf("任意键返回");
        getch();
        return head1;
    }
}
int main()
{
    int n,i=0;
    struct person *head;
    head=NULL;
    printf("欢迎使用h通讯录!\n");
    printf("是否开始,请选择: 1 是   2 否\n");
    scanf("%d",&n);
    switch (n)
    {
    case 1: system("cls");break;
    case 2: return 0;
    }
    while(i!=6)
    {
        i=print();
        switch(i)
        {
        case 1:system("cls");head=input(head);system("cls");break;
        case 2:system("cls");head=del(head);system("cls");break;
        case 3:system("cls");chazhao(head);system("cls");break;
        case 4:system("cls");change(head);system("cls");break;
        case 5:system("cls");head=output(head);system("cls");break;
        case 6:return 0;
        }
    }
    head=NULL;
    return 0;
}                  
搜索更多相关主题的帖子: name next telephone position include 
2012-03-15 16:33
快速回复:链表排序出错了 谁帮看一下 多谢!
数据加载中...
 
   



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

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