Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > General Computer Discussion > General Computer Support > c# programs

Reply
Thread Tools

c# programs

 
 
lieutaryan lieutaryan is offline
Junior Member
Join Date: Sep 2009
Posts: 2
 
      09-13-2009
i want stack operation console program implementing with all methods
 
Reply With Quote
 
 
 
 
lieutaryan lieutaryan is offline
Junior Member
Join Date: Sep 2009
Posts: 2
 
      09-13-2009
/// Implement the stack operation
/// Created by: Bikram Panjikar,MCA, BMSIT, Bangalore
/// Date: 14/09/2009
/// Note:--
/// Pop: removes an element from the stack
/// Peek: Allows the user to view the top element in the collection
/// Count:Count the total number of elements in the stack

using System;
using System.Collections; //for implementing stack class

public class MyStack
{
Stack mystack;
public MyStack()
{
mystack = new Stack();
}

public void PushElement(int x)
{
//Push an element onto a stack
if (x > 0)
mystack.Push(x);
}
public int PopElement()
{
//Pop and element off the stack
if (mystack.Count > 0)
{
//boxing
object i = mystack.Pop();
return ((int)i);
}
else
return (0);
}

public int PeekElement()
{
//View the top element on the stack
if (mystack.Count > 0)
{
object i = mystack.Peek();
return ((int)i);
}
else
return (0);
}
public int CountElement()
{
//number of elements in the stack
object i = mystack.Count;
return ((int)i);
}



public void print_Elements()
{
//print all of the elements on the stack
if (mystack.Count > 0)
{
Console.WriteLine("\n\t\t\tThe Contents of the stack is: \n");
IEnumerator myenum = mystack.GetEnumerator();
while (myenum.MoveNext())
{
Console.WriteLine("\t\t\tElement: {0}", myenum.Current);
}
Console.WriteLine();
}
else
Console.WriteLine("\n\t\t\tStack is empty!!!\n");
}
}

class Class1
{
static void Main(string[] args)
{
MyStack t = new MyStack();
String choice;
do
{
Console.WriteLine("\n*** Stack operation ***");
Console.WriteLine("----------------------");
Console.WriteLine("1. Push");
Console.WriteLine("2. Pop");
Console.WriteLine("3. Peek");
Console.WriteLine("4. Count");
Console.WriteLine("5. Display");
Console.WriteLine("6. Exit");
Console.WriteLine("----------------------");
a: Console.Write("\nEnter the choice: ");
choice = Console.ReadLine();
if (choice != "1" && choice != "2" && choice != "3" && choice != "4" && choice != "5" && choice != "6")
{
Console.WriteLine("\nInvalid Choice!!!\n");
goto a;
}

switch (choice)
{
case "1":

Console.Write("\nEnter the element: ");
int objToPush = int.Parse(Console.ReadLine());
Console.WriteLine("\n\t\t\tpushing: {0}", objToPush + "\n");
// push the object on the stack
t.PushElement(objToPush);
break;
case "2":
int objPoped = t.PopElement();
if (objPoped == 0)
Console.WriteLine("\n\t\t\tStack is empty!!!\n");
else
Console.WriteLine("\n\t\t\tpoping: {0}", objPoped + "\n");
break;
case "3":
//Print the top element of the stack
int objPeek = t.PeekElement();
if (objPeek == 0)
Console.WriteLine("\n\t\t\tStack is empty!!!\n");
else
Console.WriteLine("\n\t\t\tTop Element of the stack: {0}", objPeek + "\n");
break;
case "4":
//Print the total num of element in the stack
Console.WriteLine("\n\t\t\tTotal num of elements in the stack: {0}", t.CountElement() + "\n");
break;
case "5":
//print the contents of the Stack
t.print_Elements();
break;
}
} while (choice != "6");

}
}
 
Reply With Quote
 
 
 
 
april198474 april198474 is offline
Junior Member
Join Date: Sep 2009
Posts: 6
 
      09-24-2009
Hi,

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Stack sk = new Stack();
Stack sk2 = new Stack();
foreach (int i in new int[4]{ 1, 2, 3, 4 })
{
sk.Push(i);
sk2.Push(i);
}

foreach (int i in sk)
{
Console.WriteLine(i);
}

sk.Pop();
Console.WriteLine("Pop");
foreach (int i in sk)
{
Console.WriteLine(i);
}

sk2.Peek();


Console.WriteLine("Peek");
foreach (int i in sk2)
{
Console.WriteLine(i);
}

while (sk2.Count != 0)
{
int i = (int)sk2.Pop();
sk2.Pop();
}
Console.WriteLine("clear");
foreach (int i in sk2)
{
Console.WriteLine(i);
}
}
}
}

April
____________________
Comm100-Free Hosted Online Live Chat
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
retrieveing programs deleted in add or remove programs in control panel truebluedave Computer Support 2 08-23-2005 10:32 PM
Programs take a long time to launch from A.Programs Me MCSE 9 01-20-2005 04:05 PM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C++ 12 01-10-2005 03:35 AM
Re: How to see all programs after Start -> All Programs Ben Leal Computer Support 1 08-06-2003 01:58 AM



Advertisments