【数据结构与算法篇】单链表实现通讯录(近350行实现代码)
🥕个人主页:开敲🍉
🔥所属专栏:数据结构与算法🍅
目录
1. SListContact.h 头文件的声明
2. SListContact.c 源文件的定义
3. Test.c 源文件的测试
1. SListContact.h 头文件的声明
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
typedef struct Contact
{
char name[10];
char gender[5];
int age;
char tel[20];
char addr[20];
}Contact;
typedef struct SListContact
{
Contact val;
struct SListContact* next;
}SLC;
//初始化
void SLCInit(SLC** pslc);//打印
void SLCPrint(SLC* pslc);//尾插
void SLCPushBack(SLC** pslc);//头插
void SLCPushHead(SLC** pslc);
//尾删
void SLCPopBack(SLC** pslc);
//头删
void SLCPopHead(SLC** pslc);
//指定插入
void SLCPushDesi(SLC** pslc,int y);
//指定删除
void SLCPopDesi(SLC** pslc, int y);
//查找
void SLCFind(SLC** pslc,char* y);
//更改
void SLCChange(SLC** pslc, char* y);
2. SListContact.c 源文件的定义
#define _CRT_SECURE_NO_WARNINGS 1
#include "SListContact.h"
//初始化
void SLCInit(SLC** pslc)
{
SLC* ptmp = (SLC*)malloc(sizeof(SLC));
if (ptmp == NULL)
{
perror("malloc");
exit(-1);
}
*pslc = ptmp;
(*pslc)->next = NULL;
}//打印
void SLCPrint(SLC* pslc)
{
assert(pslc->next);
SLC* phead = pslc->next;
printf("姓名 性别 年龄 电话 地址\n");
while (phead)
{
printf("%s %s %d %s %s\n", phead->val.name,
phead->val.gender,
phead->val.age,
phead->val.tel,
phead->val.addr);
phead = phead->next;
}
printf("\n\n");
}//创建节点
SLC* SLCBuyNode()
{
SLC* ptmp = (SLC*)malloc(sizeof(SLC));
if (ptmp == NULL)
{
perror("malloc");
exit(-1);
}
ptmp->next = NULL;
return ptmp;
}//尾插
void SLCPushBack(SLC** pslc)
{
assert(*pslc);
SLC* phead = *pslc;
SLC* pget = SLCBuyNode();
while (phead->next)
{
phead = phead->next;
}
phead->next = pget;
printf("请输入要添加联系人的信息:\n");
scanf("%s %s %d %s %s", pget->val.name,
pget->val.gender,
&pget->val.age,
pget->val.tel,
pget->val.addr);
}
//头插
void SLCPushHead(SLC** pslc)
{
assert(*pslc);
SLC* phead = *pslc;
SLC* pget = SLCBuyNode();
pget->next = phead->next;
phead->next = pget;
printf("请输入要添加联系人的信息:\n");
scanf("%s %s %d %s %s", pget->val.name,
pget->val.gender,
&pget->val.age,
pget->val.tel,
pget->val.addr);
}//尾删
void SLCPopBack(SLC** pslc)
{
assert((*pslc)->next);
SLC* pcur = *pslc;
while (pcur->next->next)
{
pcur = pcur->next;
}
SLC* del = pcur->next;
pcur->next = NULL;
free(del);
del = NULL;
}
//头删
void SLCPopHead(SLC** pslc)
{
assert((*pslc)->next);
SLC* pcur = (*pslc)->next;
(*pslc)->next = pcur->next;
free(pcur);
pcur = NULL;
}
//寻找节点(姓名)
SLC* FindNodeName(SLC* pslc,char* y)
{
SLC* ptmp = pslc;
while (ptmp)
{
if (strcmp(ptmp->val.name, y) == 0)
{
return ptmp;
}
ptmp = ptmp->next;
}
return NULL;
}
//寻找节点(位置)
SLC* FindNodeAddr(SLC* pslc, int y)
{
SLC* ptmp = pslc->next;
int n = 1;
while (ptmp)
{
if (n == y)
{
return ptmp;
}
ptmp = ptmp->next;
n++;
}
return NULL;
}
//指定插入
void SLCPushDesi(SLC** pslc,int y)
{
assert(*pslc);
SLC* phead = *pslc;
SLC* pget = FindNodeAddr(phead, y);
if (pget == NULL)
{
printf("该位置不可插入数据!\n");
exit(-1);
}
SLC* ppush = SLCBuyNode();
while (phead->next != pget)
{
phead = phead->next;
}
ppush->next = pget;
phead->next = ppush;
printf("请输入要插入的联系人信息:\n");
scanf("%s %s %d %s %s", ppush->val.name,
ppush->val.gender,
&ppush->val.age,
ppush->val.tel,
ppush->val.addr);
}
//指定删除
void SLCPopDesi(SLC** pslc, int y)
{
assert((*pslc)->next);
SLC* phead = *pslc;
SLC* pget = FindNodeAddr(phead, y);
if (pget == NULL)
{
printf("该位置无可删除数据!\n");
exit(-1);
}
while (phead->next != pget)
{
phead = phead->next;
}
phead->next = pget->next;
free(pget);
pget = NULL;
}
//查找
void SLCFind(SLC** pslc,char* y)
{
assert(*pslc);
SLC* ptmp = *pslc;
SLC* pget = FindNodeName(ptmp, y);
if (pget == NULL)
{
printf("查无此人!\n");
exit(-1);
}
printf("查询成功,该联系人信息为:\n");
printf("%s %s %d %s %s\n\n", pget->val.name,
pget->val.gender,
pget->val.age,
pget->val.tel,
pget->val.addr);
}//更改
void SLCChange(SLC** pslc, char* y)
{
assert((*pslc)->next);
SLC* ptmp = *pslc;
SLC* pget = FindNodeName(ptmp, y);
if (pget == NULL)
{
printf("查无此人!\n");
exit(-1);
}
printf("请输入更改后的联系人信息:\n");
scanf("%s %s %d %s %s", pget->val.name,
pget->val.gender,
&pget->val.age,
pget->val.tel,
pget->val.addr);
}
3. Test.c 源文件的测试
#define _CRT_SECURE_NO_WARNINGS 1
#include "SListContact.h"
void SListContactTest()
{
//初始化
SLC* slc;
SLCInit(&slc);
//尾插
SLCPushBack(&slc);
SLCPushBack(&slc);
SLCPushBack(&slc);
SLCPrint(slc);
//头插
SLCPushHead(&slc);
SLCPrint(slc);
//尾删
SLCPopBack(&slc);
SLCPrint(slc);
//头删
SLCPopHead(&slc);
SLCPrint(slc);
//指定插入
SLCPushDesi(&slc,2);
SLCPrint(slc);
//指定删除
SLCPopDesi(&slc, 2);
SLCPrint(slc);
//查找
SLCFind(&slc, "zhangsan");
//更改
SLCChange(&slc, "zhangsan");
SLCPrint(slc);
}int main()
{
SListContactTest();
return 0;
}