0% found this document useful (0 votes)
51 views18 pages

DS Lab 10

The document contains C++ code for implementing a recursive solution to the Tower of Hanoi problem. It defines a move function that takes the number of disks to move (n), the source pole, helper pole, and destination pole as parameters. The function recursively calls itself to move n-1 disks from source to helper using destination as the helper pole, prints moving the nth disk, and then recursively calls itself again to move the remaining n-1 disks from helper to destination using source as the helper pole.

Uploaded by

aryan jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views18 pages

DS Lab 10

The document contains C++ code for implementing a recursive solution to the Tower of Hanoi problem. It defines a move function that takes the number of disks to move (n), the source pole, helper pole, and destination pole as parameters. The function recursively calls itself to move n-1 disks from source to helper using destination as the helper pole, prints moving the nth disk, and then recursively calls itself again to move the remaining n-1 disks from helper to destination using source as the helper pole.

Uploaded by

aryan jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

ANS 1:-

#include <bits/stdc++.h>

using namespace std;

class stackss

int stack[100], n=100, top=-1;

public:

void push(int val)

if(top>=n-1)

cout<<"Stack Overflow"<<endl;

else

top++;

stack[top]=val;

void pop()

if(top<=-1)

cout<<"Stack Underflow"<<endl;

else

cout<<"The popped element is "<< stack[top] <<endl;

top--;}
}

void display()

if(top>=0)

cout<<"Stack elements are:";

for(int i=top; i>=0; i--)

cout<<stack[i]<<" ";

cout<<endl;

else

cout<<"Stack is empty";

};

int main()

stackss s;

int ch, val;

cout<<"1) Push in stack"<<endl;

cout<<"2) Pop from stack"<<endl;

cout<<"3) Display stack"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter choice: "<<endl;

