Dasturlash” fanidan 2-Laboratoriya ishi Tayyorladi: Ibrohimov Akmalxon toshkent 2023 Topshiriq


Download 126.7 Kb.
Sana04.05.2023
Hajmi126.7 Kb.
#1424632
Bog'liq
2-lab ishi - Jahongir Barotov



O’ZBEKISTON RESPUBLIKASI AXBOROT
TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI
RIVOJLANTIRISH VAZIRLIGI

MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT
AXBOROT TEXNOLOGIYALARI UNIVERSITETI
DASTURLASH” fanidan


2-Laboratoriya ishi
Tayyorladi: Ibrohimov Akmalxon

TOSHKENT 2023
Topshiriq

8-raqam
23. Stek elementlari teskari tartibda joylashtirib chiqilsin.
Dastur kodi:
#include
#include
using namespace std;


void reverseStack(stack &s) {
stack tempStack;
while (!s.empty()) {
int temp = s.top();
s.pop();
tempStack.push(temp);
}
s = tempStack;
}


void printStack(stack s) {
while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}
cout << endl;
}


int main() {
stack myStack;
myStack.push(1);
myStack.push(2);
myStack.push(3);
myStack.push(4);
myStack.push(5);
cout << "Original Stack: ";
printStack(myStack);


reverseStack(myStack);


cout << "Reversed Stack: ";
printStack(myStack);


return 0;
}
Natija:

34/9. Stek elementlari teskari tartibda joylashtirib chiqilsin.
Dastur kodi:
#include
using namespace std;


#define MAX_SIZE 100


class Stack {
private:
int top;
int arr[MAX_SIZE];
public:
Stack() {
top = -1;
}
bool isEmpty() {
return top == -1;
}
bool isFull() {
return top == MAX_SIZE - 1;
}
void push(int x) {
if(isFull()) {
cout << "Stack overflow error!" << endl;
return;
}
arr[++top] = x;
}
void pop() {
if(isEmpty()) {
cout << "Stack underflow error!" << endl;
return;
}
top--;
}
void display() {
if(isEmpty()) {
cout << "Stack is empty!" << endl;
return;
}
cout << "Stack elements: ";
for(int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
};


int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.display();
s.pop();
s.display();
s.push(50);
s.display();
return 0;
}
Natija:



Xulosa
C++ dasturlash tilida mavzu bilan tanishib chiqdim va topshiriqda berilgan masalalarni ishlab ko’nikmamni oshirdim.






Download 126.7 Kb.

Do'stlaringiz bilan baham:




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