雙向迴圈隊列

來源:互聯網
上載者:User

ListNode.h

template<typename Type>class DoublyList;template<typename Type>class ListNode{private:Type m_data;ListNode *m_pprior;ListNode *m_pnext;public:Type GetData();private:friend class DoublyList<Type>;ListNode():m_pprior(NULL),m_pnext(NULL){};ListNode(const Type item,ListNode<Type> *prior=NULL,ListNode<Type> *next = NULL):m_data(item),m_pprior(prior),m_pnext(next){}~ListNode(){m_pprior = NULL;m_pnext = NULL;}Type GetData(){return m_data;}};

DoublyList.h

#include "ListNode.h"template<typename Type>class DoublyList{public:DoublyList():head(new ListNode<Type>){head->m_pprior = head;head->m_pnext = head;}~DoublyList(){MakeEmpty();delete head;}public:void MakeEmpty();int Length();ListNode<Type> *Find(int n = 0);ListNode<Type> *FindData(Type item);bool Insert(Type item,int n = 0);Type Remove(int n = 0);Type Get(int n = 0);void Print();private:ListNode<Type> *head;};

DoublyList.cpp

template<typename Type> void DoublyList<Type>::MakeEmpty(){ListNode<Type>* p;while(head->m_pnext != head){p = head->m_pnext;head->m_pnext = p->m_pnext;p->m_pnext->m_pprior = head;delete p;}}template<typename Type> int DoublyList<Type>::Length(){int count = 0;ListNode<Type>* p = head;while(p->m_pnext != head){p = p->m_pnext;count++;}return count;}template<typename Type> ListNode<Type>* DoublyList<Type>::Find(int n = 0){if(n < 0){cout<<"The n is out of boundary"<<endl;return NULL;}int count = 0;ListNode<Type> *p = head;while(p->m_pnext != head){p=p->m_pnext;if(count == n)return p;count++;}cout<<"The n is out of boundary"<<endl;return NULL;}template<typename Type> bool DoublyList<Type>::Insert(Type item,int n){if(n < 0){cout<<"The n is out of boundary"<<endl;return 0;}int  count = 0;ListNode<Type> *p = head;while(1){if(count == n){ListNode<Type> *q = new ListNode<Type>(item,p,p->m_pnext);p->m_pnext->m_pprior = q;p->m_pnext = q;return 1;}p = p->m_pnext;count++;if(p == head)break;}cout<<"The n is out of boundary"<<endl;return 0;}template<typename Type> Type DoublyList<Type>::Remove(int n = 0){if(n < 0){cout<<"The n is out of boundary"<<endl;return 0;}int  count = 0;ListNode<Type> *p = head;while(1){if(count == n){ListNode<Type> *q = p->m_pnext;p->m_pnext = q->m_pnext;p->m_pnext->m_pprior = p;Type temp = q->GetData();delete q;return temp;}p = p->m_pnext;count++;if(p == head)break;}cout<<"The n is out of boundary"<<endl;return 0;}template<typename Type> Type DoublyList<Type>::Get(int n = 0){if(n < 0){cout<<"The n is out of boundary"<<endl;return 0;}int  count = 0;ListNode<Type> *p = head;while(p->m_pnext != head){p = p->m_pnext;if(count == n)return p->GetData();count++;}cout<<"The n is out of boundary"<<endl;return 0;}template<typename Type> void DoublyList<Type>::Print(){ListNode<Type> *p = head;while(p->m_pnext != head){p = p->m_pnext;cout<<p->GetData()<<' ';}cout<<endl;}template<typename Type> ListNode<Type>* DoublyList<Type>::FindData(Type item){ListNode<Type> *p = head;while(p->m_pnext != head){p = p->m_pnext;if(p->GetData() == item)return p;}cout<<"can't find the element"<<endl;return 0;}

Test.cpp

#include <iostream>#include "DoubleList.h"using namespace std;int main(){DoublyList<int> list;for(int i=0;i<20;i++){list.Insert(i*3,i);}cout<<"the Length of the list is "<<list.Length()<<endl;list.Print();for(int i=0;i<5;i++){list.Insert(3,i*3);}cout<<"the Length of the list is "<<list.Length()<<endl;list.Print();list.Remove(5);cout<<"the Length of the list is "<<list.Length()<<endl;list.Print();cout<<list.FindData(54)->GetData()<<endl;cout<<"The third element is "<<list.Get(3)<<endl;list.MakeEmpty();cout<<"the Length of the list is "<<list.Length()<<endl;list.Print();return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.