Queue और Stack
एक Queue किसी दुकान की line जैसी है जहां पहले अंदर आने वाले व्यक्ति को पहले service मिलती है, जबकि एक Stack plates का ढेर है जहां सबसे ऊपर रखी आखिरी plate पहले उठाई जाती है। हर एक की चीज़ें निकालने की अपनी order है।
In this page:
Syntax
Queue<type> queue = new Queue<type>();
queue.Enqueue(item);
queue.Dequeue(); // first in, first out
Stack<type> stack = new Stack<type>();
stack.Push(item);
stack.Pop(); // last in, first out
Queue and Stack
Queue<T> Enqueue/Dequeue उपयोग करती एक first-in-first-out collection है, जबकि Stack<T> Push/Pop उपयोग करती last-in-first-out है।
Queues task processing order जैसी चीज़ें model करती हैं; stacks undo history या recursive call tracking जैसी चीज़ें model करती हैं। दोनों किसी खाली collection से हटाने की कोशिश करने पर एक exception फेंकती हैं।
Note:
Queue के लिए "दुकान में line" और Stack के लिए "एक-दूसरे पर रखी plates" सोचें।उदाहरण: Queue and Stack
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Queue<string> line = new Queue<string>();
line.Enqueue("first");
line.Enqueue("second");
Console.WriteLine(line.Dequeue());
Stack<string> history = new Stack<string>();
history.Push("page1");
history.Push("page2");
Console.WriteLine(history.Pop());
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- किसी खाली collection पर
PopयाDequeuecall करना, जोInvalidOperationExceptionफेंकता है। - order को मिला देना: एक stack last in, first out है और एक queue first in, first out है।
Peekउपयोग करना और इसके item हटाने की उम्मीद करना।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: