C#中的Stack.Pop()方法
C#中的Stack.Pop()方法用于删除并返回堆栈顶部的对象。
语法
语法如下-
public virtual object Pop ();
示例
现在让我们看一个例子-
using System;
using System.Collections;
public class Demo {
public static void Main() {
Stack stack = new Stack();
stack.Push("Inspiron");
stack.Push("Alienware");
stack.Push("Projectors");
stack.Push("Monitors");
stack.Push("XPS");
stack.Push("Laptop");
stack.Push("Notebook");
Console.WriteLine("Stack elements...");
foreach(string val in stack) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements = "+stack.Count);
Console.WriteLine("Element at the top = "+ stack.Peek());
stack.Push("Ultrabook");
stack.Push("Cameras");
stack.Push("Keyboards");
Console.WriteLine("\nStack elements...updated");
foreach(string val in stack) {
Console.WriteLine(val);
}
Console.WriteLine("Element at the top = "+ stack.Peek());
Console.WriteLine("\nCount of elements (updated) = "+stack.Count);
Console.WriteLine("\nRemoving and returning the element at the top = "+ stack.Pop());
Console.Write("Count of elements (updated) = "+stack.Count);
Console.WriteLine("\nStack elements...updated");
foreach(string val in stack) {
Console.WriteLine(val);
}
}
}输出结果
这将产生以下输出-
Stack elements... Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements = 7 Element at the top = Notebook Stack elements...updated Keyboards Cameras Ultrabook Notebook Laptop XPS Monitors Projectors Alienware Inspiron Element at the top = Keyboards Count of elements (updated) = 10 Removing and returning the element at the top = Keyboards Count of elements (updated) = 9 Stack elements...updated Cameras Ultrabook Notebook Laptop XPS Monitors Projectors Alienware Inspiron
示例
现在让我们来看另一个示例-
using System;
using System.Collections;
public class Demo {
public static void Main() {
Stack stack = new Stack();
stack.Push("Inspiron");
stack.Push("Alienware");
stack.Push("Projectors");
stack.Push("Monitors");
stack.Push("XPS");
stack.Push("Laptop");
stack.Push("Notebook");
Console.WriteLine("Stack elements...");
foreach(string val in stack) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements = "+stack.Count);
Console.WriteLine("Element Speakers is the stack? = "+stack.Contains("Speakers"));
stack.Push("Headphone");
stack.Push("Keyboard");
stack.Push("Earphone");
Console.WriteLine("\nStack elements...updated");
foreach(string val in stack) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements (updated) = "+stack.Count);
Console.WriteLine("Element Alienware is the stack? = "+stack.Contains("Alienware"));
Stack stack2 = (Stack)stack.Clone();
Console.WriteLine("\nStack elements...cloned");
foreach(string val in stack2) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements (stack2) = "+stack2.Count);
Console.WriteLine("Top of the Stack (stack2) = "+stack2.Peek());
Console.WriteLine("\nRemoving and returning the element at the top (stack2) = "+ stack2.Pop());
Console.WriteLine("Count of elements (stack2) = "+stack2.Count);
Console.WriteLine("\nStack elements...(stack2) ");
foreach(string val in stack) {
Console.WriteLine(val);
}
}
}输出结果
这将产生以下输出-
Stack elements... Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements = 7 Element Speakers is the stack? = False Stack elements...updated Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements (updated) = 10 Element Alienware is the stack? = True Stack elements...cloned Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements (stack2) = 10 Top of the Stack (stack2) = Earphone Removing and returning the element at the top (stack2) = Earphone Count of elements (stack2) = 9 Stack elements...(stack2) Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志