What is Stack in Data Structure ?

Answer : A stack is a container of objects that are performed…

What is Stack in Data Structure ?

  • A stack is a container of objects that are performed based on last-in first-out (LIFO) principle.
  • A stack is a limited access data structure-elements can be added and removed from the stack only at the top. A stack is a recursive data structure.
  • The structural definition of a Stack is either empty or it consists of a top and the rest.
  • There are two basic operations performed in a Stack they are:
    • Push()
    • Pop()
    • Push()-push function is used to add or insert new item into the stack.
POP Operation
    • Pop()-pop function is used to delete or remove an item from the stack.
 Push Operation
  • When a stack is completely full, it is said to be Overflow state and if stack is completely empty, it is said to be Underflow state.
  • Stack allows operations at one end only.

Other operations used in Stack

  • Peek()-The peek() function gets the top element of the stack, without deleting it.
  • isEmpty() -The isEmpty() function checks whether the stack is empty or not.
  • isFull()-The isFull() function is used to check whether the stack is full or not.

Features of stacks

  • Dynamic data structures
  • Do not have a fixed size
  • Do not consume a fixed amount of memory
  • Size of stack changes with each push() and pop() operation. Each push() and pop() operation increases and decreases the size of the stack by 1, respectively.

Implementation of Stack

 Stack in Data Structure

Sample Code

[pastacode lang=”javascript” manual=”import%20java.io.*%3B%0A%0A%0A%2F*%20Java%20program%20to%20implement%20basic%20stack%20%0Aoperations%20*%2F%0Aclass%20Stack%20%0A%7B%20%0A%20%20%20%20static%20final%20int%20MAX%20%3D%201000%3B%20%0A%20%20%20%20int%20top%3B%20%0A%20%20%20%20int%20a%5B%5D%20%3D%20new%20int%5BMAX%5D%3B%20%2F%2F%20Maximum%20size%20of%20Stack%20%0A%20%20%0A%20%20%20%20boolean%20isEmpty()%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20return%20(top%20%3C%200)%3B%20%0A%20%20%20%20%7D%20%0A%20%20%20%20Stack()%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20top%20%3D%20-1%3B%20%0A%20%20%20%20%7D%20%0A%20%20%0A%20%20%20%20boolean%20push(int%20b)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20if%20(top%20%3E%3D%20(MAX-1))%20%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Stack%20Overflow%22)%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%20%0A%20%20%20%20%20%20%20%20%7D%20%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20a%5B%2B%2Btop%5D%20%3D%20b%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(b%20%2B%20%22%20pushed%20into%20the%20stack%22)%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%20%0A%20%20%20%20%20%20%20%20%7D%20%0A%20%20%20%20%7D%20%0A%20%20%0A%20%20%20%20int%20pop()%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20if%20(top%20%3C%200)%20%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Stack%20Underflow%22)%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%200%3B%20%0A%20%20%20%20%20%20%20%20%7D%20%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20b%20%3D%20a%5Btop–%5D%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20b%3B%20%0A%20%20%20%20%20%20%20%20%7D%20%0A%20%20%20%20%7D%20%0A%7D%20%0A%20%20%0A%2F%2F%20Driver%20code%20%0Aclass%20Main%20%0A%7B%20%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20Stack%20s1%20%3D%20new%20Stack()%3B%20%0A%20%20%20%20%20%20%20%20s1.push(80)%3B%20%0A%20%20%20%20%20%20%20%20s1.push(75)%3B%20%0A%20%20%20%20%20%20%20%20s1.push(90)%3B%20%0A%20%20%20%20%20%20%20%20System.out.println(s1.pop()%20%2B%20%22%20Popped%20from%20the%20stack%22)%3B%20%0A%20%20%20%20%7D%20%0A%7D” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”javascript” manual=”80%20pushed%20into%20the%20stack%0A75%20pushed%20into%20the%20stack%0A90%20pushed%20into%20the%20stack%0A80%20Popped%20from%20the%20stack” message=”” highlight=”” provider=”manual”/]
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like