标题:C语言 函数以及链表问题
只看楼主
ldtiger
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-11-18
结帖率:100%
 问题点数:0 回复次数:0 
C语言 函数以及链表问题
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>//包含system函数头文件
#define LEN sizeof(struct student)
struct student
{
        char num[20];//ID号码
        char name[100];//用户姓名
        char phone[20];//电话号码
        char addr[100];//通讯地址
      
        struct student *next;
};
void face(void)//功能选择面板
{
        
  
        
        printf("\t\t\t1.增加联系人信息\n");
        printf("\t\t\t2.显示通讯录中记录\n");
        printf("\t\t\t3.删除联系人信息\n");
        printf("\t\t\t4.以学号查询信息\n");
        printf("\t\t\t5.以姓名查询信息\n");
        printf("\t\t\t6.以电话号码查询信息\n");
        printf("\t\t\t7.以地址查询所需的信息\n");
        printf("\t\t\t8.退出\n");
        
}
void print(struct student *head)
{
        struct student *p;
        p=head;
        system("CLS");//调用DOS命令CLS能够清屏
        if(head!=NULL)
                do
                {
                        printf("联系人学号:%s\n",p->num);
                        printf("联系人姓名:%s\n",p->name);
                        printf("联系人电话号码:%s\n",p->phone);
                        printf("学生地址:%s\n",p->addr);
                       
                  
                        p=p->next;
                }while(p!=NULL);
        else
        {
                printf("对不起,没有任何联系人记录!!\n\n");
               
        }
}

//增添电子通讯录中的内容,即创建连表过程
struct student *append(struct student *head)
{
        struct student *p0=NULL,*p1,*p2;//p0为要插入的新节点
        p1=head;
        p2=head;
        system("CLS");
                printf("\t\t创建并添加联系人信息");
                printf("\n");
                p0=(struct student *)malloc(LEN);
                printf("请输入联系人学号:");
                        gets(p0->num);
                printf("请输入联系人姓名:");
                        gets(p0->name);
                printf("请输入联系人电话号码:");
                        gets(p0->phone);
                printf("请输入联系人地址:");
                        gets(p0->addr);
               
        //对插入的节点排序,按姓名的拼音顺序
                if(head==NULL)
                {head=p0;p0->next=NULL;}
                else
                {
                        while((strcmp(p0->name,p1->name)>0)&&(p1->next!=NULL))
                                {p2=p1;p1=p1->next;}
                        if((strcmp(p0->name,p1->name))<=0)
                        {
                                if(head==p1)
                                        head=p0;
                                else
                                        p2->next=p0;
                                p0->next=p1;
                        }
                        else
                                {p1->next=p0;p0->next=NULL;}
                printf("添加了联系人信息成功");
      
                printf("\n\n");
        
        }
        return(head);
        
}
//电子通讯录的维护(删除),通过输入联系人ID号码删除联系人数据
struct student *del(struct student *head)
{
        struct student *p1,*p2;
        char num[12];
        system("CLS");
        printf("用户信息记录删除功能 \n");
      
        printf("输入要删除的联系人学号:");
        gets(num);
        p1=head;
        if (head==NULL)
                {
                        printf("对不起,没有任何联系人纪录!!\n");
                        
                        return(head);
                }
        while(p1!=NULL)
        {
                if(strcmp(p1->num,num)==0)
                {
                        if(p1==head)
                        head=p1->next;
                        else p2->next=p1->next;
                        free(p1);
                        printf("删除记录成功!!\n");
                        return(head);
                }
                p2=p1;
                p1=p1->next;
        }
        printf("对不起,没有要删除的联系人纪录!!\n");
        return(head);
}
//电子通讯录的查找,关键字为用户姓名;
void searchnum(struct student *head)
{
        struct student *p1,*p2;
        char num[20];
        p1=head;
        p2=p1;
        system("CLS");
           
        
        
        printf("输入要查找联系人的学号:");
        gets(num);
        while(p1!=NULL)
        {
                if(strcmp(p1->num,num)==0)
                {
                        printf("联系人学号:");
                                puts(p1->num);
                        printf("联系人姓名:");
                                puts(p1->name);
                        printf("联系人电话号码:");
                                puts(p1->phone);
                        printf("联系人地址:");
                                puts(p1->addr);
                           
                                break;
                }
                p2=p1;
                p1=p1->next;
        }
        if(p1==NULL)
                printf("对不起,没有该联系人的纪录\n");
        
}

