本人由于刚学数据结构,求各位大神帮帮忙。。。用C语言创建一个班级通讯录
(1)可以实现插入一个同学的通讯录记录;
(2)能够删除某位同学的通讯录;
(3)对通讯录打印输出。
3、实验要求
(1)定义通讯录内容的结构体;
(2)建立存储通讯录的链表结构并初始化;
(3)建立主函数:
1)建立录入函数(返回主界面)
2)建立插入函数(返回主界面)
3)建立删除函数(返回主界面)
4)建立输出和打印函数(返回主界面)
I)通过循环对所有成员记录输出
II)输出指定姓名的某个同学的通讯录记录
5)退出
[解决办法]
- C/C++ code
#include <stdio.h>#include <stdlib.h>#include<string.h>typedef struct student { char name[10]; int num; struct student * next;}STU;STU *creat(int n); //创建链表STU *look_for(STU *head,int m); //寻找结点STU * del_point(STU *head,int m); //删除结点 STU *insert_point(STU *head,STU *pi); //插入结点void link_free(STU *head); //void print_all(STU *head); //显示链表STU *look_name(STU *head,char *name);int main(){ int num,num1; char look_name1[10]; STU * head=NULL; STU *pb,*p; STU *pi; pi=(STU *)malloc(sizeof(STU)); head=creat(5); print_all(head); //============look_for(STU *head,int m)测试模块===== printf("please input the number you look for.\n"); scanf("%d",&num); pb=look_for(head,num); if(pb!=NULL) printf("search the number:%d name: %s \n",pb->num,pb->name); else printf("can not find the number %d\n",num); //==============look_name(STU *head,char name[])测试模块===== printf("please input the name what you want look for\n"); scanf("%s",look_name1); p=look_name(head,look_name1); if(p!=NULL) printf("search the number:%d name: %s \n",pb->num,pb->name); else printf("can not find the name %d\n",num); //===========del_point(STU *head,int m)测试模块============= printf("the num you want del\n"); scanf("%d",&num1); head=del_point(head,num1); print_all(head); //============insert_point(STU *head,STU *pi)测试模块====== printf("please input insert pi->num pi->age\n"); scanf("%d %s",&pi->num,&pi->name); pi->next=NULL; head=insert_point(head,pi); print_all(head); link_free(head); return 0;}/*===============链表创建函数=============================*/STU *creat(int n) //创建链表{ int i; STU * head,*pb,*pf; for(i=0;i<n;i++) { pb=(STU *)malloc(sizeof(STU)); printf("please input num name:\n"); scanf("%d %s",&(pb->num),&(pb->name)); getchar(); if(i==0) { head=pf=pb; } else { pf->next=pb; pf=pb; } } pb->next=NULL; return head;}/*=======================================================*//*===============寻找结点函数=============================*/STU *look_for(STU *head,int m) //寻找结点{ STU *pf; pf=head; while(pf!=NULL) { if(pf->num==m) return pf; else pf=pf->next; } return NULL;}/*==========================================================*//*===============结点删除函数===============================*/STU * del_point(STU *head,int m) //删除结点{ STU *pb,*pf; pb=pf=head; if(head==NULL) { printf("link is empty\n"); return NULL; } while((pb->next!=NULL)&&(pb->num!=m)) //寻找 所要删除的结点 { pf=pb; pb=pb->next; } if(pb->num==m) //找到了 所要删除的结点 { if(pb==head) //所删除的是 头结点 { head=head->next; } else // 所删除的是普通结点 { pf->next=pb->next; } free(pb); //释放所删除结点的 空间 } else { printf("no this point.\n"); } return head;}/*================遍历链表函数===============================*/void print_all(STU *head) //显示 链表 元素{ STU *pb; pb=head; while(pb!=NULL) { printf("num=:%d, name=:%s\n",pb->num,pb->name); pb=pb->next; }}/*=============================================================*//*===============结点插入函数==================================*/STU *insert_point(STU *head,STU *pi) //插入结点{ STU *pf,*pb; pb=pf=head; if(head==NULL) //空链表 插入 { head=pi; head->next=NULL; return head; } while((pb->next!=NULL)&&(pb->num<pi->num)) //比较大小 按顺序插入 { pf=pb; pb=pb->next; } if(pb->num>=pi->num) //找到了所要插入的位置 (从小到大) { if(pb==head) //插入到 链表 的 头 { head=pi; pi->next=head; } else { //插入到普通位置 pf->next=pi; pi->next=pb; } } else //插入到链表的 尾 { pb->next=pi; pi->next=NULL; } return head;}/*=============================================================*/void link_free(STU *head){ STU *pb; pb=head; while(head!=NULL) { pb=head; head=head->next; free(pb); }}//==============look_name()函数========STU *look_name(STU *head,char *name) //寻找结点{ STU *pf; pf=head; while(pf!=NULL) { if(strcmp(pf->name,name)==0) return pf; else pf=pf->next; } return NULL;}