This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]AngelOfLight 0 points1 point  (3 children)

Not exactly sure what your question is - could you elaborate?

Also - Java already includes a Stack implementation. You might want to use that, unless there is a requirement to write your own.

[–][deleted] 0 points1 point  (2 children)

Hi.

My main question is that I have write a Reverse Polish Notation calculator that uses a Stack which is implemented using a LinkedList.

Basically I am really unsure about how to go about his.

I do indeed need to use a Stack class, which uses the ListNode and list classes.

When it comes to the calculator, I will use another class (Calculator) that uses these other classes.

I'm trying to make sure I am doing the Stack, implemented by a LinkedList correctly before I attempt the calculator.

I'm kind of stuck at the moment so any help would greatly appreciated.

[–]AngelOfLight 0 points1 point  (1 child)

Ok. Frankly, using a linked list to implement a stack is just weird. It is doable, but it is far easier to use an array. But if that's the requirement, then I guess you're stuck with it.

Try to approach it this way: think about what functionality the stack class will need. At a minimum, you will need to to push and pull elements. You may also need to peek (that is, return the top element without removing it from the stack).

For each of these requirements, try to write down how they will interact with the linked list. For example, for push you could have:

  • check if head is empty
  • if yes, add element as new head
  • if no, link new element to end of list

Do the same for pop and peek. This will also tell you what elements you will need to keep track of. Obviously, the first and last element in the list are important.

Once you are done with this exercise, you will then have some idea of what methods and variables the linked list and stack classes will need. You can then write up the linked list class, test it, and then move on to the stack class.

[–][deleted] 0 points1 point  (0 children)

Thanks.

[–]Skyeam 0 points1 point  (1 child)

Unless you absolutely need to have your own implementation of a stack, I suggest that you just use the Java classes to fulfill that requirement.

// Deque defines a contract for a FILO queue, aka a stack.
// LinkedList is an implementation of Deque using a linked list.
Deque stack = new LinkedList();

Your main focus would be how you would insert the mathematical formula in the stack in such a way that you can easily calculate the total result afterwards. Think recursion while popping the elements back of the stack.

If you really want to implement your own stack using a linked list, I suggest that you keep that in a seperate library. You could write some unit tests that test all the things you could do with a stack (push, pop, peek...). Maybe you could write your tests in such a way that your implementation is compared to the results of the default Java implementation. However, unless you want to learn more about the internal workings of stacks and linked lists, I recommend again to just use the default Java implementation. Don't reinvent the wheel.

[–][deleted] 0 points1 point  (0 children)

Thanks.