Definition of Stack
Stack is a linear data structure that follows the principle of LIFO (Last In First Out). The principle of LIFO means the element which enters at the end of the stack will be the first element to come out of the stack. The insertion, as well as deletion of the elements, will be performed from the top only. A Stack is an abstract data type with a predefined capacity because it can store elements of a limited size.
Application of Stack
Memory management:- The stack manages the memory. The memory is known as stack memory as all the variables are assigned in a function called stack memory.
Expression Conversion:- Concept of the stack is used in the conversion of different expressions like:-
Infix to prefix Infix to postfix Prefix to infix Prefix to postfix Postfix to infix
DFS(Depth First Search):- Depth First Search is a technique of Graph that uses the stack data structure.
Tower of Hanoi is a technique that is based on the application of stack.
Example of a stack in real life
A pile of books, books are kept in a pile in such a way that the last book we kept on the self will be the first book we can take out.
An undo mechanism in text editors, this operation is accomplished by keeping all text changes in a stack.
Sometimes dinner plates are also kept in a sequence that represents the concept of stack.
Stack Operations
There can be many operations that can be performed by stack in which few are listed below:-
push(): When we insert an element in a stack then the operation is known as a push. If the stack is full then the overflow condition occurs.
pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is empty means that no element exists in the stack, this state is known as an underflow state.
isEmpty(): It determines whether the stack is empty or not.
isFull(): It determines whether the stack is full or not.
peek(): It returns the element at the given position.
display(): It prints all the elements available in the stack.
Implementation of stack
Array Representation:-In array implementation, the stack is formed using an array. All the operations are performed using arrays.
Linked Representation:-In Linked implementation, the stack is formed using Linked List. All the operations are performed using Linked List.
Definition of Queue
Queue is a linear data structure that follows the principle of FIFO (First In First Out). The principle of FIFO means the insertion of element will be done using rear pointer and deletion will be done using front pointer. Queue is also an abstract data structure.
Types of queue
Single Queue:- In a singly ended queue the last elements will be inserted from the rear and deletion will take place from the front.
Doubled Ended Queue:- In the doubly ended queue, both insertion and deletion will take place from both front and rear points.
Priority Queue:- In the priority queue, some priority is assigned to the data and operation is performed according to their priorities.
Circular Queue:- In a Circular Queue is a type of queue in which the last element of the queue is connected to the first element of the queue.
Application of Queue
Managing requests on a single shared resource such as CPU scheduling and disk scheduling.
Handling hardware or real-time systems interrupts.
Example of Queue in real life
Some examples of queues in real life are:-
People on an escalator
Cashier line in a store