I have been working on how to create a second stack object called 'st2' only by accessing 'st' contents through it's methods. That is simply, to populate stack2 with stack1’s contents. To have a duplicate of stack1’s contents (and order) in stack2.

namespace stack
{
    class Stack
    {
        private int[] arr = new int[100];              // A Stack is implemented as an array here.

        private int freeSlotPointer = 0;                // Points to the next free slot (if there is one).

        public int Capacity()
        {
            return arr.Length;
        }

        public int Count()
        {
            return freeSlotPointer;
        }

        public int Remaining()
        {
            return Capacity() - Count();
        }
        public bool IsFull()                            //
        {                                               //
            return freeSlotPointer == arr.Length;       // Stack is full if pointing past the end of arr.
        }                                               //

        public bool IsEmpty()                           //
        {                                               //
            return freeSlotPointer == 0;                // If first free slot is 0, stack is empty.
        }                                               //

        public int Push(int n)                          // 
        {                                               //
            if (!IsFull())                              // As long as stack isn't full
            {                                           //
                arr[freeSlotPointer++] = n;             // Store value *and then* increment freeSlotPointer.
                //
                return n;                               // Return the Pushed value (see Peek for good reason why).
            }                                           //
            else                                        // 
                throw new Exception("Stack is Full.");  // Only option is to raise an exception.

        }

        public bool Contains(int n)                     // Can't use iterator (foreach ...)
        {                                               //
            for (int j = 0; j < freeSlotPointer; ++j)    // index for every slot below where freeSlotPointer is currently.
            {                                           //
                if (arr[j] == n)                        // If n is found in arr....
                {                                       //
                    return true;                        // We're done.
                }                                       //
            }                                           //
            //
            return false;                               // n was not found.
        }

        public int Pop()                                //
        {                                               //
            if (!IsEmpty())                             // If stack not empty.
            {                                           //
                return arr[--freeSlotPointer];          // *First*, decrement freeSlotPointer then use as index to find value.
            }                                           //
            else                                        // 
                throw new Exception("Stack is Empty."); // Only choice is to raise an exception.
        }                                               //

        public int Peek()                              //
        {                                               //
            return Push(Pop());                         // Pop what's on the stack and Push it back on. Push() returns pushed value.
        }                                               //

        public void Clear()                             //
        {                                               //
            freeSlotPointer = 0;                        // Just reset freeSlotPointer.
        }                                               //

        //private void copyStack(Stack st, Stack st2)
        //{
        //    if (!st.IsEmpty())
        //    {
        //        Stack item = st.Pop();
        //        copyStack(st, st2);
        //        st2.push(item);
        //    }
        //}

    }

    class Program
    {
        static void Main(string[] args)
        {
            Stack st = new Stack();

            st.Push(45);
            st.Push(13);
            st.Push(3);
            st.Push(31);
            st.Push(19);
            st.Push(87);
            st.Push(242);
            st.Push(99);
            st.Push(3123);
            st.Push(16856);

            Console.WriteLine(st.Capacity());
            Console.WriteLine(st.Remaining());
            Console.WriteLine(st.Count());

            st.Pop();
            st.Pop();


            Console.WriteLine("\n\n ");

            Console.WriteLine(st.Capacity());
            Console.WriteLine(st.Remaining());
            Console.WriteLine(st.Count());

            Console.ReadKey();

            Stack st2 = new Stack();



            Console.ReadKey();

        }


    }
} 

Probably one of the easiest ways is by using the constructor:

Add these lines to the Stack class

public Stack()
{

}
public Stack(Stack input)
{
    arr = input.arr;
}

And use it like this:

Stack st2 = new Stack(st);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.