Stack & Queue
Stack vs Queue
Stack and Queue are two important data structures in the programming world and have a variety of usage. They are a secondary data structure which can build using an array or linked list. They have their own functions. Stack can solve recursive problems while Queue can be used for ordered processing.
Stack
- A stack is a container of objects that are inserted and removed in which elements use the Last-In-First-Out principle (LIFO).
- The delete operation is known as POP, in relation to the physical idea of popping something from a stack
- The LIFO structure also means we can only remove the item that was most recently added
- The PUSH operation can be used to add items to a stack. which refers to the physical idea of pushing an item onto the top of a stack
- The Stack data structure is a natural recursive data structure and suits well for recursive problems e.g. implementing pre-order, post-order, and in-order traversal of the binary tree, while Queue is a sequential data structure and can be used to provide ordering in processing.
Prefix, Postfix, Infix
- Infix : Given two operands A and B and an operator X , the infix notation implies that X will be placed in between A and B i.e . A X B
- Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2). Example : *+AB-CD (Infix : (A+B) * (C-D) )
- Postfix : An expression is called the postfix expression if the operator appears in the expression after the operands. Simply of the form (operand1 operand2 operator). Example : AB+CD-* (Infix : (A+B * (C-D) )
Queue
Queues are implemented using a First-In-Fist-Out (FIFO) principle, meaning that the items are removed from the list in exactly the same order in which they were added to it. For example, it simulates the physical approach of queuing systems used while waiting in line. The first in line will be the first to leave, with new people being added to the back of the queue.
When we insert an item onto the queue this is known as the ENQUEUE function, whilst deleting an item is referred to as the DEQUEUE function.
Comments
Post a Comment