how to use stack in C#

Posted by Raj_Chennai under C# on 3/17/2012 | Points: 10 | Views : 2547 | Status : [Member] | Replies : 5
how to use stack in C# .Give me the example

raj


Responses

Posted by: Hmanjarawala on: 3/17/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi here is the solution:

private void AddToList()
{
listBox1.Items.Clear();
if (stack.Peek() != null)
{
var array = stack.ToArray();
for (int i = array.Length - 1; i > -1; i--)
{
var o = (object[])array[i];
listBox1.Items.Add(o[0].ToString() + " " + o[1].ToString());
}
}
}

private void button1_Click(object sender, EventArgs e)
{
stack.Push(new object[] { txtName.Text, txtRate.Text });
//if (result == 1)
//{
// MessageBox.Show("Employee Lastname: " + txtName.Text + "\n Employee Payrate: " + txtRate.Text + "");
//}
//else
// MessageBox.Show("You can't add an empty value");
AddToList();
txtName.Clear();
txtRate.Clear();
}

private void button2_Click(object sender, EventArgs e)
{
if (stack.Count > 0)
{
stack.Pop();
AddToList();
}
}


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/17/2012 [Member] Silver | Points: 25

Up
0
Down
Hi Raj,
Here is a link for your reference:
http://www.codeproject.com/Articles/17029/The-use-of-Stacks-in-C

Thanks,
Sanjay

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 3/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi


you can refer this link

http://www.dotnetperls.com/stack

http://msdn.microsoft.com/en-us/library/system.collections.stack.aspx

R D Patel

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajni.Shekhar on: 3/20/2012 [Member] Bronze | Points: 25

Up
0
Down
Stack objStack = new Stack();
objStack.Push("abc");
objStack.Push("xyz");
objStack.Push("lmn");
objStack.Pop();

Thanks,
Rajni Shekhar

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Veerac85 on: 3/20/2012 [Member] Starter | Points: 25

Up
0
Down
DOTNET has very good API on creating ADT types using STACK

Stack stack = new Stack();

to add items
stack.Push("one");

to remove items
stack.Pop("two");

You could refer to http://technico.qnownow.com/2012/03/20/how-to-use-stack-class-in-c/ for a complete example covering various scenarios

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response