欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    数据结构_C源代码.docx

    • 资源ID:68465681       资源大小:73.77KB        全文页数:104页
    • 资源格式: DOCX        下载积分:12金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要12金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    数据结构_C源代码.docx

    1、顺序表Seqlist.hconst int DefaultSize=100;template <typename Typo class SeqListpublic:SeqList(int sz=DefaultSize):m_nmaxsize(sz),m_ncurrentsize(-l)if(sz>0)m_elements=new Typem_nmaxsize;)SeqList()delete m_elements;int Length() constreturn m_ncurrentsize+l;)int Find(Type x) const;int lsElement(Type x) const;int lnsert(Type xjnt i);int Remove(Type x);int lsEmpty()return m_ncurrentsize=-l;)int lsFull()/get the length/find the position of x/is it in the list/insert data/delete datareturn m_ncurrentsize=m_nmaxsize-l;)Type Get(int i)/get the ith datafindthereturni<011 i>m_ncurrentsize?(cout«"can'telement"«endlzO):m_elementsi;void Print();private:Type *m_elements;const int m_nmaxsize; int m_ncurrentsize;|;template <typename Type> int SeqList<Type>:Find(Type x) constfor(int i=O;i<m_ncurrentsize;i+)if(m_elementsi=x)return i;cout«Hcan't find the element you want to find"«endl;return -1;template <typename Typo int SeqList<Type>:lsElement(Type x) const if(Find(x)=-l)return 0;return 1;template <typename Type> int SeqList<Type>:lnsert(Type x, int i)if(i<0| |i>m_ncurrentsize+l| | m_ncurrentsize=m_nmaxsize-l) cout«"the operate is illegal"«endl;return 0;m_ncurrentsize+;for(int j=m_ncurrentsize;j>i;j)m_elementsj=m_elementsj-l;m_elementsi=x;return 1;template <typename Type> int SeqList<Type>:Remove(Type x)int size=m_ncurrentsize;for(int i=O;i<m_ncurrentsize;)if(m_elementsi=x)for(int j=i;j<m_ncurrentsize;j+) m_elementsj=m_elementsj+l;m_ncurrentsize-;continue;)i+;if(size=m_ncurrentsize)cout«"can't find the element you want to remove"«endl; return 0;)return 1;删除所有为X的数template <typename Type> void SeqList<Type>:Print()for(int i=O;i<=m_ncurrentsize;i+)cout«i+l«":t"«m_elementsi«endl;cout«endl«endl;Test.cpp#include <iostream>include "SeqList.h"using namespace std;int main()(SeqList<int> test(15);int array15=2,5,8,l,9,9,7,6,4,3,2,9,7,7,9;for(int i=0;i<15;i+)test.lnsert(arrayi,O);test.lnsert(l,O);cout«(test.Find(0)?"can't be found ":"Be found ")« 0 « endl«endl;test.Remove(7);test.Print();test.Remove(9);test.Print();test.Remove(O);test.Print();return 0;2、单链表ListNode.htemplate<typename Type> class SingleList;/List 类的前视声明template<typename Type> class ListNode private:friend typename SingleList<Type>ListNode():m_pnext(NULL)ListNode(const Type item/ListNode<Type> *next=NULL):m_data(item)/m_pnext(next) ListNode()m_pnext = NULL;public:Type GetData();friend ostream& operator«<Type>(ostream& zListNode<Type>&);private:Type m_data;ListNode *m_pnext;|;template<typename Type> Type ListNode<Type>:GetData() return this->m_data;template<typename Type> ostream& operator«(ostream& os,ListNode<Type>& out) os«out.m_data;return os;|SingleList.hinclude "UstNode.h"template<typename Type> class SingleListpublic:SingleList():head(new ListNode<Type>()SingleList()MakeEmptyO;delete head;public:void MakeEmpty();/make the list emptyint Length();/get the lengthListNode<Type> *Find(Type valuejnt n); /find thd nth data which is equal to valueListNode<Type> *Find(int n);/find the nth databool lnsert(Type itemjnt n=O);/insert the data in the nth positionType Remove(int n=O);/remove the nth databool RemoveAII(Type item);/remove all the data which is equal to itemType Get(int n);/get the nth datavoid Print();/print the listprivate:ListNode<Type> *head;template<typename Type> void SingleList<Type>:MakeEmpty()ListNode<Type> *pdel;while(head->m_pnext!=NULL)pdel=head->m_pnext;head->m_pnext=pdel->m_pnext;delete pdel;template<typename Type> int SingleList<Type>:Length()ListNode<Type> *pmove=head->m_pnext;int count=0;while(pmove!=NULL)pmove=pmove->m_pnext;count+;)return count;template<typename Type> ListNode<Type>* SingleList<Type>:Find(int n)if(n<0)cout«"The n is out of boundaryH«endl;return NULL;)ListNode<Type> *pmove=head->m_pnext;for(int i=0;i<n&&pmove;i+)pmove=pmove->m_pnext;if(pmove=NULL)cout«"The n is out of boundary"«endl;return NULL;return pmove;template<typename Type> ListNode<Type>* SingleList<Type>:Find(Type valuejnt n) if(n<l)cout«"The n is illegal"«endl;return NULL;ListNode<Type> *pmove=head;int count=0;while(count!=n&&pmove)pmove=pmove->m_pnext;if(pmove->m_data=value)count+;)if(pmove=NULL)cout«"can't find the element"«endl;return NULL;return pmove;template<typename Type> bool SingleList<Type>:lnsert(Type item, int n) if(n<0)cout«"The n is illegal"«endl;return 0;ListNode<Type> *pmove=head;ListNode<Type> *pnode=new ListNode<Type>(item);if(pnode=NULL)cout«"Application error!"«endl;return 0;for(int i=0;i<n&&pmove;i+)pmove=pmove->m_pnext;if(pmove=NULL)cout«"the n is illegal"«endl;return 0;pnode->m_pnext=pmove->m_pnext;pmove->m_pnext=pnode;return 1;template<typename Type> bool SingleList<Type>:RemoveAII(Type item)ListNode<Type> *pmove=head;ListNode<Type> *pdel=head->m_pnext;while(pdel!=NULL)if(pdel->m_data=item)pmove->m_pnext=pdel->m_pnext;delete pdel;pdel=pmove->m_pnext;continue;pmove=pmove->m_pnext;pdel=pdel->m_pnext;return 1;template<typename Type> Type SingleList<Type>:Remove(int n) if(n<0)cout«Hcan't find the element"«endl;exit ;ListNode<Type> *pmove=headz*pdel;for(int i=0;i<n&&pmove->m_pnext;i+)pmove=pmove->m_pnext;)if(pmove->m_pnext=NULL)cout«"can't find the element"«endl;exit(l);pdel=pmove->m_pnext;pmove->m_pnext=pdel->m_pnext;Type temp=pdel->m_data;delete pdel;return temp;template<typename Type> Type SingleList<Type>:Get(int n)if(n<0)cout«"The n is out of boundary"«endl;exit(l);ListNode<Type> *pmove=head->m_pnext;for(int i=0;i<n;i+)pmove=pmove->m_pnext;if(NULL=pmove)cout«"The n is out of boundary"«endl;exit(l);return pmove->m_data;template<typename Type> void SingleList<Type>:Print() ListNode<Type> *pmove=head->m_pnext;cout«nhead"while(pmove)cout«"一>"«pmove->m_data;pmove=pmove->m_pnext;cout«"一>over"«endl«endl«endl;test.cpp#include <iostream> using namespace std;#include "SingleList.h"int main()(SingleList<int> list;for(int i=O;i<2O;i+)list.lnsert(i*3,i);for(int i=0;i<5;i+)list.lnsert(3J*3);cout«"the Length of the list is "«list.Length()«endl;list.Print();list.Remove(5);cout«Hthe Length of the list is H«list.Length()«endl;list.Print();list.RemoveAII(3);cout«"the Length of the list is "«list.Length()«endl;list.Print();cout«"The third element is u«list.Get(3)«endl;cout«*list.Find(184)«endl;list.Find(lOO);list.MakeEmpty();cout«"the Length of the list is "«list.Length()«endl;list.Print();return 0;3、双向链表NodeList.htemplate<typename Type> class DoublyList;template<typename Type> class ListNode 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;public:Type GetData();private:Type m_data;ListNode *m_pprior;ListNode *m_pnext;;template<typename Type> Type ListNode<Type>:GetData() return this->m_data;|DoubleList.h#include "ListNode.h" template<typename Type> class DoublyList public:DoublyList():head(new ListNode<Type>() /the head node point to itself head->m_pprior=head;head->m_pnext=head;)DoublyList()MakeEmptyO;delete head;public:void MakeEmpty(); /make the list emptyint Length(); /get the length of the listListNode<Type> *Find(int n=O); /find the nth dataListNode<Type> * FindData(Type item); /find the data which is equal to item bool lnsert(Type itemjnt n=O); /insert item in the nth data Type Remove(int n=0); /delete the nth data Type Get(int n=0); /get the nth data void Print();/print the listprivate:ListNode<Type> *head;template<typename Type> void DoublyList<Type>:MakeEmpty() ListNode<Type> *pmove=head->m_pnext/*pdel;while(pmove!=head)pdel=pmove;pmove=pdel->m_pnext;delete pdel;head->m_pnext=head;head->m_pprior=head;template<typename Type> int DoublyList<Type>:Length()ListNode<Type> *pprior=head->m_pprior/*pnext=head->m_pnext;int count=0;while(l)if(pprior->m_pnext=pnext) break;if(pprior=pnext&&pprior!=head)count+;break;count+=2;pprior=pprior->m_pprior;pnext=pnext->m_pnext;)return count;template<typename Typo ListNode<Type>* DoublyList<Type>:Find(int n = 0) if(n<0)cout«"The n is out of boundary"«endl;return NULL;ListNode<Type> *pmove=head->m_pnext;for(int i=0;i<n;i+)pmove=pmove->m_pnext;if(pmove=head)cout«"The n is out of boundary"«endl;return NULL;return pmove;|template<typename Type> bool DoublyList<Type>:lnsert(Type itemjnt n) if(n<0)cout«"The n is out of boundary"«endl;return 0;)ListNode<Type> *newnode=new ListNode<Type>(item)/*pmove=head;if(newnode=NULL)cout«"Application Erorr!"«endl;exit(l);)for(int i=0;i<n;i+) /find the position for insertpmove=pmove->m_pnext;if(pmove=head)cout«"The n is out of boundary"«endl;return 0;/insert the datanewnode->m_pnext=pmove->m_pnext;newnode->m_pprior=pmove;pmove->m_pnext=newnode;newnode->m_pnext->m_pprior=newnode;return 1;template<typename Typo Type DoublyList<Type>:Remove(int n = 0) if(n<0)cout«"The n is out of boundaryH«endl;exit(l);ListNode<Type> *pmove=headz*pdel;for(int i=0;i<n;i+) /find the position for deletepmove=pmove->m_pnext;if(pmove=head)cout«"The n is out of boundary"«endl;exit(l);/delete the datapdel=pmove;pmove->m_pprior->m_pnext=pdel->m_pnext;pmove->m_pnext->m_pprior=pdel->m_pprior;Type temp=pdel->m_data;delete pdel;return temp;template<typename Type> Type DoublyList<Type>:Get(int n = 0) if(n<0)cout«"The n is out of boundaryH«endl;exit(l);)ListNode<Type> *pmove=head;for(int i=0;i<n;i+)pmove=pmove->m_pnext;if(pmove=head)cout«"The n is out of boundary"«endl;exit ;)return pmove->m_data;template<typename Type> void DoublyList<Type>:Print()ListNode<Type> *pmove=head->m_pnext;cout«Hhead"while(pmove!=head)cout«n一>"«pmove->m_data;pmove=pmove->m_pnext;)cout«"一>over"«endl«endl«endl;template<typename Type> ListNode<Type>* DoublyList<Type>:FindData(Type item) ListNode<Type> *pprior=head->m_pprior/*pnext=head->m_pnext;while(pprior->m_pnext!=pnext && pprior!=pnext) /find the data in the two direction if(pprior->m_data=item)return pprior;)if(pnext->m_data=item)return pnext;)pprior=pprior->m_pprior;pnext=pnext->m_pnext;cout«"can't find the element"«endl;return NULL;Test.cpp#include <iostream>include ,DoublyList.h"using namespace std;int main()(DoublyList<int> list;for(int i=0;i<20;i+)list.lnsert(i*3,i);cout«nthe Length of the list is u«list.Length()«endl;list.Print();for(int i=0;i<5;i+)list.lnsert(3,i*3);cout«Hthe Length of the list is n«list.Length()«endl;list.Print();list.Remove(5);cout«Hthe 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;4、循环链表UstNode.htemplate<typename Type> class CircularList;template<typename Type> class ListNode private:friend class CircularList<Type>ListNode():m_pnext(NULL)ListNode(const Type item/ListNode<Type> *next=NULL):m_data(item)zm_pnext(next) ListNode()m_pnext=NULL;private:Type m_data;ListNode *m_pnext;;CircularList.hinclude "ListNode.h" template<typename Type> class CircularList public:CircularList():head(new ListNode<Type>() head->m_pnext=head;CircularList()MakeEmptyO; delete head;public:void MakeEmptyO; /clear the listint Length();/get the lengthListNode<Type> *Find(Type valuejnt n); /find the nth data which is equal to value/find the nth data/insert the data into the nth data of the list/delete the nth data/delete all the dates which are equal to valueListNode<Type> *Find(int n);bool lnsert(Type itemjnt n=O);Type Remove(int n=O);bool RemoveAII(Type item);Type Get(int n); /get the nth data void Print(); /print the listprivate:ListNode<Type> *head;template<typename Type> void CircularList<Type>:MakeEmpty() ListNode<Type> *pdelz*pmove=head;while(pmove->m_pnext!=head)pdel=pmove->m_pnext;pmove->m_pnext=pdel->m_pnext;delete pdel;template<typename Type> int CircularList<Type>:Length() ListNode<Type> *pmove=head;int count=0;while(pmove->m_pnext!=head) pmove=pmove->m_pnext;count+;return count;template<typename Type> ListNode<Type>* CircularList<Type>:Find(int n)if(n<0)cout«"The n is out of boundaryn«endl;return NULL;)ListNode<Type> *pmove=head->m_pnext;for(int i=0;i<n&&pmove!=head;i+)pmove=pmove->m_pnext;if(pmove=head)cout«"The n is out of boundaryn«endl;return NULL;)return pmove;)template<typename Type> ListNode<Type>* CircularList<Type>:Find(Type valuejnt n) if(n<l)cout«"The n is illegal,«endl;return NULL;ListNode<Type> *pmove=head;int count=0;while(count!=n)pmove=pmove->m_pnext;if(pmove->m_data=value)count+;)if(pmove=head)cout«"can't find the element"«endl;return NULL;)return pmove;template<typename Type> bool CircularList<Type>:lnsert(Type item, int n)if(n<0)cout«"The n is out of boundaryH«endl;return 0;)ListNode<Type> *pmove=head;ListNode<Type> *pnode=new ListNode<Type>(item);if(pnode=NULL)cout«"Application error!"«endl;exit(l);)for(int i=O;i<n;i+)pmove=pmove->m_pnext;if(pmove=head)cout«"The n is out of boundary"«endl;return 0;)pnode->m_pnext=pmove->m_pnext;pmove->m_pnext=pnode;return 1;)template<typename Type> bool CircularList<Type>:RemoveAII(Type item) ListNode<Type> *pmove=head;ListNode<Type> *pdel=head->m_pnext;while(pdel!=head)if(pdel->m_data=item)pmove->m_pnext=pdel->m_pnext;delete pdel;pdel=pmove->m_pnext;continue;pmove=pmove->m_pnext;pdel=pdel->m_pnext;return 1;template<typename Type> Type CircularList<Type>:Remove(int n)if(n<0)cout«"can't find the element"«endl;exit(l);)ListNode<Type> *pmove=head/*pdel;for(int i=0;i<n&&pmove->m_pnext!=head;i+) pmove=pmove->m_pnext;if(pmove->m_pnext=head)cout«"can't find the element"«endl;exit(l);pdel=pmove

    注意事项

    本文(数据结构_C源代码.docx)为本站会员(无***)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开