| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 318 人关注过本帖
标题:结构体及链表一个小问题
只看楼主 加入收藏
tank1901
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-1-6
收藏
 问题点数:0 回复次数:0 
结构体及链表一个小问题
下面的源程序的struct student结构体如果将学号numchar型改为int或者float型,后面的应该怎么改啊,我试过不过运行出错,麻烦大虾出手~~


#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>//包含system函数头文件
#define LEN sizeof(struct student)
struct student
{
        char num[20];//学号        char name[100];//用户姓名
        char phone[20];//电话号码
        char addr[100];//通讯地址
      
        struct student *next;
};
void menu(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");
        
}



//增添电子通讯录中的内容,即创建连表过程
struct student *create(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)
        {
                menu();
                printf("选择你需要操作的功能号码:");
                scanf("%d",&c);
                getchar();
                switch(c)
                {
                case 1:head=create(head);break;
                case 2:
                case 3:head=del(head);break;
                case 4:searchnum(head);break;
                case 5:searchname(head);break;
                case 6:searchphone(head);break;
                case 7:searchaddr(head);break;
      case 8:exit(0);break;
                default :printf("Enter error!!\n");        
                }
               
                printf("请按ENTER返回功能操作菜单\n");

                getchar();
                system("CLS");
        
        }
        
}
搜索更多相关主题的帖子: 电话号码 结构体 源程序 姓名 用户 
2011-01-06 12:45
快速回复:结构体及链表一个小问题
数据加载中...
 
   



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

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