0

Is there an easy solution to implement LIFO solution based on Java?

For instance I have the following

In (50,150,200) Out (all after all the inputs) (250,80)

The results should be something like this:

Initial - Final 200 - 200 200 - 50 150 - 80 150 - 20 50 - 50 (Remaining)

Many thanks in advance,

I thought about LinkedList:

 if (outList.getFirst()>inList.getLast()){
                                        def pieces = outList.getFirst()-inList.getLast()
                                        valoreFinal.add(inList.getLast())
                                        valoreInitial.add(inList.getLast())
                                        iList.removeLast()
                                    }

And so on, but I dont this this is the optimal solution.

Many thanks in advance,

Katherine99
  • 980
  • 4
  • 21
  • 40

1 Answers1

0

The Deque object is meant as a LIFO type Stack.

https://docs.oracle.com/javase/7/docs/api/java/util/Deque.html

It supports both LIFO and FIFO implementations.

LIFO would be implemented with the addFirst(x) and removeFirst() methods.

user681574
  • 553
  • 2
  • 15