c# programs

Discussion in 'General Computer Support' started by lieutaryan, Sep 13, 2009.

  1. lieutaryan

    lieutaryan

    Joined:
    Sep 13, 2009
    Messages:
    2
    i want stack operation console program implementing with all methods:damnmate:
    lieutaryan, Sep 13, 2009
    #1
    1. Advertising

  2. lieutaryan

    lieutaryan

    Joined:
    Sep 13, 2009
    Messages:
    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, Sep 13, 2009
    #2
    1. Advertising

  3. lieutaryan

    april198474

    Joined:
    Sep 17, 2009
    Messages:
    2
    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, Sep 24, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Me
    Replies:
    9
    Views:
    486
  2. Ben Leal
    Replies:
    1
    Views:
    3,039
    Richard Wright
    Aug 6, 2003
  3. truebluedave
    Replies:
    2
    Views:
    696
  4. Edge
    Replies:
    9
    Views:
    438
  5. vvcd
    Replies:
    0
    Views:
    1,193
Loading...

Share This Page