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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

数据结构--单链表single linked list(无表头哨兵)重写

發布時間:2024/7/5 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构--单链表single linked list(无表头哨兵)重写 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

針對上次寫的單鏈表中不足的地方進行修改:

1.構造函數需要讓用戶輸入(bad)

2.函數功能不單一,既操作鏈表,還打印輸出(bad)

代碼鏈接(包含無頭\有頭單鏈表、循環單鏈表、雙鏈表、循環雙鏈表)

接口 singleList.h

// // Created by mingm on 2019/3/18. // #ifndef _SINGLELIST_H #define _SINGLELIST_Hstruct SNode //結點的基本數據類型 {int data;SNode* pNext; };typedef unsigned int UINT; typedef SNode* ListNode;class SingleList {public:SingleList(void);~SingleList(void);bool IsEmpty() const;//判斷鏈表是否為空UINT GetLength() const;//獲取當前鏈表的長度ListNode GetHeadNode() const;//獲取鏈表的頭結點ListNode GetTailNode() const;//獲取鏈表的尾結點ListNode GetMidNode();//獲得鏈表的中間結點void AddHead(const int &data);//在鏈表的頭部插入新的結點void AddTail(const int &data);//在鏈表的尾部插入新的結點bool posInList(ListNode pos);//檢查節點指針是否在鏈表中ListNode InsertAt(ListNode pos, const int &data);//在指定結點前插入數據,并返回新結點的地址ListNode ModifyAt(ListNode pos, const int &data);//修改指定結點的數據,并返回當前節點的地址ListNode RemoveAt(ListNode pos);//刪除指定結點,并返回被刪除結點的下一結點的地址ListNode RemoveAtBack(UINT nCountBack);//刪除倒數第n個節點,并返回被刪除結點的下一結點的地址ListNode Find(const int &data);//在當前鏈表中找到和要查找數據相等的第一個結點的地址void Erase();//刪除鏈表的所有結點void PrintList() const;//打印鏈表所有結點的數據void Reverse();//反轉鏈表private:ListNode m_pHead; //頭結點UINT m_nListLen; //鏈表數據長度 };#endif

類實現文件?singleList.cpp

//單向鏈表(不帶頭) // Created by mingm on 2019/3/18. // #include "singleList.h" #include <iostream>SingleList::SingleList():m_pHead(NULL),m_nListLen(0){}SingleList::~SingleList() {Erase(); } void SingleList::Erase() {if(m_pHead != NULL){ListNode temp = m_pHead, del;while(temp != NULL){del = temp;temp = temp->pNext;delete del;}m_pHead = NULL;m_nListLen = 0;} } bool SingleList::IsEmpty() const {return m_pHead == NULL; } UINT SingleList::GetLength() const {return m_nListLen; } ListNode SingleList::GetHeadNode() const {return m_pHead; } ListNode SingleList::GetTailNode() const {if(!m_pHead)return NULL;ListNode temp = m_pHead;while(temp->pNext != NULL){temp = temp->pNext;}return temp; } ListNode SingleList::GetMidNode() //快慢指針法 {ListNode fast = m_pHead, slow = m_pHead;while(fast){if(fast->pNext != NULL){fast = fast->pNext->pNext;}else{break;}slow = slow->pNext;}//簡潔寫法 // while(fast && fast->pNext) // { // fast = fast->pNext->pNext; // slow = slow->pNext; // }return slow; } void SingleList::AddHead(const int &data) {ListNode newNode = new SNode;newNode->data = data;newNode->pNext = NULL;if(m_pHead == NULL){m_pHead = newNode;}else{newNode->pNext = m_pHead;m_pHead = newNode;}m_nListLen++; } void SingleList::AddTail(const int &data) {ListNode newNode = new SNode;newNode->data = data;newNode->pNext = NULL;if(m_pHead == NULL){m_pHead = newNode;}else{GetTailNode()->pNext = newNode;}m_nListLen++; } bool SingleList::posInList(ListNode pos) {if(m_pHead == NULL)return false;else{ListNode temp = m_pHead;while(temp && temp != pos){temp = temp->pNext;}if(temp == NULL) //指針地址不在鏈表內return false;elsereturn true;} } ListNode SingleList::InsertAt(ListNode pos, const int &data) {if(pos == NULL)return NULL;else if(pos == m_pHead){AddHead(data);return m_pHead;}else{ListNode temp = m_pHead;ListNode preNode = NULL;while(temp && temp != pos){preNode = temp;temp = temp->pNext;}if(temp == NULL) //指針地址不在鏈表內return NULL;else{ListNode newNode = new SNode;newNode->data = data;newNode->pNext = NULL;newNode->pNext = pos;preNode->pNext = newNode;m_nListLen++;return newNode;}} } ListNode SingleList::ModifyAt(ListNode pos, const int &data) {if(!posInList(pos))return NULL;else{pos->data = data;return pos;} } ListNode SingleList::RemoveAt(ListNode pos) {if(!posInList(pos))return NULL;else{ListNode temp = m_pHead;if(pos == m_pHead){temp = pos->pNext;delete pos;m_nListLen--;m_pHead = temp;return temp;}else{while(temp->pNext != pos){temp = temp->pNext;}temp->pNext = pos->pNext;delete pos;m_nListLen--;return temp->pNext;}} } ListNode SingleList::RemoveAtBack(UINT nCountBack)//先讓快指針先走n-1步,然后快慢指針一起動,快指針到達尾部,慢指針指向倒數第n個 {if(nCountBack == 0 || nCountBack > m_nListLen)return NULL;else{ListNode fast = m_pHead;ListNode slow = m_pHead;for(int i = 0; i < nCountBack-1 && fast; ++i){fast = fast->pNext;}while(fast->pNext){fast = fast->pNext;slow = slow->pNext;}fast = RemoveAt(slow);return fast;} } ListNode SingleList::Find(const int &data) {ListNode temp = m_pHead;while(temp && temp->data != data){temp = temp->pNext;}return temp; } void SingleList::PrintList() const {std::cout << "----- print start ----" << std::endl;ListNode temp = m_pHead;for(int i = 0; i < m_nListLen && temp; ++i){std::cout << "No. " << i+1 << " node is " << temp->data << std::endl;temp = temp->pNext;}std::cout << "----- print end ----" << std::endl; } void SingleList::Reverse() //就地反轉法 {if(m_pHead && m_pHead->pNext){ListNode preNode, curNode, tempNode;preNode = curNode = tempNode = m_pHead;curNode = preNode->pNext;preNode->pNext = NULL;while(curNode->pNext){tempNode = curNode;curNode = curNode->pNext;tempNode->pNext = preNode;preNode = tempNode;}curNode->pNext = preNode;m_pHead = curNode;} }

