0% found this document useful (0 votes)
5 views3 pages

DS Lab Assignment2

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)
5 views3 pages

DS Lab Assignment2

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/ 3

DS LAB ASSIGNMENT

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;

void reverseString(string &str) {


stack<char> s;

for (int i = 0; i < str.length(); i++) {


s.push(str[i]);
}

for (int i = 0; i < str.length(); i++) {


str[i] = s.top();
s.pop();
}
}

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 sumUpperTriangular(int matrix[10][10], int n) {


int sum = 0;

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


for (int j = i; j < n; j++) {
sum += matrix[i][j];
}
}
return sum;
}

int main() {
int n, matrix[10][10];
cout << "Enter the size of the matrix: ";
cin >> n;

cout << "Enter the matrix elements:\n";


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}

int sum = sumUpperTriangular(matrix, n);


cout << "Sum of upper triangular elements: " << sum << endl;

return 0;
}
OUTPUT
Enter the size of the matrix: 3
Enter the matrix elements:
123
456
789
Sum of upper triangular elements: 26

You might also like