Go Back   Velocity Reviews > General Computer Discussion > General Help Related Topics
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

General Help Related Topics - c# programs

 
Thread Tools Search this Thread
Old 09-13-2009, 10:24 AM   #1
Default c# programs


i want stack operation console program implementing with all methods


lieutaryan
lieutaryan is offline   Reply With Quote
Old 09-13-2009, 10:25 AM   #2
lieutaryan
Junior Member
 
Join Date: Sep 2009
Posts: 2
Default
/// 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");

}
}


lieutaryan
lieutaryan is offline   Reply With Quote
Old 09-24-2009, 02:50 AM   #3
april198474
Junior Member
 
Join Date: Sep 2009
Posts: 6
Default
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


april198474
april198474 is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
2007/11/29 Boris 7 new programs, Logic Studio 8 for Mac, MicrosoftVisual Studio 2008 Professional Edition, Microsoft Windows Vista UltimateNov-2007.Win32/64, other new programs ola@mail.gr DVD Video 0 11-29-2007 06:15 AM
disabling program(s) davis.angwenyi@gmail.com A+ Certification 2 03-19-2007 04:49 PM
DVD recording of TV programs muncybob DVD Video 15 11-09-2006 11:19 AM
Burner Program(s) Don't Recognize DVD Writers John DVD Video 2 01-01-2005 09:37 PM
Diagnostic programs 2004 - 2002, SolarWinds 2002 Broadband Engineer's Edition, SolarWinds Orion Network Performance Monitor, other ! te2 A+ Certification 0 09-02-2004 07:42 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

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