Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- using namespace std;
- struct Node
- {
- double info;
- Node* next;
- };
- void printList(Node* head) {
- Node* current = head;
- while (current != NULL) {
- cout << current->info << " ";
- current = current->next;
- }
- cout << endl;
- }
- void createList(Node*& head, int n) {
- Node* newNode, * temp = NULL;
- for (int i = 0; i < n; i++) {
- newNode = new Node;
- cout << "Введите значение узла: ";
- cin >> newNode->info;
- newNode->next = NULL;
- if (head == NULL) {
- head = newNode;
- temp = head;
- }
- else {
- temp->next = newNode;
- temp = temp->next;
- }
- }
- }
- Node* getNode(Node* head, int index) {
- Node* elem = head;
- int i = 0;
- while (i < index && elem != NULL) {
- elem = elem->next;
- i++;
- }
- return elem;
- }
- void insert2NodesBeforeLast(Node*& head, double firstValue, double secondValue) {
- Node* first, * second;
- first = new Node;
- second = new Node;
- first->next = second;
- second->next = NULL;
- Node* elem = head;
- if (head == NULL) {
- first->info = firstValue;
- second->info = secondValue;
- head = first;
- }
- else {
- while (elem->next != NULL) {
- elem = elem->next;
- }
- elem->next = first;
- double temp = elem->info;
- elem->info = firstValue;
- first->info = secondValue;
- second->info = temp;
- }
- }
- void delFirstNegative(Node*& head) {
- Node* elem = head;
- int i = 0;
- int indexOfNegative;
- while (elem != NULL) {
- if (elem->info < 0) {
- if (elem == head) {
- head = head->next;
- return;
- }
- indexOfNegative = i;
- getNode(head, indexOfNegative - 1)->next = getNode(head, indexOfNegative + 1);
- return;
- }
- elem = elem->next;
- i++;
- }
- }
- void moveMaxToEnd(Node*& head) {
- Node* elem = head;
- Node* max = head;
- int i = 0;
- int indexOfMax = 0;
- while (elem->next != NULL) {
- if (elem->info > max->info) {
- max = elem;
- indexOfMax = i;
- }
- elem = elem->next;
- i++;
- }
- if (elem->info > max->info) {
- max = elem;
- indexOfMax = i;
- }
- if (max->next != NULL) {
- if (max == head) {
- head = head->next;
- }
- elem->next = max;
- getNode(head, indexOfMax - 1)->next = getNode(head, indexOfMax + 1);
- max->next = NULL;
- }
- }
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- Node* head = NULL;
- int size;
- cout << "Введите длину списка для создания: ";
- cin >> size;
- createList(head, size);
- cout << "Получившийся список:" << endl;
- printList(head);
- double firstValue, secondValue;
- cout << "Введите 2 значения, которые нужно вставить перед последним элементом: ";
- cin >> firstValue >> secondValue;
- insert2NodesBeforeLast(head, firstValue, secondValue);
- cout << "Список с добавленными элементами:" << endl;
- printList(head);
- delFirstNegative(head);
- cout << "Список после удаления первого отрицательного элемента:" << endl;
- printList(head);
- moveMaxToEnd(head);
- cout << "Список после перемещения максимального элемента в конец:" << endl;
- printList(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement