Tashkent University of Information Technologies named after al-Khwarizmi, Faculty of Information Security Laboratory work of Alisherov Muhiddin, a student of group 716-20


Download 1.65 Mb.
Sana17.06.2023
Hajmi1.65 Mb.
#1525748
Bog'liq
DSA lab2 Alisherov M.





Tashkent University of Information Technologies named after al-Khwarizmi, Faculty of Information Security

Laboratory work of Alisherov Muhiddin, a student of group 716-20, on the subject of data structure and algorithmization

laboratory work


2021-2022

TASK 1


  1. Write a program to perform matching of parenthesis using stack.

//Alisherov Muxiddin


#include
using namespace std;
bool areBracketsBalanced(string expr)
{
stack s;
char x;
string a;
cout<<"enter parentheses: ";
cin>>a;
for (int i = 0; i < expr.length(); i++) {
if (expr[i] == '(' || expr[i] == '['
|| expr[i] == '{') {
s.push(expr[i]);
continue;
}
if (s.empty())
return false;
switch (expr[i]) {
case ')':
x = s.top();
s.pop();
if (x == '{' || x == '[')
return false;
break;
case '}':
x = s.top();
s.pop();
if (x == '(' || x == '[')
return false;
break;
case ']':
x = s.top();
s.pop();
if (x == '(' || x == '{')
return false;
break; }
}
return (s.empty()); }
int main() {
string expr = "{()}[]";
if (areBracketsBalanced(expr))
cout << "Valid parentheses";
else
cout << " Invalid parentheses ";
return 0; }

TASK 2
5. Write a procedure for deleting the minimal element of the queue


// Alisherov Muxiddin
#include
using namespace std;
void remove(int t, queue& q)
{ queue ref;
int s = q.size();
int cnt = 0;
while (q.front() != t and !q.empty()) {
ref.push(q.front());
q.pop();
cnt++;
}
if (q.empty()) {
cout << "element not found!!" << endl;
while (!ref.empty()) {
q.push(ref.front());
ref.pop(); }
} else {
q.pop();
while (!ref.empty()) {
q.push(ref.front());
ref.pop(); }
int k = s - cnt - 1;
while (k--) {
int p = q.front();
q.pop();
q.push(p);} }
}
void print(queue qr)
{ while (!qr.empty()) {
cout << qr.front() << " ";
qr.pop();}
cout << endl;
} int main()
{ queue q;
q.push(10);
q.push(20);
q.push(30);
q.push(40);
q.push(50);
q.push(60);
print(q);
remove (40, q);
print(q);
remove (30, q);
print(q);
remove (13, q);
print(q);
return 0;
}
TASK 3 5. Write a procedure of permutation of the maximal and minimal elements of the stack
// Alisherov Muxiddin
#include
using namespace std;
class StackWithMax
{ stack mainStack;
stack trackStack;
public:
void push(int x)
{ mainStack.push(x);
if (mainStack.size() == 1)
{ trackStack.push(x);
return;
}
if (x > trackStack.top())
trackStack.push(x);
else
trackStack.push(trackStack.top());
}
int getMax()
{
return trackStack.top();
}
int pop()
{
mainStack.pop();
trackStack.pop();
}
};
int main()
{ StackWithMax s;
s.push(5);
cout << s.getMax() << endl;
s.push(4);
cout << s.getMax() << endl;
s.push(13);
cout << s.getMax() << endl;
s.push(10);
cout << s.getMax() << endl;
s.push(19);
cout << s.getMax() << endl;
s.push(25);
cout << s.getMax() << endl;
return 0;
}



conclusions about the work :


Well, actually The purpose is to help the programmer navigate through the code and also spot any improper matching, which would cause the program to not compile or malfunction. If a closing bracket is left out, for instance, the compiler will not know that the end of a block of code has been reached.
In the second work, I created a program that deletes the specified number in the queue, and in it I write that the following numbers 10 20 30 40 50 60 are in the queue, from which the numbers 30, 40 and 13 are deleted. then the sequence 10 20 50 60 appears in the black window as shown in the screenshot, and at the end the text "item not found" appears because the queue given to us is not 13(screenshot 2).
In mathematics, especially in order theory, the maximum element of a pre-ordered set S is an element S that is not smaller than the other elements of the set S. An element of S that is not larger than the other elements.
In this work, too, I entered the numbers 5 4 13 10 19 25, and the program then displays the maximum element on the black screen by comparing the maximum element with the smaller one. this is why the following numbers appear on the black screen.(screenshot 3).

Alisherov Muxiddin Xusniddin o’g’li





Download 1.65 Mb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling