【链性队列】模拟机排序

  1 //模拟机排序
  2 //运用链性队列(先进先出)
  3 //
  4 
  5 #include<iostream>
  6 
  7 using namespace std;
  8 
  9 typedef struct _NODE_
 10 {
 11     int a;        //没有再用一个DATA来封数据域,这里只有数据 int a
 12     _NODE_* pNext;
 13 
 14 }Node,*pNode;
 15 
 16 
 17 
 18 class CQueue  //封装成queue类
 19 {
 20 private:            //私有成员pTop 和 pBase 
 21     pNode pTop;
 22     pNode pBase;
 23     int iNodeCount;
 24 
 25 
 26 public:
 27     CQueue()            //构造函数 和 析构函数
 28     {
 29         pTop = pBase = NULL;
 30         iNodeCount = 0;
 31     }
 32     ~CQueue()
 33     {
 34 
 35     }
 36 // 初始化, 进队, 出队, 判断为空, 销毁
 37     void InitQueue();        
 38     bool InQueue(int a);    
 39     bool OutQueue(int &a);
 40     bool IsEmpty();
 41 
 42     void DestroyQueue();
 43 
 44 };
 45 
 46 
 47 
 48 
 49 void CQueue::InitQueue()
 50 {
 51     if(pTop != NULL || iNodeCount != 0)
 52     {
 53         DestroyQueue();
 54     }
 55     pTop = pBase = NULL;
 56     iNodeCount = 0;
 57 }
 58 
 59 
 60 bool CQueue::InQueue(int a)
 61 {
 62     //新建立一个结点,并将a的值赋给结点结构体中的变量a;
 63     pNode pNodeTemp = new Node;
 64 
 65     if(pNodeTemp != NULL)
 66     {
 67         pNodeTemp->a = a;
 68         pNodeTemp->pNext = NULL;
 69     
 70         //如果队列中没有结点
 71         if(pTop == NULL)
 72         {
 73             pTop = pBase = pNodeTemp;
 74         }
 75         //否则,用一般的尾插法.
 76         else
 77         {
 78             pBase->pNext = pNodeTemp;
 79             pBase = pNodeTemp;
 80         }
 81     
 82         //iNodeCount ++ 计数
 83     
 84         iNodeCount++;
 85     
 86         return true;
 87     }
 88     //如果申请结点未成功,返回false
 89     return true;
 90 
 91 }
 92 bool CQueue::OutQueue(int &a)
 93 {
 94     if(pTop == NULL)
 95     {
 96         return false;
 97     }
 98     //先进先出的特性就是尾插然后头出
 99     else
100     {
101         pNode pNodeTemp = pTop;
102 
103         pTop = pTop->pNext;
104         
105         a = pNodeTemp->a;
106 
107         delete pNodeTemp;
108 
109         iNodeCount--;
110 
111         if(iNodeCount == 0)
112         {
113             pBase = NULL;
114         }
115         return true;
116     }
117 }
118 
119 
120 bool CQueue::IsEmpty()
121 {
122     if(iNodeCount == 0)
123     {
124         return true;
125     }
126 
127     return false;
128 }
129 
130 void CQueue::DestroyQueue()
131 {
132     pNode pNodeTemp = pTop;
133 
134     while(pTop != NULL)
135     {
136         pNodeTemp = pTop->pNext;
137 
138         delete pTop;
139 
140         pTop = pNodeTemp;
141 
142         iNodeCount--;
143     }
144 
145     if(iNodeCount == 0)
146     {
147         pBase = NULL;
148     }
149 }
150 
151 
152 int main()
153 {
154     CQueue Object[10];
155 
156     int i = 0;
157     int j = 0;
158     int b = 0;
159 
160 
161     int a[11]={113,241,423,235,242,758,666,575,484,898,603};
162     //三位数的模拟机测试
163     for(i=0;i<11;i++)
164     {
165         cout<<a[i]<<" ";
166     }
167 
168     for(i=0;i<11;i++)
169     {
170         b = a[i]%10;
171 
172         Object[b].InQueue(a[i]);
173     }
174     for(i=0,j=0;i<10;i++)
175     {
176         while(!Object[i].IsEmpty())
177         {
178             Object[i].OutQueue(a[j]);
179             j++;
180         }
181     }
182 
183     cout<<endl;
184 
185     for(i=0;i<11;i++)
186     {
187         cout<<a[i]<<" ";
188     }
189     for(i=0;i<10;i++)
190     {
191         Object[i].InitQueue();
192     }
193 
194     for(i=0;i<11;i++)
195     {
196         b = a[i]/10%10;
197 
198         Object[b].InQueue(a[i]);
199     }
200     for(i=0,j=0;i<10;i++)
201     {
202         while(!Object[i].IsEmpty())
203         {
204             Object[i].OutQueue(a[j]);
205             j++;
206         }
207     }
208 
209     cout<<endl;
210 
211     for(i=0;i<11;i++)
212     {
213         cout<<a[i]<<" ";
214     }
215 
216     for(i=0;i<10;i++)
217     {
218         Object[i].InitQueue();
219     }
220 
221 
222 
223 
224     for(i=0;i<11;i++)
225     {
226         b = a[i]/10/10;
227 
228         Object[b].InQueue(a[i]);
229     }
230     for(i=0,j=0;i<10;i++)
231     {
232         while(!Object[i].IsEmpty())
233         {
234             Object[i].OutQueue(a[j]);
235             j++;
236         }
237     }
238 
239     cout<<endl;
240 
241     for(i=0;i<11;i++)
242     {
243         cout<<a[i]<<" ";
244     }
245 
246     cout<<endl;
247     
248     
249     return 0;
250 }

 

posted @ 2013-10-22 20:48  Geekers  阅读(168)  评论(0编辑  收藏  举报