![]() |
|
|
|
#1 |
|
i want stack operation console program implementing with all methods
![]() lieutaryan |
|
|
|
|
|
|
#2 |
|
Junior Member
Join Date: Sep 2009
Posts: 2
|
/// 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 |
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Sep 2009
Posts: 6
|
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 |
|
|
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |