C++ 使用模板插入函数

C++ 使用模板插入函数,c++,class,templates,operator-overloading,C++,Class,Templates,Operator Overloading,嘿,伙计们,我只是有个小问题。当我将数字插入我的有序列表时,除了我插入的最后一个数字外,所有数字都会出现。我的count变量显示,目前,所有6个都在那里。但是当我打印列表时,最后一个就不见了。对您的任何帮助都将不胜感激。时间至关重要,因为它在晚上11点到期 为有序列表插入函数: template <class T> void OListType<T>::insert (const T& item) { NodeType<T> *curr=this-&

嘿,伙计们,我只是有个小问题。当我将数字插入我的有序列表时,除了我插入的最后一个数字外,所有数字都会出现。我的count变量显示,目前,所有6个都在那里。但是当我打印列表时,最后一个就不见了。对您的任何帮助都将不胜感激。时间至关重要,因为它在晚上11点到期

为有序列表插入函数:

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;
}
模板
void OListType::insert(常量和项目){
节点类型*curr=this->head,*prev=NULL;
while(curr!=NULL&&curr->itemnext;
}
if(prev==NULL){
此->头=新节点类型;
此->标题->项目=项目;
本->头->下一个=当前;
}
否则{
prev->next=新节点类型;
上一步->下一步->项目=项目;
上一个->下一个->下一个=当前;
}
++这个->计数;
}
主程序:

#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;
}
#包括
#包括“ListType.h”
#包括“UListType.h”
#包括“OListType.h”
使用名称空间std;
int main(){
寡头型寡头;
UListType UList_int;
奥林匹克国际插页(45);
整版插入(100);
奥林匹克国际插页(7);
奥林匹克国际插页(83);
奥林匹克国际插页(29);
奥林匹克国际插页(49);
cout
模板
标准:ostream&operatornext;
while(temp!=NULL){
下一步;
}
}
返回;
}

已经对NodeType*temp=list.head;:)进行了以下更改。

您的insert()似乎很好。你能加上你的“接线员”吗?好的,我加上了。告诉我你的想法
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;
}