You are on page 1of 19

DATA STRUCTURE

Lab 6
Problem 1:
#include<iostream>
using namespace std;
#define SIZE 10
class Stack
{
private:
int arr[SIZE];
int top;
public:
Stack()
{
top = -1;
}
virtual void intailziestack()
{
arr[SIZE] = NULL;
}
virtual bool isempty()
{
if (top == -1)
{
return true;
}
return false;

}
virtual bool isFull()
{
if (top >= SIZE-1)
{
return true;
}
return false;
}
virtual void push(int n)
{
if (isFull())
{
cout << "Stack is Full :)" << endl;
}
top++;
arr[top] = n;

}
virtual int pop()
{
if (isempty())
{
cout << "Stack is Empty :)" << endl;
return 0;
}
else
{
return (arr[top--]);
}
}
virtual int Top()
{
if (isempty())
{
cout << "Stack is Empty :)" << endl;
return 0;
}
else
{
return arr[top];
}
}
virtual void display()
{
if (top >= 0)
{
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
else
cout << "Stack is empty :)" << endl;
}

};
int main()
{
Stack ss;
//pushing 10 elements to stack
for (int i = 0; i < 10; i++)
{
ss.push(i+1);
}
//Popped out the elements
ss.pop();
ss.pop();
cout << "Elements of stack are : ";
ss.display();

//Check top the stack


cout << "Top of the stack is : " << ss.Top()<<endl;

//Add to more elements for checking the stack is full or not


ss.push(9);
ss.push(10);
cout << "Current Stack Elements : ";
ss.display();

if (ss.isFull())
{
cout << "Stack is FULL :)" << endl;
}
if(ss.isempty());
{
cout << "Stack is not Empty :)" << endl;
}
system("pause");
return 0;

}
Output:

Program no.2:
#include<iostream>
using namespace std;
#define SIZE 10
class Stack
{
private:
int arr[SIZE];
int top;
public:
Stack()
{
top = -1;
}

bool isempty()
{
if (top == -1)
{
return true;
}
return false;

}
bool isFull()
{
if (top >= SIZE-1)
{
return true;
}
return false;
}
void push(int n)
{
if (isFull())
{
cout << "Stack is Full :)" << endl;
}
top++;
arr[top] = n;

}
int pop()
{
if (isempty())
{
cout << "Stack is Empty :)" << endl;
return 0;
}
else
{
return (arr[top--]);
}
}
int Top()
{
if (isempty())
{
cout << "Stack is Empty :)" << endl;
return 0;
}
else
{
return arr[top];
}
}

void reverse()
{
int cArray[10];
for (int i = top; i>=0; i--)
{
cArray[i] = pop();
}
for (int i = 0; i <10; i++)
{
cout << cArray[i] << " ";
}
cout << endl;
}
void display()
{
if (top >= 0)
{
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
else
cout << "Stack is empty :)" << endl;
}
};
int main()
{
Stack ss;
//pushing 10 elements to stack
for (int i = 0; i < 10; i++)
{
ss.push(i+1);
}
cout << "Elements of stack are : ";
ss.display();

cout << "Reversed Elements of stacks is : ";


ss.reverse();

system("pause");
return 0;
}

Output:

Program no.3:
#include<iostream>
using namespace std;
struct node
{
int data;
node* next;
};
class stack
{
private :
node * top;
public:
stack()
{
top = NULL;
}
void push(int d)
{
node*temp = new node;
temp->data = d;
temp->next = top;
top = temp;
}
int pop()
{
if (top > 0)
{
node *temp = new node;
temp = top;
top = temp->next;
return (temp->data);

delete temp;
temp = NULL;

}
else
{
return 0;
}
}
void Display()
{
node *temp = new node;
temp = top;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void reverse(stack& s1)
{
stack s2;
while (!s1.isEmpty())
{
s2.push(s1.pop());
}
s1 = s2;
}

//check the stack is empty or not


bool isEmpty()
{
return (top == 0);
}
};
int main()
{

stack s;
cout << "Insertion of Elements" << endl;
s.push(5);
s.push(10);
s.push(15);
s.push(20);
s.push(30);

cout << "\nStack Elements : ";


s.Display();
cout << "\nAfter Poping out Elements" << endl;
s.pop();
s.pop();
//s.pop();
cout << "\nStack Elements : ";
s.Display();

cout << "\nAfter reverse : ";


s.reverse(s);
s.Display();

system("pause");
return 0;
}

Output:

Problem no.4:
#include <iostream>
#include <string>
using namespace std;

struct node
{
char data;
node* next;

};
//ADT stack
class Stack
{
private:
node* top ;
public:
Stack()
{
top = NULL;
}
void push(char c)
{
node* temp = new node;
temp->data = c;
temp->next = top;
top = temp;
}
char pop()
{
char d;
node* temp = top;
d = top->data;
top = top->next;

delete temp;
temp = NULL;
return d;
}

bool empty()
{
if (top == NULL)
{
return true;
}
return false;

}
char gettop()
{
return (top->data);
}
bool isoperator(char op)
{
if (op == '+' || op == '/' || op == '-' || op == '^' || op == '*' || op ==
'%') {
return true;
}
else
{
return false;
}
}
bool precedence(char op)
{
if (top == NULL)
{
return true;
}
if (op == '+' && top->data == '*')
{
return false;
}
else if (op == '+' && top->data == '/')
{
return false;
}
else if (op == '-' && top->data == '*')
{
return false;
}
else if (op == '-' && top->data == '/')
{
return false;
}
else if (op == '-' && top->data == '+')
{
return false;
}
else if (op == '+' && top->data == '-')
{
return false;
}
else if (op == '*' && top->data == '/')

{
return false;
}
else if (op == '/' && top->data == '*')
{
return false;
}
else
{
return true;
}
}

void printstack()
{
node* newnode = top;
while (newnode != NULL)
{
cout << newnode->data<<" ";
newnode = newnode->next;
}

}
void convert(string infix)
{
int length;
char Chr;
int count = 0;
int j = 0;

length = infix.length();
string postfix[10];
for (int i = 0; i < length; i++)
{
Chr = infix[i];
if (!isoperator(Chr))
{
postfix[j] = Chr;
j++;
}
else if (isoperator(Chr) && empty())
{
push(Chr);
}
else
{
if (precedence(Chr))
{
push(Chr);
}
else
{
while (precedence(Chr) == false)
{
postfix[j] = pop();
j++;
}
push(Chr);
}
}

while (top != NULL)


{
postfix[j] = pop();
j++;
}
cout << "\nYour postfix string is : ";
for (int x = 0; x < length; x++)
{
cout << postfix[x];
}
cout << endl;
}

};

int main()
{
//The string given in Algorithm
string infix = "6+2*5-8/4";

string postfix;
Stack s;

cout << "Your infix string is : " << infix;


s.convert(infix);

system("pause");
return 0;
}
Output:

Problem 5:
#include<iostream>
#include<stack>
#include<string>
using namespace std;

struct node
{
int data;
struct node* next;
};
struct node* top = NULL;

int checkPrec(char op)


{
if (op == '^')
{
return 3;
}
else if (op == '/' || op == '*')
{
return 2;
}
else if (op == '+' || op == '-')
{
return 1;
}
else
{
return -1;
}
}

void push(int n)
{
node* temp = new node;
temp->data = n;
temp->next = top;
top = temp;
}
void pop()
{
if (top == NULL)
cout << "Stack Is Empty" << endl;
else
{
cout << "The popped element is : " << top->data << endl;
top = top->next;
}
}

// PART A
void convertToPostfix(string str)
{
//by using built in stack
stack<char> STK;

string Exp;
for (int i = 0; i < str.length(); i++)
{
char input_ch = str[i];

if ((input_ch >= 'A' && input_ch <= 'Z') || (input_ch >= 'a' && input_ch <=
'z') || (input_ch >= '0' && input_ch <= '9'))
{
Exp = Exp + input_ch;
}
else if (input_ch == '(')
{
STK.push('(');
}
else if (input_ch == ')')
{
while (STK.top() != '(')
{
Exp = Exp + STK.top();
STK.pop();
}
STK.pop();
}

else
{
while (!STK.empty() && checkPrec(str[i]) <= checkPrec(STK.top()))
{
Exp = Exp + STK.top();
STK.pop();
}
STK.push(input_ch);
}
}

while (!STK.empty())
{
Exp += STK.top();
STK.pop();
}
cout << Exp << endl;
}

// PART B
bool checkOperand(char chr)
{
return (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z');
}
string convertToInfix(string exp)
{
stack<string> STK;
for (int i = 0; exp[i] != '\0'; i++)
{
if (checkOperand(exp[i]))
{
string op(1, exp[i]);
STK.push(op);
}
else
{
string op1 = STK.top();
STK.pop();
string op2 = STK.top();
STK.pop();
STK.push("(" + op2 + exp[i] + op1 + ")");
}
}
return STK.top();
}

// PART C

bool isOprand(char op)


{
switch (op)
{
case '+':
case '-':
case '/':
case '*':
return true;
}
return false;
}

string prefixToPostfix(string Exp)


{
stack<string> STK;
int len = Exp.size();
for (int i = len - 1; i >= 0; i--)
{
if (isOprand(Exp[i]))
{
string op1 = STK.top();
STK.pop();
string op2 = STK.top();
STK.pop();
string temp = op1 + op2 + Exp[i];
STK.push(temp);
}
else
{
STK.push(string(1, Exp[i]));
}
}
return STK.top();
}

int main()
{
//Infix to Postfix conversion
cout << "\tA:Infix to Postfix conversion" << endl;
string Exp1 = "a+b*(c^d-e)^(f+g*h)-i";
cout << "\nInfix expression : ";
cout << Exp1 << endl;
cout << "\nPostfix expression : ";
convertToPostfix(Exp1);

//Postfix to Infix
cout << "\n\n\tB:Postfix to Infix conversion" << endl;
string Exp2 = "abcd^e-fgh*+^*+i-";
cout << "\nPostfix expression : ";
cout << Exp2 << endl;
cout << "\nInfix expression : ";
cout << convertToInfix(Exp2) << endl;

//Prefix to Postfix
cout << "\n\n\tC:Prefix to Postfix conversion" << endl;
string Exp3 = "*+AB-CD";
cout << "\nPrefix expression : ";
cout << Exp3 << endl;
cout << "\nPostfix expression : ";
cout << prefixToPostfix(Exp3) << endl;

cout << endl;


system("pause");
return 0;
}
Output:

Problem 6:
#include<iostream>
using namespace std;
bool isSafe(int** arr, int x, int y, int n)
{
for (int row = 0; row < x; row++)
{
if (arr[row][y] == 1)
{
return false;
}
}
int row = x;
int col = y;
while (row >= 0 && col >= 0 )
{
if (arr[row][col] == 1)
{
return false;
}
row--;
col--;
}
row = x;
col = y;
while (row >= 0 && col < n)
{
if (arr[row][col] == 1)
{
return false;
}
row--;
col++;

}
return true;
}
bool nQueen(int** arr, int x, int n)
{
if (x >= n)
{
return true;
}
for (int col = 0; col < n; col++)
{
if (isSafe(arr, x, col, n))
{
arr[x][col] = 1;

if (nQueen(arr, x + 1, n))
{
return true;
}
arr[x][col] = 0;
}

}
return false;
}
int main()
{
int n;
cin >> n;
int** arr = new int* [n];
for (int i = 0; i < n; i++)
{
arr [i]= new int[n];
for (int j = 0; j < n; j++)
{
arr[i][j] = 0;
}
}
if (nQueen(arr, 0, n))
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout<<arr[i][j]<<" ";
}
cout << endl;
}
}

system("pause");
return 0;
}

Output:

You might also like