Muhammad al-Xorazmiy nomidagi Toshkent Axborot Texnalogiyalari Universiteti Radio va Mobil aloqa fakulteti 810_20-guruh talabasi
Sanoyev Lazizbekning Ma’lumotlar tuzilmasi va Algoritmlar fanidan
Labaratoriya ishi_5
Guruh:810_20
Bajardi: Sanoyev Lazizbek
Tekshirdi: Bo’riyev Yusuf
15-Variant
1-topshiriq
Stekda7ta element mavjud.Mazkur stekni elementlardanbo’shatish dasturini tuzing.
/* C++ program to implement basic stack
operations */
#include using namespace std;
#define MAX 1000
class Stack {
int top;
public:
int a[MAX]; // Maximum size of Stack
Stack() { top = -1; }
bool push(int x);
int pop();
int peek();
bool isEmpty();
};
bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
a[++top] = x;
cout << x << " stekga surildi\n";
return true;
}
}
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::peek()
{
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
// Driver program to test above functions
int main()
{
class Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " Popped from stack\n";
//print all elements in stack :
cout<<"Stackda mavjud elementlar : ";
while(!s.isEmpty())
{
// print top element in stack
cout<// remove top element in stack
s.pop();
}
return 0;
}
2-topshiriq
Берилган рўйхатдан охирги элементлари олиниб кейинги файлга узатилсин.
#include
#include #include using namespace std;
//function for printing the elements in a list
void showlist(list g)
{
list :: iterator it;
for(it = g.begin(); it != g.end(); ++it)
cout << '\t' << *it;
cout << '\n';
}
int main()
{
list gqlist1, gqlist2;
for (int i = 0; i < 10; ++i)
{
gqlist1.push_back(i * 2);
gqlist2.push_front(i * 3);
}
cout << "\n1-roʻyxat (gqlist1) bu : ";
showlist(gqlist1);
cout << "\n2-roʻyxat (gqlist1) bu: ";
showlist(gqlist2);
cout << "\ngqlist1.front(): " << gqlist1.front();
cout << "\ngqlist1.back() : " << gqlist1.back();
cout << "\ngqlist1.pop_front() : ";
gqlist1.pop_front();
showlist(gqlist1);
cout << "\ngqlist2.pop_back() : ";
gqlist2.pop_back();
showlist(gqlist2);
cout << "\ngqlist1.reverse() : ";
gqlist1.reverse();
showlist(gqlist1);
cout << "\ngqlist2.sort(): ";
gqlist2.sort();
showlist(gqlist2);
return 0;
}