DS Lab Assignment2
DS Lab Assignment2
SANSKRITI DEBNATH
06301032023
QUES 5
#include <iostream>
using namespace std;
class TwoStacks {
int *arr;
int size;
int top1, top2;
public:
TwoStacks(int n) {
size = n;
arr = new int[n];
top1 = -1;
top2 = n;
}
void push1(int x) {
if (top1 < top2 - 1) {
arr[++top1] = x;
} else {
cout << "Stack Overflow\n";
}
}
void push2(int x) {
if (top1 < top2 - 1) {
arr[--top2] = x;
} else {
cout << "Stack Overflow\n";
}
}
int pop1() {
if (top1 >= 0) {
int x = arr[top1--];
return x;
} else {
cout << "Stack Underflow\n";
return -1;
}
}
int pop2() {
if (top2 < size) {
int x = arr[top2++];
return x;
} else {
cout << "Stack Underflow\n";
return -1;
}
}
};
int main() {
TwoStacks ts(5);
ts.push1(5);
ts.push2(10);
ts.push2(15);
ts.push1(11);
ts.push2(7);
cout << "Popped element from stack1 is " << ts.pop1() << endl;
cout << "Popped element from stack2 is " << ts.pop2() << endl;
return 0;
}
OUTPUT
Popped element from stack1 is 11
Popped element from stack2 is 7
QUES 6
#include <stack>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
reverseString(str);
cout << "Reversed string: " << str << endl;
return 0;
}
OUTPUT
Enter a string: SANSKRITI DEBNATH
Reversed string: HTANBED ITIRKSNAS
Ques 7
#include <iostream>
using namespace std;
int main() {
int n, matrix[10][10];
cout << "Enter the size of the matrix: ";
cin >> n;
return 0;
}
OUTPUT
Enter the size of the matrix: 3
Enter the matrix elements:
123
456
789
Sum of upper triangular elements: 26