/*嚴版資料結構 p23 演算法 2.4*/
#include
#include
#include
"f:\c_kind_project\datastruct\_privacy\_privacy.h"
intmain()
;for
(i=1
;i<
11;i++)if
(listinsert_sq
(l,i,i+10)
==-1)
i=1;while
(i<=l.length)
return0;
}
演算法2.4 順序表l第i個位置插入e
status listinsert_sq
(sqlist &l,
int i,
int e)
if(l.length>=l.listsize)
l.elem=newbase;
l.listsize+
=listincrease;
}int
*q=&
(l.elem[i-1]
);//找到插入位置
for(
int*p=
&l.elem[l.length-1]
;p>=q;
--p)
//移動元素!!!
*(p+1)
=*q;
*q=e;
++l.length;
printf
("插入成功;位置:%d;元素值:%d\n"
,i,e)
;return1;
}
插入位置是從1開始,而線性表儲存位址從0開始,所以需要 i -1 ;
int *q=&(l.elem[i-1]); 需要對指標有一定的理解可參見:指標
int *newbase = (int *)realloc(l.elem,(l.listsize+listincrease)*sizeof(int));
realloc函式用的很少,大概用法:
所謂移動元素,實際上是修改元素的值,將前乙個元素值賦給後乙個元素;
printf 是為了測試,實際不需要;
傳送門:
嚴蔚敏版資料結構-序言
嚴蔚敏版資料結構-順序表演算法總結
嚴蔚敏版資料結構 演算法2 7
include include include f c kind project datastruct privacy privacy.h void virgin sqlist l int main void void virgin sqlist l 演算法2.7 合併順序表,並將元素按從小到大排序...
資料結構 嚴蔚敏版 緒論
資料 所有能被輸入到計算機中,且能被計算機處理的符號的集合。資料元素 資料中的乙個 個體 資料結構中討論的基本單位。資料結構 帶結構的資料元素的集合。資料的邏輯結構可以分為 1.集合結構 資料元素關係 結構之間的元素同屬於乙個集合體 2.線性結構 資料元素關係 結構之間的元素存在一對一的關係 3.樹...
資料結構 嚴蔚敏
最近一直想找一本純資料結構的書來學習,找來找去都沒有找到一本合適的書籍,相比之下國內的書籍之中,嚴蔚敏和吳偉民的還算是經典版了,很多國內其他資料結構教材都參考這本書的。但缺點是很多都是偽 對程式設計初學者來說有一些難度,甚至有些考研的同學來看這本書有很多還看不懂,並且裡面也有些容易迷惑人的地方。出於...