C語言單鏈表實現通訊錄管理系統

本文實例為大傢分享瞭C語言單鏈表實現通訊錄管理系統的具體代碼,供大傢參考,具體內容如下

本人前幾天剛剛自學瞭單鏈表,趁熱打鐵,趕緊寫一個小小的項目練練手。

單鏈表的實現在本人之前的博客中有:C語言編寫一個鏈表

通訊錄管理系統

保存人的信息有: 
名字   name
電話   telephone
性別   sex
年齡   age

用一個結構體來裝這些信息:

struct infor{
 char name[20];
 int age;
 char sex[8];
 char telephone[16];
};

實現功能有:

增加聯系人
刪除聯系人
修改聯系人
尋找聯系人
顯示聯系人

首先建立鏈表的基本功能創建頭鏈表,創建節點,插入節點

struct addre* Creathead(){                        //創建頭鏈表
 struct addre *headnode = (struct addre*)malloc(sizeof(struct addre));
 if (headnode == NULL){
  printf("malloc error\n");
 }
 headnode->next = NULL;
 return headnode;

}

struct addre* Creatlist(struct infor *list){      //創建節點
 struct addre *node = (struct addre*)malloc(sizeof(struct addre));
 if (node == NULL){
  printf("malloc error\n");
 }
 node->next = NULL;
 node->people.age = list->age;
 strcpy(node->people.name, list->name);
 strcpy(node->people.sex, list->sex);
 strcpy(node->people.telephone, list->telephone);

 return node;
}

void Addlist(struct addre *headnode,struct infor *list){ //插入節點
 struct addre *t = headnode;
 while (t->next != NULL){
  t = t->next;
 }
 struct addre *nodelist = Creatlist(list);
 t->next = nodelist;
 nodelist->next = NULL;
}

然後在實現通訊錄的功能

void Addpeople(struct addre* node){                     //添加人的信息
 struct infor *a=malloc(sizeof(struct infor));       // 創建動態信息結構體指針
 if (a == NULL){
  printf("malloc error\n");
 }
 printf("請輸入名字\n");
 scanf("%s", a->name);
 printf("請輸入年齡\n");
 scanf("%d", &a->age);
 printf("請輸入性別\n");
 scanf("%s", a->sex);
 printf("請輸入電話號碼\n");
 scanf("%s", a->telephone);
 Addlist(node, a);                        //用尾插法插入該人信息
 printf("添加成功!\n");
}
void Deletepeople(struct addre *node){                //刪除人的信息
 char *str = malloc(sizeof(char)* 10);
 if (str == NULL){                                 //通過名字尋找
  printf("malloc error\n");
 }
 printf("請輸入要刪除人的姓名\n");
 scanf("%s", str);
 struct addre *strat = node;
 struct addre *end = node->next;
 int flag = 0;                                    //判斷是否找到 0為未找到,1 找到
 while (end){                    //判斷end的  不然會越界
  if (strcmp(end->people.name, str) == 0){
   flag = 1;
   break;
  }
  node = node->next;                           //到下一個鏈表
  strat = node;                               //一個指向前面 一個指向後面,刪除將end刪除,前面那個直接指向end的指向
  end = node->next;
 }
 if (flag){
  strat->next = end->next;
  printf("刪除成功\n");
  free(end);
 }
 else{
  printf("沒找到!\n");
 }
}

void Modifyinfor(struct addre *node){              //修改人的信息
 char *str = malloc(sizeof(char)* 10);          //通過名字尋找
 if (str == NULL){
  printf("malloc error\n");
 }
 printf("請輸入要修改人的姓名\n");
 scanf("%s", str);
 int flag = 0;
 while (node){
  if (strcmp(node->people.name, str) == 0){
   flag = 1;
   break;
  }
  node = node->next;
 }
 if (flag){
  printf("請重新輸入該人信息\n");
  printf("請輸入名字\n");
  scanf("%s", node->people.name);
  printf("請輸入年齡\n");
  scanf("%d", &node->people.age);
  printf("請輸入性別\n");
  scanf("%s", node->people.sex);
  printf("請輸入電話號碼\n");
  scanf("%s", node->people.telephone);
  printf("修改成功\n");
 }
 else{
  printf("沒找到\n");
 }
}
void Foundpeople(struct addre *node){                //找到某人的信息並打印出來
 char *str = malloc(sizeof(char)* 10);            //通過名字尋找
 if (str == NULL){
  printf("malloc error\n");
 }
 printf("請輸入要查找人的姓名\n");
 scanf("%s", str);
 int flag = 0;
 while (node){
  if (strcmp(node->people.name, str) == 0){
   flag = 1;
   break;
  }
  node = node->next;
 }
 if (flag){
  printf("name\tage\tsex\ttelephone\n");
  printf("%s\t", node->people.name);
  printf("%d\t", node->people.age);
  printf("%s\t", node->people.sex);
  printf("%s\t", node->people.telephone);
 }
 else{
  printf("沒找到!\n");
 }
}

void Display(struct addre *node){
 struct addre *pmove = node->next; //要從頭節點的下一個節點顯示信息
 printf("name\tage\tsex\ttelephone\n");
 while (pmove){
  printf("%s\t%d\t%s\t%s\n", pmove->people.name, pmove->people.age, pmove->people.sex, pmove->people.telephone);
  pmove = pmove->next;
 }
}

其它代碼

菜單:

void Menu(){
 printf("+-----------------+\n");
 printf("+-1.add   2.delet-+\n");
 printf("+-3.modify 4.seek-+\n");
 printf("+-5.display6.exit-+\n");
 printf("+-----------------+\n");
 printf("Please Enter Your Select\n");
}

本人使用的時多文件的方式上面的代碼都在函數定義的源文件裡實現

main函數的源文件代碼:

#include"addressbank.h"

/***********************
信息:
名字   name
年齡   age
電話   telephone
性別   sex
功能:
 增加
 刪除
 修改
 尋找
 打印
 顯示
***********************/
int main(){
 struct addre* node = Creathead();
 int quit = 0;
 while (!quit){
  
  Menu();
  int select = 0;
  scanf("%d", &select);
  switch (select){
  case 1:
   Addpeople(node);
   break;
  case 2:
   Deletepeople(node);
   break;
  case 3:
   Modifyinfor(node);
   break;
  case 4:
   Foundpeople(node);
   break;
  case 5:
   Display(node);
   break;
  case 6:
   quit = 1;
   break;
  default:
   printf("Enter Error!\n");
   break;
  }
 }
 system("pause");
 return 0;
}

聲明的頭文件:

#ifndef __ADDRESSBANK_H__
#define __ADDRESSBANK_H__

#include<stdio.h>
#include<string.h>
struct infor{//信息結構體
 char name[20];
 int age;
 char sex[8];
 char telephone[16];
};
struct addre{ //鏈表
 struct infor people;
 struct addre *next;
};
//功能函數
extern void Menu();
extern void Addpeople(struct addre *node);
extern void Deletepeople(struct addre *node);
extern void Modifyinfor(struct addre *node);
extern void Foundpeople(struct addre *node);
extern void Display(struct addre *node);
//下面未鏈表函數
extern struct addre* Creatlist(struct infor *list);
extern struct addre* Creathead();
extern void Addlist(struct addre *headnode, struct infor *list);

#endif

代碼可直接復制使用,如有不足請提出,後續修改謝謝

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: