4 ms·
> If you have a long, narrow, single-door, multi-car garage that fits a single-file line of cars… that’s a stack: last in, first out. That seems to represent a
by _RPM 12y ago
> If you have a long, narrow, single-door, multi-car garage that fits a single-file line of cars… that’s a stack: last in, first out.
That seems to represent a queue as well. If we assume the last car in is at the end of the line, then, well, we have a queue, not a stack. A stack is hard to represent in real life everyday events. At least it is harder to represent than a queue.
The way I think of a stack is a singly linked list. A linked list where insertion time is O(1).
- Someone 12y agoThe idea is that you cannot get the first car out of the garage if the second one still is there because that garage, with its single door, is a single-lane dead end street. A queue would be a single-lane one-way street, like in a car wash.
- brianmcc 12y agoStack is Last In, First Out: LIFO. Queue is First In First Out, FIFO. It's insertion and exit order which really defines it.