Stack Implementation Using Array

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication2

{

    class Program

    {

        public class ArrayStack

        {

            protected object[] data;

            protected int pointer;

            public ArrayStack(int capacity)

            {

                data = new object[capacity];

                pointer = 0;

            }

            public bool IsEmpty()

            {

                if (pointer == 0)

                {

                    return true;

                }

                else

                {

                    return false;

                }

            }

            public void Push(object o)

            {

                if (pointer < data.Length)

                {

                    data[pointer] = o;

                    pointer++;

                }

            }

            public object Pop()

            {

                if (IsEmpty())

                {

                    return null;

                }

                else

                {

                    pointer–;

                    object o = data[pointer];

                    data[pointer] = null;

                    return o;

                }

            }

        }

        static void Main(string[] args)

        {

            // Create a stack with maximum capacity 10

            ArrayStack aStack = new ArrayStack(10);

            // Push 10 elements onto the stack

            for (int i = 0; i < 10; i++)

            {

                string objToPush = "obj" + i;

                // Display what we’re doing

                Console.WriteLine("Pushing: {0}", objToPush);

                // Push the object on the stack

                aStack.Push(objToPush);

            }

            Console.WriteLine("****************");

            // Pop all elements from the stack

            while (!aStack.IsEmpty())

            {

                // Pop an object from the stack

                string objPoped = (string)aStack.Pop();

 

                // Display what we’re doing

                Console.WriteLine("Poping: {0}", objPoped);

            }

        }

    }

}
 
Pushing: obj0
Pushing: obj1
Pushing: obj2
Pushing: obj3
Pushing: obj4
Pushing: obj5
Pushing: obj6
Pushing: obj7
Pushing: obj8
Pushing: obj9
****************
Poping: obj9
Poping: obj8
Poping: obj7
Poping: obj6
Poping: obj5
Poping: obj4
Poping: obj3
Poping: obj2
Poping: obj1
Poping: obj0
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s