ImageVerifierCode 换一换
格式:DOCX , 页数:71 ,大小:28.55KB ,
资源ID:7606620      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7606620.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++数据结构.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++数据结构.docx

1、C+数据结构8#include using namespace std; template struct Node T data; Node *next; ; template class LinkList public: LinkList( ); /建立只有头结点的空链表 LinkList(); /析构函数 int Length(); /求单链表的长度 void Insert(T x); /在单链表中的表尾位置插入元素值为x的结点 void Reverse( ); /逆置链表 void DispList( ); /遍历单链表,按序号依次输出各元素 private: Node *first;

2、/单链表的头指针 ; template void LinkList:Reverse() Node *p=first-next; Node *s; first-next=first; while(p!=first) s=p; p=p-next; s-next=first-next; first-next=s; template void LinkList:DispList() Node *p=first-next; coutThe length:Length()endl; coutThe elements:endl; while(p!=first) coutdatanext; coutendl;

3、 template void LinkList:Insert (T x) Node *s , *p; p=first; while(p-next!=first&p-next-datanext; s=new Node; s-data=x; s-next=p-next; p-next=s; template int LinkList:Length() Node *p=first-next; int count = 0 ; while( p!=first ) count+; p=p-next; return count ; /构造函数 template LinkList: LinkList( ) f

4、irst=new Node; first-next=first; /析构函数:释放链表各个结点所占内存单元 template LinkList: LinkList( ) Node *s; s=first-next; while (s!=first) first-next=s-next; delete s; s=first-next; delete s; int main( ) LinkList sa; int x; while(1) cinx; if(!x)break; sa.Insert(x); sa.DispList(); sa.Reverse(); sa.DispList(); retu

5、rn 0; 9#include #include using namespace std; template struct DNode T data; DNode *prior, *next; ; template class DLinkList public: DLinkList( ); /建立只有头结点的双向循环链表链表 DLinkList(); /析构函数 int Length(); /求链表的长度 void Insert(T x); /表尾插入x void DispListF( ); /正向遍历链表,按照逻辑序号输出各元素 void DispListR( ); /反向遍历链表,按照反向

6、逻辑序号输出各元素 private: DNode *first; /链表的头指针 ; template void DLinkList:Insert(T x) DNode *s, *p = first, *q= first-next; s=new DNode; s-data=x; while(p!=first) p=p-next; p-next = s ; s-prior = p; s-next = q ; q-prior = s ; template void DLinkList: DispListR() DNode *p=first-next; coutThe length:Length()

7、endl; coutThe elements:endl; while(p!=first) coutdatanext; coutendl; template void DLinkList: DispListF() DNode *p=first-prior; coutThe length:Length()endl; coutThe elements:endl; while(p!=first) coutdataprior; coutendl; template int DLinkList:Length() int count = 0 ; DNode *p=first-next; while(p!=f

8、irst) count+; p=p-next; return count; /* *前置条件:链表不存在 *输 入:无 *功 能:构建一个只有头结点的双向循环链表 *输 出:无 *后置条件:构建一个链表 */template DLinkList: DLinkList( ) first=new DNode; first-next=first-prior=first; /* *前置条件:链表存在 *输 入:无 *功 能:释放链表各个结点所占内存单元 *输 出:无 *后置条件:链表结点被释放 */template DLinkList: DLinkList() DNode *s; s=first-ne

9、xt; while (s!=first) first-next=s-next; delete s; s=first-next; delete s; int main() DLinkList DL; string subject; int i,n; cinn; for(i=1;isubject; DL.Insert(subject); DL.DispListF(); DL.DispListR(); return 0; 10#include using namespace std; const int M=100; int main() int i,k,n,m,count; int nameM;

10、cinnm; for (i = 0;i n;i +)/编号 namei= i +1; k = 0; for (i = 0;i n;i +) count = 0; while (count m) /每到m个人就输入 while (namek = 0) k = (k + 1)%n; count +; k = (k + 1)%n; k -; if (k 0) k = n-1; coutnamek ; namek = 0; return 0; 15#include #include using namespace std; const int QueueSize=5; template /定义模板类C

11、irQueue class CirQueue public: CirQueue( ); /构造函数,置空队 CirQueue( ); /析构函数 void EnQueue(T x); /将元素x入队 T DeQueue( ); /将队头元素出队 T GetQueue( ); /取队头元素(并不删除) bool Empty( ); /判断队列是否为空,空返回true,否则返回false bool Full(); /判断队列是否为满,满返回true,否则返回false private: T dataQueueSize; /存放队列元素的数组 int front, rear; /队头和队尾指针,分别

12、指向队头元素所在数组的前一下标和队尾元素的数组下标 ; template T CirQueue:GetQueue() if(rear=front) throw Downflow; int i = (front+1)%QueueSize; return datai; template T CirQueue:DeQueue() if(rear=front) throw Downflow; front = (front+1)%QueueSize; return datafront; template void CirQueue:EnQueue(T x) if(rear+1)%QueueSize=fr

13、ont) throw Overflow; rear=(rear+1)%QueueSize; datarear=x; /* * 前置条件:队列不存在 * 输 入:无 * 功 能:初始化队列 * 输 出:无 * 后置条件:创建一个空队列 */template CirQueue:CirQueue( ) front=rear=QueueSize-1; /* * 前置条件:队列已存在 * 输 入:无 * 功 能:销毁队列 * 输 出:无 * 后置条件:释放队列所占用的存储空间 */template CirQueue:CirQueue( ) /* * 前置条件:队列已存在 * 输 入:无 * 功 能:判断

14、队列是否为空 * 输 出:如果队列为空,返回1,否则,返回0 * 后置条件:队列不变 */template bool CirQueue:Empty( ) return front=rear; /* * 前置条件:队列已存在 * 输 入:无 * 功 能:判断队列是否为满 * 输 出:如果队列为满,返回1,否则,返回0 * 后置条件:队列不变 */template bool CirQueue:Full( ) return (rear+1) % QueueSize = front; int main() CirQueue Q; string x; while(1) cinx; if(x=#) bre

15、ak; try coutEnQueue:; Q.EnQueue(x); coutxn; catch(const char *ms) coutx msendl; while(!Q.Empty() x=Q.DeQueue(); coutDeQueue:xendl; try x=Q.GetQueue(); catch(const char *ms) coutGetQueue:The queue is empty,msendl; return 0; 16#include #include using namespace std; const int QueueSize=5; template /定义模

16、板类CirQueue class CirQueue public: CirQueue( ); /构造函数,置空队 CirQueue( ); /析构函数 void EnQueue(T x); /将元素x入队 T DeQueue( ); /将队头元素出队 T GetQueue( ); /取队头元素(并不删除) bool Empty( ); /判断队列是否为空,空返回true,否则返回false bool Full(); /判断队列是否为满,满返回true,否则返回false private: T dataQueueSize; /存放队列元素的数组 int front, rear;/队头和队尾指针,

17、分别指向队头元素所在数组的前一下标和队尾元素的数组下标 int count; /记录队列中数据个数 ; template T CirQueue:GetQueue() if(Empty() throw Downflow; int i = (front+1) % QueueSize ; return datai; template T CirQueue:DeQueue() if(Empty() throw Downflow; count-; front = (front+1) % QueueSize ; return datafront; template void CirQueue:EnQueu

18、e(T x) if(Full() throw Overflow; count+; rear=(rear+1)%QueueSize; datarear=x; /* * 前置条件:队列不存在 * 输 入:无 * 功 能:初始化队列 * 输 出:无 * 后置条件:创建一个空队列 */template CirQueue:CirQueue( ) front=rear=QueueSize-1; count=0; /* * 前置条件:队列已存在 * 输 入:无 * 功 能:销毁队列 * 输 出:无 * 后置条件:释放队列所占用的存储空间 */template CirQueue:CirQueue( ) /*

19、* 前置条件:队列已存在 * 输 入:无 * 功 能:判断队列是否为空 * 输 出:如果队列为空,返回1,否则,返回0 * 后置条件:队列不变 */template bool CirQueue:Empty( ) if(count=0) return true; return false; /* * 前置条件:队列已存在 * 输 入:无 * 功 能:判断队列是否为满 * 输 出:如果队列为满,返回1,否则,返回0 * 后置条件:队列不变 */template bool CirQueue:Full( ) if (count=QueueSize) return true; return false;

20、 int main() CirQueue Q; string x; while(1) cinx; if(x=#) break; try coutEnQueue:; Q.EnQueue(x); coutxn; catch(const char *ms) coutx msendl; while(!Q.Empty() x=Q.DeQueue(); coutDeQueue:xendl; try x=Q.GetQueue(); catch(const char *ms) coutGetQueue:The queue is empty,msendl; return 0; 17#include using

21、namespace std; template struct Node T data; Node *next; ; template class Trans private: Node *first; public: Trans(); Trans(); void Convert(int num,int d); void Push(T x); ; template Trans:Trans() first=new Node; first-next=NULL; template Trans:Trans() Node *s; s=first; while(s) first=s-next; delete

22、 s; s=first; template void Trans:Push(T x) Node *p; p=new Node; p-data=x; p-next=first-next; first-next=p; template void Trans:Convert(int num,int d) while(num) int i=num%d; Push(i); num=(num-i)/d; Node *p; p=first-next; while(p) Node *s; s=p; first-next=s-next; coutdata; delete s; p=first-next; cou

23、tendl; int main() Trans sa; int n,m; cinnm; sa.Convert(n,m); return 0; 19#include #include #include using namespace std; const int QueueSize=100; /循环队列的最大长度 template /定义模板类CirQueue class CirQueue public: CirQueue( ); /构造函数,置空队 CirQueue( ); /析构函数 void EnQueue(T x); /将元素x入队 T DeQueue( ); /将队头元素出队 T GetQueue( ); /取队头元素(并不删除) bool Empty( ); /判断队列是否为空 bool Ful

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1