C++ 實現(xiàn)靜態(tài)單鏈表的實例

創(chuàng)新互聯(lián)憑借專業(yè)的設計團隊扎實的技術支持、優(yōu)質(zhì)高效的服務意識和豐厚的資源優(yōu)勢,提供專業(yè)的網(wǎng)站策劃、網(wǎng)站設計制作、成都網(wǎng)站建設、網(wǎng)站優(yōu)化、軟件開發(fā)、網(wǎng)站改版等服務,在成都10余年的網(wǎng)站建設設計經(jīng)驗,為成都千余家中小型企業(yè)策劃設計了網(wǎng)站。
利用數(shù)組實現(xiàn)的靜態(tài)單鏈表,與嚴蔚敏書實現(xiàn)略有不同,不另設回收空間。有任何BUG或錯誤,希望各位朋友多多反饋~~感激不盡
/* Author : Moyiii
* Mail: lc09@vip.qq.com
* 靜態(tài)鏈表實現(xiàn),僅作學習之用,當然如果
* 你想拿去用,隨你好啦。
*/
#include <iostream>
using namespace std;
#define MAX_LIST_SIZE 100
class Node
{
public:
int data;
int cur;
};
class SLinkList
{
public:
SLinkList();
//和普通的線性鏈表區(qū)別不是很大。除了兩個分配
//和回收節(jié)點空間的函數(shù),具體算法請參考課本或
//網(wǎng)絡資料
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;//這個可以不要,默認等于0
int space;
int length;
Node *elems;
};
SLinkList :: SLinkList()
{
// 0號位置為頭幾點,不可以更改,初始指向自己
// 從1~MAXLENGTH為可分配節(jié)點,最初由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é)點鏈表中取下一個節(jié)點
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é)點空間
bool SLinkList :: deleteNode(int pos)
{
if(pos == 0)
{
cout << "Free space Error!" << endl;
return false;
}
elems[pos].cur = space;
space = pos;
return true;
}
//插入節(jié)點,思路類似,找到被刪除節(jié)點的前一個節(jié)點
//然后更改指向
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é)點回收給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++實現(xiàn)靜態(tài)單鏈表的實例
文章轉(zhuǎn)載:http://chinadenli.net/article48/jhgehp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機、營銷型網(wǎng)站建設、網(wǎng)站制作、手機網(wǎng)站建設、建站公司、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)