c 實現順序表(資料結構)

2021-10-10 15:24:55 字數 3773 閱讀 3484

#pragma once        // 防止重複編譯

#include

#include

using namespace std;

template<

class

type

>

class

seqlist

bool isempty()

const

public

:void

push_back

(const type &x)

;//尾插;此const修飾函式形參,函式體內不能修改形參的值

void

push_front

(const type &x)

;//頭插

void

show_list()

;//顯示

void

pop_back()

;//尾刪

void

pop_front()

;//頭刪

void

insert_pos

(int pos,

const type &x)

;//按位置插

void

insert_val

(const type &x)

;//按值插

void

delete_pos

(int pos)

;//按位置刪

void

delete_val

(const type &x)

;//按值刪

int find

(const type &key)

;//按值查

int length()

const

;//求表長

void

clear()

;//清除資料

void

destroy()

;//摧毀該順序表

void

reserv()

;//反轉

void

sort

(/*int low,int high*/);

//排序

private

:enum

; type *base;

size_t capacity;

// size_t 32位系統中unsigned int(4位) ,64位系統中unsigned long(8位),用於計數,sizeof操作符的返回值型別為size_t

size_t size;};

template<

class

type

>

seqlist::

seqlist

(size_t sz)

template<

class

type

>

void seqlist::

push_back

(const type &x)

base[size++

]= x;}

template<

class

type

>

void seqlist::

push_front

(const type &x)

for(int i=size; i>0;

--i)

// 從後往前遍歷

base[0]

= x;

size++;}

template<

class

type

>

void seqlist::

show_list()

cout

class

type

>

void seqlist::

pop_back()

size = size-1;

} template<

class

type

>

void seqlist::

pop_front()

size--;}

template<

class

type

>

void seqlist::

insert_pos

(int pos,

const type &x)if(

isfull()

)for

(int i=size; i>pos;

--i)

base[pos]

= x;

size++;}

template<

class

type

>

void seqlist::

insert_val

(const type &x)

template<

class

type

>

void seqlist::

delete_pos

(int pos)

size--;}

template<

class

type

>

void seqlist::

delete_val

(const type &x)

for(int i=pos; i++i)

size--;}

template<

class

type

>

int seqlist::

find

(const type &key)

return-1

;}template<

class

type

>

int seqlist::

length()

const

template<

class

type

>

void seqlist::

clear()

} template<

class

type

>

void seqlist::

destroy()

template<

class

type

>

void seqlist::

reserv()

} template<

class

type

>

void seqlist::

sort()

//排序}}

/*template///快速排序

void seqlist::sort(int low,int high)

int first = low;

int last = high;

int key = base[first]; //用字表的第乙個記錄作為樞軸

while(first < last)

base[first] = base[last];//將比第乙個小的移到低端

while(first < last && base[first] <= key)

base[last] = base[first];//將比第乙個大的移到高階

}base[first] = key;//樞軸記錄到位

sort(low, first-1);

sort(first+1, high);

}*/

資料結構 用C 實現順序表

sequence list array 線性表的順序儲存結構,指的是用一段位址連續的儲存單元依次儲存線性表的資料元素 順序表為靜態儲存分配,需要事先確定容量 老師課上提供了乙個class模板,如下 那麼,我們現在可以根據這個模板開始造輪子了。實現思路很簡單,按照上面提供的函式用自己的方法一步步完善就...

C 資料結構 順序表

迴圈後面加 是個好行為,不然很容易犯低階錯誤 導致乙個變數的位置放錯了,看了很久沒看出bug 順序表 include includeusing namespace std const int maxsize 25 typedef struct seqlist int main cout endl r...

C 資料結構 順序表

順序表,顧名思義儲存在計算機指定記憶體區域的一塊連續的儲存結構,跟我們一起排隊做廣播體操的那種方式 儲存物理結構 物理記憶體空間上是連續的 儲存邏輯關係 儲存值之間的關係為一對一 使用場景 一般訪問資料量比較大,新增和刪除操作不頻繁的資料 那麼我們這裡實現的語言是用的c 對於線性表的一些特性我們這裡...