← Back to C# Course | Chapter 9: Collections | Lesson 5 of 7

Queue और Stack

एक Queue किसी दुकान की line जैसी है जहां पहले अंदर आने वाले व्यक्ति को पहले service मिलती है, जबकि एक Stack plates का ढेर है जहां सबसे ऊपर रखी आखिरी plate पहले उठाई जाती है। हर एक की चीज़ें निकालने की अपनी order है।

In this page:

  1. Queue and Stack
Syntax
csharp
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

markup
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());
    }
}
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. #}
आम गलतियां
  1. किसी खाली collection पर Pop या Dequeue call करना, जो InvalidOperationException फेंकता है।
  2. order को मिला देना: एक stack last in, first out है और एक queue first in, first out है।
  3. Peek उपयोग करना और इसके item हटाने की उम्मीद करना।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.