Привет, ребята, у меня есть небольшая проблема. Когда я вставляю числа в свой список, каждый из них появляется, кроме последнего, который я вставляю. Переменная count, которую я показываю, показывает, что на данный момент все 6 находятся там. Но когда я печатаю список, последний отсутствует. Любая помощь с оценкой. Время имеет существенное значение, поскольку оно должно быть в 11:00
Вставить функцию для упорядоченного списка:
template <class T>
void OListType<T>::insert (const T& item) {
NodeType<T> *curr=this->head, *prev=NULL;
while (curr!=NULL && curr->item<item) {
prev = curr;
curr = curr->next;
}
if (prev==NULL) {
this->head = new NodeType<T>;
this->head->item=item;
this->head->next=curr;
}
else{
prev->next=new NodeType<T>;
prev->next->item=item;
prev->next->next=curr;
}
++this->count;
}
Основная программа:
#include <iostream>
#include "ListType.h"
#include "UListType.h"
#include "OListType.h"
using namespace std;
int main() {
OListType<int> OList_int;
UListType<int> UList_int;
OList_int.insert(45);
OList_int.insert(100);
OList_int.insert(7);
OList_int.insert(83);
OList_int.insert(29);
OList_int.insert(49);
cout<< "The Ordered List size after inserts: ";
cout<< OList_int.size() << endl << endl;
cout << "The Ordered List values after inserts: ";
cout << OList_int << endl << endl;
return 0;
}
Код перегрузки Ostream:
template <class U>
std::ostream &operator<< (std::ostream &out, const ListType<U> &list) {
if (!list.empty()) {
NodeType<U> *temp=list.head->next;
out << list.head->item;
temp=temp->next;
while(temp!=NULL) {
out << "," << temp->item;
temp=temp->next;
}
}
return out;
}
Если вам нужно больше кода, я предоставил его.
template <class U>
std::ostream &operator<< (std::ostream &out, const ListType<U> &list) {
if (list.head) {
NodeType<U> *temp=list.head;
out << list.head->item;
temp=temp->next;
while(temp!=NULL) {
out << "," << temp->item;
temp=temp->next;
}
}
return out;
}
сделали следующее изменение NodeType * temp = list.head; :)