創作內容

18 GP

C# Stack Queue

作者:貓貓風 ฅ●ω●ฅ│2017-12-02 14:14:53│巴幣:44│人氣:893
.













Stack 跟 Queue 也都是最基本的資料結構之一

基本原則就是  Stack  為先進後出,類似電梯的進出模式

Queue 為先進先出,像是以前的收費站,也可以用水管來形容

這兩種資料結構都可以用來儲存所需要的資訊

但會依照用途的不同而採用不同的資料結構

向是經典的老鼠走迷宮就是用到 Stack來實作

主要為模擬遇到牆壁時會倒退的情況,因此用會用到先進後出的特性來返回原路徑

Queue則廣泛用於網路封包的傳遞,由於Server端可能要同時處理多個Client端

的封包,因此就會用Queue來儲存,將先進來的資料先做處理

如此一來也不會因為傳輸延遲而導致封包的接收順序亂掉

也可以更容易的還原完整的封包


以下為這次的範例程式,主要為動態模擬 Stack 跟 Queue 資料在進入與拋出的狀態

可以調整資料進出的速度,可以更明顯的觀察到兩者的差別



  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Collections;  
  10. using System.Threading;  
  11.   
  12. namespace Stack_Queue  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         // Creates and initializes a new Stack.  
  17.         Stack stack = new Stack();  
  18.         // Creates and initializes a new Queue.  
  19.         Queue queue = new Queue();  
  20.         Random rnd = new Random(Guid.NewGuid().GetHashCode());  
  21.         private delegate void UpdateUI(string str, Control ctl);  
  22.         private delegate void UpdateUI2(Queue Q, Control ctl);  
  23.         Thread _queue_stack_in, _queue_stack_out;  
  24.   
  25.   
  26.         public Form1()  
  27.         {  
  28.             InitializeComponent();  
  29.         }  
  30.   
  31.         private void Form1_Load(object sender, EventArgs e)  
  32.         {  
  33.             _queue_stack_in = new Thread(Queue_Stack_In);  
  34.             _queue_stack_out = new Thread(Queue_Stack_Out);  
  35.   
  36.             _queue_stack_in.Start();  
  37.             _queue_stack_out.Start();  
  38.   
  39.         }  
  40.   
  41.             private void Queue_Stack_In()  
  42.             {  
  43.                while(true)  
  44.                {  
  45.                  int ramd_int = rnd.Next(10);  
  46.                  stack.Push(ramd_int);  
  47.                  updateText(ramd_int.ToString(), textBox1);  
  48.                  updateText2(queue, textBox2);  
  49.   
  50.                  queue.Enqueue(ramd_int);  
  51.                  updateText(ramd_int.ToString(),textBox6);  
  52.                  updateText2(queue,textBox4);  
  53.                  Thread.Sleep(Int32.Parse(numericUpDown1.Value.ToString()));  
  54.                }  
  55.             }  
  56.   
  57.             private void Queue_Stack_Out()  
  58.             {  
  59.                 while (true)  
  60.                 {  
  61.                     if (queue.Count > 0)  
  62.                     {  
  63.                      updateText(queue.Dequeue().ToString(), textBox5);  
  64.                     }  
  65.                     else  
  66.                     {  
  67.                         updateText("Empty", textBox5);  
  68.                     }  
  69.   
  70.                     if(stack.Count > 0)  
  71.                     {  
  72.                         updateText(stack.Pop().ToString(), textBox3);  
  73.                     }  
  74.                     else  
  75.                     {  
  76.                         updateText("Empty", textBox3);  
  77.                     }  
  78.                     Thread.Sleep(Int32.Parse(numericUpDown2.Value.ToString()));  
  79.                 }  
  80.             }  
  81.   
  82.             private void updateText(string str, Control ctl)  
  83.             {  
  84.                 if (this.InvokeRequired)  
  85.                 {  
  86.                     UpdateUI uu = new UpdateUI(updateText);  
  87.                     this.Invoke(uu, str, ctl);  
  88.                 }  
  89.                 else  
  90.                 {  
  91.                     ctl.Text = str;  
  92.                 }  
  93.             }  
  94.   
  95.             private void updateText2(Queue Q, Control ctl)  
  96.             {  
  97.                 if (this.InvokeRequired)  
  98.                 {  
  99.                     UpdateUI2 uu = new UpdateUI2(updateText2);  
  100.                     this.Invoke(uu, Q, ctl);  
  101.                 }  
  102.                 else  
  103.                 {  
  104.                      ctl.Text = "";  
  105.   
  106.                      foreach (Object obj in queue)  
  107.                      {  
  108.                          ctl.Text += obj+" | ";  
  109.                      }  
  110.                 }  
  111.             }  
  112.   
  113.             private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
  114.             {  
  115.                 _queue_stack_in.Abort();  
  116.                 _queue_stack_out.Abort();  
  117.             }  
  118.         }  
  119.     }  


  執行畫面
   


運行畫面:


引用網址:https://home.gamer.com.tw/TrackBack.php?sn=3807484
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:C#

留言共 1 篇留言

珀伽索斯(Ama)
輸入800,輸出1000,
還真想知道其中200哪裡去了?[e11]

12-04 23:55

貓貓風 ฅ●ω●ฅ
那只是IN OUT速度 12-04 23:58
我要留言提醒:您尚未登入,請先登入再留言

18喜歡★s1234567 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:三十挑戰練強者 通關紀錄... 後一篇:C# Indexer...

追蹤私訊切換新版閱覽

作品資料夾

leon770530巴友
我的小屋首頁共有4篇健身相關證照心得文,對健身有興趣的朋友可以來看看喔!看更多我要大聲說昨天23:43


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】