This is a homework assignment so it would be difficult for us to provide guidance to you without doing your homework or understanding what your homework restrictions are. Therefore we'll provide some general guidelines.
- There is already a Stack and generic Stack classes in .NET. Since these are already included in code you might want to rename your version to something more descriptive to avoid confusion.
- Your
Stack
class should derive fromIEnumerable<T>
ideally so it can be enumerated using theforeach
. If that isn't possible then don't have it derive from anything. Of course if you haven't learned about interfaces yet then don't worry about it. -
StackExtensions
appears to be an extension class but yourStack
class derives from it. That doesn't really make sense. You're creating a derived type from a type providing extensions to an existing type. SoStackExtensions
should not derive from anything nor have anything derive from it. - Use descriptive variable names in your code.
elem
andparameters
are not descriptive. - You don't appear to be using the
size
field at all so remove it.
As for your Merge
not working. There is no way to currently implement this method as the caller might expect it to work. If you call a Merge
method and pass it a Stack
object then you'd expect the original Stack
to be updated but the argument to be left alone. In your current Stack
implementation there is no way to get the list of items it contains without also removing them from the stack. Therefore you cannot enumerate a stack and copy the items to another stack. To get Merge
to work you need to expand your Stack
type to allow you to get the items in the stack (in order) without removing them. Then in the Merge
method you would enumerate the provided stacks represented by elements
. For each element you would then get all the items in the stack and call Add
on each one to add them to your target stack called elem
. Note that this method you call needs to return the items in the order you want them added to the new stack. So if you called Add(A)
followed by Add(B)
and then called Pop
you'd expect to get B
and then A
. If you merged this into a new stack then you'd expect the same order. Therefore the method that gets the items should return the items in whatever order List
has them in.
In your Pop
method it seems like it won't return the last item. Assume there is 1 item left. You check to see if there are any items left (1) and then get the last item. You then remove the last item from the list. Now you check to see if the list is empty and if so set the top to null. But that means you just erased the last item you were going to retrieve. I don't believe you need the last if statement. top
will remain set until you add a new item to the list.
Also note that your Pop
method shouldn't have a try-catch around it. Handling errors is up to the caller of this method, not the list. With that code wrapped there is no way for the caller to know they aren't getting any more items back because you are silently handling the exception. You should consider exposing some sort of IsEmpty
method that tells the caller whether they can safely call Pop
.