上期回顧: 【數(shù)據(jù)結(jié)構(gòu)|C語(yǔ)言版】順序表
個(gè)人主頁(yè): C_GUIQU
前言
各位小伙伴大家好!上期小編給大家講解了數(shù)據(jù)結(jié)構(gòu)中的順序表,接下來(lái)講講順序表該如何應(yīng)用。
1. 基于動(dòng)態(tài)順序表實(shí)現(xiàn)通訊錄
1.1 通訊錄功能
(1)能夠保存聯(lián)系人的姓名、年齡、性別、電話(huà)、住址
(2)添加聯(lián)系人信息
(3)刪除聯(lián)系人信息
(4)修改聯(lián)系人信息
(5)查找聯(lián)系人信息
(6)查看通訊錄中所有聯(lián)系人信息
(7)清空通訊錄
(8)每次加載通訊錄時(shí)自動(dòng)載入歷史通訊錄,退出通訊錄后自動(dòng)保存通訊錄信息
1.2 代碼實(shí)現(xiàn)
1.2.1 SeqList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <Windows.h>
#include "Contact.h"
typedef Info SLDataType; //順序表元素種類(lèi)為存放個(gè)人信息的結(jié)構(gòu)體
typedef struct SeqList
{
SLDataType* a;
size_t size;
size_t capicity;
} SeqList;
// 順序表初始化
void SeqListInit(SeqList* psl);
// 檢查空間,如果滿(mǎn)了,進(jìn)行增容
void CheckCapacity(SeqList* psl);
// 順序表尾插
void SeqListPushBack(SeqList* psl, SLDataType x);
// 順序表刪除pos位置的值
void SeqListErase(SeqList* psl, size_t pos);
// 順序表銷(xiāo)毀
void SeqListDestory(SeqList* psl);
1.2.2 SeqList.c
#include "SeqList.h"
void SeqListDestory(SeqList* psl)
{
assert(psl);
free(psl->a);
psl->a = NULL;
psl->capicity = 0;
psl->size = 0;
}
void SeqListInit(SeqList* psl)
{
assert(psl);
psl->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
if (psl->a == NULL)
{
perror("malloc fail");
return;
}
psl->size = 0;
psl->capicity = 4;
}
void CheckCapacity(SeqList* psl)
{
assert(psl);
if (psl->size == psl->capicity)
{
SLDataType* tmp = (SLDataType*)realloc(psl->a, sizeof(SLDataType) * psl->capicity * 2);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
psl->a = tmp;
psl->capicity *= 2;
}
}
void SeqListPushBack(SeqList* psl, SLDataType x)
{
assert(psl);
CheckCapacity(psl);
psl->a[psl->size++] = x;
}
void SeqListErase(SeqList* psl, size_t pos)
{
assert(psl);
assert(0 <= pos && pos < psl->size);
while (pos < psl->size - 1)
{
psl->a[pos] = psl->a[pos + 1];
pos++;
}
psl->size--;
}
1.2.3 Contact.h
#pragma once
#define NAME_MAX 100
#define GENDER_MAX 10
#define TEL_MAX 11
#define ADDR_MAX 100
struct SqeList;
//因?yàn)檫@里不能聲明SeqList.h,不然會(huì)造成嵌套聲明,所以就單獨(dú)聲明一下順序表
typedef struct SeqList contact;
//要實(shí)現(xiàn)的是通訊錄,所以得把順序表?yè)Q個(gè)名,但換湯不換藥
typedef struct PersonInfo
{
char name[NAME_MAX]; //姓名
int age; //年齡
char gender[GENDER_MAX]; //性別
char telephone[TEL_MAX]; //電話(huà)
char address[ADDR_MAX]; //住址
}Info;
void InitContact(contact* pcon);//初始化通訊錄
void DestoryContact(contact* pcon);//銷(xiāo)毀通訊錄
int FindByName(contact* pcon, char* name);//通過(guò)姓名查找聯(lián)系人
void AddContact(contact* pcon);//添加聯(lián)系人
void DelContact(contact* pcon);//刪除聯(lián)系人
void ModifyContact(contact* pcon);//修改聯(lián)系人信息
void FindContact(contact* pcon);//查找聯(lián)系人
void ShowContact(contact* pcon);//展示聯(lián)系人列表
void ClearContact(contact* pcon);//清空通訊錄
void SaveContact(contact* pcon);//保存通訊錄
void LoadContact(contact* pcon);//載入歷史通訊錄
1.2.4 Contact.c
#include "SeqList.h"
void InitContact(contact* pcon)//初始化通訊錄
{
SeqListInit(pcon);
}
void DestoryContact(contact* pcon)//銷(xiāo)毀通訊錄
{
SeqListDestory(pcon);
}
int FindByName(contact* pcon, char* name)//通過(guò)姓名查找聯(lián)系人
{
for (size_t i = 0; i < pcon->size; i++)
{
if (strcmp(name, pcon->a[i].name) == 0)
{
return i;
}
}
return -1;
}
void AddContact(contact* pcon)//添加聯(lián)系人
{
CheckCapacity(pcon);
printf("請(qǐng)輸入姓名:\n");
scanf("%s", pcon->a[pcon->size].name);
printf("請(qǐng)輸入年齡:\n");
scanf("%d", &(pcon->a[pcon->size].age));
printf("請(qǐng)輸入性別:\n");
scanf("%s", pcon->a[pcon->size].gender);
printf("請(qǐng)輸入電話(huà):\n");
scanf("%s", pcon->a[pcon->size].telephone);
printf("請(qǐng)輸入住址:\n");
scanf("%s", pcon->a[pcon->size].address);
pcon->size++;
system("cls");
printf("添加成功!\n");
}
void DelContact(contact* pcon)//刪除聯(lián)系人
{
char name[100];
printf("請(qǐng)輸入要?jiǎng)h除的聯(lián)系人:\n");
scanf("%s", name);
int index = FindByName(pcon, name);
if (index == -1)
{
printf("要?jiǎng)h除的用戶(hù)不存在!\n");
return;
}
SeqListErase(pcon, index);
system("cls");
printf("刪除成功!\n");
}
void ModifyContact(contact* pcon)//修改聯(lián)系人信息
{
char name[100];
printf("請(qǐng)輸入要修改的聯(lián)系人:\n");
scanf("%s", name);
int index = FindByName(pcon, name);
if (index == -1)
{
printf("要修改的用戶(hù)不存在!\n");
return;
}
printf("請(qǐng)輸入修改后的姓名:\n");
scanf("%s", pcon->a[index].name);
printf("請(qǐng)輸入修改后的年齡:\n");
scanf("%d", &(pcon->a[index].age));
printf("請(qǐng)輸入修改后的性別:\n");
scanf("%s", pcon->a[index].gender);
printf("請(qǐng)輸入修改后的電話(huà):\n");
scanf("%s", pcon->a[index].telephone);
printf("請(qǐng)輸入修改后的住址:\n");
scanf("%s", pcon->a[index].address);
system("cls");
printf("修改成功!\n");
}
void FindContact(contact* pcon)//查找聯(lián)系人
{
char name[100];
printf("請(qǐng)輸入要查找的聯(lián)系人:\n");
scanf("%s", name);
int index = FindByName(pcon, name);
if (index == -1)
{
printf("要查找的用戶(hù)不存在!\n");
return;
}
system("cls");
printf("查找成功!\n");
printf("姓名:%s\n", pcon->a[index].name);
printf("年齡:%d\n", pcon->a[index].age);
printf("性別:%s\n", pcon->a[index].gender);
printf("電話(huà):%s\n", pcon->a[index].telephone);
printf("住址:%s\n", pcon->a[index].address);
}
void ShowContact(contact* pcon)//展示聯(lián)系人列表
{
if (pcon->size == 0)
{
printf("通訊錄為空!\n");
return;
}
printf("姓名 年齡 性別 電話(huà) 地址\n");
for (size_t i = 0; i < pcon->size; i++)
{
printf("%s %d %s %s %s\n",
pcon->a[i].name,
pcon->a[i].age,
pcon->a[i].gender,
pcon->a[i].telephone,
pcon->a[i].address);
}
}
void ClearContact(contact* pcon)//清空通訊錄
{
pcon->size = 0;
printf("通訊錄清空成功!\n");
}
void SaveContact(contact* pcon)//保存通訊錄
{
FILE* pf = fopen("contact.txt", "wb");
if (pf == NULL)
{
perror("fopen fail");
return;
}
for (size_t i = 0; i < pcon->size; i++)
{
fwrite(pcon->a + i, sizeof(Info), 1, pf);
}
printf("通訊錄數(shù)據(jù)保存成功!\n");
fclose(pf);
}
void LoadContact(contact* pcon)//載入歷史通訊錄
{
FILE* pf = fopen("contact.txt", "rb");
if (pf == NULL)
{
perror("fopen fail");
return;
}
Info info;
while (fread(&info, sizeof(Info), 1, pf))
{
SeqListPushBack(pcon, info);
}
printf("通訊錄數(shù)據(jù)載入成功!\n");
fclose(pf);
}
1.2.5 test.c
#include "SeqList.h"
void Menu()
{
printf("****************通訊錄******************\n");
printf("****** 1.添加聯(lián)系人 2.刪除聯(lián)系人 ******\n");
printf("****** 3.修改聯(lián)系人 4.查找聯(lián)系人 ******\n");
printf("****** 5.查看通訊錄 6.清空通訊錄 ******\n");
printf("****** 0.退出通訊錄 ******\n");
printf("****************************************\n");
}
int main()
{
contact con;
InitContact(&con); //初始化通訊錄
LoadContact(&con); //加載歷史通訊錄
int option = -1;
do {
Menu();
printf("請(qǐng)選擇:\n");
scanf("%d", &option);
system("cls"); //適當(dāng)?shù)那迤量雌饋?lái)更簡(jiǎn)潔
switch (option)
{
case 1:
//添加聯(lián)系人
AddContact(&con);
break;
case 2:
//刪除聯(lián)系人
DelContact(&con);
break;
case 3:
//修改聯(lián)系人
ModifyContact(&con);
break;
case 4:
//查找聯(lián)系人
FindContact(&con);
break;
case 5:
//查看通訊錄
ShowContact(&con);
break;
case 6:
//清空通訊錄
ClearContact(&con);
break;
case 0:
//退出通訊錄
printf("通訊錄退出中...\n");
break;
default:
printf("非法操作,請(qǐng)重新輸入\n");
break;
}
} while (option);
SaveContact(&con); //保存通訊錄
DestoryContact(&con); //銷(xiāo)毀通訊錄
return 0;
}
1.3 控制臺(tái)測(cè)試
1.3.1 添加聯(lián)系人
1.3.2 刪除聯(lián)系人
1.3.3 修改聯(lián)系人
1.3.4 查找聯(lián)系人
1.3.5 清空通訊錄
1.3.6 通訊錄讀檔和存檔
2. 好題測(cè)驗(yàn)
2.1 好題展示
【經(jīng)典算法OJ題1|移除元素】
【經(jīng)典算法OJ題2|合并兩個(gè)有序數(shù)組】
2.2 答案解析
【移除元素】
int removeElement(int* nums, int numsSize, int val)
{
int left = 0;
for(int right = 0; right < numsSize; right++)
{
if(nums[right] != val)
{
nums[left] = nums[right];
left++;
}
}
return left;
}
【合并兩個(gè)有序數(shù)組】文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-853116.html
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n)
{
int p1 = 0, p2 = 0;
int sorted[m + n];
int cur;
while (p1 < m || p2 < n)
{
if (p1 == m)
{
cur = nums2[p2++];
}
else if (p2 == n)
{
cur = nums1[p1++];
}
else if (nums1[p1] < nums2[p2])
{
cur = nums1[p1++];
}
else
{
cur = nums2[p2++];
}
sorted[p1 + p2 - 1] = cur;
}
for (int i = 0; i != m + n; ++i)
{
nums1[i] = sorted[i];
}
}
結(jié)語(yǔ)
以上就是小編對(duì)順序表應(yīng)用的講解。
如果覺(jué)得小編講的還可以,還請(qǐng)一鍵三連?;ト鼗?!
持續(xù)更新中~!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-853116.html
到了這里,關(guān)于【數(shù)據(jù)結(jié)構(gòu)|C語(yǔ)言版】順序表應(yīng)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!