DS Lab 10
DS Lab 10
#include <bits/stdc++.h>
class stackss
public:
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else
top++;
stack[top]=val;
void pop()
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else
top--;}
}
void display()
if(top>=0)
cout<<stack[i]<<" ";
cout<<endl;
else
cout<<"Stack is empty";
};
int main()
stackss s;
cout<<"4) Exit"<<endl;
do {
cin>>ch;
switch(ch) {
case 1: {
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>
class Node
public:
int data;
Node *next;
};
newnode->data = val;
newnode->next = top;
top = newnode;
void pop(Node*&top)
if(top==NULL)
cout<<"Stack Underflow"<<endl;
else
top = top->next;
}
}
void display(Node*top)
Node* ptr;
if(top==NULL)
cout<<"stack is empty";
else
ptr = top;
ptr = ptr->next;
cout<<endl;
int main()
do {
cin>>ch;
switch(ch) {
case 1: {
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>
bool isOperand(char x)
stack<string> s;
if (isOperand(exp[i]))
s.push(op);
else
s.pop();
op1 + ")");
return s.top();
int main()
string exp ;
cin>>exp;
return 0;
ANS 4:-
#include<bits/stdc++.h>
int prec(char c)
if(c == '^')
return 3;
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;
ns+=s[i];
st.push('(');
char c = st.top();
st.pop();
ns += c;
}
if(st.top() == '(')
char c = st.top();
st.pop();
else{
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;
int main()
string exp;
cin>>exp;
infixToPostfix(exp);
return 0;
ANS 5:-
#include <bits/stdc++.h>
bool isOperator(char c)
int getPriority(char C)
if (C == '-' || C == '+')
return 1;
return 2;
else if (C == '^')
return 3;
return 0;
}
string infixToPostfix(string infix)
int l = infix.size();
stack<char> char_stack;
string output;
if (isalpha(infix[i]) || isdigit(infix[i]))
output += infix[i];
char_stack.push('(');
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;
int l = infix.size();
reverse(infix.begin(), infix.end());
if (infix[i] == '(') {
infix[i] = ')';
i++;
infix[i] = '(';
i++;
reverse(prefix.begin(), prefix.end());
return prefix;
int main()
{
string s;
cin>>s;
return 0;
ANS 6:-
#include<bits/stdc++.h>
int value;
value = ch;
return float(value-'0');
return 1;
return -1;
return 1;
return -1;
return b+a;
return b-a;
return b*a;
return b/a;
return pow(b,a);
else
return INT_MIN;
int a, b;
stack<float> stk;
string::iterator it;
if(isOperator(*it) != -1)
a = stk.top();
stk.pop();
b = stk.top();
stk.pop();
stk.push(operation(a, b, *it));
stk.push(scanNum(*it));
return stk.top();
int main() {
string post;
cin>>post;
return 0;
ANS 7:-
#include<bits/stdc++.h>
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;
cin>>n;
move(n,'A','B','C');