C# में Stack.Pop() मेथड स्टैक के शीर्ष (top) पर स्थित ऑब्जेक्ट को हटाता है और उसे लौटाता है। यह LIFO (Last-In-First-Out) सिद्धांत का पालन करता है।
सिंटैक्स
public virtual object Pop ();
उदाहरण 1: स्ट्रिंग स्टैक से एलिमेंट पॉप करना
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 (after Pop) = " + 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 (after Pop) = 9 Stack elements...updated Cameras Ultrabook Notebook Laptop XPS Monitors Projectors Alienware Inspiron
उदाहरण 2: Clone और 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 Speakers in 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 in 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 stack2) {
Console.WriteLine(val);
}
}
}
आउटपुट
Stack elements... Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements = 7 Element Speakers in stack? = False Stack elements...updated Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements (updated) = 10 Element Alienware in 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
मुख्य बिंदु
Pop()शीर्ष एलिमेंट को हटाता है और लौटाता है।- यदि स्टैक खाली है तो
InvalidOperationExceptionफेंकता है। Peek()केवल देखता है, हटाता नहीं।Clear()सभी एलिमेंट्स हटा देता है बिना लौटाए।