Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node {
- node* next;
- int info;
- };
- void createQueue(node*& Begin, node*& End, int n) {
- for (int i = 0; i < n; i++) {
- node* el = new node;
- el->info = rand() % 101 - 50;
- cout << el->info << " ";
- el->next = NULL;
- if (End == nullptr) {
- Begin = End = el;
- }
- else {
- End->next = el;
- End = el;
- }
- }
- }
- bool isEmpty(node* Begin) {
- if (Begin == nullptr)
- cout << "Очередь пуста\n";
- return Begin == nullptr;
- }
- void queueList(node* Begin) {
- if (isEmpty(Begin))
- return;
- node* temp = Begin;
- while (temp != nullptr) {
- cout << temp->info << " ";
- temp = temp->next;
- }
- }
- void pop(node*& Begin) {
- if (isEmpty(Begin))
- return;
- node* temp = Begin;
- temp = Begin;
- Begin = Begin->next;
- delete temp;
- }
- int peek(node* Begin) {
- if (isEmpty(Begin))
- return -1;
- return Begin->info;
- }
- int findMax(node* Begin) {
- if (isEmpty(Begin))
- return INT_MIN;
- int maxElement = Begin->info;
- node* temp = Begin->next;
- while (temp != nullptr) {
- if (temp->info > maxElement)
- maxElement = temp->info;
- temp = temp->next;
- }
- return maxElement;
- }
- void moveMaxToFront(node*& Begin) {
- if (isEmpty(Begin))
- return;
- int maxElement = findMax(Begin);
- if (Begin->info == maxElement)
- return;
- node* prevMax = nullptr;
- node* temp = Begin;
- while (temp->info != maxElement) {
- prevMax = temp;
- temp = temp->next;
- }
- prevMax->next = temp->next;
- temp->next = Begin;
- Begin = temp;
- }
- int main() {
- srand((unsigned)(time(NULL)));
- setlocale(LC_ALL, "Rus");
- int n;
- node* Begin = nullptr, * End = nullptr;
- cout << "Введите количество узлов: ";
- cin >> n;
- createQueue(Begin, End, n);
- cout << endl << endl;
- queueList(Begin);
- moveMaxToFront(Begin);
- cout << "Очередь после перемещения максимального элемента в начало: ";
- queueList(Begin);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment