1 Stack 2 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 8 namespace DataStructure 9 { 10 11 interface IStack<T> 12 { 13 void Push(T item); //入栈操作 14 T Pop(); //出栈操作 15 T GetTop(); //取栈顶元素 16 int GetLength(); //求栈的长度 17 bool IsEmpty(); //判断栈是否为空 18 void Clear(); //清空操作 19 } 20 /// <summary> 21 /// 顺序栈 22 /// </summary> 23 /// <typeparam name="T"></typeparam> 24 class SequenceStack<T> : IStack<T> 25 { 26 private int maxsize; //顺序栈的容量 27 private T[] data; //数组,用于存储顺序栈中的数据元素 28 private int top; //指示顺序栈的栈顶 29 30 //索引器 31 public T this[int index] 32 { 33 get { return data[index]; } 34 set { data[index] = value; } 35 } 36 37 //容量属性 38 public int Maxsize 39 { 40 get { return maxsize; } 41 set { maxsize = value; } 42 } 43 44 //栈顶属性 45 public int Top 46 { 47 get 48 { 49 return top; 50 } 51 } 52 53 public SequenceStack(int size) 54 { 55 data = new T[size]; 56 maxsize = size; 57 top = -1; 58 } 59 60 //求栈的长度 61 public int GetLength() 62 { 63 return top + 1; 64 } 65 //清空顺序栈 66 public void Clear() 67 { 68 top = -1; 69 } 70 //判断顺序栈是否为空 71 public bool IsEmpty() 72 { 73 if (top == -1) 74 { 75 return true; 76 } 77 else 78 return false; 79 } 80 //判断栈是否为满 81 public bool IsFull() 82 { 83 if (top == maxsize - 1) 84 { 85 return true; 86 } 87 else 88 return false; 89 } 90 91 //入栈 92 public void Push(T elem) 93 { 94 if (IsFull()) 95 { 96 Console.WriteLine("Stack is Full !"); 97 return; 98 } 99 data[++top] = elem; 100 101 } 102 103 //出栈 104 public T Pop() 105 { 106 T tem = default(T); 107 if (IsEmpty()) 108 { 109 Console.WriteLine("Stack is Empty !"); 110 return default(T); 111 } 112 tem = data[top]; 113 --top; 114 return tem; 115 } 116 117 //获取栈顶元素 118 public T GetTop() 119 { 120 if (IsEmpty()) 121 { 122 Console.WriteLine("Stack is Empty !"); 123 return default(T); 124 } 125 return data[top]; 126 } 127 128 } 129 130 /// <summary> 131 /// 用顺序栈解决火车车厢重排问题 132 /// </summary> 133 class TrainArrangwBySeqStack 134 { 135 //车厢重排算法,K个缓冲铁轨,车厢初始排序存放在P中 136 public bool RailRoad(int[] p, int n, int k) 137 { 138 //创建与缓冲铁轨对应的堆栈 139 SequenceStack<int>[] H; 140 H = new SequenceStack<int>[k + 1]; 141 for (int i = 0; i <= k; i++) 142 H[i] = new SequenceStack<int>(p.Length); 143 int NowOut = 1; //下次要输出的车厢 144 int minH = n + 1; //缓冲铁轨中编号最小的车厢 145 int minS = 0; //minH号车厢对应的缓冲铁轨 146 147 //车厢重排 148 for (int i = 0; i < n; i++) 149 { 150 if (p[i] == NowOut) 151 { 152 Console.WriteLine("Move car {0} from input to output", p[i]); 153 NowOut++; 154 //从缓冲铁轨中输出 155 while (minH == NowOut) 156 { 157 Output(ref minH, ref minS, ref H, k, n); 158 NowOut++; 159 } 160 } 161 else 162 { 163 if (!Hold(p[i], ref minH, ref minS, ref H, k, n)) 164 { 165 return false; 166 } 167 } 168 } 169 return true; 170 } 171 172 //在一个缓冲区中放入车厢C 173 public bool Hold(int c, ref int minH, ref int minS, ref SequenceStack<int>[] H, int k, int n) 174 { 175 //如果没有可用的缓冲铁轨,则返回false 176 //否则返回true 177 //为车厢c寻找最优的铁轨 178 //初始化 179 180 int BestTrack = 0; //目前最优的铁轨 181 int BestTop = n + 1; //最优铁轨上的头辆车厢 182 int x; //车厢索引 183 184 //扫描缓冲铁轨 185 for (int i = 1; i <= k; i++)//!!!i=1 186 { 187 if (!H[i].IsEmpty()) 188 { 189 //铁轨i不为空 190 x = H[i][H[i].Top]; 191 if (c < x && x < BestTop) 192 { 193 //铁轨i顶部的车厢编号最小 194 BestTop = x; 195 BestTrack = i; 196 } 197 } 198 else//铁轨i为空 199 { 200 if (BestTrack == 0) 201 { 202 BestTrack = i; 203 } 204 break; 205 } 206 } 207 if (BestTrack == 0) 208 return false;//没有可用铁轨 209 210 //把车厢c送入缓冲铁轨 211 H[BestTrack].Push(c); 212 Console.WriteLine("Move car{0} from input to holding track {1}", c, BestTrack); 213 //必要时修改minH minS 214 if (c < minH) 215 { 216 minH = c; 217 minS = BestTrack; 218 } 219 return true; 220 } 221 222 //把车厢从缓冲区铁轨送至出轨处,同时修改minH minS 223 public void Output(ref int minH, ref int minS, ref SequenceStack<int>[] H, int k, int n) 224 { 225 int c;//车厢索引 226 //从堆栈minS中删除编写、好最小的车厢minH 227 c = H[minS].Pop(); 228 Console.WriteLine("Move car{0} from holding track {1} to output", minH, minS); 229 //通过检查所有的栈顶,搜索新的minH minS 230 minH = n + 2; 231 for (int i = 1; i <= k; i++) 232 { 233 if (!H[i].IsEmpty() && (c = H[i][H[i].Top]) < minH) 234 { 235 minH = c; 236 minS = i; 237 } 238 } 239 } 240 } 241 242 /// <summary> 243 /// 链栈结点 244 /// </summary> 245 class StackNode<T> 246 { 247 private T data; //数据域 248 private StackNode<T> next; //引用域 249 250 public StackNode() 251 { 252 data = default(T); 253 next = null; 254 } 255 256 public StackNode(T val) 257 { 258 data = val; 259 next = null; 260 } 261 262 public StackNode(T val, StackNode<T> p) 263 { 264 data = val; 265 next = p; 266 } 267 268 //数据域属性 269 public T Data 270 { 271 get { return data; } 272 set { data = value; } 273 } 274 275 //引用域属性 276 public StackNode<T> Next 277 { 278 get { return next; } 279 set { next = value; } 280 } 281 282 283 284 } 285 286 /// <summary> 287 /// 链栈 288 /// </summary> 289 /// <typeparam name="T"></typeparam> 290 class LinkStack<T> : IStack<T> 291 { 292 private StackNode<T> top; //栈顶指示器 293 private int size; //栈中元素的个数 294 295 //栈顶指示器属性 296 public StackNode<T> Top 297 { 298 get { return top; } 299 set { top = value; } 300 } 301 302 //元素个数属性 303 public int Size 304 { 305 get { return size; } 306 set { size = value; } 307 } 308 309 public LinkStack() 310 { 311 top = null; 312 size = 0; 313 } 314 315 316 //判断链栈是否为空 317 public bool IsEmpty() 318 { 319 if ((top == null) && (size == 0)) 320 return true; 321 else 322 return false; 323 } 324 325 326 public int GetLength() 327 { 328 return size; 329 } 330 331 public void Clear() 332 { 333 top = null; 334 size = 0; 335 } 336 337 //入栈操作 338 //在单链表的起始处插入一个结点 339 public void Push(T item) 340 { 341 StackNode<T> q = new StackNode<T>(item); 342 if (top == null) 343 { 344 top = q; 345 } 346 else 347 { 348 //将新结点的next指向栈顶指示器top所指向的结点 349 q.Next = top; 350 //将栈顶指示器top指向新结点 351 top = q; 352 } 353 ++size; 354 } 355 356 //出栈操作 357 public T Pop() 358 { 359 if (IsEmpty()) 360 { 361 Console.WriteLine("Stack is empty !"); 362 return default(T); 363 } 364 StackNode<T> p = top; 365 top = top.Next; 366 --size; 367 return p.Data; 368 } 369 370 //获取栈顶结点的值 371 public T GetTop() 372 { 373 if (IsEmpty()) 374 { 375 Console.WriteLine("Stack is empty !"); 376 return default(T); 377 } 378 return top.Data; 379 } 380 } 381 382 /// <summary> 383 /// 用链栈解决火车车厢重排问题 384 /// </summary> 385 class TrainArrangeByLinkStack 386 { 387 //k个缓冲铁轨,车厢初始排序存储在p中 388 public bool RailRoad(int[] p, int n, int k) 389 { 390 //创建与缓冲铁轨对应的堆栈 391 LinkStack<int>[] H; 392 H = new LinkStack<int>[k + 1]; 393 for (int i = 1; i <= k; i++) 394 { 395 H[i] = new LinkStack<int>(); 396 } 397 398 int NowOut = 1; //下一次要输出的车厢 399 int minH = n + 1; //缓冲铁轨中编号最小的车厢 400 int minS = 0; //minH号车厢对应的缓冲铁轨 401 402 //车厢重排 403 for (int i = 0; i < n; i++) 404 { 405 if (p[i] == NowOut) 406 { 407 Console.WriteLine("Move car {0} from input to output", p[i]); 408 NowOut++; 409 410 //从缓冲铁轨中输出 411 while (minH == NowOut) 412 { 413 Output(ref minH, ref minS, ref H, k, n); 414 NowOut++; 415 } 416 } 417 else 418 { 419 //将p[i]送入缓冲铁轨 420 if (!Hold(p[i], ref minH, ref minS, ref H, k, n)) 421 return false; 422 } 423 } 424 return true; 425 } 426 427 //在一个缓冲铁轨中放入车厢c 428 public bool Hold(int c, ref int minH, ref int minS, ref LinkStack<int>[] H, int k, int n) 429 { 430 //如果没有可用缓冲铁轨,则返回false 431 //否则返回true 432 //为车厢c寻找最优的缓冲铁轨 433 //初始化 434 int BestTrack = 0; //目前最优的铁轨 435 int BestTop = n + 1; //最优铁轨上的头辆车厢 436 int x; //车厢索引 437 438 //扫描缓冲铁轨 439 for (int i = 1; i <= k; i++) 440 { 441 if (!H[i].IsEmpty()) 442 { 443 //铁轨不为空 444 x = H[i].Top.Data; 445 if (c < x && x < BestTop) 446 { 447 BestTop = x; 448 BestTrack = i; 449 } 450 } 451 else 452 { 453 if (BestTrack == 0) 454 BestTrack = i; 455 break; 456 } 457 } 458 if (BestTrack == 0) 459 return false;//没有可用铁轨 460 //把车厢c送入缓冲铁轨 461 H[BestTrack].Push(c); 462 Console.WriteLine("Move car {0} from input to holding track {1}", c, BestTrack); 463 464 if (c < minH) 465 { 466 minH = c; 467 minS = BestTrack; 468 } 469 return true; 470 } 471 472 //把车厢从缓冲铁轨送至出轨处,同时修改minH minS 473 public void Output(ref int minH,ref int minS ,ref LinkStack <int>[] H,int k,int n) 474 { 475 int c; //车厢索引 476 c = H[minS].Pop(); 477 Console.WriteLine("Move car {0} form holding track {1} to output", minH, minS); 478 //通过检查所有的栈顶,搜索新的minH和minS 479 minH = n + 2; 480 for (int i = 1; i <= k; i++) 481 { 482 if (!H[i].IsEmpty() && (c = H[i].Top.Data) < minH) 483 { 484 minH = c; 485 minS = i; 486 } 487 } 488 } 489 } 490 491 class Stack 492 { 493 494 static void Main() 495 { 496 int[] p = new int[] { 3, 6, 9, 2, 4, 7, 1, 8, 5 }; 497 int k = 3; 498 499 500 //用顺序栈解决火车车厢重排问题 501 TrainArrangwBySeqStack tas = new TrainArrangwBySeqStack(); 502 bool results; 503 results = tas.RailRoad(p, p.Length, k); 504 do 505 { 506 if (results == false) 507 { 508 Console.WriteLine("need more holding track, please enter additional number:"); 509 k = k + Convert.ToInt32(Console.ReadLine()); 510 results = tas.RailRoad(p, p.Length, k); 511 } 512 } while (results == false); 513 Console.ReadLine(); 514 515 516 //用链栈解决火车车厢重排问题 517 TrainArrangeByLinkStack ta = new TrainArrangeByLinkStack(); 518 bool result; 519 result = ta.RailRoad(p, p.Length, k); 520 do 521 { 522 if (result == false) 523 { 524 Console.WriteLine("need more holding track,please enter additional number:"); 525 k = k + Convert.ToInt32(Console.ReadLine()); 526 result = ta.RailRoad(p, p.Length, k); 527 } 528 } while (result == false); 529 Console.ReadLine(); 530 } 531 } 532 }