链表写通讯录

来源:互联网 发布:英国的青年知乎 编辑:程序博客网 时间:2024/05/22 06:31

主函数:main.c

/*****************************************************copyright (C), 2016-2017, Lighting Studio. Co.,     Ltd. File name:Author:ZXM_wobrm    Version:0.1    Date:2017.01.19 Description:Funcion List: *****************************************************/#include "../../include/myhead.h"int main(){TYPE *p;TYPE *pp;TYPE *pin;//插入的指针int sn;//输入的搜索int dn;//输入的删除int flag=0;//标记//int flags;//输入的标记char ch = '*';//继续程序标记//int ch2=0;int j;while(ch=='*'){if(flag==0){//con_format();/*printf("\t\t\t1 : 创建新表\n\t\t\t2 : 输出表中数据\n\t\t\t3 :搜索表中数据\n\t\t\t4 : 删除表中数据\n\t\t\t5 : 在表中插入数据\n\t\t\t6 : 退出!\n");*/printf("\n\n\n\n");printf("\t\t");printf("请输入下列数字执行命令\n\t\t");for(j=0;j<k;j++){printf("**");}printf("\n");printf("\t\t1  |  创建通讯录!  |\n\t\t");for(j=0;j<k;j++){printf("**");}printf("\n");printf("\t\t2  |  打印通讯录!  |\n\t\t");for(j=0;j<k;j++){printf("**");}printf("\n");printf("\t\t3  |  搜索通讯录!  |\n\t\t");for(j=0;j<k;j++){printf("**");}printf("\n");printf("\t\t4  |  删除通讯录!  |\n\t\t");for(j=0;j<k;j++){printf("**");}printf("\n");printf("\t\t5  |  插入通讯录!  |\n\t\t");for(j=0;j<k;j++){printf("**");}printf("\n");printf("\t\t6  |  排序通讯录!(未)  |\n\t\t");for(j=0;j<k;j++){printf("**");}printf("\n");printf("\t\t7  |  退出通讯录!  |\t\t\n");printf("\n\n\n\n");scanf("%d",&flag);}else if(flag==1){p=creat();printf("\n\n");printf("\t\t创建成功!\n");//print(p)flag=0;}else if(flag==2){print(p);flag=0;}else if(flag==3){printf("input need scarch number:\n");scanf("%d",&sn);search(p,sn);//print(p);flag=0;}else if(flag==4){printf("input need delete number:\n");scanf("%d",&dn);pp=del(p,dn);print(pp);flag=0;}else if(flag==5){printf("insert number name and phonenum:\n");pin = (TYPE *) malloc(len);scanf("%d %s %s",&pin->number,pin->name,pin->phonenum);insert(p,pin);print(p);flag=0;}else if(flag==7){ch='#';}//////if(flag!=1&&flag!=2&&flag!=3&&flag!=4&&flag!=5&&flag!=6)else //if(flag==7){//printf("error!输入*号继续,输入其他字符退出!\n");//fflush(stdin);//ch='#';scanf("%c",&ch);flag=0;}//printf("number    phonenum  \n");//printf("%3d\t%5.1f\n",p->number,p->phonenum);}/*printf("是否退出!输入*继续,输入其他键退出!\n");scanf("%c",&ch);if(ch=='*'){goto jx;}else*/    return 0;}


头文件include.h

/* * myhead.h * *  Created on: 2017-1-20 *      Author: zxm_wobrm */#ifndef MYHEAD_H_#define MYHEAD_H_#include <stdlib.h>#include <stdio.h>#include <string.h>#define TYPE struct stu#define TYPE_SOAT struct stu_soat#define len sizeof(struct stu)#define len_soat sizeof(TYPE_SOAT)#define k 11  //格式控制struct stu   //通讯录结构体{int number;char name[20];char phonenum[20];TYPE *next;};struct stu_soat  //按name排序{int number;char name[20];char phonenum[20];TYPE *next;};int n;  //全局变量,存放通讯录人数void con_format(void);  //输出命令TYPE *creat(void);  //创建通讯录void print(TYPE *head);  //打印通讯录TYPE *search(TYPE *head,int ns); //搜索通讯录TYPE *del(TYPE *head,int dn); //删除通讯录TYPE *insert(TYPE *head,TYPE *pi); //插入通讯录TYPE_SOAT *name_soat(TYPE *head);  //按name排序#endif /* MYHEAD_H_ */


