国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

通訊錄(鏈表、文件)

2019-11-11 07:47:01
字體:
供稿:網(wǎng)友
#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<string.h>#define len sizeof(struct Node)#define ERROR   1#define OK      0struct Node{char name[30];    int data;    struct Node *next;};typedef struct Node  LinkList; /* 定義LinkList *//* 初始化順序線性表生成一個(gè)頭返回head */LinkList * InitList() { LinkList *head;   head=(LinkList *)malloc(len);  /* 產(chǎn)生頭結(jié)點(diǎn),并使L指向此頭結(jié)點(diǎn) */   if(head==NULL)     /* 存儲(chǔ)分配失敗 */    PRintf("malloc is not good");   head->next=NULL;   /* 指針域?yàn)榭?*/   return head;}/*數(shù)據(jù)的輸出打印*/void print(LinkList *head){LinkList *p;p=head->next;if(p==NULL)printf("this is empty/n");while(p!=NULL){printf("姓名: %-10s 號(hào)碼: %-10d/n",p->name,p->data);p=p->next;}}/*  初始化后   數(shù)據(jù)添加   采用(頭插法) */LinkList * CreateListHead(LinkList *head) {printf("*表示輸入結(jié)束/n");LinkList *p;int i;//head = (LinkList *)malloc(len);//head->next = NULL;            /*  先建立一個(gè)帶頭結(jié)點(diǎn)的單鏈表 *///p=head->next;//////////////while(1){p = (LinkList *)malloc(len); /*  生成新結(jié)點(diǎn) */printf("請(qǐng)輸入姓名:/n");scanf("%s",p->name);if(strcmp(p->name,"*") == 0)break;printf("請(qǐng)輸入號(hào)碼:/n");scanf("%d",&p->data);p->next = head->next;    head->next = p;/*  插入到表頭 */}return head;}/* 刪除 *//* 操作結(jié)果:輸入要?jiǎng)h除的  元素     進(jìn)行刪除 */LinkList * ListDelete(LinkList *head) { LinkList *pre,*after,*p;p=(LinkList *)malloc(len);//插入時(shí)申請(qǐng)空間printf("請(qǐng)輸入要?jiǎng)h除姓名:/n");scanf("%s",p->name);pre = head;after=head->next;while(after != NULL){ if(strcmp(after->name,p->name)==0){ pre->next=after->next;free(after);break;}else{pre=after;after=after->next;//錯(cuò)誤地點(diǎn)}}if(after == NULL)printf("this data is not exist/n");return head;}/* 插入 *//* 操作結(jié)果:把數(shù)據(jù)插在第 n 個(gè)位置 */LinkList * ListInsert(LinkList *head,int n){LinkList *new,*look;;new=(LinkList *)malloc(len);//插入時(shí)申請(qǐng)空間printf("添加  新聯(lián)系人/n");printf("新加  姓名:/n");scanf("%s",new->name);printf("新加  號(hào)碼:/n");scanf("%d",&new->data);int count=0;if(head==NULL){head=new;new->next=NULL;}else{if(n==0){new->next=head;head=new;}else{look=head;while((look!=NULL)&&(count<n-1)){look=look->next;count++;}if(count==n-1){new->next=look->next;look->next=new;}if(new==NULL)printf("out of the range,");}}return head;} /*   排序*//*  按照   名字    排序*/LinkList *ListSort(LinkList *head){LinkList *p1,*p2,*p,*s;int i,j,n=1;   s=head->next; /* s指向第一個(gè)結(jié)點(diǎn) */   while(s)        /*數(shù)元素的個(gè)數(shù),計(jì)數(shù)...........遍歷得到n*/                    {         n++;        s=s->next;       }p=(LinkList *)malloc(len);for(i=0;i<(n-1);i++){ p1=head;p2=p1->next;for(j=0;j<(n-i-1);j++){if(strcmp(p1->name,p2->name)>0){p->data=p1->data;p1->data=p2->data;p2->data=p->data;strcpy(p->name,p1->name);strcpy(p1->name,p2->name);strcpy(p2->name,p->name);}p1=p2;p2=p2->next;}}return head;}/*     查找     通過名字,找到電話號(hào)碼*/LinkList *xuzhao(LinkList *head){LinkList *p,*x;p=head;int count=0;x=(LinkList *)malloc(len);//申請(qǐng)空間printf("****輸入要查找姓名****/n");scanf("%s",x->name);while(1){ if( strcmp(p->name,x->name)==0 ){printf("姓名%-10s 號(hào)碼:%-10d/n",p->name,p->data);break;}p=p->next;if(p==NULL){ printf("*********查無此人*************/n");break;}}}LinkList * xiugai(LinkList *head){LinkList *p,*x,*q;p=head;int count=0;x=(LinkList *)malloc(len);printf("輸入要修改姓名/n");scanf("%s",x->name);while(1){ if( strcmp(p->name,x->name)==0 ){break;}p=p->next; if(p == NULL){printf("這個(gè)名字不存在/n");return head;}}printf("修改為:姓名   號(hào)碼/n");scanf("%s %d",p->name,&p->data);printf("姓名%-10s 號(hào)碼:%-10d/n",p->name,p->data);return head;}/*桌面顯示*/int beijing(){printf("********************************************/n");printf("**選擇:1   創(chuàng)建通訊錄                          /n");printf("**選擇:2   查找聯(lián)系人                        /n");printf("**選擇:3   刪除聯(lián)系人                          /n");printf("**選擇:4   添加聯(lián)系人                         /n");printf("**選擇:5   對(duì)聯(lián)系人排序                      /n");printf("**選擇:6   對(duì)聯(lián)系人信息修改               /n");printf("**選擇:0   退出編輯                           /n");printf("********************************************/n");}/*將數(shù)據(jù)進(jìn)行保存*/void save(LinkList *head){LinkList *p;FILE *fp;p=head->next; fp=fopen("s2.txt","wt");if( fp == NULL){printf("fopen error");exit(-1);}while( p != NULL){fwrite(p,len,1,fp);p=p->next;}fclose(fp);}/*將文件數(shù)據(jù)寫入入鏈表*/LinkList * out_liaobiao(LinkList *head){LinkList *tmp,*p;FILE * fp;  fp=fopen("s2.txt","rt");if(fp == NULL)    {        printf("fopen error/n");        exit(1);    }  tmp=(LinkList *)malloc(len);while( fread(tmp,len,1,fp))//fread 返回?cái)?shù)據(jù)塊的數(shù)目{tmp->next = head->next;    head->next = tmp;tmp=(LinkList *)malloc(len);}return head;fclose(fp);}/*將文件數(shù)據(jù)寫入入鏈表*//*   也可以用另外一種方法*//*LinkList * out_liaobiao(LinkList *head){FILE * fp;LinkList *p;int i = 0;long llen;if( (fp=fopen("s2.txt","rt")) == NULL ){printf("fopen error/n");        exit(1);}fseek(fp,0,2);llen= ftell(fp);fseek(fp,0,0);while(llen != 0){p=(LinkList *)malloc(len);if( p == NULL)printf("tmp empty/n");fread(p,len,1,fp);p->next = head->next;    head->next = p;llen=llen-len;}if(llen==0)return head;fclose(fp);}*/int main(){LinkList *head;int num;head=InitList();head=out_liaobiao(head);print(head); while(1){ beijing();printf("請(qǐng)選擇/n");scanf("%d",&num);switch(num){case 0: save(head);exit(0);case 1: head=CreateListHead(head); //f_write(head);//save(head);print(head);break; case 2: xuzhao(head);              break;case 3: head=ListDelete(head);print(head); break;case 4: head=ListInsert(head,1);print(head); break;case 5: head=ListSort(head);print(head); break;case 6: head=xiugai(head);print(head); break;/*case 7: head=InitList();print(head);break;case 8: head=out_liaobiao(head);print(head); break;*/default : printf("這是錯(cuò)誤的,請(qǐng)重新選擇/n"); break;}}}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 二手房| 普兰店市| 新沂市| 铜鼓县| 左权县| 兴和县| 高淳县| 集贤县| 资源县| 西安市| 道真| 万源市| 黔西县| 邵阳县| 崇仁县| 平遥县| 西林县| 香河县| 宜宾市| 伊金霍洛旗| 庆云县| 二手房| 藁城市| 刚察县| 乐陵市| 桑日县| 天峻县| 丹寨县| 康马县| 鹿邑县| 宁津县| 任丘市| 湟源县| 巫溪县| 蒙阴县| 河曲县| 济南市| 龙泉市| 龙川县| 米林县| 宜宾市|