數據結構與算法-順序表應用

一.通訊錄的創建

首先我們要理解的是通訊錄本身就是以順序表為底層的

只不過順序表中的數組,這里我們是用結構體來替代,用來存儲用戶的信息

由于是通訊錄的本質就是順序表,所以順序表的任何方法它都能套用

Contact.h:

#pragma once
#define Name_MAX 20
#define Gender_MAX 20
#define Tel_MAX 20
#define ADDR_MAX 100//定義聯系人數據 自定義結構體
//姓名 性別 年齡 電話 地址
typedef struct personInfo
{char name[Name_MAX];char gender[Gender_MAX];int age;char tel[Tel_MAX];char addr[ADDR_MAX];
}peoInfo;//重命名為peoInfo//把順序表改個名字,叫做通訊錄
//前置聲明,用結構體SL沒命名之前的聲明
//SL是在結構體創建后才改的名
typedef struct SeqList Contact;//初始化
void ContactInit(Contact* con);//銷毀
void ContactDesTroy(Contact*con);//添加數據
void ContactAdd(Contact* con);//刪除
void ContactDel(Contact* con);//修改
void ContactModify(Contact* con);
//
//查找
void ContactFind(Contact* con);
//
//展示
void ContactShow(Contact* con);

SeqList.h:

這個是順序表本身的運用代碼的頭文件

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
#include<string.h>
//定義順序表的結構//#define N 100
//
靜態順序表
//struct SeqList
//{
//	int arr[N];
//	int size;//有效數據個數
//};typedef peoInfo SLDataType;
//動態順序表
typedef struct SeqList
{SLDataType* arr;int size; //有效數據個數int capacity; //空間大小
}SL;//typedef struct SeqList SL;//順序表初始化
void SLInit(SL* ps);
//順序表的銷毀
void SLDestroy(SL* ps);
//void SLPrint(SL s);//頭部插入刪除 / 尾部插入刪除
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);void SLPopBack(SL* ps);
void SLPopFront(SL* ps);//指定位置之前插入/刪除數據
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);//查找數據
//int SLFind(SL* ps, SLDataType x);

Contact.c:

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
#include"Contact.h"void ContactInit(Contact* con)
{SLInit(con);
}void ContactDesTroy(Contact* con)
{SLDestroy(con);
}//添加數據
void ContactAdd(Contact* con)
{peoInfo info;printf("請輸入聯系人姓名:\n");scanf("%s", info.name);printf("請輸入聯系人性別:\n");scanf("%s", info.gender);printf("請輸入聯系人年齡:\n");scanf("%d", &info.age);printf("請輸入聯系人電話:\n");scanf("%s", info.tel);printf("請輸入聯系人地址:\n");scanf("%s", info.addr);//添加SLPushBack(con,info);
}//以名字為依據查找數據
int FindByName(Contact* con, char name[])
{for (int i = 0; i < con->size; i++){if (0 == strcmp(con->arr[i].name, name))return i;}return -1;
}//刪除
void ContactDel(Contact* con)
{//刪除的數據一定要存在才能刪除//查找(名字為依據)(依據不固定)char name[Name_MAX];printf("請輸入你要刪除的聯系人的名字:\n");scanf("%s", name);int find = FindByName(con, name);if (find < 0){printf("刪除失敗,聯系人不存在\n");return;}SLErase(con, find);printf("刪除成功\n");
}//展示
void ContactShow(Contact* con)
{printf("%s %s %s %s %s\n", "姓名", "性別", "年齡", "電話", "地址");for (int i = 0; i < con->size; i++){printf("%3s %3s  %3d  %3s  %3s\n",con->arr[i].name,con->arr[i].gender,con->arr[i].age,con->arr[i].tel,con->arr[i].addr);}
}//修改
void ContactModify(Contact* con)
{char name[Name_MAX];printf("請輸入你要修改的聯系人的名字:\n");scanf("%s", name);int find = FindByName(con, name);if (find < 0){printf("修改失敗,聯系人不存在\n");return;}printf("請輸入新的姓名:\n");scanf("%s", con->arr[find].name);printf("請輸入新的性別:\n");scanf("%s", con->arr[find].gender);printf("請輸入新的年齡:\n");scanf("%d", &con->arr[find].age);printf("請輸入新的電話:\n");scanf("%s", con->arr[find].tel);printf("請輸入新的住址:\n");scanf("%s", con->arr[find].addr);printf("修改成功!\n");
}//查找
void ContactFind(Contact* con)
{char name[Name_MAX];printf("請輸入要查找的聯系人姓名\n");scanf("%s", name);int find = FindByName(con, name);if (find < 0){printf("要查找的聯系人數據不存在!\n");return;}// 姓名 性別 年齡 電話  地址// 11   11   11   11   11printf("%s %s %s %s %s\n", "姓名", "性別", "年齡", "電話", "地址");printf("%3s   %3s   %3d   %3s   %3s\n", con->arr[find].gender,con->arr[find].age,con->arr[find].tel,con->arr[find].addr);
}

