资源简介
数据结构各种算法实现(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
代码片段和文件信息
- 上一篇:c++ 课程设计学籍管理系统
- 下一篇:C++ Builder 网络小游戏 五子棋
相关资源
- IC卡停车场管理系统源代码
- 消解原理实现有界面
- C++ 课程设计 大作业 背单词小软件
- VS2005下TCP网络编程,亲自调试成功w
- MFC__快捷菜单管理器
- MFC最小化至系统托盘源码
- C++C#互通DES加密算法
- OPCDA服务器与客户程序开发指南 书籍
- [MPI与OpenMP并行程序设计:C语言版].(
- C++ Qt设计模式第2版中文版
- 读取图片并显示的MFC程序
- MFC贪吃蛇完美运行
- Vc++编写的计算器简单小程序
- Programming -- Principles and Practice Using C
- 中国海洋大学C++课程期末资料
- The C++ Programming Language 4th Edition.pdf
- visual c++串口通信技术详解
- MFC实现TCP数据传输
- VC++ CLR编写带图形界面的简单计算器
- 测试驱动嵌入式C语言开发中英文合集
- vc++6.0 绿色版 完整版支持win7,win8系统
- 停车场管理系统 MFC 数据结构课程设计
- C#图解教程.pdf
- 零基础学TMS320F281x DSP C语言开发.pdf
- 郑莉C++语言程序设计第3版[PDF+ppt+源码
- C++ Qt设计模式 第2版 中文版 PDF
- C++面向对象程序设计 谭浩强主编真正
- Dev-Cpp 5.11 TDM-GCC 4.9.2
- C++Primer课后习题解答(第1~18章完整答
- 啊哈c语言.pdf
评论
共有 条评论