头文件
SList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
typedef PersonInfo SLDataType;
typedef struct SListNode {
SLDataType a;
struct SListNode* next;
}SListNode;
void SLDestory(SListNode** phead);
void SLPrint(SListNode* phead);
void SLPushBack(SListNode** pphead, SLDataType x);
void SLPushFront(SListNode** pphead, SLDataType x);
void SLPopBack(SListNode** pphead);
void SLPopFront(SListNode** pphead);
SListNode* SLFind(SListNode* phead, SLDataType x);
void SLInsertFront(SListNode** pphead, SLDataType pos, SLDataType x);
void SLInsertBack(SListNode* phead, SLDataType pos, SLDataType x);
void SLInsertFront_1(SListNode** pphead, SListNode* pos, SLDataType x);
void SLInsertBack_1(SListNode* pos, SLDataType x);
void SLErase(SListNode** pphead, SListNode* pos);
void SLEraseAfter(SListNode* pos);
Contact.h
#pragma once
#define NAME_MAX 50
#define GENDER_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100
typedef struct SListNode Contact;
typedef struct PersonInfo {
char name[NAME_MAX];
char gender[GENDER_MAX];
char tel[TEL_MAX];
char addr[ADDR_MAX];
}PersonInfo;
void ContactDestory(Contact** con);
void ContactPushBack(Contact** con);
void ContactPrint(Contact* con);
void SaveContact(Contact* con);
void LoadContact(Contact** con);
void ContactDele(Contact** con);
void ContactFind(Contact* con);
void ContactModify(Contact** con);
实现文件
SList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
SListNode* BuyNode(SLDataType x)
{
SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
if (newnode == NULL)
{
perror("malloc fail!\n");
exit(1);
}
newnode->a = x;
newnode->next = NULL;
return newnode;
}
void SLDestory(SListNode** pphead)
{
assert(pphead);
SListNode* pcur = *pphead;
while (pcur)
{
SListNode* tmp = pcur->next;
free(pcur);
pcur = tmp;
}
*pphead = NULL;
}
void SLPushBack(SListNode** pphead, SLDataType x)
{
assert(pphead);
SListNode* newnode = BuyNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else {
SListNode* ptail = *pphead;
while (ptail->next != NULL)
{
ptail = ptail->next;
}
ptail->next = newnode;
}
}
void SLPushFront(SListNode** pphead, SLDataType x)
{
assert(pphead);
SListNode* newnode = BuyNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SLPopBack(SListNode** pphead)
{
assert(pphead && *pphead);
if ((*pphead)->next == NULL)
{
SLDestory(pphead);
return;
}
SListNode* pprev = *pphead;
SListNode* ptail = *pphead;
while (ptail->next != NULL)
{
pprev = ptail;
ptail = ptail->next;
}
pprev->next = NULL;
free(ptail);
ptail = NULL;
}
void SLPopFront(SListNode** pphead)
{
assert(pphead && *pphead);
SListNode* pcur = *pphead;
*pphead = pcur->next;
free(pcur);
pcur = NULL;
}
void SLErase(SListNode** pphead, SListNode* pos)
{
assert(pphead && *pphead);
assert(pos);
if (*pphead == pos)
{
SLPopFront(pphead);
return;
}
SListNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
void SLEraseAfter(SListNode* pos)
{
assert(pos && pos->next);
SListNode* next = pos->next;
pos->next = next->next;
free(next);
next = NULL;
}
Contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<string.h>
#include"Contact.h"
#include"SList.h"
void ContactDestory(Contact** con)
{
SLDestory(con);
}
void ContactPushBack(Contact** con)
{
assert(con);
Contact* phead = *con;
PersonInfo tmp;
printf("请输入联系人姓名:\n");
scanf("%s", tmp.name);
printf("请输入联系人性别:\n");
scanf("%s", tmp.gender);
printf("请输入联系人电话:\n");
scanf("%s", tmp.tel);
printf("请输入联系人地址:\n");
scanf("%s", tmp.addr);
SLPushBack(con, tmp);
}
Contact* Findbyname(Contact* con,char name[NAME_MAX])
{
assert(con);
Contact* pcur = con;
while (pcur)
{
if (strcmp(pcur->a.name, name) == 0) {
return pcur;
}
pcur = pcur->next;
}
return NULL;
}
void ContactDele(Contact** con)
{
assert(con);
Contact* phead = *con;
char name[NAME_MAX];
printf("请输入要删除的联系人姓名:\n");
scanf("%s", name);
Contact* find = Findbyname(*con, name);
if (find == NULL)
{
printf("无此联系人信息!\n");
return;
}
SLErase(con, find);
}
void ContactFind(Contact* con)
{
assert(con);
char name[NAME_MAX];
printf("请输入要查找的联系人姓名:\n");
scanf("%s", name);
Contact* find = Findbyname(con, name);
if (find == NULL)
{
printf("无此联系人信息!\n");
return;
}
printf("姓名 性别 电话 地址\n");
printf("%s %s %s %s\n", find->a.name
, find->a.gender
, find->a.tel
, find->a.addr);
}
void ContactModify(Contact** con)
{
assert(con);
char name[NAME_MAX];
printf("请输入要修改的联系人姓名:\n");
scanf("%s", name);
Contact* find = Findbyname(*con, name);
if (find == NULL)
{
printf("无此联系人信息!\n");
return;
}
printf("请输入新的联系人姓名:\n");
scanf("%s", find->a.name);
printf("请输入联系人性别:\n");
scanf("%s", find->a.gender);
printf("请输入联系人电话:\n");
scanf("%s", find->a.tel);
printf("请输入联系人地址:\n");
scanf("%s", find->a.addr);
printf("修改成功!\n");
}
void ContactPrint(Contact* con)
{
Contact* pcur = con;
printf("姓名 性别 电话 地址\n");
while (pcur) {
printf("%s %s %s %s\n", pcur->a.name
, pcur->a.gender
, pcur->a.tel
, pcur->a.addr);
pcur = pcur->next;
}
}
void SaveContact(Contact* con)
{
FILE* pf = fopen("Contact.txt", "wb");
if (pf == NULL) {
perror("fopen fail!\n");
return;
}
Contact* pcur = con;
while (pcur) {
fwrite(&(pcur->a), sizeof(PersonInfo), 1, pf);
pcur = pcur->next;
}
fclose(pf);
pf = NULL;
printf("保存成功!\n");
return;
}
void LoadContact(Contact** con)
{
FILE* pf = fopen("Contact.txt", "rb");
if (pf == NULL) {
perror("fopen fail!\n");
return;
}
PersonInfo a;
PersonInfo* tmp = &a;
while (fread(tmp,sizeof(PersonInfo),1,pf) )
{
SLPushBack(con, *tmp);
}
printf("读取成功!\n");
}
测试代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
#include"Contact.h"
void Test2(Contact* con)
{
LoadContact(&con);
int input = 0;
do {
menu();
scanf("%d", &input);
switch (input)
{
case 1:
ContactPushBack(&con);
break;
case 2:
ContactDele(&con);
break;
case 3:
ContactModify(&con);
break;
case 4:
ContactFind(con);
break;
case 5:
ContactPrint(con);
default:
break;
}
} while (input);
printf("正在退出...\n");
SaveContact(con);
}
int main()
{
Contact* con = NULL;
Test2(con);
ContactDestory(&con);
return 0;
}