欢迎来到天天文库
浏览记录
ID:57644544
大小:75.50 KB
页数:6页
时间:2020-08-30
《火车车厢重排-问题解决-c++(队列方法).doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、火车车厢重排问题c++解决方法(队列实现)//Queue.h#ifndefQUEUE_H#defineQUEUE_H#includeusingnamespacestd;templateclassQueue{public:virtualvoidMakeEmpty()=0;virtualvoidEnqueue(Tx)=0;virtualTDequeue()=0;virtualboolIsEmpty()const=0;virtualboolIsFull()const=0;virtualTGetFirstI
2、tem()=0;};classEmptyQueue{};classFullQueue{};#endif//LinkQueue.h//链表队列#ifndefLINKQUEUE_H#defineLINKQUEUE_H#include#include"Queue.h"usingnamespacestd;templatestructNode{Tdate;Node*next;};templateclassLinkQueue:publicQueue{template3、ameT>friendostream&operator<<(ostream&s,constLinkQueue&lq);public:LinkQueue();~LinkQueue();voidMakeEmpty();voidEnqueue(Tx);TDequeue();boolIsEmpty()const;boolIsFull()const;TGetFirstItem();TGetlastItem();private:Node*front;Node*rear;};templateLinkQueue4、::LinkQueue(){front=newNode;front->next=NULL;rear=front;}templateLinkQueue::~LinkQueue(){MakeEmpty();deletefront;}templatevoidLinkQueue::Enqueue(Tx){Node*newnode;newnode=newNode;newnode->date=x;newnode->next=NULL;rear->next=newnode;rear5、=newnode;}templateTLinkQueue::Dequeue(){if(IsEmpty())throwEmptyQueue();Node*p;p=front->next;Tx=p->date;front->next=p->next;if(p->next==NULL)rear=front;deletep;returnx;}templateboolLinkQueue::IsFull()const{returnfalse;}templateboolL6、inkQueue::IsEmpty()const{returnfront==rear;}templateTLinkQueue::GetFirstItem(){if(IsEmpty())throwEmptyQueue();Node*p;p=front->next;returnp->date;}templateTLinkQueue::GetlastItem(){if(IsEmpty())throwEmptyQueue();returnrear->date;}template7、ypenameT>voidLinkQueue::MakeEmpty(){Node*p;while(front->next!=NULL){p=front->next;front->next=p->next;deletep;}}templateostream&operator<<(ostream&s,constLinkQueue&lq){Node*p;p=lq.front->next;while(p!=NULL){s<date<<"";p=p->next;}returns;}#endif//8、TrainResort.h//火车车厢重排代码核心部分#ifndefTRAINRESORT#defineTRAINRESORT#include#include
3、ameT>friendostream&operator<<(ostream&s,constLinkQueue&lq);public:LinkQueue();~LinkQueue();voidMakeEmpty();voidEnqueue(Tx);TDequeue();boolIsEmpty()const;boolIsFull()const;TGetFirstItem();TGetlastItem();private:Node*front;Node*rear;};templateLinkQueue
4、::LinkQueue(){front=newNode;front->next=NULL;rear=front;}templateLinkQueue::~LinkQueue(){MakeEmpty();deletefront;}templatevoidLinkQueue::Enqueue(Tx){Node*newnode;newnode=newNode;newnode->date=x;newnode->next=NULL;rear->next=newnode;rear
5、=newnode;}templateTLinkQueue::Dequeue(){if(IsEmpty())throwEmptyQueue();Node*p;p=front->next;Tx=p->date;front->next=p->next;if(p->next==NULL)rear=front;deletep;returnx;}templateboolLinkQueue::IsFull()const{returnfalse;}templateboolL
6、inkQueue::IsEmpty()const{returnfront==rear;}templateTLinkQueue::GetFirstItem(){if(IsEmpty())throwEmptyQueue();Node*p;p=front->next;returnp->date;}templateTLinkQueue::GetlastItem(){if(IsEmpty())throwEmptyQueue();returnrear->date;}template7、ypenameT>voidLinkQueue::MakeEmpty(){Node*p;while(front->next!=NULL){p=front->next;front->next=p->next;deletep;}}templateostream&operator<<(ostream&s,constLinkQueue&lq){Node*p;p=lq.front->next;while(p!=NULL){s<date<<"";p=p->next;}returns;}#endif//8、TrainResort.h//火车车厢重排代码核心部分#ifndefTRAINRESORT#defineTRAINRESORT#include#include
7、ypenameT>voidLinkQueue::MakeEmpty(){Node*p;while(front->next!=NULL){p=front->next;front->next=p->next;deletep;}}templateostream&operator<<(ostream&s,constLinkQueue&lq){Node*p;p=lq.front->next;while(p!=NULL){s<date<<"";p=p->next;}returns;}#endif//
8、TrainResort.h//火车车厢重排代码核心部分#ifndefTRAINRESORT#defineTRAINRESORT#include#include
此文档下载收益归作者所有