• 大小: 0.59M
    文件类型: .pdf
    金币: 1
    下载: 0 次
    发布日期: 2021-03-27
  • 语言: C/C++
  • 标签: c  

资源简介


数据结构各种算法实现(C++模板)
cout<<the operate is illegal"<<endl return 0; m ncurrentsize++ for (int j=m ncurrentsize: j>1:j--)( m elements [j]=m elements[1-1]; m_elements [i]=X; return 1 template <typename Type> int SeqList<Type>: Remove(Type x)t int size=m ncurrentsize: for (int i=0; i<m ncurrentsize; )1 if(m elements[i]==x)f for (int j=i; j<m_ ncurrentsize; j++)t m elements [ j]=m elements [j+1; m ncurrentsize--; 1++ if(size==m_ ncurrentsize) cout<<"cant find the element you want to remove"<<endl; return 0 return 1j template <typename Type> void Seqlist<Type>:: Printf for (int i=0; i<=m ncurrentsize; i++) cout<<+1<<":t"<<m_elements[i]<<endl cout<cendl<cendl; Test. cpp include <iostream> # include Seglist h using namespace std int maino SeqList<int> test(15); int array[15]={2,5,8,1,9,9,7,6,4,3,2,9,7,7,9} for(int i=0; 1<15; i++)f test. Insert(arrayli,0) test. Insert(1,0) cout<<(test. Find(o)? can't be found ": Be found ")<<0 << endl<<endl; test. Remove(7)i test Print(; test. Remove(9); test Print(; test. Remove(0)j test Print(); return 0 2.单链表 ListNode h template<typename Type> class singleList; template<typename Type> class ListNodet private friend typename Singlelist<Type> ListNode(: m pnext(NULL) ListNode(const Type item, ListNode<Type> *next=NULL): m data(item),m_pnext(next)t cListNode(i m pnext=NULL, public Type GetData() friend ostream& operator<<<Type>(ostream&, ListNode<Type&j private Type m data; ListNode >m pnext template<typename Type> Type ListNode<Type>: GetDataot return this->m data template<typename Type> ostream& operator<<(ostream& os, ListNode< Type>& out)f os<<out. m data return os Singlelist. h include ListNode h template<typename Type> class singlelisti ublic Singlelist(: head (new ListNode< Type>()1 csingleListoi MakeEmpty; delete head void MakeEmpty oi //make the list empty int Length(); //get the length ListNode<Type> *Find (Type value, int n); //find thd nth data which is equal to value ListNode<Type> *Find(int n); //find the nth data bool Insert (Type item, int n=0)3 //insert the data in the nth position Type Remove(int n=0); //remove the nth data bool RemoveAll(Type item); //remove all the data which is equal to item Type Get(int n); //get the nth data void Print(; //print the list private ListNode<Type> *head; template<typename Type> void singlelist<Type>:: Make Empty ()t ListNode<Type>*pdel; While(head->m_ pnext! =NULL)L del=head->m pnext head->m_ pnext=pdel->m pnext delete pdel template<typename Type> int SingleList<Type>: Length(i ListNode<Type>>pmove=head->m pnext int count=0 while(pmove! =NULL )f pmove=pmove->m_pnext; count++; return count j template<typename Type> ListNode<Type>* Singlelist<Type>:: Find(int n) if(nco)t cout<<The n is out of boundary "<<endl; return NULL; ListNode<Type>*pmove=head->m_pnext; for(int i=0; i<n&&pmove; i++)f pmove=pmove->m_ pnext; if(pmove==NULL cout<< The n is out of boundary"<endl eturn null return pmove template<typename Type> ListNode<Type>* singleList<Type>:: Find (Type value, int n)f if(n<1){ cout<< The n is illegal" <<endl return NULL ListNode<Type>*pmovehead int counte while(count !=n&&pmovet pmove=pmove->m_pnext; f( lue)t count++ if(pmove==NULL )f cout<<"cant find the element <<endl; eturn null template<typename Type> bool SingleList<Type>:: Insert(Type item, int n)f if(no) cout<<The n is illegal"<<endl turn a ListNode< Type>*pmove=head; ListNode <Type> *pnode=new ListNode<Type>(item); if(pnode==NULL )f cout<<Application error!"<<endl; eturn 0; for(int i=0; i<n&& pmove; i++)t pmove=pmove->m_pnext; if(pmove==NULL) cout<< the n is illegal"<<endl; return 0; pnode->m_pnext=pmove->m_pnext; pmove->_pnext=pnodej return 1 template<typename Type> bool Singlelist<Type>:: RemoveAll(Type item)i ListNode <Type>*pmove=head ListNode<Type> *pdel=head->m_pnext; while(pdel!=NULL) f(pdel->m data==item)t pmove->m_ pnext=pdel-m_pnext; pde=-pmove->m pnext; continue, pmove=pmove->m_ pnext; pde l=pdel->m_ pnext return 1 template<typename Type> Type SingleList<Type>:: Remove(int n)i if(n<9){ cout<< can t find the element <<endl exit(1) ListNode<Type>*pmove=head, *pdel for (int i=0; i<n&&pmove-m_pnext; i++)t pmove=pmove ->m_ pnext; if(pmove->m_pnext==NULL)( coutss can 't find the element <<endl exit(1); pdel-pmove->m pnext; pmove->mpnext=pdel-m_pnext; Type temp=pdel->m data; delete pdelj return temp template<typename Type> Type Singlelist<Type>:: Get(int n)t if(no)f cout<<"The n is out of boundary "<cendl; exit(1) ListNode<T head-m_ pnext for (int i=0; i<n; 1++)t pmove=pmove->m pnext if(NULL==pmove )t cout<< The n is out of boundary"<endl; return pmove->m data; template<typename Type> void SingleList<Type>:: Print ot ListNode<Type> 'pmove=head->m_ pnext cout<< head while(pmove)i cout<"---><<pmove->m data pmove=pmove->m pnext; cout<<--->over"<<endl<<endl<endl Test. cpp tinc lude < iostream> using namespace std; include " SingleList. h int main() Singlelist<int> list for(int i=0; i<20; i++)1 list. Insert(i*3,i); for(int i-0; i<5; i++)f list. Insert(3,1*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(; list. RemoveAll(3); cout<< the Length of the list is"<<list Length(<<endl; list Print() cout<<"The third element is"<<list. Get(3)<<endl; cout<<*list Find(18, 1)<endl list Find(100); list. MakeEmpty o; cout<<"the Length of the list is"<<list Length(<<endl; list. Printo; return e } 3.双向链表 Nodelist. h template<typename Type> class doublyListi template<typename Type> class ListNodef private friend cl ublyList<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)t ListNode(i m_pprior=NULL m pnext=NULL public Type GetData(; private ListNode *m pprior; ListNode *m pnext; template<typename Type> Type ListNode<Type>: GetData(t return this->m data; Doublelist h

资源截图

代码片段和文件信息

评论

共有 条评论