標籤:insert new 思路 .cpp 插入 list div include pos
按照書上的要求實現了一下單鏈表;單鏈表的實現可能以前看過幾次了;現在想想最主要的幾個操作演算法應該能夠寫了吧;遇到的問題:
1. 鏈表節點寫成private;所已給出了訪問的介面;
2.模板類的.h和.cpp實現寫在同一個檔案;
3.感覺以後的資料結構實現還是用純c的實現好一些;然後書主要是思路
節點類:
#ifndef SINGLELIST_LISTNODE_H_#define SINGLELIST_LISTNODE_H_template <class T>class ListNode{public: //定義為private時,所以在會用到訪問 T data; ListNode<T> * pNext;public: ListNode() : pNext(nullptr){} ListNode(T value_) :data(value_), pNext(nullptr){} ~ListNode(){} void SetpNext(ListNode<T>* next_); void SetData(T value_); ListNode<T>* GetpNext(); T& GetData();};template <class T>void ListNode<T>::SetpNext(ListNode<T>* next_){ pNext = next_;}template<class T>void ListNode<T>::SetData(T value_){ data = value_;}template<class T>ListNode<T>* ListNode<T>::GetpNext(){ return pNext;}template<class T>T& ListNode<T>::GetData(){ return data;}#endif
鏈表類:
#ifndef SINGLELIST_SINGLELIST_H_#define SINGLELIST_SINGLELIST_H_#include "ListNode.h"template<class T>class SingleList{private: ListNode<T>* head; ListNode<T>* tail;public: SingleList(); ~SingleList(); bool AddTail(T value_); bool RemoveTail(); bool InsertAt(int index_, T value_); bool RemoveAt(int index_); T& GetAt(int index_); bool IsEmpty(); int GetCount(); void RemoveAll(); ListNode<T>* GetHead(); ListNode<T>* GetTail(); ListNode<T>* GetNodeAt(int index_); ListNode<T>* GetCur(); ListNode<T>* TowardCur();};template<class T>bool SingleList<T>::AddTail(T value_){ ListNode<T>* pointer = new ListNode<T>(value_); tail->SetpNext(pointer); tail = tail->pNext; tail->pNext = nullptr; if (tail != nullptr) { return true; } else { return false; }}template<class T>//在索引值指向的節點前插入新節點bool SingleList<T>::InsertAt(int index_, T value_){ if (index_ > this->GetCount() || index_ < 0) { cerr << "A wrong position!\n"; return false; } ListNode<T>* current = head; while (index_) { current = current->pNext; index_--; } ListNode<T>* add = new ListNode<T>(value_); add->pNext = current->pNext; current->pNext = add; if (current != nullptr) { return true; } else { return false; }}template<class T>bool SingleList<T>::RemoveTail(){ return RemoveAt(this->GetCount() - 1);}template<class T>bool SingleList<T>::RemoveAt(int index_){ if (index_ > this->GetCount() || index_ < 0) { cerr << "A wrong position!\n"; return false; } ListNode<T>* current = head; while (index_ - 1) { current = current->pNext; index_--; } ListNode<T>* deletePoint = current->pNext; if (current->pNext->pNext == nullptr) { current->pNext = nullptr; } else current->pNext = current->pNext->pNext; delete deletePoint; return true;}template<class T>SingleList<T>::SingleList(){ head = new ListNode<T>(); tail = head; tail->pNext = NULL;}template<class T>SingleList<T>::~SingleList(){ RemoveAll(); delete head;}template<class T>T& SingleList<T>::GetAt(int index_){ if (index_ > GetCount() || index_ < 0) { cerr << "A wrong position!\n"; } ListNode<T>* cur; cur = head->pNext; while (index_) { cur = cur->pNext; index_--; } return cur->GetData();}template<class T>bool SingleList<T>::IsEmpty(){ return head->pNext == NULL;}template<class T>int SingleList<T>::GetCount(){ int count = 0; ListNode<T>* cur = head->pNext; while (cur != nullptr) { cur = cur->pNext; count++; } return count;}template<class T>void SingleList<T>::RemoveAll(){ ListNode<T>* cur; while (head->pNext != nullptr) { cur = head->pNext; head->pNext = cur->pNext; delete cur; } tail = head;}template<class T>ListNode<T> * SingleList<T>::GetNodeAt(int index_){ if (index_ > this->GetCount() - 1 || index_ < 0) { cerr << "A wrong position!\n"; } ListNode<T>* handle = head->pNext; while (index_) { handle = handle->pNext; index_--; } return handle;}template <class T>ListNode<T>* SingleList<T>::GetHead(){//返回頭指標 return head;}template <class T>ListNode<T>* SingleList<T>::GetTail(){//返回尾指標 return tail;}template <class T>ListNode<T>* SingleList<T>::GetCur(){ return cur;}template <class T>ListNode<T>* SingleList<T>::TowardCur(){ cur = cur->GetLink(); return cur}#endif
測試函數:
#include <iostream> #include "SingleList.h" using namespace std;int main(){ SingleList<int> list; for (int i = 0; i < 9; i++) list.AddTail(i); cout << list.GetCount() << endl; cout << list.GetAt(3) << endl; list.RemoveAt(3); cout << list.GetCount() << endl; cout << list.GetAt(3) << endl; list.RemoveAll(); cout << list.GetCount() << endl; system("PAUSE"); return 0;}
結果:
後續用純c實現鏈表的基本操作:建立,增,刪,改,查。
4.2.2 演算法之美--單鏈表實現