主要就是一個主函數,關於鏈表類,上節有注釋。
#include<stdio.h><br />template<class T> class DouListNode<br />{<br /> T data;<br /> DouListNode<T>* link;<br /> DouListNode<T>* prior;<br /> public:<br /> DouListNode():link(NULL),prior(NULL){}<br /> DouListNode(T value):link(NULL),prior(NULL),data(value){}<br /> ~DouListNode(){};<br /> void SetLink(DouListNode<T>* next);<br /> void SetPrior(DouListNode<T>* pre);<br /> DouListNode<T>* GetLink();<br /> DouListNode<T>* GetPrior();<br /> T& GetData();<br />};<br />template<class T><br />void DouListNode<T>::SetLink(DouListNode<T>* next)<br />{<br /> link=next;<br />}<br />template<class T><br />void DouListNode<T>::SetPrior(DouListNode<T>* pre)<br />{<br /> prior=pre;<br />}<br />template<class T><br />DouListNode<T>* DouListNode<T>::GetLink()<br />{<br /> return link;<br />}<br />template<class T><br />DouListNode<T>* DouListNode<T>::GetPrior()<br />{<br /> return prior;<br />}<br />template<class T><br />T& DouListNode<T>::GetData()<br />{<br /> return data;<br />} </p><p>template<class T> class DouList<br />{<br /> DouListNode<T>* head;<br /> DouListNode<T>* tail;<br /> DouListNode<T>* cur;<br /> public:<br /> DouList();<br /> ~DouList(){};<br /> bool AddTail(T value);<br /> bool AddHead(T value);<br /> void RemoveThis(bool direction);<br /> void RemoveAll();<br /> void SetBegin();<br /> int GetCount();<br /> void TowardCur();<br /> void BackCur();<br /> DouListNode<T>* GetCur();<br /> DouListNode<T>* GetHead();<br /> DouListNode<T>* GetTail();<br /> void InsertAfter(T value);<br /> bool IsEmpty();<br /> T GetNext();<br /> T GetPrior();<br />};<br />template<class T><br />DouList<T>::DouList() //預設建構函式<br />{<br /> head=tail=new DouListNode<T>; //建立表頭結點,初始化頭,尾指標<br /> cur=NULL;<br /> head->SetLink(head); //初始迴圈<br /> head->SetPrior(tail);<br />}<br />template<class T><br />bool DouList<T>::AddTail(T value) //表尾插入一個新結點<br />{<br /> DouListNode<T>* add=new DouListNode<T>(value); //建立一個新結點,資料根據value判斷<br /> tail->SetLink(add);<br /> add->SetPrior(tail);<br /> tail=tail->GetLink();<br /> tail->SetLink(head); //讓表尾結點指向表頭結點<br /> head->SetPrior(add); //讓表頭結點的前驅為表尾結點<br /> if(tail!=NULL)<br /> {<br /> return true;<br /> }<br /> else<br /> return false;<br />}<br />template<class T><br />bool DouList<T>::AddHead(T value)//向表頭結點與鏈表第一個結點之間插入新節點<br />{<br /> DouListNode<T>* add=new DouListNode<T>(value);<br /> add->SetPrior(head);<br /> add->SetLink(head->GetLink());<br /> head->GetLink()->SetPrior(add);//讓鏈表中第一個結點的前驅為add,則它成為鏈表的FIRST結點<br /> head->SetLink(add);<br /> if(tail==head)<br /> {<br /> tail=head->GetLink();<br /> }<br /> if(add!=NULL)<br /> {<br /> return true;<br /> }<br /> else<br /> return false;<br />}<br />template<class T><br />void DouList<T>::RemoveThis(bool direction)//把當前cur指向的結點刪除,然後cur的移動方向根據direction決定<br />{<br /> if(cur==head)//如果cur處於表頭,表頭結點不能刪除,順序移動cur<br /> {<br /> if(direction==0)//沿著link方向移動cur<br /> cur=cur->GetLink();<br /> if(direction==1)//沿著prior方向移動cur<br /> cur=cur->GetPrior();<br /> }<br /> DouListNode<T>* preCur=NULL;<br /> DouListNode<T>* nextCur=NULL;<br /> preCur=cur->GetPrior();//preCur指向cur的前驅結點<br /> nextCur=cur->GetLink();//nextCur指向cur的後繼結點<br /> preCur->SetLink(cur->GetLink());//cur的前驅直接指向後繼結點<br /> nextCur->SetPrior(cur->GetPrior());//cur的后街結點直接指向前驅結點<br /> if(direction==0)<br /> cur=nextCur;//向link方向移動cur<br /> if(direction==1)<br /> cur=preCur;//向prior方向移動cur </p><p>}<br />template<class T><br />void DouList<T>::RemoveAll()//刪除所有結點(頭結點例外吧O(∩_∩)O哈哈~)<br />{<br /> SetBegin();//從頭開始刪除<br /> int length=GetCount();<br /> for(int i=0;i<length;i++)//迴圈刪除,乾淨<br /> {<br /> RemoveThis(0);<br /> }<br /> cur=head;<br />}<br />template<class T><br />void DouList<T>::SetBegin()//置cur到表頭<br />{<br /> cur=head;<br />}<br />template<class T><br />int DouList<T>::GetCount()<br />{<br /> int num=0;//讓他記錄鏈表長度<br /> DouListNode<T>* here=cur;//利用一個here對鏈表進行遍曆<br /> while(cur->GetLink()!=here)<br /> {<br /> cur=cur->GetLink();<br /> ++num;<br /> }<br /> cur=cur->GetLink();//遍曆完成,cur迴歸到原來的位置<br /> return num;<br />}<br />template<class T><br />void DouList<T>::TowardCur()<br />{<br /> cur=cur->GetLink();<br />}<br />template<class T><br />void DouList<T>::BackCur()<br />{<br /> cur=cur->GetPrior();<br />}<br />template<class T><br />DouListNode<T>* DouList<T>::GetCur()<br />{<br /> return cur;<br />}<br />template<class T><br />DouListNode<T>* DouList<T>::GetHead()<br />{<br /> return head;<br />}<br />template<class T><br />DouListNode<T>* DouList<T>::GetTail()<br />{<br /> return tai;<br />}<br />template<class T><br />bool DouList<T>::IsEmpty()//鏈表是否為空白<br />{<br /> return head->GetLink()==head;<br />}<br />template<class T><br />void DouList<T>::InsertAfter(T value)<br />{<br /> DouListNode<T>* add=new DouListNode<T>(value);<br /> DouListNode<T>* nextCur=cur->GetLink();<br /> cur->SetLink(add);<br /> add->SetLink(nextCur);<br /> nextCur->SetPrior(add);<br /> add->SetPrior(cur);<br /> if(cur==tail)<br /> {<br /> tail=cur->GetLink();<br /> }<br />}<br />template<class T><br />T DouList<T>::GetNext()//獲得cur指向的結點資料,然後cur向 link方向移動<br />{<br /> if(cur==head)<br /> cur=cur->GetLink();<br /> T num=cur->GetData();//返回資料<br /> cur=cur->GetLink();//移動cur<br /> return num;<br />}<br />template<class T><br />T DouList<T>::GetPrior()<br />{<br /> if(cur==head)<br /> cur=cur->GetPrior();<br /> T num=cur->GetData();<br /> cur=cur->GetPrior();<br /> return num;<br />}<br />//下面是主程式<br />#include<iostream><br />#include "ctime"<br />#include<list><br />#include<string><br />using namespace std;<br />void main()<br />{<br />int ran_num;<br />srand((unsigned)time(NULL));<br />//srand((unsigned)time(NULL));<br />//cout<<time(NULL)<<endl;<br />/*for(int i=0;i<10;i++)<br />{<br />ran_num=rand()%6;<br />cout<<ran_num<<" ";<br />}*/<br />DouList<char> planText;<br />DouList<char> cryptograph;<br />DouList<int> key;<br />DouList<char> trans;<br />planText.SetBegin();<br />planText.AddTail('Y');<br />planText.AddTail('o');<br />planText.AddTail('u');<br />planText.AddTail('a');<br />planText.AddTail('r');<br />planText.AddTail('e');<br />planText.AddTail('i');<br />planText.AddTail('n');<br />planText.AddTail('d');<br />planText.AddTail('a');<br />planText.AddTail('n');<br />planText.AddTail('g');<br />planText.AddTail('e');<br />planText.AddTail('r');<br />planText.AddTail('!');<br />planText.SetBegin();<br />cout<<"明文:"<<'/t';<br />for(int i=0;i<planText.GetCount();i++)<br />{<br />cout<<planText.GetNext()<<"*";<br />}<br />cout<<endl<<endl;<br />key.SetBegin();<br />for(int z=0;z<6;z++)<br />{<br />key.AddTail(rand()%6);<br />}<br />cout<<"密鑰:"<<'/t';<br />for(i=0;i<key.GetCount();i++)<br />{<br />cout<<key.GetNext()<<"*"<<endl;<br />}<br />cout<<endl<<endl;<br />planText.SetBegin();<br />key.SetBegin();<br />cryptograph.SetBegin();<br />{<br />for(i=0;i<planText.GetCount();i++)<br />{<br />cryptograph.AddTail(planText.GetNext()+key.GetNext());</p><p>}<br />cryptograph.SetBegin();<br />cout<<"密文:"<<'/t';<br />for(int k=0;k<cryptograph.GetCount();k++)<br />{<br />cout<<cryptograph.GetNext();<br />}<br />cout<<endl<<endl;<br />}<br />trans.SetBegin();<br />planText.SetBegin();<br />key.SetBegin();<br />cryptograph.SetBegin();<br />for(i=0;i<planText.GetCount();i++)<br />{<br />trans.AddTail(cryptograph.GetNext()-key.GetNext());<br />}<br />trans.SetBegin();<br />cout<<"解密後:"<<'/t';<br />for(int k=0;k<trans.GetCount();k++)<br />{<br />cout<<trans.GetNext();<br />}<br />cout<<endl<<endl;<br />}