Barcha ro‘yxat elementlarini ekranga chop etuvchi funksiyani yozing


Download 30.43 Kb.
Sana06.04.2023
Hajmi30.43 Kb.
#1333105

Barcha ro‘yxat elementlarini ekranga chop etuvchi funksiyani yozing.
#include
#include
#include
using namespace std;
int main()
{
srand(time(NULL));
int n;
cout << "Ro'yhat elementlari soni: ";
cin >> n;
list a;
for (int j = 0; j < n; j++)
{
a.push_back(rand() % 50 - 20);
}
for (auto i = a.begin(); i != a.end(); i++)
{
cout << *i << " ";
}
_getwch();
return 0;
}


Navbat o‘rtasidagi element o‘chirib tashlansin. Agar navbat elementlari soni toq bo‘lsa, bitta element, aks holda ikkita element o‘chirilsin.
#include
#include
using namespace std;

void removeMiddle(queue &q)


{
int n = q.size();
if (n % 2 == 1)
{
for (int i = 0; i < n / 2; i++)
{
int front = q.front();
q.pop();
q.push(front);
}
q.pop();
}
else
{
for (int i = 0; i < n / 2 - 1; i++)
{
int front = q.front();
q.pop();
q.push(front);
}
q.pop();
int front = q.front();
q.pop();
q.push(front);
}
}

int main()


{
queue q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
removeMiddle(q);
while (!q.empty())
{
cout << q.front() << " ";
q.pop();
}
cout << endl;
return 0;
}


Download 30.43 Kb.

Do'stlaringiz bilan baham:




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