• Предмет: Информатика
  • Автор: sharqwe
  • Вопрос задан 4 месяца назад

С++(Допоможіть будь ласка!!!)
Однозв'язні списки. Передбачити створення списку с  генерацією випадкових значень. Також обов'язково передбачити очищення  купи наприкінці програми! Написати програму з функціями, яка:
1) визначає порядковий номер тих елементів списку, які мають  максимальну величину;
2) упорядковує список за зростанням;
3) міняє місцями перший та останній елементи непорожнього списку Р.​

Ответы

Ответ дал: znaniainformarika
0

Ответ:

#include <iostream>

#include <cstdlib>

struct Node {

   int data;

   Node* next;

};

class LinkedList {

private:

   Node* head;

public:

   // Конструктор

   LinkedList() {

       head = nullptr;

   }

   // Деструктор

   ~LinkedList() {

       clear();

   }

   void addElement(int value) {

       Node* newNode = new Node;

       newNode->data = value;

       newNode->next = head;

       head = newNode;

   }

   void findMaxElements() {

       if (head == nullptr) {

           std::cout << "Список порожній" << std::endl;

           return;

       }

       int max = head->data;

       Node* current = head;

       int index = 0;

       std::cout << "Елементи з максимальним значенням: ";

       while (current != nullptr) {

           if (current->data > max) {

               max = current->data;

               std::cout << index << " ";

           }

           current = current->next;

           index++;

       }

       std::cout << std::endl;

   }

   void sortList() {

       if (head == nullptr || head->next == nullptr) {

           return;

       }

       Node* current = head;

       Node* nextNode = nullptr;

       int temp;

       while (current != nullptr) {

           nextNode = current->next;

           while (nextNode != nullptr) {

               if (current->data > nextNode->data) {

                   temp = current->data;

                   current->data = nextNode->data;

                   nextNode->data = temp;

               }

               nextNode = nextNode->next;

           }

           current = current->next;

       }

   }

   void swapFirstAndLast() {

       if (head == nullptr || head->next == nullptr) {

           return;

       }

       Node* current = head;

       Node* previous = nullptr;

       while (current->next != nullptr) {

           previous = current;

           current = current->next;

       }

       int temp = head->data;

       head->data = current->data;

       current->data = temp;

   }

   void clear() {

       Node* current = head;

       Node* nextNode = nullptr;

       while (current != nullptr) {

           nextNode = current->next;

           delete current;

           current = nextNode;

       }

       head = nullptr;

   }

   void displayList() {

       Node* current = head;

       while (current != nullptr) {

           std::cout << current->data << " ";

           current = current->next;

       }

       std::cout << std::endl;

   }

};

int main() {

   LinkedList list;

   srand(time(nullptr));

   for (int i = 0; i < 10; i++) {

       int value = rand() % 100;

       list.addElement(value);

   }

   std::cout << "Початковий список: ";

   list.displayList();

   list.findMaxElements();

   list.sortList();

   std::cout << "Відсортований список: ";

   list.displayList();

   list.swapFirstAndLast();

   std::cout << "Список після заміни першого та останнього елементів: ";

   list.displayList();

   list.clear();

   return 0;

}

Объяснение:


sharqwe: он не правильно работает
Вас заинтересует