C++ 通过重载格式化链表输出<&书信电报;

C++ 通过重载格式化链表输出<&书信电报;,c++,formatting,linked-list,operator-overloading,C++,Formatting,Linked List,Operator Overloading,我正在尝试设置一个链表的格式,以便它在每行上打印5个节点。我不知道如何做到这一点,因为我是新的经营者超载。这是我尝试过的,但我陷入了一种陈规,似乎无法理解这个概念 ostream &operator<<(ostream &os, List &s){ nodeType<Type>* current = s.head; int i = 0; while (current != NULL) //while mor

我正在尝试设置一个链表的格式,以便它在每行上打印5个节点。我不知道如何做到这一点,因为我是新的经营者超载。这是我尝试过的,但我陷入了一种陈规,似乎无法理解这个概念

     ostream &operator<<(ostream &os, List &s){ 

     nodeType<Type>* current = s.head; 
     int i = 0; 

 while (current != NULL) //while more data to print 
 { 
 os << current->info << " "; 
 current = current->link; 

 ++i; 

 if (i % 5 == 0) { 
     cout << '\n'; 
     i = 0; 
 } 
 } 

os << '\n'; // print the ending newline 

       return os; 
     }
ostream&operatornext;
当前->下一步=温度;
}
}
无效列表::搜索(整数搜索)
{
电流=水头;
while(head->next!=0)//在列表中循环,如果找到了数字,就这样说
{
如果(当前->数据=搜索)

cout您需要将
current
设置为
s.head
,而不仅仅是
head
,后者没有定义,因为此非成员运算符重载(顾名思义)不是成员

您也完全错误地推进了指针;您应该在每次迭代中打印一个
info
,如下所示:

编辑:如果要每行打印5,请执行以下操作:

int i = 0;

while (current != NULL) //while more data to print
{
     os << current->info << " ";
     current = current->link;

     if (i % 5 == 0) {
         cout << '\n';
         i = 0;
     } else
         ++i;
}

os << '\n'; // print the ending newline
应该是

nodeType<Type>* current = s.head;
nodeType*current=s.head;

@dtturner12这没有帮助,因为我不知道我的答案有什么问题。谢谢你的回答。我现在对推进和打印有了更好的理解。但是我该如何将其打印到5个节点部分?我希望列表打印如下12 3 4 5“新线”6 7 8 9 10
int i = 0;

while (current != NULL) //while more data to print
{
     os << current->info << " ";
     current = current->link;

     if (i % 5 == 0) {
         cout << '\n';
         i = 0;
     } else
         ++i;
}

os << '\n'; // print the ending newline
nodeType<Type> *current; //pointer to traverse the list
current = head; //set current so that it points to the first node
nodeType<Type>* current = s.head;