C 實現雙向迴圈鍊錶

2021-08-17 18:38:46 字數 3111 閱讀 1497

/***********雙向迴圈鍊錶(**除錯正常,所有功能均測試)*************/

//節點類

template

class listnode

listnode(type d, listnode *n = nullptr, listnode *p = nullptr) : data(d), next(n), prev(p){}

void setdata(type d)

};//雙向迴圈鍊錶:主要注意尾指標的調節

template

class doublelist

int size()

bool push_front(const type &x);

bool push_back(const type &x);

bool push_val(int k, const type &x);//在鍊錶第k個位置插入元素x

bool pop_front();

bool pop_back();

bool pop_data(int k);//刪除第k個節點

bool find(int k, type &x) const;

void reserse();

void show_list() const;

};//建構函式

template

doublelist::doublelist()

//析構函式

template

doublelist::~doublelist()

delete this->head;

}//頭插

template

bool doublelist::push_front(const type &x)

temp->next = this->head->next;

this->head->next->prev = temp;

temp->prev = this->head;

this->head->next = temp;

this->length++;

return true;

}//尾插

template

bool doublelist::push_back(const type &x)

//在鍊錶中第k個位置之前,插入新的節點

template

bool doublelist::push_val(int k, const type &x)

listnode *temp = new listnode(x, nullptr, nullptr);//建立乙個節點物件

//首先找到第k個節點

listnode *move = this->head->next;

int i = 1;

while (i < k)

//move是指向第k個節點

move->prev->next = temp;

temp->prev = move->prev;

temp->next = move;

move->prev = temp;

return true;

}//刪除鍊錶開始的節點

template

bool doublelist::pop_front()

listnode *temp = this->head->next;//儲存第乙個節點的位址

this->head->next = temp->next;

temp->next->prev = this->head;

delete temp;

if (this->length == 1)//考慮只有乙個元素的特殊情況

this->tail = this->head;

this->length–;

return true;

}//刪除鍊錶最後乙個元素

template

bool doublelist::pop_back()

this->tail->prev->next = this->head;

this->head->prev = this->tail->prev;

delete this->tail;

this->tail = this->head->prev;

this->length–;

return true;

}//刪除第k個節點

template

bool doublelist::pop_data(int k)

listnode *move = this->head->next;

int i = 1;

while (i < k)

if (k == this->length)

move->prev->next = move->next;

move->next->prev = move->prev;

delete move;

this->length–;

return true;

}//查詢元素

template

bool doublelist::find(int k, type &x) const

listnode *move = this->head->next;

int i = 1;

while (i < k)

x = move->data;

return true;

}//逆置鍊錶

template

void doublelist::reserse()

listnode *s = this->head->next;

listnode *p = s->next;

s->next = this->head;

this->head->prev = s;

this->tail = s;

while (p != this->head)//注意不能判斷p!=nullptr 這樣迴圈永遠不會停止,迴圈鍊錶不可能為nullptr

}//輸出鍊錶

template

void doublelist::show_list() const

cout << endl;

}//測試程式

int main()

C實現迴圈鍊錶及雙向鍊錶

在雙向鍊錶中,結點除含有資料域外,還有兩個鏈域,乙個儲存直接後繼結點位址,一般稱之為右鏈域 乙個儲存直接前驅結點位址,一般稱之為左鏈域。鍊錶的c語言實現之迴圈鍊錶及雙向鍊錶 一 迴圈鍊錶 迴圈鍊錶是與單鏈表一樣,是一種鏈式的儲存結構,所不同的是,迴圈鍊錶的最後乙個結點的指標是指向該迴圈鍊錶的第乙個結...

雙向迴圈鍊錶的c 實現

雙向迴圈鍊錶,即每個節點都擁有一前一後兩個指標且頭尾互鏈的鍊錶。各種鍊錶的簡單區別如下 單向鍊錶 基本鍊錶 單向迴圈鍊錶 不同於單向鍊錶以 null 判斷鍊錶的尾部,單向迴圈鍊錶的尾部鏈結到表頭,因此當迭代操作到表頭前即是尾部 雙向鍊錶 比單向鍊錶多出指向前乙個節點的指標,但實際上使用雙向鍊錶時很少...

雙向迴圈鍊錶 java實現

雙向迴圈煉表示意圖 雙向迴圈鍊錶實現 public class doublelink private int size 鍊錶長度 public nodehead 頭節點 constructor public doublelink 獲取鍊錶的長度 return public int size 判斷鍊錶...