頭文件:
#ifndef _STRUCT_H_
#define _STRUCT_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int i; //定義全局變量i
struct node
{
int ID;
char name[20];
char ismale[20];
char tel[20];
char qq[20];
char address[20];
char remark[20]; //保存聯繫人信息
struct node *next; //結構體類型的指針,保存下一個結構體的地址
};
void clear();
void interface();
int insert_tail( struct node *header );
void printfcon( struct node *header, int j );
void list( struct node *header );
int findnum( struct node *header, char *s, int *accumlate );
void seek( struct node *header );
int coverdata( struct node *header, int j );
void mydelete( struct node *header );
void setid( struct node *header );
int modify( struct node *header, int j );
void revise( struct node *header );
#endif
清屏 :
#include "struct.h"
void clear()
{
system("clear"); //清屏
}
界面:
#include "struct.h"
void interface() //界面函數,輸出主界面
{
printf(" ********************************************************************************* \n");
printf(" * * \n");
printf(" * * \n");
printf(" | | \n");
printf(" | 通 訊 錄 | \n");
printf(" | | \n");
printf(" | ~~~~~~~~ | \n");
printf(" | 1、添加好友; | \n");
printf(" | 2、查看列表; | \n");
printf(" | 3、刪除好友; | \n");
printf(" | 4、查找好友; | \n");
printf(" | 5、修改信息; | \n");
printf(" | 6、退出程序; | \n");
printf(" | ~~~~~~~~ | \n");
printf(" | | \n");
printf(" |功能:1.ADD、2.LIST、3.DELETE、4.SEEK、5.REVISE、6.EXIT | \n");
printf(" |請在下面輸入1、2、3、4、5、6選擇你的操作。 | \n");
printf(" | | \n");
printf(" * * \n");
printf(" * * \n");
printf(" ********************************************************************************* \n");
}
添加聯繫人:
#include "struct.h"
int insert_tail( struct node *header ) //定義添加聯繫人函數
{
char c = 0;
struct node *temp = header;
do
{
clear();
struct node *p = (struct node *)malloc(sizeof(struct node));
if( p == NULL )
{
printf("malloc error!\n");
return -1;
}
printf("請輸入聯繫人姓名:"); //錄入聯繫人信息
scanf("%s",p->name);
printf("請輸入聯繫人性別:");
scanf("%s",p->ismale);
printf("請輸入聯繫人電話號碼:");
scanf("%s",p->tel);
printf("請輸入聯繫人QQ號:");
scanf("%s",p->qq);
printf("請輸入聯繫人家庭地址:");
scanf("%s",p->address);
printf("備註:");
scanf("%s",p->remark);
p->ID = i;
i++; //錄完後讓i++
while( temp->next != NULL )
{
temp = temp->next;
}
temp->next = p; //找到最後一個結點,將結點p尾插進鏈表
p->next = NULL;
printf("是否繼續添加? (y/n) :");
__fpurge(stdin); //清除緩衝區
c = getchar();
}while( c == 'y' ); //do while語句,當c不等於y時退出循環,即不再繼續添加
}
顯示聯繫人列表:
#include "struct.h"
void list( struct node *header ) //定義聯繫人列表函數,輸出所有聯繫人信息
{
clear();
int j = 0;
for( j = 0; j <= i; j++ )
{
printfcon( header, j ); // 調用printfcon函數,輸出每個聯繫人的信息
}
}
void printfcon(struct node *header, int j) //定義打印單個聯繫人信息函數
{
struct node *p = header;
while( p->next != NULL )
{
p = p->next;
if( p->ID == j )
{
printf("編號:%d\n",p->ID); //遍歷鏈表,輸出ID爲j的聯繫人信息
printf("姓名:%s\n",p->name);
printf("性別:%s\n",p->ismale);
printf("電話:%s\n",p->tel);
printf("QQ號:%s\n",p->qq);
printf("地址:%s\n",p->address);
printf("備註:%s\n",p->remark);
printf("\n");
}
}
}
通過名字找到聯繫人編號,返回有幾個聯繫人
#include "struct.h"
int findnum( struct node *header, char *s, int *accumlate ) //定義找到聯繫人編號函數,找到編號存入數組accumlate
{
int m = 0;
int j = 0;
struct node *p = header;
for( m = 0; m <= i; m++ )
{
if( strcmp( s, p->name ) == 0 )
{
accumlate[j++] = p->ID; //比較字符串s和p->name,如果返回值爲0,代表相等,把p->ID存入數組
}
p = p->next;
}
return j; //返回數組中有幾個值,即能找到幾個聯繫人
}
查找聯繫人:
#include "struct.h"
void seek( struct node *header ) //定義查找函數,通過姓名查找
{
char c = 0;
struct node *p = header;
do
{
clear();
int j = 0;
int temp = 0;
int accum = 0;
int a[20] = {0};
char name[20] = {0};
printf("請輸入你想查找的聯繫人的姓名:");
scanf("%s",name);
accum = findnum( p, name, a ); //調用findnum函數,並把找到幾個聯繫人賦給accum
if( accum == 0 )
{
printf("此聯繫人不存在!\n");
}
else if( accum == 1 ) //如果找到一個聯繫人,輸出這個聯繫人的信息
{
temp = a[0];
printfcon( p, temp );
}
else if( accum > 1 ) //找到不止一個聯繫人,輸出全部聯繫人信息
{
printf("有%d個聯繫人!\n",accum);
for( j = 0; j < accum; j++ )
{
temp = a[j];
printfcon( p, temp );
}
}
printf("是否繼續查找? (y/n) :");
__fpurge(stdin);
c = getchar();
}while( c == 'y' ); //do while語句用來控制是否繼續查找,當輸入爲y時繼續查找
}
刪除聯繫人:
#include "struct.h"
void mydelete( struct node *header ) //定義刪除聯繫人函數
{
char c = 0;
struct node *p = header;
do
{
clear();
int j = 0;
int temp = 0;
int accum = 0;
int a[20] = {0};
char name[20] = {0};
printf("請輸入你想刪除的聯繫人的姓名:");
scanf("%s",name);
accum = findnum( p, name, a ); //調用findnum函數,並把找到幾個聯繫人賦給accum
if( accum == 0 )
{
printf("此聯繫人不存在!\n");
}
else if( accum == 1 )
{
temp = a[0];
coverdata( p, temp );
}
else if( accum > 1 )
{
printf("有%d個聯繫人!\n",accum); //如果找到不止一個聯繫人,一個個輸出這些聯繫人信息,讓操作者選擇是否要刪除
for( j = 0; j < accum; j++ )
{
temp = a[j];
printfcon( p, temp );
coverdata( p, temp );
}
}
setid( p );
printf("是否繼續刪除? (y/n): \n");
__fpurge(stdin);
c = getchar();
}while( c == 'y' ); //do while語句,用來控制是否繼續刪除,只有輸入y時才繼續刪除
}
int coverdata( struct node *header, int j ) //定義覆蓋信息函數
{
printf("是否刪除該聯繫人?(y/n)\n");
__fpurge(stdin);
if( getchar() == 'y' ) //當輸入的字符爲y時執行操作
{
struct node *prev = NULL;
struct node *p = header;
while( p->next != NULL )
{
prev = p;
p = p->next;
if( p->ID == j )
{
prev->next = p->next;
printf("刪除成功!\n");
printf("\n");
free(p);
p = NULL;
i--;
return 0; //當ID等於j時,讓p前一個結點指向p後一個結點,釋放p,讓i-1
}
}
printf("delete error!\n");
}
}
void setid( struct node *header )
{
struct node *p = header;
int j = 0;
while( p->next !=NULL )
{
p = p->next; //跳過頭結點或指向下一個結點
p->ID = j++; //重置每個結點的ID
}
}
修改聯繫人:
#include "struct.h"
void revise( struct node *header ) //定義修改信息函數
{
char c = 0;
struct node *p = header;
do
{
clear();
int j = 0;
int temp = 0;
int accum = 0;
int a[20] = {0};
char name[20] = {0};
printf("請輸入你想修改的聯繫人的姓名:");
scanf("%s",name);
accum = findnum( p, name, a );
if( accum == 0 )
{
printf("此聯繫人不存在!\n");
}
else if( accum == 1 )
{
temp = a[0];
printfcon( p, temp );
modify( p, temp );
}
else if( accum > 1 )
{
printf("有%d個聯繫人!\n",accum);
for( j = 0; j < accum; j++ )
{
temp = a[j];
printfcon( p, temp ); //有不止一個聯繫人時一個個輸出聯繫人信息,讓其修改
modify( p, temp );
}
}
printf("是否繼續修改? (y/n) :\n");
__fpurge(stdin);
c = getchar();
}while( c == 'y' ); //do while語句用來控制是否繼續修改,當輸入y時繼續循環修改
}
int modify( struct node *header, int j ) //定義修改函數
{
int temp = 0;
int rev = 1;
struct node *p = header;
while( p->next != NULL )
{
p = p->next;
if( p->ID == j )
{
printf("1--姓名\n");
printf("2--性別\n");
printf("3--電話\n");
printf("4--qq號\n");
printf("5--住址\n");
printf("6--備註\n");
printf("請輸入你要修改的項:\n");
do
{
rev = 1;
__fpurge(stdin);
if(scanf("%d",&temp) != 1)
{
printf("輸入錯誤!請重新輸入!\n");
rev = -1;
}
else if( temp >= 7 || temp <= 0 )
{
printf("輸入錯誤!請重新輸入!\n");
rev = -1; //當選擇的值沒有傳入或者不爲1-6時。輸出輸入錯誤請重輸
}
}while( rev == -1 );
switch(temp) //用switch語句選擇要修改的信息
{
case 1:{
printf("請輸入你要改成的姓名:");
scanf("%s",p->name);
printf("修改成功!\n");
printf("\n");
break;
}
case 2:{
printf("請輸入你要改成的性別:");
scanf("%s",p->ismale);
printf("修改成功!\n");
printf("\n");
break;
}
case 3:{
printf("請輸入你要改成的電話:");
scanf("%s",p->tel);
printf("修改成功!\n");
printf("\n");
break;
}
case 4:{
printf("請輸入你要改成的qq號:");
scanf("%s",p->qq);
printf("修改成功!\n");
printf("\n");
break;
}
case 5:{
printf("請輸入你要改成的住址:");
scanf("%s",p->address);
printf("修改成功!\n");
printf("\n");
break;
}
case 6:{
printf("請輸入你要改成的備註:");
scanf("%s",p->remark);
printf("修改成功!\n");
printf("\n");
break;
}
default:printf("輸入錯誤!\n");
break;
}
}
}
}
main函數
#include "struct.h"
int main()
{
struct node *header = (struct node *)malloc(sizeof(struct node)); //定義頭指針
if( header == NULL )
{
printf("malloc error!\n");
return -1;
}
for( ; ; )
{
int temp = 0;
clear();
interface(); //調用interface函數,輸出主界面
__fpurge(stdin);
scanf("%d",&temp);
switch(temp) //switch語句選擇功能
{
case 1:{
insert_tail( header );
break;
}
case 2:{
list( header );
printf("按enter返回主界面!\n");
__fpurge(stdin);
getchar(); //暫停界面,有輸入才退出本次選擇語句
break;
}
case 3:{
mydelete( header );
break;
}
case 4:{
seek( header );
printf("按enter返回主界面!\n");
__fpurge(stdin);
getchar();
break;
}
case 5:{
revise( header );
break;
}
case 6:exit(0);
default:{
printf("輸入錯誤!");
printf("按enter返回主界面!\n");
__fpurge(stdin);
getchar();
break;
}
}
}
return 0;
}