Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).

Mainly the following three basic operations are performed in the stack:

  • Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
  • Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
  • Peek or Top: Returns top element of stack.
  • isEmpty: Returns true if stack is empty, else fals.
  • How to understand a stack practically?
    There are many real life examples of stack. Consider the simple example of plates stacked over one another in canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow LIFO/FILO order.Time Complexities of operations on stack:
  • push(), pop(), esEmpty() and peek() all take O(1) time. We do not run any loop in any of these operations.
[ad type=”banner”]
  • Applications of stack:
    • Balancing of symbols
    • Infix to Postfix /Prefix conversion
    • Redo-undo features at many places like editors, photoshop.
    • Forward and backward feature in web browsers
    • Used in many algorithms like Tower of Hanoi, tree traversals, stock span problem, histogram problem.
    • Other applications can be Backtracking, Knight tour problem, rat in a maze, N queen problem and sudoku solver

     

  • Implementation:
    There are two ways to implement a stack:

    • Using array
    • Using linked list
  • Implementing Stack using Arrays
[ad type=”banner”]

C++ Programming:

[pastacode lang=”cpp” manual=”%2F*%20C%2B%2B%20program%20to%20implement%20basic%20stack%0A%20%20%20operations%20*%2F%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%23define%20MAX%201000%0A%20%0Aclass%20Stack%0A%7B%0A%20%20%20%20int%20top%3B%0Apublic%3A%0A%20%20%20%20int%20a%5BMAX%5D%3B%20%20%20%20%2F%2FMaximum%20size%20of%20Stack%0A%20%0A%20%20%20%20Stack()%20%20%7B%20top%20%3D%20-1%3B%20%7D%0A%20%20%20%20bool%20push(int%20x)%3B%0A%20%20%20%20int%20pop()%3B%0A%20%20%20%20bool%20isEmpty()%3B%0A%7D%3B%0A%20%0Abool%20Stack%3A%3Apush(int%20x)%0A%7B%0A%20%20%20%20if%20(top%20%3E%3D%20MAX)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Stack%20Overflow%22%3B%0A%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20a%5B%2B%2Btop%5D%20%3D%20x%3B%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0Aint%20Stack%3A%3Apop()%0A%7B%0A%20%20%20%20if%20(top%20%3C%200)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Stack%20Underflow%22%3B%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20x%20%3D%20a%5Btop–%5D%3B%0A%20%20%20%20%20%20%20%20return%20x%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0Abool%20Stack%3A%3AisEmpty()%0A%7B%0A%20%20%20%20return%20(top%20%3C%200)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20struct%20Stack%20s%3B%0A%20%20%20%20s.push(10)%3B%0A%20%20%20%20s.push(20)%3B%0A%20%20%20%20s.push(30)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20s.pop()%20%3C%3C%20%22%20Popped%20from%20stack%5Cn%22%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Pros: Easy to implement. Memory is saved as pointers are not involved.
Cons: It is not dynamic. It doesn’t grow and shrink depending on needs at runtime.

Output:

10 pushed to stack
20 pushed to stack
30 pushed to stack
30 popped from stack
Top item is 20

Categorized in: