Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;

class Stackarray
{

        int[] stack = new int[20];
        int top = 0;

    public void push()
    {
        int n;
        System.Console.WriteLine(" Enter the number of elements in the array ");
        n = int.Parse(System.Console.ReadLine());

        for (int i = 1; i <= n; i++)
        {
            System.Console.WriteLine(" Enter the {0} element ", i);
            stack[i] = int.Parse(System.Console.ReadLine());



        }
        System.Console.WriteLine(" Values Entered");
    }


    public void display()
    {

        System.Console.WriteLine("\n \n Elements are");
        for (int j = top; j >= 1; j--)
        {
            System.Console.WriteLine("{0}", stack[j]);

        }
        System.Console.ReadLine();
        break;
    }






   static void Main(string[] args)
    {

        System.Console.WriteLine(" Program for the Stack using Arrays ");
        System.Console.WriteLine("----------------------------------");

        System.Console.WriteLine (" Enter your choice ");
        System.Console.WriteLine (" 1. Push ");
        System.Console.WriteLine(" 2.Display");


        Stackarray s = new Stackarray();

        int ch;
        ch = int.Parse(System.Console.ReadLine());

        switch (ch)
        {
            case 1:  s.push();
                break;
            case 2: s.display();
                break;
        }

        System.Console.ReadKey();

     }



}
Posted
Updated 19-Oct-13 21:52pm
v2
Comments
BillWoodruff 20-Oct-13 5:14am    
I'm glad you got a helpful answer ! from .NET version 1.1 on, .NET does have a Stack data-structure you can use.

1 solution

You need to include that code in a loop so that it repeats until the user terminates it, like:
C#
static void Main(string[] args)
{

    System.Console.WriteLine(" Program for the Stack using Arrays ");
    System.Console.WriteLine("----------------------------------");
    Stackarray s = new Stackarray();

    while (true)
    {
        System.Console.WriteLine(" Enter your choice ");
        System.Console.WriteLine(" 1. Push ");
        System.Console.WriteLine(" 2. Display");
        System.Console.WriteLine(" 3. Exit");

        int ch = 0;
        ch = int.Parse(System.Console.ReadLine());
    
        switch (ch)
        {
            case 1:
                s.push();
                break;
            case 2:
                s.display();
                break;
            case 3:
                break;
        }
        if (ch == 3)
            break;  // terminate the loop
    
    }

 }
 
Share this answer
 
Comments
krishyuva 20-Oct-13 4:20am    
Thank you Richard
Richard MacCutchan 20-Oct-13 4:21am    
Happy to help. May I also recommend Charles Petzold's free ebook: .NET Book Zero.
krishyuva 21-Oct-13 4:37am    
Thank you again Richard .I'll go through that book.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900