一 (1)C 實現簡單的線性表(順序儲存結構)

2021-06-20 21:02:59 字數 1712 閱讀 9633

作為新手,初學c++和資料結構,也想發發博文,分享點自己的學習所得,也請諸位看官幫忙指正,能提提意見是極好的了!

首先呢,我給各位看官講個笑話:

乙個統計學家在調查了大量的資料後發現,兩個互不串通的人同時帶炸彈上飛機的概率幾乎是零,於是他每次坐飛機都會隨身攜帶乙個炸彈。哈哈,程式設計師的你,今天笑了嗎?

我的第一篇博文就分享下簡單的用c++實現的順序儲存結構的線性表吧!以下是鄙人自己寫的簡單**:

#include using namespace std;

const int maxsize = 100;//定義線性表最大長度為100

templateclass sqlist

;//開始實現各成員函式

templatevoid sqlist::initlist()

templatebool sqlist::listempty()

templatevoid sqlist::clearlist()

templatebool sqlist::getelem(int location, t &element)

element = *(data + location - 1);

return true;

}templateint sqlist::locateelem(t element)

return 0;

}templatebool sqlist::listinsert(int location, t element)

*(data+location-1) = element;

length+=1;

return true;

}templatebool sqlist::listdelete(int location, t &element)

element = *(data + location - 1);

for (int i = location - 1; i < (length - 1); i++)

length -= 1;

return true;

}templateint sqlist::listlength()

int main()

{ //例項化乙個類

sqlistmysql;

//生成空表

mysql.initlist();

//往表中插入資料

mysql.listinsert(1,5);

mysql.listinsert(2,8);

mysql.listinsert(3,2);

mysql.listinsert(4,6);

mysql.listinsert(5,15);

mysql.listinsert(2,9);

//輸出表的長度和表中資料

cout<<"插值後,表的當前長度為:"<>deletelocation;

int deleteelement(0);

mysql.listdelete(deletelocation, deleteelement);

cout<<"刪除了表中第"<>searchelement;

int searchlocation = mysql.locateelem(searchelement);

if (!searchlocation)

{ cout<<"當前表中無此元素"<

C 實現線性表的順序儲存(順序表)

關於資料結構中的 線性表 佇列 棧 的相關講解,請看 c 實現資料結構中的線性表 typedef int elementtype define maxsize 10 struct sequence typedef int element type define maxsize 10 include ...

1 線性表的順序儲存 順序表

採用順序儲存結構的線性表通常稱為順序表,其在記憶體中是以陣列的形式來儲存。如下所示 這。這。就是陣列啊。為嘛叫這麼奇怪的名字?下面是順序表的定義 typedef struct sqlist elemtype代表資料型別 有了定義,不難寫出順序表的建立程式。下面,以int型別陣列為基礎建立順序表 in...

C 實現線性表順序儲存例項(一)

下面是結合c 類模板,給出的線性表順序儲存結構實現的例項,剛入門c 不久,程式中如有bug還望大家多多指教!線性表順序儲存結構實現 include stdio.h include iostream using namespace std const int maxsize 100 templatec...