Professional Documents
Culture Documents
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.
Stack data structure is used for evaluating the given expression. For example
5 * ( 6 + 2 ) - 12 / 4
{,}, 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.
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
Memory Management is the important function of the Operating System. Stack also plays the main role
when it comes to Memory Management.
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
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() {
}
int knightTour(int x, int y, int move, int sol[N][N], int xMove[N], int yMove[N]) {
return true;
for (int k = 0; k < 8; k++) {
xNext = x + xMove[k];
yNext = y + yMove[k];
sol[xNext][yNext] = move;
return true;
else
}
}
return false;
bool findKnightTourSol() {
for (int x = 0; x < N; x++) //initially set all values to -1 of solution matrix
sol[x][y] = -1;
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();
}
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>
if (n == 1)
{
return;
}
{
}
}
int main()
return 0;
int fact(int n) {
if (n == 0 || n == 1)
return 1;
else
int main() {
int n, r, per;
cout<<"Enter n : ";
cin>>n;
cout<<"\nEnter r : ";
cin>>r;
return 0;