日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构 链表(二)

發布時間:2025/3/20 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构 链表(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈表

    • 雙向鏈表的C++實現
    • 單向鏈表的成環問題
    • 環中元素個數
    • 環開始的節點位置

雙向鏈表的C++實現

#pragma once typedef struct Node {int value;Node* prev;Node* next; }node;class DoubleLinklist { private:int size;node* head;node* tail; public:DoubleLinklist();~DoubleLinklist();//獲取鏈表大小int GetSize();//判斷是否為空bool IsEmpty();//尾插法插入元素節點void Insert(node* n);//頭插法插入元素節點void HeadInsert(node* n);//指定位置后插入數據void PosInsert(int pos, node* n);//獲取指定位置元素,并打印void GetPosElem(int pos);//搜索指定元素并打印位置void SearchElem(int elem);//刪除指定元素void DelPosElem(int pos);//打印鏈表void ShowLinklist();//反轉打印鏈表void ReverseShowLinklist();//清除所有數據void ClearLinklist(); }; #include"doublelinlist.h" #include<iostream> using std::cout; using std::cin; using std::endl;DoubleLinklist::DoubleLinklist() {this->size = 0;this->head = new node;this->head->value = NULL;this->head->prev = this->head->next = NULL;this->tail = new node;this->tail->next = this->tail->prev = NULL;this->tail->value = NULL; }DoubleLinklist::~DoubleLinklist() {delete[]head;delete[]tail; }int DoubleLinklist::GetSize() {cout << "NOW LINKLIST SIZE == ";cout << this->size << endl;return this->size; }bool DoubleLinklist::IsEmpty() {if (this->size)return false;elsereturn true; }void DoubleLinklist::Insert(node* n) {if (n){node* temp = new node;temp->next = temp->prev = NULL;temp->value = n->value;if (this->IsEmpty()){this->head->next = temp;temp->next = this->tail;this->tail->prev = temp;temp->prev = this->head;}else{node* pnode = this->tail->prev;pnode->next = temp;temp->next = this->tail;this->tail->prev = temp;temp->prev = pnode;}this->size++;}else{cout << "UNDEFINE NODE\n";}}void DoubleLinklist::HeadInsert(node* n) {if (n){node* temp = new node;temp->next = temp->prev = NULL;temp->value = n->value;if (this->IsEmpty()){this->head->next = temp;temp->next = this->tail;this->tail->prev = temp;temp->prev = this->head;}else{node* pnode = this->head->next;this->head->next = temp;temp->next = pnode;pnode->prev = temp;temp->prev = this->head;}this->size++;}else{cout << "UNDEFINED NODE\n";} }void DoubleLinklist::PosInsert(int pos, node* n)//注意插入時前后節點的指針賦值順序 { //一般先將插入節點的前后指針next、prev賦值if (n) //再改變前節點的next指針,后節點的prev指針{node* temp = new node;temp->next = temp->prev = NULL;temp->value = n->value;if (pos > this->size || pos < 0){cout << "NO SUCH POSITION, DEFULT INSERT IT IN THE END\n";this->Insert(n);}else if (pos == this->size)this->Insert(n);else if (pos == 0)this->HeadInsert(n);else{node* pnode;if (pos <= (this->size / 2)){pnode = this->head;for (int i = 0; i < pos; ++i){pnode = pnode->next;}temp->next = pnode->next;temp->prev = pnode;pnode->next->prev = temp;pnode->next = temp;}else{pnode = this->tail;for (int i = 0; i < (this->size - pos + 1); ++i){pnode = pnode->prev;}temp->next = pnode->next;temp->prev = pnode;pnode->next->prev = temp;pnode->next = temp;}}this->size++;}else{cout << "UNDEFINED NODE\n";} }void DoubleLinklist::GetPosElem(int pos) {if (pos <= 0 || pos > this->size)cout << "NO SUCH POSITION\n";else{node* pnode;if (pos <= (this->size / 2)){pnode = head;for (int i = 0; i < pos; ++i){pnode = pnode->next;}}else{pnode = tail;for (int i = 0; i < (this->size + 1 - pos); ++i){pnode = pnode->prev;}}cout << "POSITION [" << pos << "] == ";cout << pnode->value << endl;} }void DoubleLinklist::SearchElem(int elem) {if (this->IsEmpty())cout << "NO DATA!\n";else{node* temp = this->head;int flags = 0;for (int i = 0; i < this->size; ++i){temp = temp->next;if (temp->value == elem){flags++;cout << "FIND IT, POSITION == ";cout << i+1 << endl;}}if(!flags)cout << "THERE IS NO DATA YOU WANT\n";} }void DoubleLinklist::DelPosElem(int pos) {node* temp;if (pos <= 0 || pos > this->size)cout << "POSITION ERROR\n";else if (pos == 1){temp = this->head->next;head->next = temp->next;temp->next->prev = head;delete temp;}else if (pos == this->size){temp = this->tail->prev;tail->prev = temp->prev;temp->prev->next = this->tail;delete temp;}else{temp = this->head;for (int i = 0; i < pos; ++i){temp = temp->next;}temp->next->prev = temp->prev;temp->prev->next = temp->next;delete temp;}this->size--; }void DoubleLinklist::ShowLinklist() {if (this->IsEmpty())cout << "LINKLIST NULL\n";else{node* temp = this->head;for (int i = 0; i < this->size; ++i){temp = temp->next;cout << "[" << i + 1 << "] == " << temp->value << endl;}} }void DoubleLinklist::ReverseShowLinklist() {if (this->IsEmpty())cout << "LINKLIST NULL\n";else{node* temp = this->tail;for (int i = 0; i < this->size; ++i){temp = temp->prev;cout << "[" << i + 1 << "] == " << temp->value << endl;}} }void DoubleLinklist::ClearLinklist() {if (this->IsEmpty())cout << "LINKLIST NULL\n";else{node* temp = this->head->next;for (int i = 0; i < this->size; ++i){temp = temp->next;delete temp->prev;}} } #include"doublelinlist.h" #include<iostream> using std::cout; using std::endl;int main() {DoubleLinklist lk;node n[10];for (int i = 0; i < 10; ++i){n[i].value = 5 + i;n[i].next = n[i].prev = nullptr;}lk.Insert(&n[2]);lk.Insert(&n[4]);lk.Insert(&n[3]);lk.Insert(&n[1]);lk.Insert(&n[8]);lk.Insert(&n[5]);lk.Insert(&n[0]);lk.ShowLinklist();cout << endl;lk.ReverseShowLinklist();cout << "\n";lk.HeadInsert(&n[1]);lk.HeadInsert(&n[9]);lk.ShowLinklist();cout << endl;lk.ReverseShowLinklist();cout << "\n";lk.PosInsert(4, &n[6]);lk.PosInsert(6, &n[7]);lk.ShowLinklist();cout << "\n";lk.DelPosElem(3);lk.DelPosElem(4);lk.ShowLinklist();cout << "\n";lk.SearchElem(2);lk.SearchElem(17);lk.SearchElem(8);lk.ClearLinklist();return 0; }

測試結果,基本沒有錯誤出現

[1] == 7 [2] == 9 [3] == 8 [4] == 6 [5] == 13 [6] == 10 [7] == 5[1] == 5 [2] == 10 [3] == 13 [4] == 6 [5] == 8 [6] == 9 [7] == 7[1] == 14 [2] == 6 [3] == 7 [4] == 9 [5] == 8 [6] == 6 [7] == 13 [8] == 10 [9] == 5[1] == 5 [2] == 10 [3] == 13 [4] == 6 [5] == 8 [6] == 9 [7] == 7 [8] == 6 [9] == 14[1] == 14 [2] == 6 [3] == 7 [4] == 9 [5] == 11 [6] == 8 [7] == 12 [8] == 6 [9] == 13 [10] == 10 [11] == 5[1] == 14 [2] == 6 [3] == 9 [4] == 8 [5] == 12 [6] == 6 [7] == 13 [8] == 10 [9] == 5THERE IS NO DATA YOU WANT THERE IS NO DATA YOU WANT FIND IT, POSITION == 4

單向鏈表的成環問題


通過數學歸納法簡單推斷,即可得出使用快慢指針,快指針2格一跳,慢指針1格一跳,快慢指針終會在環里相遇,并且在相遇之時,慢指針還未走滿一個環。

//承接上一章中單向鏈表類,存在一個頭節點 bool Linklist::LinklistIsRing() {if (this->size == 0)return false;node* fast = this->head->next;node* slow = this->head->next;while (fast != NULL && fast->next != NULL){fast = fast->next->next;slow = slow->next;if (fast == slow)return true;}return false; }

環中元素個數

int Linklist::RingCount() {if (this->size == 0)return -1;node* fast = this->head->next;node* slow = this->head->next;int count = 0;while (fast != NULL && fast->next != NULL){fast = fast->next->next;slow = slow->next;if (fast == slow)break;}if (fast == NULL)//追加判斷不為環的情況下直接退出return -1;do{slow = slow->next;count++;} while (slow == fast);return count; }

環開始的節點位置

node* Linklist::GetRingStart() {if (this->size == 0)return NULL;node* fast = this->head->next;node* slow = this->head->next;int count = 0;while (fast != NULL && fast->next != NULL){fast = fast->next->next;slow = slow->next;if (fast == slow)break;}if (fast == NULL)return NULL;slow = this->head->next;//慢指針回歸到頭,快指針更改速度為一個,下次相遇即為環的開始節點while (slow != fast){slow = slow->next;fast = fast->next;}return slow; }

總結

以上是生活随笔為你收集整理的数据结构 链表(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。