Navbatda birinchi va oxirgi elementlar o‘rni almashtirilsin


Download 51.95 Kb.
Sana05.05.2023
Hajmi51.95 Kb.
#1431041
Bog'liq
24NomerDedline2 (2)


24 Nomer 2 dedline
1 - Variant

  1. Navbatda birinchi va oxirgi elementlar o‘rni almashtirilsin

#include
#include

using namespace std;

void swap_first_last(queue& q) {
// Check if the queue is empty.
if (q.empty()) {
return;
}

// Store the first element in a temporary variable.


int first_element = q.front();

// Remove the first element from the queue.


q.pop();

// Enqueue the first element at the end of the queue.


q.push(first_element);

// Remove the last element from the queue.


q.pop();

// Enqueue the temporary variable at the beginning of the queue.


q.push(first_element);
}

int main() {


// Create a queue.
queue q;

// Enqueue some elements.


q.push(1);
q.push(2);
q.push(3);

// Swap the first and last elements.


swap_first_last(q);

// Print the queue.


while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
cout << endl;

return 0;


}

Download 51.95 Kb.

Do'stlaringiz bilan baham:




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