Implementing FIFO using LIFO

前端 未结 2 2008
挽巷
挽巷 2021-01-12 03:00

Looking at some algorithm exercices on the net, I found an interesting one :

How would you implement a FIFO using a LIFO ?

I tried myself but I ended up wit

2条回答
  •  耶瑟儿~
    2021-01-12 03:32

    Both LIFO and FIFO can be implemented with an array, the only difference between them is in the way tail and head pointers work. Given you start with LIFO, you can add two extra pointers that would reflect FIFO's tail and head, and then add methods to Add, Remove an so on using the FIFO pointers.

    The output type would be as fast as a dedicated FIFO or LIFO type, however it would support both. You would need to use distinctive type members, like AddToStack/AddToQueue, RemoveFromStack/RemoveFromQueue etc.

提交回复
热议问题