- This topic explains about, how to use STACK in C# programming.
Stack – Using STACK in C#
- Stack is a kind of abstract data type or collection in which the operation of the collection are the addition of the item to the collection known as PUSH and removal of the item is known as POP.
- PUSH and POP in STACK follows Last-In-First-Out (LIFO) data structures.
Stack Class – Using STACK in C#
- Stack Class represents a simple last-in-first-out (LIFO) non-generic collection of objects.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
- Syntax for STACK Class
|
1 2 3 4 5 |
[SerializableAttribute] [ComVisibleAttribute(true)] public class Stack : ICollection, IEnumerable,ICloneable |
Stack.Push Method – Using STACK in C#
- Helps to insert an object to the top of the stack.
- Syntax
|
1 2 3 |
// Declaration public virtual void Push(object obj) { } |
Stack.Pop Method – Using STACK in C#
- Helps to remove an object at the top of the stack.
- Syntax
|
1 2 3 |
// Declaration public virtual void Pop(void As, void Object) { } |
Stack.Contains Method – Using STACK in C#
- Determine whether an element is in the stack.
- Syntax
|
1 2 3 |
// Declaration public virtual bool Contains(object obj) { } |
Stack.Count – Using STACK in C#
- Helps to get the number of elements contained in the Stack
Main methods used in STACK
PUSH – Push or add an item into the stack data structure.
Syntax: Stack.Push(Object)
Object: The item to be inserted.
Pop: Pop removes an item that inserted last in Stack.
Syntax: Stack.Pop()
Return: The last object in the Stack
Contains: Check the object contains in the stack
Syntax: Stack.Contains(Object)
Object: The specified Object to be seach
Using STACK in C# – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { //Declare an object for stack Stack<string> StackTable = new Stack<string>(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { // add data to the stack table StackTable.Push("Sunday"); StackTable.Push("Monday"); StackTable.Push("Tuesday"); StackTable.Push("Wednesday"); StackTable.Push("Thursday"); StackTable.Push("Friday"); StackTable.Push("Saturday"); int cnt; cnt = StackTable.Count; //Display entered item to richtextbox through stack POP while ((cnt > 0)) { RichTextBox1.Text = (RichTextBox1.Text + (StackTable.Pop() + "\r\n")); cnt = (cnt - 1); } } private void button2_Click(object sender, EventArgs e) { // add data to the stack table StackTable.Push("Sunday"); StackTable.Push("Monday"); StackTable.Push("Tuesday"); StackTable.Push("Wednesday"); StackTable.Push("Thursday"); StackTable.Push("Friday"); StackTable.Push("Saturday"); // Pop an item from the stack table if (StackTable.Contains("Thursday")) { StackTable.Pop(); } else { MessageBox .Show("Not Exist"); } // Display item that currently available in STACK RichTextBox1.Text = ""; int cnt; cnt = StackTable.Count; while ((cnt > 0)) { RichTextBox1.Text = (RichTextBox1.Text + (StackTable.Pop() + "\r\n")); cnt = (cnt - 1); } } } } |
Using STACK in C#






