how to fix error in code in console application

vitaminchik 486 Reputation points
2023-02-14T20:25:56.94+00:00

Hello. I wrote the code, everything works fine, but the Merge method and Concat doesn't work. How can I fix the problem? The Merge method must concatenate 2 objects. By this example:1.      var s = new Stack("a", "b", "c")

2.      s.Merge(new Stack("1", "2", "3"))

3.      // in stack s - "a", "b", "c", "3", "2", "1"

And Concat method:

1.      var s =Stack.Concat(new Stack("a", "b", "c"), new Stack("1", "2", "3"), new Stack("А", "B", "C"));

// in stack- "c", "b", "a" "3", "2", "1", "C", "B", "A"

I don't quite understand how to implement this. Can you recommend literature for better understanding?

Is it possible to improve the code?

https://github.com/aminchikkk/Homework/blob/main/Program.cs

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2023-02-14T20:53:14.65+00:00

    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.

    1. 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.
    2. Your Stack class should derive from IEnumerable<T> ideally so it can be enumerated using the foreach. 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.
    3. StackExtensions appears to be an extension class but your Stack 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. So StackExtensions should not derive from anything nor have anything derive from it.
    4. Use descriptive variable names in your code. elem and parameters are not descriptive.
    5. 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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.