C#, Lesson 9 of "Xiaobai Learning Programs": Stack (Stack), the data type of first in last out

1 text format


/// <summary> /// Lesson 9 of
"Xiaobai Learning Programs":
Stack
, the person who goes in first can only come out last;
/// There are not many stack application scenarios, but the stack works best in some key places.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button9_Click(object sender, EventArgs e)
{     // #1 Define a narrow elevator (stack)     Stack<string> st = new Stack<string>();

    // #2 Elevator up! In what order do they come out?
    st.Push("tony");
    st.Push("jim");
    st.Push("john");
    st.Push("tom");
    st.Push("philip");

    // #3 Printed order!
    StringBuilder sb = new StringBuilder();
    // Serial number
    int idx = 1;
    while (st.Count > 0)
    {         // Display who is the idx person st.Pop()? How many st.Count people are there in the elevator?         // st.Pop Popup?         sb.AppendLine(idx + ": " + st.Pop() + "leave " + st.Count + "peoples.<br>");         idx++;     }




    webBrowser1.DocumentText = sb.ToString();
}
 

2 code format


/// <summary>
/// 《小白学程序》第九课:堆栈(Stack)
/// 堆栈与队列是相似的数据形态;特点是:先进后出;
/// 比如:狭窄的电梯,先进去的人只能最后出来;
/// 堆栈应用场景不是很多,但某些关键的地方用堆栈效果最好。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button9_Click(object sender, EventArgs e)
{
    // #1 定义一个狭窄的电梯(堆栈)
    Stack<string> st = new Stack<string>();

    // #2 电梯上人啦!按什么顺序出来呢?
    st.Push("tony");
    st.Push("jim");
    st.Push("john");
    st.Push("tom");
    st.Push("philip");

    // #3 打印出来的顺序!
    StringBuilder sb = new StringBuilder();
    // 序号
    int idx = 1;
    while (st.Count > 0)
    {
        // 显示 第 idx 个人是谁 st.Pop() ?电梯里还有 st.Count 几个人?
        // st.Pop 弹出?
        sb.AppendLine(idx + ": " + st.Pop() + " leave " + st.Count + " peoples.<br>");
        idx++;
    }

    webBrowser1.DocumentText = sb.ToString();
}

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/132523228