Skip to content
Advertisement

why is my stack s giving null here when it has elements pushed inside it. what am i missing?

this is my code for class stackoperations in which i am working on. right now only size is implemented

JavaScript

this is my provided class that i can use from instructor. its in a different package i dont know if that makes any difference

JavaScript

this is my testing scenario

JavaScript

public class TestMyStackOperations {

JavaScript

and this is my result it shows

JavaScript

why does stack s go back to zero ? are we not pushing item inside it ?

Advertisement

Answer

I see you have several mistakes in your code.

Why is returning 0 your size method?

What is happening is that you are setting a value which point to the same object in memory (it seems this is by reference, but no) When you do this:

JavaScript

The parameter for s is pointed to the same object in memory, that means, that if you modify that object inside the MyStackOperations.size() method, will modify also your object s (MyStack s = new MyStack())

Now, inside your MyStackOperations.size() method, you are modifying the parameter s, when you use temp.pop(); that happens because as I said, it’s a parameter which point to the same object in memory.

JavaScript

You also have a little mistake with check_size variable, because you never reset the value, so, when you call the size method, check_size will have the last value when it was incremented.

You can test your code with a debug or you can use a simple System.out.println(s.list) each time you call MyStackOperations.size() method:

JavaScript

The s.getList() is a method which will return your linked list used inside the MyStack class. In the solution I added that method.

When you run/debug your program you will be able to see that the s object is empty.

How can you solution that?

The fastest way is use the size() method provided by the LinkedList class. In your class MyStack you will have to add a method which returns the size:

JavaScript

And inside your main method, you have to checkSize like this:

JavaScript

Is there a way to fix that using your own method size?

Of course, if you want to use your own method to check the size, it’s ok! That’s no problem, it’s not recommended, but If you want you to use it, no problem. You can fix your code using the clone() method. This method is provided by Object class (all class you create inherits from Object class, and it’s not necessary to add extends Object).

So, inside your MyStack class, it must be like this:

JavaScript

It’s used list.clone(); to get a copy of the list (but not the reference now). So, if you typed ms.list=list, that will be the same objects, because it’s assigned a pointer to the same reference.

Now, inside your MyStackOperations class:

JavaScript

And finally inside the main, a little example:

JavaScript

I hope this help you! 🙂 If you have any doubt, ask the question and I will try to solve it!

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement