Stack<t>: simple .net program to show working of stack collection.
The System.Collections.Generic
namespace contains interfaces and classes that define generic
collections, which allow users to create strongly typed collections that
provide better type safety and performance than non-generic strongly
typed collections.
An introduction to DOT NET:
An introduction to DOT NET:
Generic collection: Stack
Generic means it support specific data type (int,float string..etc)
Stack is LIFO Last in first out manner.
Here stack is used to save data and retrieved it in LIFO manner.
You can run program and see the working procedure of it.
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 stackgenriccollection
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
Stack<string> st = new Stack<string>();
private
void button1_Click(object
sender, EventArgs e)
{
if
(textBox1.Text == "")
{
MessageBox.Show("enter value in textbox");
}
else
{
st.Push(textBox1.Text);
MessageBox.Show("inserted");
}
}
private
void button2_Click(object
sender, EventArgs e)
{
if
(st.Count > 0)
{
Object
obj = new Object();
obj = st.Pop();
MessageBox.Show(obj.ToString());
}
else
{
MessageBox.Show("no element in stack");
}
}
private
void Form1_Load(object
sender, EventArgs e)
{
}
private
void button3_Click(object
sender, EventArgs e)
{
if
(st.Count <= 0)
{
MessageBox.Show("stack is empty");
}
else
{
Object
obj = new Object();
obj = st.Peek();
MessageBox.Show(obj.ToString());
}
}
private
void button4_Click(object
sender, EventArgs e)
{
MessageBox.Show(st.Count.ToString());
}
private
void button5_Click(object
sender, EventArgs e)
{
if
(st.Count <= 0)
{
MessageBox.Show("stack is empty");
}
else
{
if
(st.Contains(textBox2.Text))
{
MessageBox.Show("available");
}
else
{
MessageBox.Show("not available");} } } } }
Posted By:Tanuj Kumar
On:Shareyourconscience
Comments
Post a Comment