创建通讯录 creat

#include "../../include/myhead.h"TYPE * creat(void)//创建链表{TYPE *head;TYPE *p1,*p2;n=0;p1=p2=(TYPE * )malloc(len);printf("input the first  number name and phonenum:\n");scanf("%d %s %s",&p1->number,p1->name,p1->phonenum);head = NULL;while(p1->number!=0){n++;if(n==1)head = p1;elsep2->next=p1;p2=p1;p1 = (TYPE * )malloc(len);printf("input other number name and phonenum:\n");scanf("%d %s %s",&p1->number,p1->name,p1->phonenum);//printf("\t\tcreat successful!\n")}p2->next = NULL;return (head);}

搜索链表 search


#include "../../include/myhead.h"TYPE *search(TYPE *head,int ns)//搜索链表{TYPE *ps;int is=0;int j;ps=head;if(ps->number==ns){printf("\t\tThis is the %dth contact and information is:\n",is+1);printf("\t\tn   number   name   phonenumber\n\t\t");for(j=0;j<k+8;j++){printf("**");}printf("\n");printf("\t\t%d  |  %d  |  %s  |  %s  |\n",is+1,ps->number,ps->name,ps->phonenum);//return ps;}while(ps->number!=ns&&ps->next!=NULL){is++;ps=ps->next;if(ps->number==ns){printf("\t\tThis is the %dth contact and information is:\n",is+1);printf("\t\tn   number   name   phonenumber\n\t\t");for(j=0;j<k+8;j++){printf("**");}printf("\n");printf("\t\t%d  |  %d  |  %s  |  %s  |\n",is+1,ps->number,ps->name,ps->phonenum);//return ps;}if(ps->number!=ns&&ps->next==NULL){printf("node %d had no been found\n",ns);}}}


删除通讯录 delete


#include "../../include/myhead.h"TYPE *del(TYPE *head,int dn){TYPE *pf,*pb;//int f=0;if(head==NULL){printf("This is empty\n");goto end;//f=1;}//if(f==0)//{pb=head;while(pb->number!=dn&&pb->next!=NULL){pf=pb;pb=pb->next;}if(pb->number==dn){if(pb==head){head=pb->next;}else {pf->next=pb->next;}free(pb);n-=1;printf("\t\t delete successful !\n");}else printf("\t\tThe node not been found !\n");//}end: return head;}

插入通讯录 insert


#include "../../include/myhead.h"TYPE * insert(TYPE *head,TYPE *pi){TYPE *pf,*pb;pb=head;n++;if(head==NULL){head=pi;pi->next=NULL;}else{while((pi->number>pb->number)&&(pb->next!=NULL)){pf=pb;pb=pb->next;}if(pi->number<=pb->number){if(head==pb)head=pi;else pf->next=pi;pi->next=pb;}else{pb->next=pi;pi->next=NULL;}}return head;}

输出通讯录 print

#include "../../include/myhead.h"void print(TYPE *head)//输出链表{//TYPE *p;int i=1,j=0;printf("\n\n\t\t%d information in the address list: \n\n",n);printf("\t\tn   number   name   phonenumber\n\t\t");for(j=0;j<k+8;j++){printf("**");}printf("\n");//p=head;//if(head!=0)//{while(head!=NULL){printf("\t\t%d  |  %d  |  %s  |  %s  |  \n\t\t",i++,head->number,head->name,head->phonenum);head=head->next;for(j=0;j<k+8;j++){printf("**");}printf("\n");}//}/*for(j=0;j<50;j++){printf("**");}*/}

效果图:














0 0
原创粉丝点击