You are on page 1of 13

Question No: 01

Knight - Tour problem, N - Queen Problem.

Applications of Stack:
There are number of Applications of Stack which are discussed below. Stack is
a type of Data Structure in which we can enter and retrieve data from one end only. This entering and
retrieving of data is also called Push and Pop Operation in Stack.

Here are some applications:

1. Stacks can be used for expression evaluation.

Stack data structure is used for evaluating the given expression. For example

5 * ( 6 + 2 ) - 12 / 4

2. Stacks can be used to check parenthesis matching in an expression.

In Programming, we make use of different type of parenthesis, like – (,),

{,}, which are used for opening and closing a block of code.

So, these parentheses get stored in Stack and control the flow of our program.

3. Stacks can be used for Conversion from one form of expression to another.

Converting one form of expressions to another is one of the important applications of stacks.

 Infix to prefix
 Infix to postfix
 Prefix to Infix
 Prefix to Postfix
 Postfix to Infix
 Postfix to Infix

4. Stacks can be used for Memory Management.

Memory Management is the important function of the Operating System. Stack also plays the main role
when it comes to Memory Management.

5. Stack data structures are used in backtracking problems.

Backtracking is another application of Stack. It is a recursive algorithm that is used for solving the
optimization problem.
Knight – Tour problem:
#include <iostream>

#include <iomanip>

#define N 8

using namespace std;

int sol[N][N];

bool isValid(int x, int y, int sol[N][N]) {     //check place is in range and not assigned yet

   return ( x >= 0 && x < N && y >= 0 && y < N && sol[x][y] == -1);

void displaySolution() {

   for (int x = 0; x < N; x++) {

      for (int y = 0; y < N; y++)

         cout << setw(3) << sol[x][y] << " ";

      cout << endl;

   }

int knightTour(int x, int y, int move, int sol[N][N], int xMove[N], int yMove[N]) {

   int xNext, yNext;

   if (move == N*N)     //when the total board is covered

      return true;
   for (int k = 0; k < 8; k++) {

      xNext = x + xMove[k];

      yNext = y + yMove[k];

      if (isValid(xNext, yNext, sol)) {     //check room is preoccupied or not

         sol[xNext][yNext] = move;

         if (knightTour(xNext, yNext, move+1, sol, xMove, yMove) == true)

            return true;

         else

            sol[xNext][yNext] = -1;// backtracking

   }

   }

   return false;

bool findKnightTourSol() {

   for (int x = 0; x < N; x++)     //initially set all values to -1 of solution matrix

      for (int y = 0; y < N; y++)

         sol[x][y] = -1;

   //all possible moves for knight

   int xMove[8] = {  2, 1, -1, -2, -2, -1,  1,  2 };

   int yMove[8] = {  1, 2,  2,  1, -1, -2, -2, -1 };

   sol[0][0]  = 0;     //starting from room (0, 0)


   if (knightTour(0, 0, 1, sol, xMove, yMove) == false) {

      cout << "Solution does not exist";

      return false;

   } else

      displaySolution();

   return true;

int main() {

   findKnightTourSol();

N- Queen problem:
#include<iostream>
using namespace std;
#define N 4
void printBoard(int board[N][N]) {
   for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++)
         cout << board[i][j] << " ";
         cout << endl;
   }
}
bool isValid(int board[N][N], int row, int col) {
   for (int i = 0; i < col; i++) //check whether there is queen in the left or not
      if (board[row][i])
         return false;
   for (int i=row, j=col; i>=0 && j>=0; i--, j--)
      if (board[i][j]) //check whether there is queen in the left upper diagonal or not
         return false;
   for (int i=row, j=col; j>=0 && i<N; i++, j--)
      if (board[i][j]) //check whether there is queen in the left lower diagonal or not
         return false;
   return true;
}
bool solveNQueen(int board[N][N], int col) {
   if (col >= N) //when N queens are placed successfully
      return true;
   for (int i = 0; i < N; i++) { //for each row, check placing of queen is possible or not
      if (isValid(board, i, col) ) {
         board[i][col] = 1; //if validate, place the queen at place (i, col)
         if ( solveNQueen(board, col + 1)) //Go for the other columns recursively
            return true;
         board[i][col] = 0; //When no place is vacant remove that queen
   }
   }
   return false; //when no possible order is found
}
bool checkSolution() {
   int board[N][N];
   for(int i = 0; i<N; i++)
   for(int j = 0; j<N; j++)
   board[i][j] = 0; //set all elements to 0
   if ( solveNQueen(board, 0) == false ) { //starting from 0th column
      cout << "Solution does not exist";
      return false;
   }
   printBoard(board);
   return true;
}
int main() {
   checkSolution();
}

Q No 2: How you can solve the problem permutations in data structure


using C++.
We can solve the problem permutation in data structure using C++ by using Backtracking and by using
STL means by Strings. There is also a simple code of C++ to find the permutations by using simple
formula of Permutation.

Codes:

Using Backtracking:
#include <iostream>
using namespace std; 
void permutations(string str, int i, int n)
{
    // base condition
    if (i == n - 1)
    {
        cout << str << endl;
        return;
    }
 
    for (int j = i; j < n; j++)
    {
                swap(str[i], str[j]);        // STL `swap()` used
 
        permutations(str, i + 1, n);
 
        swap(str[i], str[j]);
    }
}
 
int main()
{
    string str = "ABC";
 
    permutations(str, 0, str.length());
 
    return 0;
}

Using STL:
#include <iostream>

#include <algorithm>

using namespace std;

void permutations(string str, int n, string result)

        if (n == 1)

    {

        cout << result + str << endl;

        return;
    }

    for (int i = 0; i < n; i++)

    {

        

        permutations(str.substr(1), n - 1, result + str[0]);

        rotate(str.begin(), str.begin() + 1, str.end());

    }

}
 

int main()

    string str = "ABC";

    string result;        // empty string

    permutations(str, str.size(), result);

return 0;

By simple Formula method:


#include <iostream>

using namespace std;

int fact(int n) {

   if (n == 0 || n == 1)

   return 1;

   else

   return n * fact(n - 1);

int main() {

   int n, r, per;
   cout<<"Enter n : ";

   cin>>n;

   cout<<"\nEnter r : ";

   cin>>r;

   per = fact(n) / fact(n-r);

   cout << "\nPermutation : " << per;

   return 0;

You might also like