SeqList.c:

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void SLInit(SL* ps)
{ps->arr = NULL;ps->size = ps->capacity = 0;
}
//順序表的銷毀
void SLDestroy(SL* ps)
{if (ps->arr) //等價于  if(ps->arr != NULL){free(ps->arr);}ps->arr = NULL;ps->size = ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{//插入數據之前先看空間夠不夠if (ps->capacity == ps->size){//申請空間//malloc calloc realloc  int arr[100] --->增容realloc//三目表達式int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));//要申請多大的空間if (tmp == NULL){perror("realloc fail!");exit(1);//直接退出程序,不再繼續執行}//空間申請成功ps->arr = tmp;ps->capacity = newCapacity;}
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{溫柔的解決方式//if (ps == NULL)//{//	return;//}assert(ps); //等價與assert(ps != NULL)//ps->arr[ps->size] = x;//++ps->size;SLCheckCapacity(ps);ps->arr[ps->size++] = x;
}
//頭插
void SLPushFront(SL* ps, SLDataType x)
{assert(ps);SLCheckCapacity(ps);//先讓順序表中已有的數據整體往后挪動一位for (int i = ps->size; i > 0; i--){ps->arr[i] = ps->arr[i - 1];//arr[1] = arr[0]}ps->arr[0] = x;ps->size++;
}//void SLPrint(SL s)
//{
//	for (int i = 0; i < s.size; i++)
//	{
//		printf("%d ", s.arr[i]);
//	}
//	printf("\n");
//}
void SLPopBack(SL* ps)
{assert(ps);assert(ps->size);//順序表不為空//ps->arr[ps->size - 1] = -1;--ps->size;
}
void SLPopFront(SL* ps)
{assert(ps);assert(ps->size);//數據整體往前挪動一位for (int i = 0; i < ps->size - 1; i++){ps->arr[i] = ps->arr[i + 1]; //arr[size-2] = arr[size-1]}ps->size--;
}//指定位置之前添加
void SLInsert(SL* ps, int pos, SLDataType x)
{assert(ps);assert(pos >= 0 && pos <= ps->size);SLCheckCapacity(ps);for (int i = ps->size; i > pos; i--){ps->arr[i] = ps->arr[i - 1];}ps->arr[pos] = x;ps->size++;
}//刪除指定位置的數據
void SLErase(SL* ps, int pos)
{assert(ps);assert(pos >= 0 && pos < ps->size);for (int i = pos; i < ps->size - 1; i++){ps->arr[i] = ps->arr[i + 1];}ps->size--;
}查找數據
//int SLFind(SL* ps, SLDataType x)
//{
//	assert(ps);
//	int i = 0;
//	for (int i =0; i < ps->size; i++)
//	{
//		if (ps->arr[i] == x)
//			return i;
//	}
//	return -1;
//}

add.c:

這個是代碼運行的源文件

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
#include"Contact.h"//void SLTest01()
//{
//	SL sl;
//	SLInit(&sl);
//	SLPushBack(&sl, 1);
//	SLPushBack(&sl, 2);
//	SLPushBack(&sl, 3);
//	SLPushBack(&sl, 4);
//	SLPrint(sl);//1 2 3 4
//
//	SLPushFront(&sl, 5);
//	SLPushFront(&sl, 6);
//
//	β?
//	SLPopBack(&sl);
//	SLPrint(sl);//1 2 3 
//	SLPopBack(&sl);
//	SLPrint(sl);
//	SLPopBack(&sl);
//	SLPrint(sl);
//	SLPopBack(&sl);
//	SLPrint(sl);
//	SLPopFront(&sl);
//	SLPrint(sl);
//	...........
//	SLDestroy(&sl);
//}
//
//void SLTest02()
//{
//	SL sl;
//	SLInit(&sl);
//
//
//	SLPushBack(&sl, 1);
//	SLPushBack(&sl, 2);
//	SLPushBack(&sl, 3);
//	SLPushBack(&sl, 4);
//
//
//	/*SLInsert(&sl, 0, 5);*/
//
//	/*SLErase(&sl, 0);*/
//	/*SLPrint(sl);
//	int find=SLFind(&sl, 4);
//	if (find< 0)
//		printf("沒找到\n");
//	else
//		printf("找到了,下標是:%d\n",find);*/
//
//
//
//	SLDestroy(&sl);
//}void ContactTest01()
{Contact con;ContactInit(&con);ContactAdd(&con);ContactAdd(&con);ContactShow(&con);/*ContactDel(&con);*/ContactModify(&con);ContactShow(&con);ContactFind(&con);ContactDesTroy(&con);}//int main()
//{
//	/*SLTest01();*///SLTest02();//ContactTest01();
//	return 0;
//}int main()
{ContactTest01();
}

二.移除元素

代碼詳解:

int removeElement(int* nums, int numsSize, int val) {int str = 0 ;int dst = 0 ;while(str<numsSize){if(nums[str]==val){str++;}else{nums[dst++]=nums[str++];}}return dst;
}

tip:

此處使用雙指針的方法,還可以使用新的數組進行替換,但是題目要求受限

三.合并兩個有序數組

代碼詳解:

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) {int l1 = m - 1;int l2 = n - 1;int l3 = m + n - 1;while(l1 >= 0 && l2 >= 0 ){if(nums1[l1]<nums2[l2]){nums1[l3--]=nums2[l2--];}else{nums1[l3--]=nums1[l1--];}}while(l2>=0){nums1[l3 -- ] = nums2[l2--];}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/77907.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/77907.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/77907.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【C++】新手入門指南(下)

文章目錄 前言 一、引用 1.引用的概念和定義 2.引用的特性 3.引用的使用 4.const引用 5.指針和引用的關系 二、內聯函數 三、nullptr 總結 前言 這篇續上篇的內容新手入門指南&#xff08;上&#xff09;&#xff0c;繼續帶大家學習新知識。如果你感興趣歡迎訂購本專欄。 一、…

uniapp-商城-33-shop 布局搜索頁面以及u-search

shop頁面上有一個搜索&#xff0c;可以進行商品搜索&#xff0c;這里我們先做一個頁面布局&#xff0c;后面再來進行數據i聯動。 1、shop頁面的搜索 2、搜索的頁面代碼 <navigator class"searchView" url"/pagesub/pageshop/search/search"> …

SAP 采購訂單如何防止開票數量 大于 收貨數量呢

配置點如下&#xff1a; 事務碼&#xff1a;OMRM&#xff0c;配置如下 當過賬開票的數量 大于收貨數量的時候會提示如下&#xff1a;

Kotlin 的 suspend 關鍵字

更多相關知識 Kotlin 的 suspend 關鍵字是 Kotlin 協程的核心組成部分&#xff0c;它用于標記一個函數可以被掛起&#xff08;暫停執行&#xff09;并在稍后恢復執行&#xff0c;而不會阻塞線程。 理解 suspend 的作用需要從以下幾個方面入手&#xff1a; 1. 允許非阻塞的異步…

UDS診斷協議iso-14229 15765

Diagnostic request 形式多種: 1.SID+DID 2.SID+Sub-Func+DID 3.SID+Sub-Func SID占1個Byte,Sub-func占一個Byte,DID通常兩個Byte eg.10 01 (SID+Sub-Func) 10 03 05 02 (SID+Sub-Func+DID) 22 02 00 (SID+DID) 肯定響應抑制位 抑制正響應信息除非是收到NRC 0x78的情況下 不…

記錄一次使用面向對象的C語言封裝步進電機驅動

簡介 (2025/4/21) 本庫對目前僅針對TB6600驅動下的42步進電機的基礎功能進行了一定的封裝, 也是我初次嘗試以面向對象的思想去編寫嵌入式代碼, 和直流電機的驅動步驟相似在調用stepmotor_attach()函數和stepmotor_init()函數之后僅通過結構體數組stepm然后指定枚舉變量中的id即…

[創業之路-376]:企業法務 - 創業,不同的企業形態,個人承擔的風險、收益、稅費、成本不同

在企業法務領域&#xff0c;創業時選擇不同的企業形態&#xff0c;個人在風險承擔、收益分配、稅費負擔及運營成本方面存在顯著差異。以下從個人獨資企業、合伙企業、有限責任公司、股份有限公司四種常見形態展開分析&#xff1a; 一、個人承擔的風險 個人獨資企業 風險類型&…

GNOME桌面隱藏回收站和分區

dconf-editor 搜索 trash&#xff0c;關閉 show-trash 搜索 volumes&#xff0c;關閉 show-volumns

準確--Tomcat更換證書

具體意思是&#xff1a; Starting Coyote HTTP/1.1 on http-8080: HTTP 連接器&#xff08;端口 8080&#xff09;啟動成功了。嚴重: Failed to load keystore type PKCS12 with path conf/jlksearch.fzsmk.cn.pfx due to failed to decrypt safe contents entry: javax.crypt…

禁止ubuntu自動更新

由于ubuntu server和desktop版本都默認 啟動了&#xff0c;自動更新內核的操作。這對于生 產環境來說是不友好的。容易導致億賽通 無法啟動 默認開啟了內核自動更新所以我們關閉自 動內核更新。 1.禁止更新執行 sudo apt-mark hold linux-image-generic linux-headers-generic…

vue3 + element-plus中el-drawer抽屜滾動條回到頂部

el-drawer抽屜滾動條回到頂部 <script setup lang"ts" name"PerformanceLogQuery"> import { ref, nextTick } from "vue"; ...... // 詳情 import { performanceLogQueryByIdService } from "/api/performanceLog"; const onD…

【重走C++學習之路】16、AVL樹

目錄 一、概念 二、AVL樹的模擬實現 2.1 AVL樹節點定義 2.2 AVL樹的基本結構 2.3 AVL樹的插入 1. 插入步驟 2. 調節平衡因子 3. 旋轉處理 4. 開始插入 2.4 AVL樹的查找 2.5 AVL樹的刪除 1. 刪除步驟 2. 調節平衡因子 3. 旋轉處理 4. 開始刪除 結語 一、概念 …

char32_t、char16_t、wchar_t 用于 c++ 語言里存儲 unicode 編碼的字符,給出它們的具體定義

&#xff08;1&#xff09; #include <iostream> #include <string>int main() { std::u16string s u"C11 引入 char16_t"; // 定義 UTF-16 字符串for (char16_t c : s) // 遍歷輸出每個 char16_t 的值std::cout << std::hex << (…

redis數據類型-基數統計HyperLogLog

redis數據類型-基數統計HyperLogLog 文檔 redis單機安裝redis常用的五種數據類型redis數據類型-位圖bitmap 說明 官網操作命令指南頁面&#xff1a;https://redis.io/docs/latest/commands/?nameget&groupstringHyperLogLog介紹頁面&#xff1a;https://redis.io/docs…

邏輯思維:從混沌到秩序的理性推演在軟件開發中的應用

引言 在軟件開發的過程中&#xff0c;邏輯思維就像是開發者的“GPS導航”&#xff0c;幫助我們從混沌的需求中找到清晰的解決方案。想象一下&#xff0c;如果沒有邏輯思維&#xff0c;我們可能會在需求的海洋中迷失方向&#xff0c;最終寫出一堆“看似聰明但毫無意義”的代碼。…

Spring AI Alibaba Graph基于 ReAct Agent 的天氣預報查詢系統

1、在本示例中&#xff0c;我們僅為 Agent 綁定了一個天氣查詢服務&#xff0c;接收到用戶的天氣查詢服務后&#xff0c;流程會在 AgentNode 和 ToolNode 之間循環執行&#xff0c;直到完成用戶指令。示例中判斷指令完成的條件&#xff08;即 ReAct 結束條件&#xff09;也很簡…

HCIP(綜合實驗2)

1.實驗拓補圖 2.實驗要求 1.根據提供材料劃分VLAN以及IP地址&#xff0c;PC1/PC2屬于生產一部員工劃分VLAN10,PC3屬于生產二部劃分VLAN20 2.HJ-1HJ-2交換機需要配置鏈路聚合以保證業務數據訪問的高帶寬需求 3.VLAN的放通遵循最小VLAN透傳原則 4.配置MSTP生成樹解決二層環路問題…

使用 rebase 輕松管理主干分支

前言 最近遇到一個技術團隊的 dev 環境分支錯亂&#xff0c;因為是多人合作大家各自提交信息&#xff0c;導致出現很多交叉合并記錄&#xff0c;讓對應 log 看起來非常混亂&#xff0c;難以閱讀。 舉例說明 假設我們有一個項目&#xff0c;最初develop分支有 3 個提交記錄&a…

使用openssl為localhost創建自簽名

文章目錄 自簽名生成命令安裝安裝證書瀏覽器證書管理器 自簽名 生成命令 使用openssl生成私鑰和證書。 openssl req -x509 -newkey rsa:4096 -nodes -days 365 -subj "/CNlocalhost" -addext "subjectAltNameDNS:localhost" -keyout cert.key -out cer…

AI編程助手Cline之快速介紹

Cline 是一款深度集成在 Visual Studio Code&#xff08;VSCode&#xff09; 中的開源 AI 編程助手插件&#xff0c;旨在通過結合大語言模型&#xff08;如 Claude 3.5 Sonnet、DeepSeek V3、Google Gemini 等&#xff09;和工具鏈&#xff0c;為開發者提供自動化任務執行、智能…