新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
算法與數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)系列(二):順序表-創(chuàng)新互聯(lián)
目錄
當(dāng)前題目:算法與數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)系列(二):順序表-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://ef60e0e.cn/article/dhjcgi.html
- 一.順序表簡(jiǎn)介
- 二.問(wèn)題難點(diǎn)
- 1.基本結(jié)構(gòu)
- 2.插入
- 3.刪除
- 4.其它需注意的事項(xiàng)
- 三.代碼實(shí)現(xiàn)
- 總代碼
- 1.頭文件
- 2.初始化函數(shù)
- 3.頭插函數(shù)
- 4.尾插函數(shù)
- 5.按位置插函數(shù)
- 6.頭刪函數(shù)
- 7.尾刪函數(shù)
- 8.按位置刪函數(shù)
- 9.按值刪函數(shù)
- 10.查找函數(shù)
- 11.判滿函數(shù)
- 12.判空函數(shù)
- 13.擴(kuò)容函數(shù)
- 14.清空函數(shù)
- 15.銷(xiāo)毀函數(shù)
- 16.打印函數(shù)
- 主程序測(cè)試
- 順序表:簡(jiǎn)而言之,可以看作一個(gè)數(shù)組,把數(shù)據(jù)存儲(chǔ)在一段連續(xù)的空間中,邏輯地址是連續(xù)的,物理地址也是連續(xù)的。可以通過(guò)下標(biāo)方式直接訪問(wèn)到任意一個(gè)元素,適用于尾部操作(尾插或尾刪),因?yàn)闀r(shí)間復(fù)雜度為O(1),若在其余地方插入或刪除,則需要挪動(dòng)一次挪動(dòng)元素,時(shí)間復(fù)雜度提升至O(n)。
- 插入就是先將所有元素向后挪動(dòng)一個(gè)格子,再將值插到該插的地方
- 刪除就是將待刪除元素的后面元素向前覆蓋
- 每次需判斷函數(shù)傳過(guò)來(lái)的指針是否為空,否則對(duì)NULL解引用會(huì)出錯(cuò)
- 插入需判滿,刪除需判空
- 尾刪的時(shí)候,只需長(zhǎng)度-1即可,判斷該位置是否有元素,取決于length
#include#include
#include#include#define LIST_INIT_SIZE 100
typedef int ELEM_TYPE;
typedef struct Sqlist
{ELEM_TYPE* elem;
int length;
int listsize;
}Sqlist, * PSqlist;
//初始化
void Init_Sqlist(PSqlist sq);
//頭插
bool Insert_head(PSqlist sq, ELEM_TYPE val);
//尾插
bool Insert_tail(PSqlist sq, ELEM_TYPE val);
//按位置插
bool Insert_pos(PSqlist sq, int pos, ELEM_TYPE val);
//頭刪
bool Del_head(PSqlist sq);
//尾刪
bool Del_tail(PSqlist sq);
//按位置刪
bool Del_pos(PSqlist sq, int pos);
//按值刪
bool Del_val(PSqlist sq, ELEM_TYPE val);
//查找
int Search(PSqlist sq, ELEM_TYPE val);
//判滿
bool IsFull(PSqlist sq);
//判空
bool IsEmpty(PSqlist sq);
//擴(kuò)容
void Inc(PSqlist sq);
//清空
bool Clear(PSqlist sq);
//銷(xiāo)毀
bool Destory(PSqlist sq);
//打印
void Show(PSqlist sq);
//初始化
void Init_Sqlist(PSqlist sq)
{assert(sq != NULL);
sq->elem = (ELEM_TYPE*)malloc(sizeof(ELEM_TYPE) * LIST_INIT_SIZE);
assert(sq->elem != NULL);
memset(sq->elem, 0, sizeof(ELEM_TYPE) * LIST_INIT_SIZE);
sq->length = 0;
sq->listsize = LIST_INIT_SIZE;
}
//頭插
bool Insert_head(PSqlist sq, ELEM_TYPE val)//不論頭插尾插,插都要判滿
{assert(sq != NULL);
if (IsFull(sq))
{Inc(sq);
}
for (int i = sq->length - 1; i >= 0; i--) //頭也要挪 別想當(dāng)然
{sq->elem[i + 1] = sq->elem[i];
}
sq->elem[0] = val;
sq->length++;
return true;
}
//尾插
bool Insert_tail(PSqlist sq, ELEM_TYPE val)
{assert(sq != NULL);
if (IsFull(sq))
{Inc(sq);
}
sq->elem[sq->length++] = val;
return true;
}
//按位置插
bool Insert_pos(PSqlist sq, int pos,ELEM_TYPE val)
{assert(sq != NULL);
assert(0<= pos && pos< sq->length); //判斷pos合法性
if (IsFull(sq))
{Inc(sq);
}
for (int i = sq->length - 1; i >= pos; i--)
{sq->elem[i + 1] = sq->elem[i];
}
sq->elem[pos] = val;
sq->length++;
return true;
}
//頭刪
bool Del_head(PSqlist sq)//刪要判空
{assert(sq != NULL);
if (IsEmpty(sq))
{return false;
}
for (int i = 1; i< sq->length; i++)//注意是< 而不是<=,length不用往前走,要走的是length-1,它才是最后一個(gè)
{
sq->elem[i - 1] = sq->elem[i]; //注意雖然只有一個(gè)元素時(shí),沒(méi)有覆蓋,但length--了,其實(shí)有沒(méi)有都無(wú)所謂,
} //決定是否存在取決于length的大小
sq->length--;
return true;
}
//尾刪
bool Del_tail(PSqlist sq)
{assert(sq != NULL);
if (IsEmpty(sq))
{return false;
}
sq->length--; //取決于是否存在,在于length的大小,存在與否不是看你有沒(méi)有值,況且本來(lái)就有值,也不是看值為0
return true; //那有些數(shù)據(jù)本身就是0,所以存在與否取決于length
}
//按位置刪
bool Del_pos(PSqlist sq, int pos)
{assert(sq != NULL);
assert(pos >= 0 && pos< sq->length);
if (IsEmpty(sq))
{return false;
}
for (int i = pos + 1; i< sq->length; i++)
{sq->elem[i - 1] = sq->elem[i];
}
sq->length--;
return true;
}
//按值刪
bool Del_val(PSqlist sq, ELEM_TYPE val)
{assert(sq != NULL);
int pos = Search(sq, val);
if (pos == -1)
{return false;
}
return Del_pos(sq, pos);
}
//查找
int Search(PSqlist sq, ELEM_TYPE val)
{assert(sq != NULL);
for (int i = 0; i< sq->length; i++)
{if (sq->elem[i] == val)
{ return i;
}
}
return -1;
}
//判滿
bool IsFull(PSqlist sq)
{assert(sq != NULL);
return sq->length == sq->listsize;
}
//判空
bool IsEmpty(PSqlist sq)
{assert(sq != NULL);
return sq->length == 0;
}
//擴(kuò)容 按2倍擴(kuò)容
void Inc(PSqlist sq)
{assert(sq != NULL);
ELEM_TYPE* new_elem = (ELEM_TYPE*)realloc(sq->elem,sizeof(ELEM_TYPE) * sq->listsize * 2);
assert(new_elem != NULL);
sq->elem = new_elem;
sq->listsize *= 2;
}
//清空
bool Clear(PSqlist sq)
{assert(sq != NULL);
sq->length = 0;
return true;
}
//銷(xiāo)毀
bool Destory(PSqlist sq)
{assert(sq != NULL);
free(sq->elem);
sq->elem = NULL; //避免使用達(dá)不到效果,改變其它程序占有此內(nèi)存的數(shù)據(jù)
sq->length = sq->listsize = 0;
return true;
}
//打印
void Show(PSqlist sq)
{assert(sq != NULL);
for (int i = 0; i< sq->length; i++)
{printf("%d ", sq->elem[i]);
}
printf("\n");
}
int main()
{Sqlist sq;
Init_Sqlist(&sq);
for (int i = 0; i< 10; i++)
{Insert_tail(&sq, i + 1);
}
Show(&sq);// 1 2 3 4 5 6 7 8 9 10
Insert_head(&sq, 100);
Insert_pos(&sq, 0, 200);
Show(&sq);//200 100 1 2 3 4 5 6 7 8 9 10
Del_head(&sq);
Del_tail(&sq);
Del_pos(&sq, 0);
Del_val(&sq, 4);
Show(&sq);//1 2 3 5 6 7 8 9
return 0;
}
1.頭文件我們是通過(guò)malloc函數(shù)動(dòng)態(tài)開(kāi)辟內(nèi)存空間給到elem,然后可以通過(guò)下標(biāo)訪問(wèn)到每一個(gè)空間。因?yàn)槭莿?dòng)態(tài)開(kāi)辟的,所以數(shù)組的長(zhǎng)度等信息都是不斷發(fā)生改變的,所以我們?cè)O(shè)計(jì)了這樣的結(jié)構(gòu)體,把數(shù)組的屬性實(shí)時(shí)賦值給相應(yīng)變量
#define LIST_INIT_SIZE 100
typedef int ELEM_TYPE;
typedef struct Sqlist
{ELEM_TYPE* elem;
int length; //當(dāng)前有效長(zhǎng)度
int listsize; //總空間大小
}Sqlist,*PSqlist;
2.初始化函數(shù)初始化就是開(kāi)辟空間、賦初值
//初始化
void Init_Sqlist(PSqlist sq)
{assert(sq != NULL);
sq->elem = (ELEM_TYPE*)malloc(sizeof(ELEM_TYPE) * LIST_INIT_SIZE);
assert(sq->elem != NULL);
memset(sq->elem, 0, sizeof(ELEM_TYPE) * LIST_INIT_SIZE);
sq->length = 0;
sq->listsize = LIST_INIT_SIZE;
}
3.頭插函數(shù)//頭插
bool Insert_head(PSqlist sq, ELEM_TYPE val)//不論頭插尾插,插都要判滿
{assert(sq != NULL);
if (IsFull(sq))
{Inc(sq);
}
for (int i = sq->length - 1; i >= 0; i--) //頭也要挪 別想當(dāng)然
{sq->elem[i + 1] = sq->elem[i];
}
sq->elem[0] = val;
sq->length++;
return true;
}
4.尾插函數(shù)//尾插
bool Insert_tail(PSqlist sq, ELEM_TYPE val)
{assert(sq != NULL);
if (IsFull(sq))
{Inc(sq);
}
sq->elem[sq->length++] = val;
return true;
}
5.按位置插函數(shù)//按位置插
bool Insert_pos(PSqlist sq, int pos,ELEM_TYPE val)
{assert(sq != NULL);
assert(0<= pos && pos< sq->length); //判斷pos合法性
if (IsFull(sq))
{Inc(sq);
}
for (int i = sq->length - 1; i >= pos; i--)
{sq->elem[i + 1] = sq->elem[i];
}
sq->elem[pos] = val;
sq->length++;
return true;
}
6.頭刪函數(shù)//頭刪
bool Del_head(PSqlist sq)//刪要判空
{assert(sq != NULL);
if (IsEmpty(sq))
{return false;
}
for (int i = 1; i< sq->length; i++)//注意是< 而不是<=,length不用往前走,要走的是length-1,它才是最后一個(gè)
{
sq->elem[i - 1] = sq->elem[i]; //注意雖然只有一個(gè)元素時(shí),沒(méi)有覆蓋,但length--了,其實(shí)有沒(méi)有都無(wú)所謂,
} //決定是否存在取決于length的大小
sq->length--;
return true;
}
7.尾刪函數(shù)//尾刪
bool Del_tail(PSqlist sq)
{assert(sq != NULL);
if (IsEmpty(sq))
{return false;
}
sq->length--; //取決于是否存在,在于length的大小,存在與否不是看你有沒(méi)有值,況且本來(lái)就有值,也不是看值為0
return true; //那有些數(shù)據(jù)本身就是0,所以存在與否取決于length
}
8.按位置刪函數(shù)//按位置刪
bool Del_pos(PSqlist sq, int pos)
{assert(sq != NULL);
assert(pos >= 0 && pos< sq->length);
if (IsEmpty(sq))
{return false;
}
for (int i = pos + 1; i< sq->length; i++)
{sq->elem[i - 1] = sq->elem[i];
}
sq->length--;
return true;
}
9.按值刪函數(shù)//按值刪
bool Del_val(PSqlist sq, ELEM_TYPE val)
{assert(sq != NULL);
int pos = Search(sq, val);
if (pos == -1)
{return false;
}
return Del_pos(sq, pos);
}
10.查找函數(shù)//查找
int Search(PSqlist sq, ELEM_TYPE val)
{assert(sq != NULL);
for (int i = 0; i< sq->length; i++)
{if (sq->elem[i] == val)
{ return i;
}
}
return -1;
}
11.判滿函數(shù)//判滿
bool IsFull(PSqlist sq)
{assert(sq != NULL);
return sq->length == sq->listsize;
}
12.判空函數(shù)//判空
bool IsEmpty(PSqlist sq)
{assert(sq != NULL);
return sq->length == 0;
}
13.擴(kuò)容函數(shù)//擴(kuò)容 按2倍擴(kuò)容
void Inc(PSqlist sq)
{assert(sq != NULL);
ELEM_TYPE* new_elem = (ELEM_TYPE*)realloc(sq->elem,sizeof(ELEM_TYPE) * sq->listsize * 2);
assert(new_elem != NULL);
sq->elem = new_elem;
sq->listsize *= 2;
}
14.清空函數(shù)//清空
bool Clear(PSqlist sq)
{assert(sq != NULL);
sq->length = 0;//長(zhǎng)度為0,意味著沒(méi)有元素了,如果要賦值為0,也沒(méi)有必要,因?yàn)橛行┰氐闹稻褪?,所以不能當(dāng)成判別標(biāo)準(zhǔn)
return true;
}
15.銷(xiāo)毀函數(shù)//銷(xiāo)毀
bool Destory(PSqlist sq)
{assert(sq != NULL);
free(sq->elem);
sq->elem = NULL; //避免使用達(dá)不到效果,改變其它程序占有此內(nèi)存的數(shù)據(jù)
sq->length = sq->listsize = 0;
return true;
}
16.打印函數(shù)//打印
void Show(PSqlist sq)
{assert(sq != NULL);
for (int i = 0; i< sq->length; i++)
{printf("%d ", sq->elem[i]);
}
printf("\n");
}
主程序測(cè)試int main()
{Sqlist sq;
Init_Sqlist(&sq);
for (int i = 0; i< 10; i++)
{Insert_tail(&sq, i + 1);
}
Show(&sq);// 1 2 3 4 5 6 7 8 9 10
Insert_head(&sq, 100);
Insert_pos(&sq, 0, 200);
Show(&sq);//200 100 1 2 3 4 5 6 7 8 9 10
Del_head(&sq);
Del_tail(&sq);
Del_pos(&sq, 0);
Del_val(&sq, 4);
Show(&sq);//1 2 3 5 6 7 8 9
return 0;
}
測(cè)試結(jié)果
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
當(dāng)前題目:算法與數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)系列(二):順序表-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://ef60e0e.cn/article/dhjcgi.html