Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में Stack के शीर्ष पर ऑब्जेक्ट इन्सर्ट करें

Stack<T>.Push() मेथड का उपयोग करके स्टैक के शीर्ष पर ऑब्जेक्ट इन्सर्ट करें (LIFO - लास्ट इन फर्स्ट आउट)।

उदाहरण 1: इंटीजर Stack

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Stack<int> stack = new Stack<int>();
        stack.Push(100);
        stack.Push(150);
        stack.Push(175);
        stack.Push(200);
        stack.Push(225);
        stack.Push(250);
        
        Console.WriteLine("Elements in the Stack:");
        foreach (var val in stack) {
            Console.WriteLine(val);
        }
        
        Console.WriteLine("Count of elements in the Stack = " + stack.Count);
        Console.WriteLine("Does Stack has the element 400? = " + stack.Contains(400));
        
        // अधिक एलिमेंट्स जोड़ें
        stack.Push(300);
        stack.Push(400);
        stack.Push(450);
        stack.Push(500);
        
        Console.WriteLine("Elements in the Stack... (UPDATED)");
        foreach (var val in stack) {
            Console.WriteLine(val);
        }
        Console.WriteLine("Count of elements in the Stack (UPDATED) = " + stack.Count);
    }
}

आउटपुट

Elements in the Stack:
250
225
200
175
150
100
Count of elements in the Stack = 6
Does Stack has the element 400? = False
Elements in the Stack... (UPDATED)
500
450
400
300
250
225
200
175
150
100
Count of elements in the Stack (UPDATED) = 10

उदाहरण 2: स्ट्रिंग Stack

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Stack<string> stack = new Stack<string>();
        stack.Push("A");
        stack.Push("B");
        stack.Push("C");
        stack.Push("D");
        stack.Push("E");
        stack.Push("F");
        stack.Push("G");
        stack.Push("H");
        
        Console.WriteLine("Count of elements = " + stack.Count);
        Console.WriteLine("Elements in Stack...");
        foreach (string res in stack) {
            Console.WriteLine(res);
        }
        
        stack.Push("M");
        Console.WriteLine("Elements in the Stack... (UPDATED)");
        foreach (var val in stack) {
            Console.WriteLine(val);
        }
        Console.WriteLine("Count of elements in the Stack (UPDATED) = " + stack.Count);
    }
}

आउटपुट

Count of elements = 8
Elements in Stack...
H
G
F
E
D
C
B
A
Elements in the Stack... (UPDATED)
M
H
G
F
E
D
C
B
A
Count of elements in the Stack (UPDATED) = 9

Stack<T> मुख्य मेथड्स

मेथडविवरणकॉम्प्लेक्सिटी
Push(T)शीर्ष पर एलिमेंट जोड़ता हैO(1)
Pop()शीर्ष एलिमेंट हटाता है और लौटाता हैO(1)
Peek()शीर्ष एलिमेंट लौटाता है (बिना हटाए)O(1)
Contains(T)एलिमेंट मौजूद है?O(n)
Clear()सभी एलिमेंट्स हटाता हैO(n)
ToArray()ऐरे में कॉपी करता हैO(n)

नॉन-जेनेरिक Stack (लिगेसी)

using System.Collections;

Stack stack = new Stack();
stack.Push(100);
stack.Push("String");  // मिक्स्ड टाइप्स अनुमत (नहीं अनुशंसित)
object top = stack.Pop();

व्यावहारिक उपयोग: Undo ऑपरेशन

public class TextEditor {
    private Stack<string> _history = new Stack<string>();
    private string _content = "";
    
    public void Type(string text) {
        _history.Push(_content);  // पिछली स्थिति सेव करें
        _content += text;
    }
    
    public void Undo() {
        if (_history.Count > 0) {
            _content = _history.Pop();
        }
    }
    
    public string Content => _content;
}

पेरफॉरमेंस नोट

  • Stack<T> आंतरिक रूप से ऐरे का उपयोग करता है
  • कैपेसिटी आवश्यकतानुसार दोगुनी होती है
  • बड़े स्टैक्स के लिए प्रारंभिक कैपेसिटी दें: new Stack<int>(1000)
  1. - C# में स्टैक क्लास में Push और Pop ऑपरेशन

    Stack क्लास ऑब्जेक्ट्स के लास्ट-इन, फर्स्ट-आउट कलेक्शन को दर्शाता है। इसका उपयोग तब किया जाता है जब आपको आइटम्स की लास्ट-इन, फर्स्ट-आउट एक्सेस की आवश्यकता होती है।Stack क्लास की प्रॉपर्टी:Count - स्टैक में एलिमेंट्स की संख्या प्राप्त करता है।Push ऑपरेशनPush ऑपरेशन का उपयोग करके स्टैक में एलिमेंट्स ज

  1. - C# में Stack क्लास

    Stack क्लास ऑब्जेक्ट्स के लास्ट-इन, फर्स्ट-आउट कलेक्शन को दर्शाता है। इसका उपयोग तब किया जाता है जब आपको आइटम्स का लास्ट-इन, फर्स्ट-आउट एक्सेस चाहिए। Stack क्लास की प्रॉपर्टी निम्नलिखित है - Count - स्टैक में एलिमेंट्स की संख्या प्राप्त करता है। Stack क्लास के मेथड्स निम्नलिखित हैं - Sr.No. मेथ

  1. - C# में Stack क्लास का Count गुण क्या है?

    Stack क्लास में कितने एलिमेंट्स जोड़े गए हैं, यह जानने के लिए आपको Count गुण का उपयोग करना होगा।आइए पहले स्टैक में एलिमेंट्स जोड़ें:Stack st = new Stack(); st.Push(H); st.Push(I); st.Push(J); st.Push(K); st.Push(L); st.Push(M); st.Push(N); st.Push(O);अब स्टैक में एलिमेंट्स की संख्या गिनें:Console.Wr