Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I need Help to Implement my own Stack<t>.Pop Method as a Generic List<t>
I found implementation on MSDN[^]
for an array. (please see screen attached for array implementation) But really could not get to understand how it works or how can I implement it for Generic List<t>. Any Help would be appreciated.
Screen: array — ImgBB[^]

thank you

What I have tried:

/// Stack is last-in-first-out (LIFO) collection.
/// <typeparam name="T"> => type of elements in the stack.</typeparam>
public class Stack<T> : IEnumerable<T>
{
    private readonly List<T> myList;
    /// Initializes a new instance of the stack class that is empty and has the default initial capacity.
    public Stack()
    {
        myList = new List<T>();
    }

    /// a new instance of the stack class that is empty and has
    /// the specified initial "capacity".
    /// <param name="capacity">The initial number of elements of stack.</param>
    public Stack(int capacity)
    {
        myList = new List<T>(capacity);
    }
    /// Initializes a new instance of the stack class that contains elements copied
    /// from the specified collection and has sufficient capacity to accommodate the
    /// number of elements copied.
    /// <param name="collection">The collection to copy elements from.</param>
    public Stack(IEnumerable<T> collection)
    {
        myList = new List<T>(collection);
    }

    /// Gets the number of elements contained in the stack.
    public int Count => myList.Count;  // 12 out of 36 Nunit tests passing.

    /// Removes and returns the object at the top of the stack.
    /// <returns>The object removed from the top of the stack.</returns>
    public T Pop()    // 6 out of 18  Nunit tests passing.
    {
        int index = myList.Count - 1;
        if (index < 0)
        {
            throw new InvalidOperationException("Empty stack");
        }
        T t = myList[index];
        myList.RemoveAt(index); //
        return t;
    }
Posted
Updated 3-Feb-22 0:19am
v5
Comments
Richard Deeming 2-Feb-22 12:12pm    
I assume you're doing this as an exercise / homework assignment - otherwise, why wouldn't you use the built-in Stack<T> class?

If so, you're probably expected to submit your own implementation, not a copy of somebody else's.
LuckyChloe 2-Feb-22 12:32pm    
That's correct. I also need to implement 5 other methods (Push()) for example. So if I could get idea about how to implement Pop() I would be able to do the rest myself.
Matt T Heffron 2-Feb-22 18:01pm    
Your Pop() method never modifies your myList, so if you call it a second time it will return the same item again. You should not use a separate myStack. Just use myList.
Matt T Heffron 2-Feb-22 18:12pm    
Also, your Stack(IEnumerable<t> collection) constructor, counts the items in the collection, and allocates the myList, but never puts them into myList!

You can use the Insert (or Add) methods to add items to the List. And you can use the RemoveAt method to remove them. With those two methods you have a fully functional stack.

As to your error message, the variable mylist only exists momentarily in the constructor. It should be declared at class level so it is known to all methods.
 
Share this answer
 
Comments
LuckyChloe 2-Feb-22 14:12pm    
Thank You Mr. Richard for your answer. However, I was not able to make it work. I am trying to give public T Pop() method same functionality as Stack.Pop() Method of built-in C# Stack<t> class.
CPallini 3-Feb-22 2:33am    
5.
You need to follow Richard's advice and make myList member of the Stack class. Then you could write, for instance
[updated, thanks to Richard Deeming]
C#
public Stack(IEnumerable<T> collection)
{
    myList = new List<T>(collection);
}
public T Pop()
{
    int index = myList.Count-1;
    if ( index < 0)
    {
        throw new InvalidOperationException("Empty stack");
    }
    T t = myList[index];
    myList.RemoveAt[index];
    return t;
}
 
Share this answer
 
v3
Comments
Richard MacCutchan 3-Feb-22 4:15am    
5 for the complete solution.
Richard Deeming 3-Feb-22 4:23am    
NB: The List<T> class has a constructor overload which accepts an IEnumerable<T> to populate the list with, so you could get rid of the foreach loop in the constructor. :)
public Stack(IEnumerable<T> collection)
{
    myList = new List<T>(collection);
}
CPallini 3-Feb-22 5:16am    
Good to know, my C# is rusty. Thank you!
LuckyChloe 3-Feb-22 6:20am    
Thank You.
It Should have been
myList.RemoveAt(index); instead of [index].
and now it compiled.
CPallini 3-Feb-22 6:29am    
You are welcome.

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