Breaking Posts

6/trending/recent

Hot Widget

Type Here to Get Search Results !

Stack

What is a Stack?
A stack is a linear data structure that operates on the Last-In-First-Out (LIFO) principle. The Stack has only one end, but the Queue has two (front and rear). It simply has one pointer, top, which points to the stack's topmost member. When a new element is added to the stack, it is placed at the top, and the element can only be removed from the stack. To put it another way, a stack may be thought of as a container in which insertion and deletion can be done from the top of the stack.
A few crucial points about stack
  • It's named a stack because it behaves like a real-life stack, such as a stack of books.
  • A Stack is an abstract data type having a predefined capacity, meaning it can only store components of a certain size.
  • It's a data structure that inserts and deletes elements in a specific order, which can be LIFO or FILO.
Working of Stack
  • The LIFO pattern is used by Stack. There are five memory blocks in the stack, as shown in the diagram below; thus, the stack's size is 5.
  • Assume we wish to store the items in a stack, which is now empty. As demonstrated below, we have taken a size 5 stack and are pushing the components one by one till the stack is filled.

  • Because our stack is full, the stack's size is 5. 
  • When we entered the new element in the stack in the situations above, we can see that it went from top to bottom. From the bottom to the top, the stack is filled.
  • When we delete something from the stack, there is only one path in and out because the other end is closed. 
  • The LIFO pattern is followed, which means that the item inserted first is removed last. 
  • Because the value 5 is put first in the previous example, it will be removed only after all the other items have been removed.

Standard Stack Operations

  • 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.
  • count(): It returns the total number of elements available in a stack.
  • change(): It changes the element at the given position.
  • display(): It prints all the elements available in the stack.

PUSH operation

  • The following are the steps involved in the PUSH operation:
  • We verify whether a stack is full before adding an element to it.
  • The overflow problem happens when we try to insert the element into a stack that is already full.
  • We set the value of top to -1 when we create a stack to ensure that it is empty.
  • When a new element is pushed into a stack, the value of the top is first incremented, i.e. top=top+1, and then the element is moved to the new top position.
  • The items will be placed till the stack reaches its maximum size.


POP operation
  • The following are the steps involved in the POP operation:
  • We check whether the stack is empty before deleting the element from it.
  • When we try to remove an element from an empty stack, we get an underflow condition.
  • If the stack isn't empty, we start with the element pointed to by the top.
  • The top is decremented by one after the pop operation, i.e. top=top-1.

Applications of Stack
  1. Balancing of symbols.
  2. String reversal.
  3. UNDO/REDO.
  4. Recursion.
  5. DFS(Depth First Search).
  6. Backtracking.
  7. Expression conversion.
  8. Memory management.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.