C#中Stack.Push()方法及示例
C#方法Stack.Push()
Stack.Push()方法用于在堆栈顶部插入一个对象。
语法:
void Stack.Push(object obj);
参数:它接受要在堆栈顶部插入的对象。
返回值:void–不返回任何内容。
示例
declare and initialize a stack:
Stack stk = new Stack();
insertting elements:
stk.Push(100);
stk.Push(200);
stk.Push(300);
stk.Push(400);
stk.Push(500);
Output:
500 400 300 200 100使用Stack.Push()方法将对象插入堆栈的C#示例
using System;
using System.Text;
using System.Collections;
namespace Test
{
class Program
{
//打印堆栈元素的功能
static void printStack(Stack s)
{
foreach (Object obj in s)
{
Console.Write(obj + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
//声明并初始化堆栈
Stack stk = new Stack();
//插入元素
stk.Push(100);
stk.Push(200);
stk.Push(300);
stk.Push(400);
stk.Push(500);
//打印堆栈元素
Console.WriteLine("Stack elements are...");
printStack(stk);
//按ENTER退出
Console.ReadLine();
}
}
}输出结果
Stack elements are... 500 400 300 200 100
参考:方法Stack.Push(Object)
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短