Задача состоит в том, чтобы поменять максимальный и минимальный элемент однонаправленной очереди на языке с++. Минимальное значение меняется на максимум, а максимум на минимум не меняется. Что не так сделал?
#include <string>
#include <stdlib.h>
#include <iostream>
#include <ctime>
using namespace std;
struct Spis {
int data;
Spis* nextNode;
};
void viewSpis(Spis* head) {
Spis* temp = head;
int counter = 0;
while (temp != NULL) {
cout << counter + 1 << "-ый элемент : " << temp->data << endl;
temp = temp->nextNode;
counter++;
}
}
Spis* pushBack(Spis* head, int data) {
if (head == NULL) {
head = new Spis;
head->data = data;
head->nextNode = NULL;
return head;
}
Spis* tempPtr = head;
while (tempPtr->nextNode != NULL) {
tempPtr = tempPtr->nextNode;
}
Spis* newElement = new Spis;
newElement->data = data;
newElement->nextNode = NULL;
tempPtr->nextNode = newElement;
return head;
}
void change(Spis* head) {
Spis* tempPtr = head;
int headValue = head->data;
int minValue = tempPtr->data;
int maxValue = tempPtr->data;
//Прошлись по всему листу и нашли минмальное значение
while (tempPtr) {
if (tempPtr->data < minValue) {
minValue = tempPtr->data;
}
if (tempPtr->data > maxValue) {
maxValue = tempPtr->data;
}
tempPtr = tempPtr->nextNode;
}
cout << "Минимум: " << minValue << endl;
cout << "Максимум: " << maxValue << endl;
tempPtr = head->nextNode;
while (tempPtr) {
if (tempPtr->data == maxValue) {
tempPtr->data = minValue;
}if (tempPtr->data == minValue) {
tempPtr->data = maxValue;
}
tempPtr = tempPtr->nextNode;
}
}
int main()
{
setlocale(0,"");
srand(time(NULL));
int size = 5;
Spis* head =nullptr;
for (int i = 0; i < size; i++) {
head = pushBack(head, rand() % 100 - 50);
}
viewSpis(head);
change(head);
cout <<"==================" << endl;
viewSpis(head);
return 0;
}