小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C++中雙向鏈表

 水中麒麟 2013-09-12
#include <iostream>
using namespace std;
class List {
public:
// 構(gòu)造函數(shù)中初始化為空鏈表
List (void) : m_head (NULL), m_tail (NULL) {}
// 析構(gòu)函數(shù)中銷毀剩余的節(jié)點(diǎn)
~List (void) {
for (Node* next; m_head; m_head = next) {
next = m_head -> m_next;
delete m_head;
}
/**/
/*
Node * next;

while(m_head)
{
next=m_head->m_next;
delete m_head;

}
*/

}
// 追加
void append (int data) {
/*
Node* node = new Node;
node -> m_data = data;
node -> m_prev = m_tail;
node -> m_next = NULL;
m_tail = node;
*/
m_tail = new Node (data, m_tail);
if (m_tail -> m_prev)
m_tail -> m_prev -> m_next = m_tail;
else
m_head = m_tail;
}
// 插入
void insert (size_t index, int data) {
for (Node* find = m_head; find;
find = find -> m_next)
if (index-- == 0) 
{
Node* node = new Node (data,
find -> m_prev, find);
if (node -> m_prev)
node -> m_prev -> m_next = node;
else
m_head = node;
node -> m_next -> m_prev = node;
return;
}
throw OverBound ();
}
// 刪除
void erase (size_t index) {
for (Node* find = m_head; find;
find = find -> m_next)
if (index-- == 0) {
if (find -> m_prev)
find -> m_prev -> m_next =
find -> m_next;
else
m_head = find -> m_next;
if (find -> m_next)
find -> m_next -> m_prev =
find -> m_prev;
else
m_tail = find -> m_prev;
delete find;
return;
}
throw OverBound ();
}
// 正遍歷
void forward (void) const {
for (Node* node = m_head; node;
node = node -> m_next)
cout << node -> m_data << ' ';
cout << endl;
}
// 反遍歷
void backward (void) const {
for (Node* node = m_tail; node;
node = node -> m_prev)
cout << node -> m_data << ' ';
cout << endl;
}
// 下標(biāo)運(yùn)算符
int& operator[] (size_t index) {
for (Node* find = m_head; find;
find = find -> m_next)
if (index-- == 0)
return find -> m_data;
throw OverBound ();
}
const int& operator[] (size_t index) const {
return const_cast<List&> (*this) [index];
}
// 測長
size_t length (void) const {
size_t len = 0;
for (Node* node = m_head; node;
node = node -> m_next)
len++;
return len;
}
private:
// 越界異常
class OverBound : public exception {
public:
const char* what (void) const throw () {
return "鏈表越界!";
}
};
// 節(jié)點(diǎn)
class Node {
public:
Node (int data = 0, Node* prev = NULL,
Node* next = NULL) :
m_data (data), m_prev (prev),
m_next (next) {}
int   m_data; // 數(shù)據(jù)
Node* m_prev; // 前指針
Node* m_next; // 后指針
};
Node* m_head; // 頭指針
Node* m_tail; // 尾指針
};
int main (void) {
try {
List list;
list.append (10);
list.append (20);
list.append (50);
list.append (60);
list.append (80);
list.forward ();
list.insert (1, 15);
list.insert (3, 40);
list.insert (6, 70);
list.backward ();
list.erase (1);
list.erase (2);
list.erase (4);
list.forward ();
size_t len = list.length ();
for (size_t i = 0; i < len; i++)
list[i]++;
const List& cr = list;
for (size_t i = 0; i < len; i++)
cout << cr[i] << ' ';
cout << endl;
// cout << cr[10] << endl;
}
catch (exception& ex) {
cout << ex.what () << endl;
return -1;
}
return 0;
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多