stack.pop()方法_C.示例中的Stack.Pop()方法

stack.pop()方法

C#Stack.Pop()方法 (C# Stack.Pop() method)

Stack.Pop() method is used to remove an object from the top of the stack. The method removes and returns the object from the top.

Stack.Pop()方法用于从堆栈顶部删除对象。 该方法从顶部删除并返回对象。

Syntax:

句法:

    Object Stack.Pop();

Parameters: None

参数:

Return value: Object – it returns the items to be removed from the top.

返回值: Object –返回从顶部删除的项目。

Example:

例:

    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);

    popping stack elements:
    stk.Pop();
    stk.Pop();
    stk.Pop();
    
    Output:
    200 100

C#示例使用Stack.Pop()方法从堆栈中删除对象 (C# example to remove an object from the stack using Stack.Pop() method)

using System;
using System.Text;
using System.Collections;

namespace Test
{
    class Program
    {
        //function to print stack elements
        static void printStack(Stack s)
        {
            foreach (Object obj in s)
            {
                Console.Write(obj + " ");
            }
            Console.WriteLine();
        }

        static void Main(string[] args)
        {
            //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);

            //printing stack elements
            Console.WriteLine("Stack elements before popping are...");
            printStack(stk);

            //popping stack elements
            object item = 0;
            item = stk.Pop();
            Console.WriteLine(item + " is popped");
            item = stk.Pop();
            Console.WriteLine(item + " is popped");
            item = stk.Pop();
            Console.WriteLine(item + " is popped");

            //printing stack elements
            Console.WriteLine("Stack elements after popping are...");
            printStack(stk);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

输出量

Stack elements before popping are...
500 400 300 200 100
500 is popped
400 is popped
300 is popped
Stack elements after popping are...
200 100

Reference: Stack.Pop Method

参考: Stack.Pop方法

翻译自: https://www.includehelp.com/dot-net/stack-pop-method-with-example-in-c-sharp.aspx

stack.pop()方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值