C語言全面講解順序表使用操作
編程環(huán)境為 ubuntu 18.04。
順序表需要連續(xù)一片存儲空間,存儲任意類型的元素,這里以存儲 int 類型數(shù)據(jù)為例。
一、順序表的結(jié)構(gòu)定義
size 為容量,length 為當(dāng)前已知數(shù)據(jù)表元素的個(gè)數(shù)
typedef struct Vector{ int *data; //該順序表這片連續(xù)空間的首地址 int size, length; } Vec;
二、順序表的結(jié)構(gòu)操作
1.初始化
Vec *init(int n){ //該順序表具有n個(gè)存儲單元 Vec *v = (Vec *)malloc(sizeof(Vec)); //在內(nèi)存棧上開辟一個(gè)空間 malloc在內(nèi)存的堆區(qū),在函數(shù)外面也能訪問 v->data = (int *)malloc(sizeof(int) * n); v->size = n; v->length = 0; return v; }
2.插入操作
int insert(Vec *v, int ind, int val) { //ind為插入元素的位置,val為插入元素的值 if(v == NULL) return 0; if(ind < 0 || ind > v->length) return 0; //判斷要插入的位置是否合法 if(v->length == v->size) { if(!expand(v)){ //擴(kuò)容失敗 printf(RED("fail to expand!\n")); } printf(GREEN("success to expand! the size = %d\n"),v->size); } for(int i = v->length; i > ind; i--){ v->data[i] = v->data[i-1]; } v->data[ind] = val; v->length += 1; return 1; }
為什么需要判斷插入的位置是否合法呢?這是因?yàn)轫樞虮硎沁B續(xù)一片存儲空間,所以內(nèi)存是連續(xù)的。
下圖以 length = 5,size = 9 為例,我們只能在下標(biāo)為 0 到 4 之間的數(shù)中插入數(shù)據(jù)。
插入一個(gè)元素示意圖
3.刪除操作
int erase(Vec *v, int ind){ //把下標(biāo)為ind的元素刪除 if(v == NULL) return 0; if(ind < 0 || ind >= v->length) return 0; for(int i = ind + 1; i < v->length; i++){ v->data[i - 1] = v->data[i]; } v->length -= 1; return 1; }
- 判斷需要?jiǎng)h除元素的下標(biāo)是否合法,與插入元素類似
- 刪除一個(gè)元素示意圖
4.擴(kuò)容操作
int expand(Vec *v){ //順序表的擴(kuò)容 //malloc 動(dòng)態(tài)申請空間,空間不一定干凈 calloc 動(dòng)態(tài)申請空間,并且清空 realloc 重新申請空間 int extr_size = v->size; int *p; while(extr_size) { p = (int *)realloc(v->data, sizeof(int) * (v->size + extr_size)); if(p != NULL) break; //p不為空,說明擴(kuò)容成功,這個(gè)時(shí)候直接跳出循環(huán) extr_size >>= 1; //否則就把額外擴(kuò)容的空間除以2,降低要求 } if(p == NULL) return 0; //判斷跳出循環(huán)究竟是擴(kuò)容成功還是擴(kuò)容失敗,如果擴(kuò)容失敗,那就是p為空地址,找不到符合條件的內(nèi)存區(qū)域 v->size += extr_size; v->data = p; return 1; }
注意擴(kuò)容這里寫的比較巧妙,首先 int extr_size = v->size;表示先將需要擴(kuò)容的大小設(shè)置成原本的大小,然后就判斷能不能找到那么大的空間。 p = (int *)realloc(v->data, sizeof(int) * (v->size + extr_size)); 如果在系統(tǒng)中能找到這么大的容量,那么就返回找到的內(nèi)存地址的首地址,然后就可以結(jié)束跳出循環(huán);要是找不到的話那只能降低要求,把 extr_size 除以 2,看看能不能知道,如果實(shí)在找不到,extr_size 為 0,就會跳出循環(huán)。然后可以通過判斷 p 是不是空指針來判斷程序是找到能夠擴(kuò)容的空間退出的還是找不到退出的。
要是對 malloc、calloc 和 realloc 不熟悉的,可以看我這篇博文:C語言深入探索動(dòng)態(tài)內(nèi)存分配的使用
5.釋放操作
void clear(Vec *v){ //釋放空間 if(v == NULL) return; free(v->data); free(v); return; }
先釋放數(shù)據(jù),再釋放整個(gè)順序表。
6.輸出
void output(Vec *v){ if(v == NULL) return ; printf("["); for(int i = 0; i < v->length; i++){ i && printf(", "); printf("%d", v->data[i]); } printf("]\n"); return ; }
三、示例
#include <stdio.h> #include<stdlib.h> #include<time.h> //#include<windows.h> #define COLOR(a, b) "\033[" #b "m" a "\033[0m" #define GREEN(a) COLOR(a, 32) #define RED(a) COLOR(a, 31) typedef struct Vector{ int *data; //該順序表這片連續(xù)空間的首地址 int size, length; } Vec; Vec *init(int n){ //該順序表具有n個(gè)存儲單元 Vec *v = (Vec *)malloc(sizeof(Vec)); //在內(nèi)存棧上開辟一個(gè)空間 malloc在內(nèi)存的堆區(qū),在函數(shù)外面也能訪問 v->data = (int *)malloc(sizeof(int) * n); v->size = n; v->length = 0; return v; } int expand(Vec *v){ //順序表的擴(kuò)容 //malloc 動(dòng)態(tài)申請空間,空間不一定干凈 calloc 動(dòng)態(tài)申請空間,并且清空 realloc 重新申請空間 int extr_size = v->size; int *p; while(extr_size) { p = (int *)realloc(v->data, sizeof(int) * (v->size + extr_size)); if(p != NULL) break; //p不為空,說明擴(kuò)容成功,這個(gè)時(shí)候直接跳出循環(huán) extr_size >>= 1; //否則就把額外擴(kuò)容的空間除以2,降低要求 } if(p == NULL) return 0; //判斷跳出循環(huán)究竟是擴(kuò)容成功還是擴(kuò)容失敗,如果擴(kuò)容失敗,那就是p為空地址,找不到符合條件的內(nèi)存區(qū)域 v->size += extr_size; v->data = p; return 1; } int insert(Vec *v, int ind, int val) { //ind為插入元素的位置,val為插入元素的值 if(v == NULL) return 0; if(ind < 0 || ind > v->length) return 0; if(v->length == v->size) { if(!expand(v)){ printf(RED("fail to expand!\n")); } printf(GREEN("success to expand! the size = %d\n"),v->size); } for(int i = v->length; i > ind; i--){ v->data[i] = v->data[i-1]; } v->data[ind] = val; v->length += 1; return 1; } int erase(Vec *v, int ind){ //把下標(biāo)為ind的元素刪除 if(v == NULL) return 0; if(ind < 0 || ind >= v->length) return 0; for(int i = ind + 1; i < v->length; i++){ v->data[i - 1] = v->data[i]; } v->length -= 1; return 1; } void output(Vec *v){ if(v == NULL) return ; printf("["); for(int i = 0; i < v->length; i++){ i && printf(", "); printf("%d", v->data[i]); } printf("]\n"); return ; } void clear(Vec *v){ //釋放空間 if(v == NULL) return; free(v->data); free(v); return; } int main(){ #define MAX_N 20 Vec *v = init(1); srand(time(0)); //設(shè)置種子 for (int i = 0; i < MAX_N; i++){ int op = rand() % 4; int ind = rand() % (v->length + 3) - 1; //取值范圍[-1, v->length + 1] int val = rand() % 100; //val為1到99之間的數(shù) switch(op){ case 0: case 1: case 2: { printf("insert %d at %d to the Vector = %d\n", val, ind, insert(v, ind, val)); }break; case 3:{ printf("erase a item at %d = %d\n",ind,erase(v, ind)); }break; } output(v); printf("\n"); } #undef MAX_N clear(v); return 0; }
輸出結(jié)果如下:
insert 82 at 0 to the Vector = 1
[82]
insert 38 at 2 to the Vector = 0
[82]
success to expand! the size = 2
insert 7 at 1 to the Vector = 1
[82, 7]
success to expand! the size = 4
insert 86 at 2 to the Vector = 1
[82, 7, 86]
erase a item at 4 = 0
[82, 7, 86]
erase a item at 4 = 0
[82, 7, 86]
insert 48 at 0 to the Vector = 1
[48, 82, 7, 86]
insert 65 at 5 to the Vector = 0
[48, 82, 7, 86]
success to expand! the size = 8
insert 92 at 4 to the Vector = 1
[48, 82, 7, 86, 92]
erase a item at 2 = 1
[48, 82, 86, 92]
insert 81 at 2 to the Vector = 1
[48, 82, 81, 86, 92]
insert 9 at 0 to the Vector = 1
[9, 48, 82, 81, 86, 92]
insert 99 at 1 to the Vector = 1
[9, 99, 48, 82, 81, 86, 92]
insert 29 at 7 to the Vector = 1
[9, 99, 48, 82, 81, 86, 92, 29]
success to expand! the size = 16
insert 38 at 0 to the Vector = 1
[38, 9, 99, 48, 82, 81, 86, 92, 29]
erase a item at 0 = 1
[9, 99, 48, 82, 81, 86, 92, 29]
erase a item at 8 = 0
[9, 99, 48, 82, 81, 86, 92, 29]
erase a item at 6 = 1
[9, 99, 48, 82, 81, 86, 29]
insert 57 at -1 to the Vector = 0
[9, 99, 48, 82, 81, 86, 29]
insert 32 at 4 to the Vector = 1
[9, 99, 48, 82, 32, 81, 86, 29]
到此這篇關(guān)于C語言全面講解順序表使用操作的文章就介紹到這了,更多相關(guān)C語言順序表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
opencv+arduino實(shí)現(xiàn)物體點(diǎn)追蹤效果
這篇文章主要為大家詳細(xì)介紹了opencv+arduino實(shí)現(xiàn)物體點(diǎn)追蹤效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01C語言數(shù)據(jù)結(jié)構(gòu)之二叉樹的非遞歸后序遍歷算法
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之二叉樹的非遞歸后序遍歷算法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10使用ShellClass獲取文件屬性詳細(xì)信息的實(shí)現(xiàn)方法
本篇文章是對ShellClass獲取文件屬性詳細(xì)信息的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05QT網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實(shí)例講解)
下面小編就為大家?guī)硪黄猀T網(wǎng)絡(luò)編程UDP下C/S架構(gòu)廣播通信(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07C++程序中使用Windows系統(tǒng)Native Wifi API的基本教程
這篇文章主要介紹了C++程序中使用Windows系統(tǒng)Native Wifi API的基本教程,包括在程序中控制無線網(wǎng)卡開關(guān)的方法,需要的朋友可以參考下2016-03-03Matlab利用隨機(jī)森林(RF)算法實(shí)現(xiàn)回歸預(yù)測詳解
這篇文章主要為大家詳細(xì)介紹了Matlab如何利用隨機(jī)森林(RF)算法實(shí)現(xiàn)回歸預(yù)測,以及自變量重要性排序的操作,感興趣的小伙伴可以了解一下2023-02-02