数据结构-火车车厢重排

类定义文件Platform.h

 1 Platform.h
 2 #ifndef PLATFORM_H
 3 #define PLATFORM_H
 4 const int QueueSize=20;
 5 class Platform
 6 {
 7  friend void Sort(Platform H[], int k);
 8    //Platform H[],为一个数组对象,每一个数组对象有一个队列
 9     friend void Init(Platform H[], int k);
10 public:
11  Platform();
12  ~Platform(){}
13  void EnQueue(int x);
14  int DeQueue();
15  int GetQueue();
16  bool isEmpty();
17  
18 private:
19  int data[QueueSize];
20  int front,rear;
21  
22 };
23 #endif
View Code

实现文件Platform.cpp

 

  1 #include<iostream>
  2 using namespace std;
  3 #include "Platform.h"
  4 Platform::Platform()
  5 {
  6 }
  7 void Init(Platform H[], int k)
  8 {
  9     // data[QueueSize]={0};
 10     /*
 11 此处不能这样写,这样写会提示错误 "missing ;before } ",
 12 因为这条语句可用在数组的初始化中而不能用在函数体中
 13 对数组进行赋值,编译器会将data[QueueSize]={0}中的{}
 14 的一对花括号当作一个函数体, 而0作为它的语句
 15 */
 16     while(k>-1)
 17     {
 18         int i;
 19         for(i=0; i<QueueSize; i++)
 20         {
 21             H[k].data[i]=0;
 22             H[k].front=0;
 23             H[k].rear=0;
 24         }
 25         k--;
 26     }
 27     /*
 28 对数组对象进行初始化,如front和rear如果在构造函数中
 29 写front=rear=0;这样的话数组对象的最后一个对象的成员变量
 30 rear得不到初始化,H[k].rear的值将会是随机值.
 31 */
 32 }
 33 /*
 34 队列的入队与出队使用的是循环队列
 35 */
 36 void Platform::EnQueue(int x)
 37 {
 38     if((rear+1)%QueueSize==front)throw "上溢";
 39     rear=(rear+1)%QueueSize;
 40     data[rear]=x;
 41 }
 42 int  Platform::DeQueue()
 43 {
 44     if(rear==front)throw "下溢";
 45     front=(front+1)%QueueSize;
 46     return data[front];
 47 }
 48 int Platform::GetQueue()
 49 {
 50     if(rear==front)throw "下溢";
 51     int i=(front+1)%QueueSize;
 52     return data[i];
 53 }
 54 bool Platform::isEmpty()
 55 {
 56     /* if(rear==front)
 57   return 1;
 58  return 0;
 59  */
 60     return (rear==front)?1:0;
 61 }
 62 void Sort(Platform H[], int k)
 63 {
 64     int nowOut=1;
 65     int i,j;
 66     bool flag=true;        //用于标志是否有元素出轨
 67     bool fg=true;
 68     int count=H[0].rear;   //得到队列的大小
 69     while(count)
 70     {
 71         int qNum = 0;          //用于得到入轨队列的队首值
 72         if(H[0].front!=H[0].rear)   //用于判断队列是否为空,这一句很重要,如果没有会产生异常
 73             qNum=H[0].GetQueue();  //得到入轨队列的队首值
 74         if(qNum==nowOut)
 75         {
 76             cout<<H[0].DeQueue();
 77             nowOut++;
 78             count--;
 79             flag=false;                //有元素出轨
 80         }  //end  of if(qNum==nowOUt)
 81         else
 82         {
 83             for( j=1; j<=k; j++)
 84             {
 85 
 86                 if(H[j].front!=H[j].rear)      //用于判断缓冲队列是否为空,如果不为空就用缓冲队列的值与nowOut进行比较
 87                 {
 88                     int nNum=H[j].GetQueue();      //得到缓冲队列的队首值
 89                     while(nNum==nowOut)
 90                     {
 91                         flag=false;
 92                         cout<<H[j].DeQueue();
 93                         nowOut++;
 94                         count--;
 95                         if(H[j].front!=H[j].rear)     //取缓冲队列的下一个队首值,但这个值并不出队,这句if很重要,如果没有会产生异常,而且也得不到预期的结果
 96                             nNum=H[j].GetQueue();
 97                     }//end of while(nNum==nowOut)
 98 
 99                 } //end of if
100             }// end of for
101         } //end of else
102         if(flag)                      //如果入轨的队首元素不等于nowOut而且缓冲轨中也没有元素等于nowOut,遇将入轨的队首元素放入缓冲队列
103         {
104             for(i=1; i<=k; i++)
105             {
106                 if(!H[i].isEmpty())   //判断队列是否为空,如果不为空则判断入轨的元素是否大于缓冲轨的队尾元素,如果大于则放入缓冲队列
107                 {
108                     if(qNum>H[i].data[H[i].rear])
109                         break;
110                 }
111                 else                   // 缓冲队列为空,只需将入轨中的元素放入缓冲队列
112                 {
113                     H[i].EnQueue(qNum);
114                     H[0].DeQueue();
115                     fg=false;
116                     break;
117                 }
118             }
119             if(i<=k&&fg)    //i<=k说明还有缓冲队列, fg说明元素没有放入空的缓冲队列
120             {
121                 H[i].EnQueue(qNum);
122                 H[0].DeQueue();
123             }
124             if(i>k)
125             {
126                 cout<<endl;
127                 cout<<"车厢不能重排"<<endl;
128                 break;
129             }
130 
131         } //end of if
132         flag=true;
133         fg=true;
134     } // end of while
135 
136 }
View Code

 

测试main函数:

 1 #include<iostream>
 2 using namespace std;
 3 #include "Platform.h"
 4 int main()
 5 {
 6     Platform platform[3];
 7     Init(platform, 2);      //对数组对象进行初始化
 8     cout<<"将581742963入队"<<endl;
 9     cout<<endl;
10     try{
11         platform[0].EnQueue(3);
12         platform[0].EnQueue(6);
13         platform[0].EnQueue(9);
14         platform[0].EnQueue(2);
15         platform[0].EnQueue(4);
16         platform[0].EnQueue(7);
17         platform[0].EnQueue(1);
18         platform[0].EnQueue(8);
19         platform[0].EnQueue(5);
20     }catch(char *s){cout<<s<<endl;}
21 
22     cout<<"经过排序后的队列是:"<<endl;
23     Sort(platform, 2);
24 
25     cout<<endl;
26     return 0;
27 }
View Code

 

 

转载于:https://www.cnblogs.com/charless/p/3409016.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值