using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TestQueue();
TestStack();
Console.Read();
}
private static void TestQueue()
{
Queue queue = new Queue();
queue.Enqueue(5);
queue.Enqueue(7);
queue.Enqueue(9);
Int32 count = queue.Count;
//output 5, 7, 9
for (Int32 index = 0; index < count; ++index)
{
Console.Write(" " + ((Int32)queue.Dequeue()).ToString());// MessageBox.Show();
}
}
public static void TestStack()
{
Stack stack = new Stack();
stack.Push(5);
stack.Push(7);
stack.Push(9);
Int32 count = stack.Count;
//output 9, 7, 5
for (Int32 index = 0; index < count; ++index)
{
Console.Write(" " + ((Int32)stack.Pop()).ToString());
}
}
}
}
本文演示了在C#中如何使用栈和队列的基本操作,包括元素的添加、删除和遍历。通过具体实例展示了栈的先进后出(LIFO)和队列的先进先出(FIFO)特性。
2430

被折叠的 条评论
为什么被折叠?



