⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 4.1.txt

📁 《Microsoft Visual C# .NET 2003开发技巧大全》源代码
💻 TXT
字号:
Listing 4.1 Pushing and Popping Objects from a Stack
using System;
using System.Collections;
namespace _6_StacksQueues
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string input;
Console.Write( “Enter a list of names separated by a space: “ );
input = Console.ReadLine();
// place names into a string array
string[] names = input.Split( ‘ ‘ );
// print out string array
PrintCollection( “Input”, names );
TestStack( names );
}
static void PrintCollection( string name, ICollection coll )
{
Console.Write( name + “: “ );
foreach( object elem in coll )
{
Console.Write( elem.ToString() + ‘ ‘ );
}
Console.WriteLine(“\n”);
}
}
static void TestStack( string[] names )
{
Stack nameStack = new Stack();
// enumerate through the array
foreach( string name in names )
{
// push name onto stack and display new stack contents
nameStack.Push( name );
Console.WriteLine( “Pushed value: {0}”, name );
PrintCollection( “New Stack”, nameStack );
}
// let user choose how many items to remove
Console.Write( “Pop how many items? “ );
int pops = Int32.Parse( Console.ReadLine() );
for( int i = 1; i <= pops; i++ )
{
// Pop value. Note that Pop also
// returns the value that was removed
Console.WriteLine( “Popped value: “ + nameStack.Pop());
// display the new stack contents
PrintCollection( “Stack after “ + i.ToString() + “ pops”,
nameStack );
}
Console.WriteLine();
}
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -