线性表做学生通讯录

weixin_58077333 2021-05-10 09:07:52
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<malloc.h> typedef struct { char num[5]; char name[20]; char phone[10]; }studata; typedef struct node { studata data; struct node *next; }listnode,*linklist; void menu() { printf("\n***************************班级通讯录***********************************\n\n"); printf("1-建立通讯录 2-查看全部记录 3-查询 4-同学加入 5-同学离开 0-退出系统 \n\n"); printf("请选择(0-5):"); }linklist creatlist() {linklist head,s,p,q; char ch; head=(listnode*)malloc(sizeof(listnode)); if(!head) exit(-1); head->next=NULL; p=head; do{ s=(linklist)malloc(sizeof(listnode)); if(!s) exit(-1); printf("请输入同学的编号:"); scanf("%s",s->data.num); printf("请输入同学的姓名:"); scanf("%s",s->data.name) ; printf("请输入同学的电话号码:"); scanf("%s",s->data.phone) ; s->next=p->next; p->next=s; printf("\n继续添加吗?(y/n)"); scanf("\n%c",&ch); }while(ch=='y'||ch=='Y'); return head; } listnode * searchlist(linklist head) {listnode *p; int x; char num[5]; char name[20]; printf("1.按编号查询 2.按姓名查询\n"); printf("请选择:"); scanf("%d",&x); if(x==1) {printf("请输入待查同学的编号:"); scanf("%s",num); for(p=head;p!=NULL&&strcmp(p->data.num,num)!=0;p=p->next) ; } else if(x==2) { printf("请输入待查同学的姓名:"); scanf("%s",name); for(p=head;p!=NULL&&strcmp(p->data.name,name)!=0;p=p->next) ; } return p; } void insertlist(linklist head,listnode *p) {p->next=head->next; head->next=p;} void delnode(linklist head) {linklist p,q; char ch; p=searchlist(head); if(!p) { printf("没有找到符合条件的记录!\n"); return ; } printf("确定要删除吗?(y/n)"); scanf("\n%c",&ch); if(ch=='n'||ch=='N') return; for(q=head;q!=NULL&&q->next!=p;q=q->next) ; q->next=p->next; free(p); printf("删除成功!\n"); } void printlist(linklist head) {linklist p; p=head->next; printf("编号 姓名 电话号码\n"); while(p!=NULL) {printf("%s %s %s\n",p->data.num,p->data.name,p->data.phone); p=p->next; }} int main() {int t;linklist head,p; menu(); while(1) { scanf("%d",&t); switch(t) {case 1: printf("建立通讯录\n"); head=creatlist(); break; case 2: printf("查看全部记录\n"); printlist(head); break; case 3: printf("查询\n"); p=searchlist(head); if(p) {printf("编号 姓名 电话号码\n"); printf("%s %s %s\n",p->data.num,p->data.name,p->data.phone); } else printf("查无此人!\n"); break; case 4: printf("插入\n"); p=(listnode*)malloc(sizeof(listnode)); printf("请输入同学的编号:"); scanf("%s",p->data.num); printf("请输入同学的姓名:"); scanf("%s",p->data.name) ; printf("请输入同学的电话号码:"); scanf("%s",p->data.phone) ; insertlist(head,p); break; case 5: printf("删除\n"); delnode(head); break; case 0: printf("退出\n"); return 0; } } } 那位大神帮忙给注释一下,不胜感激 或者是能改进一下
...全文
1989 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qzjhjxj 2021-05-20
  • 打赏
  • 举报
回复
引用 3 楼 Lik0426 的回复:
这个程序改进的哪些请讲一下,谢谢了
在通讯录链表未建立前,如果选择 查看全部记录、查询、同学加入、同学离开 模块时,给出错误提示反馈并解决 因为错误而退出程序运行的问题。查询记录的代码,从主函数中改写到 searchlist(linklist head) 函数中,让代码的书写、逻辑更合理些。输入、输出上的一些小细节处理。
Lik0426 2021-05-19
  • 打赏
  • 举报
回复
这个程序改进的哪些请讲一下,谢谢了
Lik0426 2021-05-19
  • 打赏
  • 举报
回复
这个程序改进的哪些请讲一下,谢谢了
qzjhjxj 2021-05-10
  • 打赏
  • 举报
回复 1
修改如下,供参考:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct
{
    char num[5];
    char name[20];
    char phone[10];
}studata;

typedef struct node
{
    studata data;
    struct  node *next;
}listnode,*linklist;

