單鏈表通訊錄(排序)
阿新 • • 發佈:2018-12-24
標頭檔案:
#ifndef _ADRESS_H #define _ADRESS_H #define SUCCESS 10000 #define FAILURE 10001 #define TURE 10003 #define FALSE 10004 struct adress { char name[20]; int num; struct adress *next; }; typedef struct adress Sad; typedef int ElemType; int LinkInit(Sad **l); void welcome (); void menu(); int add(Sad *l); int addressTraver(Sad *l, void (*p)(ElemType),void (*pp)(char *)); int LocatElem(Sad *l); int change ( Sad *l,int (*p)(char*,char*)); int delete (Sad *l); int rever(Sad *l); int clear(Sad *l); int destory(Sad **l); #endif
函式檔案(加上了printf,要不太麻煩)
#include "adress.h" #include <stdlib.h> #include <string.h> #include <stdio.h> void menu()// 主介面 { printf("*************welcome to use********\n"); printf("**1 功能簡介 2 新增資訊**\n"); printf("**3 檢視資訊 4 查詢資訊**\n"); printf("**5 修改資訊 6 刪除資訊**\n"); printf("**7 反轉資訊 8 清空資訊**\n"); printf("**9 毀壞資訊 10 退出 **\n"); printf("*************************************\n"); } function() { printf("!!!!!!!!想多了!!!!!!! \n"); } void welcome()//歡迎 { system("clear"); printf("\n\n\n\n\n\n\n\n\n\n\n************************************************\n"); printf("********************WELCOME*********************\n"); printf("***************老式山寨諾基亞版*****************\n"); sleep(2); } int LinkInit(Sad **l)// 初始化 { *l = (Sad *)malloc(sizeof(Sad)*1); if(NULL == *l) { return FAILURE; } (*l)->next = NULL; return SUCCESS; } int add(Sad *l)// 新增資訊 { if(NULL == l) { return FAILURE; } Sad *q = l; while(q) { Sad *p = (Sad *)malloc (sizeof (Sad)*1); printf("please input name:\n"); scanf("%s",p->name); if(strcmp(p->name,"fine") == 0) break; printf("please input the num:\n"); scanf("%d",&p->num); p->next = q->next; q->next = p; q = q->next; } char *a; //冒泡 a = (char *)malloc(sizeof(char)*20); int x = 0; Sad *s = l; Sad *k = l; for(s = l;s != NULL;s=s->next) { for(k = s->next; k != NULL;k = k->next) { if(strcmp(s->name,k->name) > 0) { strcpy(a,k->name); strcpy(k->name,s->name); strcpy(s->name,a); x = k -> num; k-> num = s->num; s->num = x; } } } return SUCCESS; /*Sad *q = l->next; //插入 Sad *f = l; while(1) { Sad *p = (Sad *)malloc (sizeof (Sad) * 1); printf("please input name:\n"); scanf("%s",p->name); if(strcmp(p->name,"fine") == 0) { free(p); break; } printf("please input the num:\n"); scanf("%d",&p->num); while(f) { printf("************\n"); if(strcmp(p->name,q->name) < 0) { printf("************\n"); p->next = f->next; f->next = p; } printf("************\n"); q = q->next; f = f->next; } if(strcmp(p->name,q->name) > 0) { q->next = p; p->next = NULL; } }*/ } int addressTraver(Sad *l, void (*p)(ElemType),void (*pp)(char *))//遍歷 { if (NULL == l) { return FAILURE; } Sad *q = l; while (q->next) { q = q -> next; pp(q->name); p(q->num); } return SUCCESS; } int LocatElem(Sad *l) //尋找資訊 { if(NULL == l) { return FAILURE; } char lookname[20] = {0}; printf("please input the name to lookfor:\n"); scanf("%s",lookname); Sad *q = l->next; while (q) { if (strcmp(lookname,q->name) == 0) { printf("name:%s\n",q->name); printf("num:%d\n",q->num); return SUCCESS; break; } q = q->next; } return FAILURE; } int change ( Sad *l,int (*p)(char*,char*))//更改資訊 { if(NULL == l) { return FAILURE; } char changename[20] = {0}; Sad *q = l->next; printf("please input the name to change:\n"); scanf ("%s",changename); while(q) { if (p(changename,q->name) == TURE) //if (strcmp(changename,q->name) == 0) { int b; printf("1 change name 2 change num \n"); scanf ("%d",&b); if(b == 1) { printf("input the name you want to change:\n"); scanf ("%s",q->name); return SUCCESS; break; } else if(b == 2) { printf("input the new num:\n"); scanf("%d",&q->num); return SUCCESS; break; } } else { q = q->next; } } if (q == NULL) { printf("There are not the name!\n"); return FAILURE; } } int delete (Sad *l)//通過搜尋名字進行刪除 { if(NULL == l) { return FAILURE; } char deletename[20] = {0}; printf("please input the name to delete:\n"); scanf("%s",deletename); Sad *q = l->next; while(q) { if (strcmp (deletename,q->name) == 0) { Sad *n = q; int e; e = n->num; q->next = n->next; free(n); printf("Delete %d success!\n",e); return SUCCESS; break; } else { q = q->next; } } if(q == NULL) { printf("There are not the name!\n"); return FAILURE; } } int rever(Sad *l)//實現排名反轉 { if (NULL == l) { return FAILURE; } Sad *p = l->next; l->next = NULL; while (p != NULL) { Sad *q = p; p = p->next; q->next = l->next; l->next = q; } return SUCCESS; } int clear(Sad *l) //清除資訊 { if(NULL == l) { return FAILURE; } Sad *p = l->next; while(p) { l->next = p->next; free(p); p = p->next; } return SUCCESS; } int destory(Sad **l) //毀壞 { if(NULL == *l) { return FAILURE; } free (*l); *l = NULL; return SUCCESS; }
測試檔案:
#include "adress.h" #include <stdio.h> #include <stdlib.h> void print(ElemType e) { printf(" num:%d \n",e); } void print2(char *a) { printf("name :%s ",a); } int Equal(char *e1,char *e2 ) { return (strcmp(e1,e1) == 0)? TURE : FALSE; } int main() { Sad *m; LinkInit(&m); int choose,ret; welcome(); while (1) { menu(); scanf("%d",&choose); switch(choose) { case 1:ret = function(); if (ret == SUCCESS) { printf(" Init success!\n"); } else { printf(" Init failure!\n"); } break; case 2:ret = add(m); if(ret == SUCCESS) { printf("Insert success!\n"); } else { printf("Insert failure!\n"); } break; case 3:ret = addressTraver(m,print,print2); if(ret == SUCCESS) { printf(" Traver success!\n"); } else { printf(" Traver failure!\n"); } break; case 4:ret = LocatElem(m); if(ret == SUCCESS) { printf(" LocatElem success!\n"); } else { printf(" LocatElem failure!\n"); } break; case 5:ret = change(m,Equal); if(ret == SUCCESS) { printf(" Change success!\n"); } else { printf(" Change failure!\n"); } break; case 6:ret = delete(m); if(ret == SUCCESS) { printf(" Delete success!\n"); } else { printf(" Delete failure!\n"); } break; case 7:ret = rever(m); if(ret == SUCCESS) { printf(" rever success!\n"); } else { printf(" rever failure!\n"); } break; case 8:ret = clear(m); if(ret == SUCCESS) { printf(" Clear success!\n"); } else { printf(" Clear failure!\n"); } break; case 9:ret = destory(&m); if(ret == SUCCESS) { printf(" Destory success!\n"); } else { printf(" Destory failure!\n"); } break; case 10 : exit(1); break; } } }