Queues (FIFO) and stacks (LIFO).

queue is a abstract data structure that operates on a first-in-first-out basis.



Application of queue

  • Print queue. If several documents were sent to print, then the one that was sent first will be printed first. Until the first document is printed, the second will not start printing.

  • Keyboard Buffer - The letters on the screen appear in the order in which you press them.

  • Handling of interrupts in real-time systems. 

Advantage: Fast to implement, simple code

Disadvantages: Removing items from the front of the queue can be slow


stack is a Last In, First Out (LIFO) data structure.




Application of stacks

  • The simplest application of a stack is to reverse a word. You push a given word to stack - letter by letter - and then pop letters from the stack.

  • When you use the Back button in your web browser, you will be taken back through the previous pages that you looked at, in reverse orderas their URLs are removed from the stack and reloaded.

  • When you use the Undo button in a word processing package, the last operation you carried out is popped from the stack and undone.

Stacks have several uses:

  • Reversing queues

  • CPU task scheduling

  • Performing Reverse Polish Calculations

  • Holding return addresses and system states for recursive function calls

  • Parsing

  • Expression Conversion(Infix to Postfix, Postfix to Prefix etc)

  • TCP/IP stack

Operations on a stack

  • push(item) - adds a new item to the top of the stack

  • pop() - removes and returns the top item from the stack


Последнее изменение: Thursday, 18 April 2024, 11:01