Tag Archives: practice

Balancing of symbols using stacks – Data structures

Balancing of symbols

Given a string consisting of symbols consisting (, {, [. You have to check whether these symbols are balanced or not.

balancing of symbols
Title – balancing of symbols

Example :

  1. (a+b)-(c+d) is balanced .
  2. ((a+b) is not balanced .

Algorithm

  1. Create a stack.
  2. While (the end of character i.e ‘\0’ is not reached){
  • If the character read is not a symbol to be balanced, ignore it.
  • If it is an opening symbol i.e (, {, [, the push it into the stack.
  • If it is a closing symbol ), }. ] then pop the elements ( if the stack is empty return an error ) unless the same opening symbol is encountered else report an error.

}

3. At the end, the stack must be empty, else report an error.

Continue reading Balancing of symbols using stacks – Data structures

Implementation of stack using array

Stack using array

Please go through this article for good understanding of stacks concept.

READ  Basic Concept of a stack - Push and pop
stack implementation
Implementation – Stack using array

Continue reading Implementation of stack using array

Josephus circle : N greek soldiers are surrounded

Josephus circle

There are N greek soldiers are in a circle who are surrounded by roman soldiers. So they decide to kill each other starting with 1 each neighboring soldier kills the Mth soldier, until only one is left.And josephus doesn’t want to die so you need tell him the position he needs to be in ( josephus circle ).

josephus circle
Josephus circle

Continue reading Josephus circle : N greek soldiers are surrounded