void menu()
{
    printf("\n***************************班级通讯录***********************************\n\n");
    printf("1-建立通讯录  2-查看全部记录  3-查询  4-同学加入  5-同学离开  0-退出系统 \n\n");
    printf("请选择(0-5):");
}

linklist creatlist()
{
    linklist head,s,p,q;
    char ch;
    head=(listnode*)malloc(sizeof(listnode));
    if(!head) exit(-1);
    head->next=NULL;
    p=head;
    do{
        s=(linklist)malloc(sizeof(listnode));
        if(!s) exit(-1);
        printf("请输入同学的编号:");
        fflush(stdout);rewind(stdin);
        scanf("%s",s->data.num);
        printf("请输入同学的姓名:");
        fflush(stdout);rewind(stdin);
        scanf("%s",s->data.name) ;
        printf("请输入同学的电话号码:");
        fflush(stdout);rewind(stdin);
        scanf("%s",s->data.phone) ;
        s->next=p->next;
        p->next=s;
        printf("\n继续添加吗?(y/n)");
        fflush(stdout);rewind(stdin);
        scanf("%c",&ch);
    }while(ch=='y'||ch=='Y');
    return head;
}

listnode *  searchlist(linklist head)
{
    listnode *p;
    int x;
    char num[5];
    char name[20];
    if(head==NULL){
       printf("无记录,请先添加记录!\n");
       return NULL;
    }
    printf("1.按编号查询  2.按姓名查询\n");
    printf("请选择:");
    fflush(stdout);rewind(stdin);
    scanf("%d",&x);
    if(x==1)
    {
       printf("请输入待查同学的编号:");
       fflush(stdout);rewind(stdin);
       scanf("%s",num);
       for(p=head;p!=NULL&&strcmp(p->data.num,num)!=0;p=p->next);
    }
    else if(x==2)
    {
       printf("请输入待查同学的姓名:");
       fflush(stdout);rewind(stdin);
       scanf("%s",name);
       for(p=head;p!=NULL&&strcmp(p->data.name,name)!=0;p=p->next);
    }
    return p;
}
void insertlist(linklist head)
{
    listnode *p;
    if(head==NULL){
       printf("无记录,请先添加记录!\n");
       return;
    }
    printf("插入\n");
    p=(listnode*)malloc(sizeof(listnode));
    printf("请输入同学的编号:");
    fflush(stdout);rewind(stdin);
    scanf("%s",p->data.num);
    printf("请输入同学的姓名:");
    fflush(stdout);rewind(stdin);
    scanf("%s",p->data.name) ;
    printf("请输入同学的电话号码:");
    fflush(stdout);rewind(stdin);
    scanf("%s",p->data.phone) ;

    p->next=head->next;
    head->next=p;
}
void delnode(linklist head)
{
    linklist p,q;
    char ch;
    if(head==NULL){
       printf("无记录,请先添加记录!\n");
       return;
    }
    p=searchlist(head);
    if(!p)
    {
       printf("没有找到符合条件的记录!\n");
       return ;
    }
    printf("确定要删除吗?(y/n)");
    fflush(stdout);rewind(stdin);
    scanf("%c",&ch);
    if(ch=='n'||ch=='N') return;
    for(q=head;q!=NULL&&q->next!=p;q=q->next);
    q->next=p->next;
    free(p);
    printf("删除成功!\n");
}
void printlist(linklist head)
{
    linklist p;
    if(head==NULL){
       printf("无记录,请先添加记录!\n");
       return;
    }
    p=head->next;
    printf("编号   姓名    电话号码\n");
    while(p!=NULL)
    {
        printf("%s     %s       %s\n",p->data.num,p->data.name,p->data.phone);
        p=p->next;
    }
}

int main()
{
    int t;
    linklist head=NULL,p=NULL;
    menu();
    while(1)
    {
        scanf("%d",&t);
        switch(t)
        {
               case 1:
                      printf("建立通讯录\n");
                      head=creatlist();
                      break;
              case 2:
                      printf("查看全部记录\n");
                      printlist(head);
                      break;
              case 3:
                      printf("查询\n");
                      p=searchlist(head);
                      if(p)
                      {
                         printf("编号   姓名    电话号码\n");
                         printf("%s    %s      %s\n",p->data.num,p->data.name,p->data.phone);
                      }else printf("查无此人!\n");
                      break;
              case 4:
                      insertlist(head);
                      break;
              case 5:
                      printf("删除\n");
                      delnode(head);
                      break;
              case 0:
                      printf("退出\n");
                      return 0;
        }
    }
    
}

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