測試主程序?test_singleList.cpp

測試程序包含長度從0開始的 list,以便檢查邊界情況下程序是否正常運行。

// // Created by mingm on 2019/3/20. // #include "singleList.cpp" #include <iostream> using namespace std; int main() {for(int k = 0; k < 5; ++k){cout << "------------ test start ----------------" << endl;SingleList intList;if(intList.IsEmpty())cout << "empty list!" << endl;cout << intList.GetLength() << " node(s) !" << endl;cout << "---------------" << endl;cout << "add 0 to " << k-1 << " into list: " << endl;for(int j = 0; j < k; ++j){intList.AddTail(j);}cout << intList.GetLength() << " node(s) !" << endl;intList.PrintList();cout << "------------- reverse list" << endl;intList.Reverse();intList.PrintList();if(intList.GetHeadNode()){cout << "head node: " << intList.GetHeadNode()->data << endl;cout << "tail node: " << intList.GetTailNode()->data << endl;cout << "middle node: " << intList.GetMidNode()->data << endl;}cout << "--------------- addTail " << k << endl;intList.AddTail(k);intList.PrintList();if(intList.posInList(intList.GetMidNode()))cout << "midNode in List !" << endl;cout << "100 insert at midNode ";if(intList.GetMidNode())cout << intList.GetMidNode()->data ;cout << " front " << endl;intList.InsertAt(intList.GetMidNode(),100);intList.PrintList();cout << "modify head to 99 " << endl;intList.ModifyAt(intList.GetHeadNode(),99);intList.PrintList();cout << "del Tail" << endl;intList.RemoveAt(intList.GetTailNode());intList.PrintList();cout << "del the " << intList.GetLength()-1 << "th node count from end !" << endl;intList.RemoveAtBack(intList.GetLength()-1);intList.PrintList();cout << "address of first " << k-3 << " is ";if(intList.Find(k-3))cout << intList.Find(k-3) << endl;elsecout << "not exits !" << endl;}return 0; }

Valgrind檢查內存是否泄漏(部分結果展示如下)

?

?

總結

以上是生活随笔為你收集整理的数据结构--单链表single linked list(无表头哨兵)重写的全部內容,希望文章能夠幫你解決所遇到的問題。

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