標籤:資料結構 模板類 c++ 雙鏈表
#ifndef DLIST_H_INCLUDED#define DLIST_H_INCLUDED#include<iostream>using namespace std;template<class Type>class DList;template<class Type>class ListNode{ friend class DList<Type>;public: ListNode():data(Type()),next(NULL),prior(NULL) {} ListNode(Type d,ListNode<Type> *n = NULL,ListNode<Type> *m = NULL):data(d),next(n),prior(m) {} ~ListNode() {}public: void SetData(Type d) {data = d;} void GetData()const {return data;}private: Type data; ListNode<Type> *next; ListNode<Type> *prior;};template<class Type>class DList{public: DList() { first = last = BuyNode(); } ~DList() { DList<Type>::destroy(); }public:void push_back(const Type &x);void push_front(const Type &x);void show_list()const;void pop_back();void pop_front();void insert_val(const Type &x);void delete_val(const Type &x);bool find(const Type &x);Type length();void clear();void destroy(); //摧毀該順序表void reserv(); //反轉void sort();protected: ListNode<Type>* BuyNode(Type x = Type()){ ListNode<Type> *p = new ListNode<Type>(x); return p;}private: ListNode<Type> *first;ListNode<Type> *last;};template<class Type>void DList<Type>::push_back(const Type &x){ ListNode<Type> *s = BuyNode(x); last->next = s; s->prior = last; last = s; first->data++;}template<class Type>void DList<Type>::push_front(const Type &x){ ListNode<Type> *s = BuyNode(x); if(first == last) { s->prior = first; first->next = s; last = s; } else { first->next->prior = s; s->prior = first; s->next = first->next; first->next = s; } first->data++;}template<class Type>void DList<Type>::show_list()const{ ListNode<Type> *p = first->next; while(p != NULL) { cout<<p->data<<" "; p = p->next; } cout<<endl;}template<class Type>void DList<Type>::pop_back(){ if(first->data == 0) return; ListNode<Type> *p = first; while(p->next != last) p = p->next; ListNode<Type> *q = p->next; p->next = NULL; last = p; delete q; first->data--;}template<class Type>void DList<Type>::pop_front(){ if(first->data <= 1) pop_back(); else { ListNode<Type> *p = first->next; p->next->prior = first; first = p->next->prior; first->next = p->next; p = NULL; delete p; first->data--; }}template<class Type>void DList<Type>::insert_val(const Type &x){ ListNode<Type> *s = BuyNode(x); ListNode<Type> *p = first->next; if(p->data > s->data) { push_front(s->data); } else if(last->data < s->data) { push_back(s->data); } else { while(p != NULL) { if(p->next->data>x) { p->next->prior = s; s->prior = p; s->next = p->next; p->next = s; first->data++; return; } p=p->next; } }}template<class Type>void DList<Type>::delete_val(const Type &x){ ListNode<Type> *p = first; while (p->next != NULL && p->next->data != x) p = p->next; if (p->next == NULL)//未找到即返回 { cout<<"未找到該值"<<endl; return; } ListNode<Type> *q = p->next; if (q == last) pop_back(); else { q->next->prior = p; p->next = q->next; } delete q; first->data--;}template<class Type>bool DList<Type>::find(const Type &x){ ListNode<Type> *p = first->next; while(p != NULL && p->data != x) p = p->next; if(p == NULL) {cout<<"未找到該值"<<endl; return false;} else {cout<<"存在該值"<<endl; return true;}}template<class Type>Type DList<Type>::length(){ cout<<"length = "<<first->data<<endl;}template<class Type>void DList<Type>::clear(){ while(first->data >0) pop_front();}template<class Type>void DList<Type>::destroy()//摧毀該順序表{ clear(); first = last = NULL; delete first;}template<class Type>void DList<Type>::reserv() //反轉{ ListNode<Type> *p = first->next; ListNode<Type> *curr = p->next; ListNode<Type> *tmp = NULL; first->next->next = NULL; while (curr) //將直接後繼指向當前指標的next { tmp = curr->next; curr->next = p; p = curr; curr = tmp; } first->next = p;}template<class Type>void DList<Type>::sort(){ ListNode<Type> *s = first->next; while (s->next) { ListNode<Type> *p = s; while(p->next) { if(s->data > p->next->data) { Type tmp = s->data; s->data = p->next->data; p->next->data = tmp; } p = p->next; } s = s->next; }}#endif // DLIST_H_INCLUDED
<pre name="code" class="cpp">#include"DList.h"int main(){ DList<int> mylist;int select = 1;int Item;int pos;while(select){cout<<"**************************************"<<endl;cout<<"* [1] push_back [2] push_front *"<<endl;cout<<"* [3] show_list [0] quit_system*"<<endl;cout<<"* [4] pop_back [5] pop_front *"<<endl;cout<<"* [6] insert_val [7] delete_val *"<<endl;cout<<"* [8] find [9] length *"<<endl;cout<<"* [10]clear [11]destroy *"<<endl;cout<<"* [12]reserv [13]sort *"<<endl;cout<<"**************************************"<<endl;cout<<"請選擇:>";cin>>select;switch(select) { case 1:cout<<"請輸入要插入的值(-1結束):>";while(cin>>Item, Item!=-1){mylist.push_back(Item);}break;case 2:cout<<"請輸入要插入的值(-1結束):>";while(cin>>Item, Item!=-1){mylist.push_front(Item);}break;case 3:mylist.show_list();break; case 4: mylist.pop_back(); break; case 5: mylist.pop_front(); break; case 6: cout<<"請輸入要插入的值:>"; cin>>Item; mylist.insert_val(Item); break;case 7:cout<<"請輸入要刪除的值:>";cin>>Item;mylist.delete_val(Item);break;case 8:cout<<"請輸入要尋找的值:>";cin>>Item;mylist.find(Item);break; case 9: mylist.length(); break; case 10: mylist.clear(); break; case 11: mylist.destroy(); break; case 12: mylist.reserv(); break; case 13: mylist.sort(); break;default:break; } }}
C++ 資料結構 雙鏈表(模板類)