cin>>ch;
switch(ch) {

case 1: {

cout<<"Enter value to be pushed:"<<endl;

cin>>val;

s.push(val);

break;

case 2: {

s.pop();

break;

case 3: {

s.display();

break;

case 4: {

cout<<"Exit"<<endl;

break;

default: {

cout<<"Invalid Choice"<<endl;

}while(ch!=4);

return 0;
ANS 2:-

#include <bits/stdc++.h>

using namespace std;

class Node

public:

int data;

Node *next;

};

void push(Node*&top,int val)

Node* newnode = new Node;

newnode->data = val;

newnode->next = top;

top = newnode;

void pop(Node*&top)

if(top==NULL)

cout<<"Stack Underflow"<<endl;

else

cout<<"The popped element is "<< top->data <<endl;

top = top->next;

}
}

void display(Node*top)

Node* ptr;

if(top==NULL)

cout<<"stack is empty";

else

ptr = top;

cout<<"Stack elements are: ";

while (ptr != NULL)

cout<< ptr->data <<" ";

ptr = ptr->next;

cout<<endl;

int main()

Node* top = NULL;

int ch, val;

cout<<"1) Push in stack"<<endl;

cout<<"2) Pop from stack"<<endl;

cout<<"3) Display stack"<<endl;


cout<<"4) Exit"<<endl;

do {

cout<<"Enter choice: "<<endl;

cin>>ch;

switch(ch) {

case 1: {

cout<<"Enter value to be pushed:"<<endl;

cin>>val;

push(top,val);

break;

case 2: {

pop(top);

break;

case 3: {

display(top);

break;

case 4: {

cout<<"Exit"<<endl;

break;

default: {

cout<<"Invalid Choice"<<endl;
}

}while(ch!=4);

return 0;

ANS 3:-

#include <bits/stdc++.h>

using namespace std;

bool isOperand(char x)

return (x >= 'a' && x <= 'z') ||

(x >= 'A' && x <= 'Z');

string getInfix(string exp)

stack<string> s;

for (int i=0; exp[i]!='\0'; i++)

if (isOperand(exp[i]))

string op(1, exp[i]);

s.push(op);

else

string op1 = s.top();


s.pop();

string op2 = s.top();

s.pop();

s.push("(" + op2 + exp[i] +

op1 + ")");

return s.top();

int main()

string exp ;

cin>>exp;

cout << getInfix(exp);

return 0;

ANS 4:-

#include<bits/stdc++.h>

using namespace std;

int prec(char c)

if(c == '^')

return 3;

else if(c == '*' || c == '/')

return 2;
else if(c == '+' || c == '-')

return 1;

else

return -1;

void infixToPostfix(string s)

std::stack<char> st;

st.push('N');

int l = s.length();

string ns;

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

if((s[i] >= 'a' && s[i] <= 'z') ||

(s[i] >= 'A' && s[i] <= 'Z'))

ns+=s[i];

else if(s[i] == '(')

st.push('(');

else if(s[i] == ')')

while(st.top() != 'N' && st.top() != '(')

char c = st.top();

st.pop();

ns += c;
}

if(st.top() == '(')

char c = st.top();

st.pop();

else{

while(st.top() != 'N' && prec(s[i]) <=

prec(st.top()))

char c = st.top();

st.pop();

ns += c;

st.push(s[i]);

while(st.top() != 'N')

char c = st.top();

st.pop();

ns += c;

cout << ns << endl;


}

int main()

string exp;

cin>>exp;

infixToPostfix(exp);

return 0;

ANS 5:-

#include <bits/stdc++.h>

using namespace std;

bool isOperator(char c)

return (!isalpha(c) && !isdigit(c));

int getPriority(char C)

if (C == '-' || C == '+')

return 1;

else if (C == '*' || C == '/')

return 2;

else if (C == '^')

return 3;

return 0;

}
string infixToPostfix(string infix)

infix = '(' + infix + ')';

int l = infix.size();

stack<char> char_stack;

string output;

for (int i = 0; i < l; i++) {

if (isalpha(infix[i]) || isdigit(infix[i]))

output += infix[i];

else if (infix[i] == '(')

char_stack.push('(');

else if (infix[i] == ')') {

while (char_stack.top() != '(') {

output += char_stack.top();

char_stack.pop();

char_stack.pop();

else {

if (isOperator(char_stack.top())) {

while (getPriority(infix[i])

<= getPriority(char_stack.top())) {

output += char_stack.top();

char_stack.pop();

}
char_stack.push(infix[i]);

return output;

string infixToPrefix(string infix)

int l = infix.size();

reverse(infix.begin(), infix.end());

for (int i = 0; i < l; i++) {

if (infix[i] == '(') {

infix[i] = ')';

i++;

else if (infix[i] == ')') {

infix[i] = '(';

i++;

string prefix = infixToPostfix(infix);

reverse(prefix.begin(), prefix.end());

return prefix;

int main()
{

string s;

cin>>s;

cout << infixToPrefix(s) <<endl;

return 0;

ANS 6:-

#include<bits/stdc++.h>

using namespace std;

float scanNum(char ch) {

int value;

value = ch;

return float(value-'0');

int isOperator(char ch) {

if(ch == '+'|| ch == '-'|| ch == '*'|| ch == '/' || ch == '^')

return 1;

return -1;

int isOperand(char ch) {

if(ch >= '0' && ch <= '9')

return 1;

return -1;

float operation(int a, int b, char op) {


if(op == '+')

return b+a;

else if(op == '-')

return b-a;

else if(op == '*')

return b*a;

else if(op == '/')

return b/a;

else if(op == '^')

return pow(b,a);

else

return INT_MIN;

float postfixEval(string postfix) {

int a, b;

stack<float> stk;

string::iterator it;

for(it=postfix.begin(); it!=postfix.end(); it++)

if(isOperator(*it) != -1)

a = stk.top();

stk.pop();

b = stk.top();

stk.pop();
stk.push(operation(a, b, *it));

else if(isOperand(*it) > 0)

stk.push(scanNum(*it));

return stk.top();

int main() {

string post;

cin>>post;

cout << "The result is: "<<postfixEval(post);

return 0;

ANS 7:-

#include<bits/stdc++.h>

using namespace std;

void move(int n, char src, char helper, char dest)

if(n==0)

return;

move(n-1,src,dest,helper);
cout<<"shift disk "<<n<<" from "<<src<<" to" <<dest<<endl;

move(n-1,helper,src,dest);

int main()

int n;

cout<<"enter the no of disc= ";

cin>>n;

move(n,'A','B','C');

You might also like