malloc詳解
線性表是乙個具有相同特性的資料元素的有限序列。線性表的邏輯表示為:
(a1,a2,…,ai , ai+1,…,an)
線性表的9個運算
線性表作用:存放資料 基本運算
如果是輸出型引數,要加&
把線性表中的所有元素按照順序儲存 方法進行儲存。
c語言複習全部**見檔案listsequence.cppmalloc 返回的是void* 所以需要 轉換一下 l = (sqlist*)malloc(sizeof(sqlist));
關於結構體,(*指標變數名).成員名 變數名.成員名 指標變數名->成員名
// 2.6 求某個元素的值
bool getelem
(sqlist *l,
int i, elemtype &e)
else
}
本演算法的時間複雜度為o(1)
體現順序表的隨機訪問特性
// 2.8 插入
bool listinsert
(sqlist*
&l,int i,
int e)
else
l->data[i]
=e; l->length++
;return true;
}}
刪除
bool listdelete
(sqlist*
&l,int i, elemtype &e)
else
l->length--
;return true;
}}
以下為全部**
#include
#include
#include
#define maxsize 20
using namespace std;
typedef
int elemtype;
// 順序表 定義
typedef
struct
sqlist;
//1、 建立順序表
void
createlist
(sqlist*
&l,elemtype a,
int n)
l->length=n;
}// 2、順序表基本運算演算法
// 2.1 初始化線性表
// 要動態分配記憶體* 輸出型引數&
void
initlist
(sqlist*
&l)// 2.2 銷毀線性表
void
destroylist
(sqlist*
&l)// 2.3 判定是否為空表
bool listempty
(sqlist* l)
// 2.4 輸出線性表長度
intlistlength
(sqlist*
&l)// 2.5 輸出線性表
void
displist
(sqlist*
&l)printf
("\n");
}// 2.6 求某個元素的值
bool getelem
(sqlist *l,
int i, elemtype &e)
else
}// 2.7 按元素值查詢
intlocateelem
(sqlist* l,
int e)
if(i>=l->length)
return0;
else
return i+1;
}// 2.8 插入
bool listinsert
(sqlist*
&l,int i,
int e)
else
l->data[i]
=e; l->length++
;return true;}}
刪除bool listdelete
(sqlist*
&l,int i, elemtype &e)
else
l->length--
;return true;}}
intmain()
; sqlist s1,s2;
sqlist* l1=
&s1;
sqlist* l2=
&s2;
createlist
(l1,a,3)
;// 這裡如果直接用&s會出錯 為啥呢不知道
initlist
(l2)
; cout<<
listlength
(l1)
<
cout<<
listempty
(l2)
<
displist
(l1)
;int e;
getelem
(l1,
2,e)
;printf
("%d\n"
,e);
printf
("%d\n"
,locateelem
(l1,e));
listinsert
(l1,1,
4);displist
(l1)
;listdelete
(l1,
2,e)
;displist
(l1)
;printf
("%d\n"
,e);
destroylist
(l2)
;destroylist
(l1)
;}
資料結構線性表 上
鍊錶 鍊錶是一種有序的列表,鍊錶的內容通常是存貯於記憶體中分散的位置上。而鍊錶串聯的方式有兩種 0 1 2 3 4 5 6 7 8 9 isdata this aarray 6 8 1 39 不過這種鍊錶最大的缺點在於放入或刪除元素,常常要大量的移動其他元素,而且陣列的大小是固定的,缺乏使用彈性。2...
資料結構 線性表 2
package com.wjy.data structure.linearlist.common public inte ce nodepackage com.wjy.data structure.linearlist.common 單鏈表結點定義 public class slnode imple...
資料結構2 線性表
什麼是線性表 邏輯上具有線性結構的儲存結構 線性表的特點 線性表中每個元素型別相同 線性表分類 根據物理結構,分為順序儲存和鏈式儲存 順序儲存 順序表 順序表的特點 快速隨機訪問,查詢和修改效率高,增刪效率低 順序表的實現 1 定義順序表頭,2 順序表初始化,3 實現順序表的操作 順序表的操作 增加...