C++ 實(shí)現(xiàn)靜態(tài)單鏈表的實(shí)例
C++ 實(shí)現(xiàn)靜態(tài)單鏈表的實(shí)例
利用數(shù)組實(shí)現(xiàn)的靜態(tài)單鏈表,與嚴(yán)蔚敏書實(shí)現(xiàn)略有不同,不另設(shè)回收空間。有任何BUG或錯(cuò)誤,希望各位朋友多多反饋~~感激不盡
/* Author : Moyiii * Mail: lc09@vip.qq.com * 靜態(tài)鏈表實(shí)現(xiàn),僅作學(xué)習(xí)之用,當(dāng)然如果 * 你想拿去用,隨你好啦。 */ #include <iostream> using namespace std; #define MAX_LIST_SIZE 100 class Node { public: int data; int cur; }; class SLinkList { public: SLinkList(); //和普通的線性鏈表區(qū)別不是很大。除了兩個(gè)分配 //和回收節(jié)點(diǎn)空間的函數(shù),具體算法請參考課本或 //網(wǎng)絡(luò)資料 int newNode(); bool deleteNode(int pos); bool insertElem(int pos, int elem); bool deleteElem(int pos); int& getElem(int pos); int getLength(); bool isEmpty(); void print(); void clear(); private: int head;//這個(gè)可以不要,默認(rèn)等于0 int space; int length; Node *elems; }; SLinkList :: SLinkList() { // 0號位置為頭幾點(diǎn),不可以更改,初始指向自己 // 從1~MAXLENGTH為可分配節(jié)點(diǎn),最初由space管理 elems = new Node[MAX_LIST_SIZE]; if(!elems) { cout << "Malloc failed!" << endl; } head = space = length = 0; for(int i = 0; i < MAX_LIST_SIZE; ++i) { elems[i].data = i; elems[i].cur = i + 1; } elems[MAX_LIST_SIZE - 1].cur = 0; elems[0].cur = 0; space = 1; } //從space指向的備用節(jié)點(diǎn)鏈表中取下一個(gè)節(jié)點(diǎn) int SLinkList :: newNode() { if(space == 0) { cout << "Space is full!" << endl; return 0; } int pos = space; space = elems[space].cur; elems[pos].cur = 0; return pos; } //回收節(jié)點(diǎn)空間 bool SLinkList :: deleteNode(int pos) { if(pos == 0) { cout << "Free space Error!" << endl; return false; } elems[pos].cur = space; space = pos; return true; } //插入節(jié)點(diǎn),思路類似,找到被刪除節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn) //然后更改指向 bool SLinkList :: insertElem(int pos, int elem) { if(length == MAX_LIST_SIZE) { cout << "Space is Full" << endl; return false; } if(pos < 1 || pos > length + 1) { cout << "Insert Over Bound" << endl; return false; } int index = head; for(int i = 1; i <= pos - 1; ++i) { index = elems[index].cur; } int node = newNode(); if(node == 0) { cout << "Space malloc failed" << endl; return false; } elems[node].data = elem; elems[node].cur = elems[index].cur; elems[index].cur = node; length++; return true; } //一回事,注意把刪除的節(jié)點(diǎn)回收給space bool SLinkList :: deleteElem(int pos) { if(pos < 1 || pos > length) { cout << "Delete Node over Bound!" << endl; return false; } int index = head; for(int i = 1; i <= pos - 1; ++i) { index = elems[index].cur; } int node = elems[index].cur; elems[index].cur = elems[node].cur; deleteNode(node); length--; return true; } void SLinkList :: print() { int index = elems[head].cur; while(index != 0) { cout << elems[index].data << " "; index = elems[index].cur; } cout << endl; return; } int SLinkList :: getLength() { return length; } bool SLinkList :: isEmpty() { if(length == 0) { return true; } else { return false; } } int& SLinkList :: getElem(int pos) { int index = head; for(int i = 1; i <= pos; ++i) { index = elems[index].cur; } return elems[index].data; } void SLinkList :: clear() { for(int i = 0; i < MAX_LIST_SIZE; ++i) { elems[i].data = i; elems[i].cur = i + 1; } elems[MAX_LIST_SIZE - 1].cur = 0; elems[0].cur = 0; space = 1; } int main() { //測試數(shù)據(jù),測試插入刪除空間是否溢出 SLinkList myList; for(int i = 1; i <= 105; ++i) { myList.insertElem(1,i); } //myList.print(); for(int i = 1; i <= 105; ++i) { myList.deleteElem(1); } //myList.print(); //普通測試 for(int i = 1; i <= 10; ++i) { myList.insertElem(1,i); } myList.print(); cout << "Length= " << myList.getLength() <<endl; myList.deleteElem(5); myList.print(); cout << "Length= " << myList.getLength() <<endl; cout << myList.isEmpty() << endl; int &elem = myList.getElem(3); elem = 99; myList.print(); myList.clear(); myList.print(); return 0; }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
上一篇:C++ 數(shù)據(jù)結(jié)構(gòu)之kmp算法中的求Next()函數(shù)的算法
欄 目:C語言
下一篇:Visual C++ 常用數(shù)據(jù)類型轉(zhuǎn)換方法詳解第1/2頁
本文標(biāo)題:C++ 實(shí)現(xiàn)靜態(tài)單鏈表的實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1422.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子