void searchname(struct student *head)
{
        struct student *p1,*p2;
        char name[20];
        p1=head;
        p2=p1;
        system("CLS");
           
      
        
        printf("输入要查找联系人的姓名:");
        gets(name);
        while(p1!=NULL)
        {
                if(strcmp(p1->name,name)==0)
                {
                        printf("联系人ID号码:");
                                puts(p1->num);
                        printf("联系人姓名:");
                                puts(p1->name);
                        printf("联系人电话号码:");
                                puts(p1->phone);
                        printf("联系人地址:");
                                puts(p1->addr);
                           
                                break;
                }
                p2=p1;
                p1=p1->next;
        }
        if(p1==NULL)
              printf("对不起,没有该联系人的纪录!!\n");

  
}





void searchphone(struct student *head)
{
        struct student *p1,*p2;
        char phone[20];
        p1=head;
        p2=p1;
        system("CLS");
           
        
        
        printf("输入要查找联系人的电话号码:");
        gets(phone);
        while(p1!=NULL)
        {
                if(strcmp(p1->phone,phone)==0)
                {
                        printf("联系人学号:");
                                puts(p1->num);
                        printf("联系人姓名:");
                                puts(p1->name);
                        printf("联系人电话号码:");
                                puts(p1->phone);
                        printf("联系人地址:");
                                puts(p1->addr);
                        
                        printf("\n");        
                                break;
                }
                p2=p1;
                p1=p1->next;
        }
        if(p1==NULL)
                printf("对不起,没有该联系人的纪录!!\n");
        
}

void searchaddr(struct student *head)
{
        struct student *p1,*p2;
        char addr[20];
        p1=head;
        p2=p1;
        system("CLS");
           
        
        
        printf("输入要查找联系人的地址:");
        gets(addr);
        while(p1!=NULL)
        {
                if(strcmp(p1->addr,addr)==0)
                {
                        printf("联系人学号:");
                                puts(p1->num);
                        printf("联系人姓名:");
                                puts(p1->name);
                        printf("联系人电话号码:");
                                puts(p1->phone);
                        printf("联系人地址:");
                                puts(p1->addr);
                           
                                break;
                }
                p2=p1;
                p1=p1->next;
        }
        if(p1==NULL)
                printf("对不起,没有该联系人的纪录!!\n");
        
}


main()
{
        
        int c;//功能选择需要的号码
   
        struct student *head=NULL;
        
        while(1)
        {
                face();
                printf("选择你需要操作的功能号码:");
                scanf("%d",&c);
                getchar();
                switch(c)
                {
                case 1:head=append(head);break;
                case 2:print(head);break;
                case 3:head=del(head);break;
                case 4:searchnum(head);break;
                case 5:searchname(head);head;
                case 6:searchphone(head);head;
                case 7:searchaddr(head);head;
    case 8:exit(0);break;
                default :printf("Enter error!!\n");        
                }
               
                printf("请按ENTER返回功能操作菜单\n");
                getchar();
                system("CLS");
        
        }
        
}


为什么当我查询名字的时候,查询不到时会直接跳到查询号码状态,如果我一开始查询学号时就不会这样,而是会返回菜单,如何修改才能把所有查询(查询不到时)都返回菜单,而不是跳到执行下一个查询功能,另外问下如何加入一个修改功能的链表函数~麻烦大虾们提供详细解决方案,谢了哈~
搜索更多相关主题的帖子: 电话号码 通讯录 联系人 C语言 姓名 
2011-01-01 23:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-330427-1-1.html




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

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