资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
数据结构-队列实现舞伴配对问题+(舞伴程序+c+) 作者: 日期:#include#include const bool TURE=1;const bool FAULT=0;using std:cout;using std:cin;using std:endl;class Node/链式堆栈的节点类public: Node();/构造函数重载1 Node(int , int , int ,Node *);/构造函数重载2 void const get_data(int &, int &,int &);/取节点数据 void put_data(int , int ,int );/置节点数据 bool put_next(Node *);/置节点的前驱节点域 bool put_prior(Node *);/置结点的后继结点域 Node* const get_next()const;/取结点的前驱结点域 Node* const get_prior()const;/取结点的后继结点域private: Node *next; Node *prior; int minute; int second; int num;Node:Node() minute=NULL; second=NULL; num=0; next=NULL; prior=NULL;Node:Node(int x, int y, int z ,Node *p) minute=x; second=y; num=z; next=NULL; prior=p;void const Node:get_data(int &x, int &y, int &z) x=minute; y=second; z=num;void Node:put_data(int x, int y, int z) minute=x; second=y; num=z;bool Node:put_next(Node *n) next=n; return TURE;bool Node:put_prior(Node *p) prior=p; return TURE;Node* const Node:get_next() const return next;Node* const Node:get_prior() const return prior;class Queuepublic: Queue();/堆栈类的构造函数 int get_length(void);/取堆栈的长度 bool push(int , int , int );/数据压栈 bool pop(int &, int & , int &);/数据出栈 void print(void); int const seach(int );/搜索堆栈数据protected: Node base;/根节点 Node *top;/顶节点 int length;Queue:Queue():base()/构造函数,数据初始化length=0; top=&base;int Queue:get_length(void)/取长度if(lengthput_next(temp); top=temp; else return FAULT; return TURE;bool Queue:pop(int &minute, int &second, int &num) if(base.get_next()!=NULL) length-; Node *temp=base.get_next(); temp-get_data(minute,second,num); if(temp-get_next()!=NULL) Node *temp1=temp-get_next(); temp1-put_prior(&base); base.put_next(temp1); else top=&base; base.put_next(NULL); delete temp; else return FAULT; return TURE;int const Queue:seach(int num)/搜索堆栈数据 Node *temp; int x,y,z; temp=&base; while(temp!=NULL) temp-get_data(x,y,z); if(x=num) return 1; temp=temp-get_next(); return 0;void Queue:print()/打印堆栈 Node *temp; int x,y,z,n=1; temp=base.get_next(); if(temp=NULL) cout数据为空get_data(x,y,z); if(y=1) coutn: 第x号人士 性别:男 跳舞次数:zendl; else coutn: 第x号人士 性别:女 跳舞次数:zget_next(); n+; void ring_over(Queue &, Queue &, Queue &, Queue &);main()int sex,n=0,man_num=0,woman_num=0,number=0,degree5050=0,time=0,lun;Queue man,buffer_man,woman,buffer_woman;dosystem(cls);docout请输入n+1号人物性别(1:男士2:女士0:输入完毕)(总数不得超过100)sex;while(sex2);if(sex=1)man.push(n+1,sex,NULL);man_num+;n+;if(sex=2)woman.push(n+1,sex,NULL);woman_num+;n+;if(sex=0)system(cls);cout输入结束,共有woman_num+man_num名人士参与舞会endl;cout打印男士信息endl;man.print();cout打印女士信息=0&sex=2);system(cls);docout请输入舞会轮数:lun;while(lun0);while(timelun)for(;)if(woman.get_length()=0)ring_over(man,woman,buffer_man,buffer_woman);time+;break;else if(man.pop(man_num,sex,number)=0)ring_over(man,woman,buffer_man,buffer_woman);time+;break;elsebuffer_man.push(man_num,sex,number+1);if(woman.pop(woman_num,sex,number)buffer_woman.push(woman_num,sex,number+1);elsering_over(man,woman,buffer_man,buffer_woman);time+;break;degreeman_numwoman_num+;system(cls);cout第lun舞会结束endl;ring_over(man,woman,buffer_man,buffer_woman);cout打印每人的跳舞总数:endl;cout打印男士信息endl;man.print();cout打印女士信息endl;woman.print();docout请输入甲得号码man_num;while(man.seach(man_num)=0);docout请输入乙得号码woman_num;while(woman.seach(woman_num)=0);cout第man_num号男士和第woman_num号女士共跳了degreeman_numwoman_num次舞endl;return 0;void ring_over(Queue &man, Queue &woman, Queue &buffer_man, Queue &buffer_woman)int n,sex,number;while(buffer_woman.pop(n,sex,number)
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号