gpt4 book ai didi

c++ - 通过重载格式化链表输出 <<

转载 作者:行者123 更新时间:2023-11-28 08:10:05 25 4
gpt4 key购买 nike

我正在尝试格式化一个链表,以便它在每行上打印 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;
}

其余代码

列表.cpp

List::List()
{
node *head = NULL;
node *precurrent = NULL;
node *current = NULL;
int *temp = 0;
insert = 0;
search = 0;
remove = 0;
}

List::~List()
{
while (head != 0)
remove();
}

void List::insert(int insert)
{
if (head==null) \\If there is no list already, create a new head.
{
temp = new Node;
temp->data = insert;
head = temp;
}
else \\otherwise, insert the new node after current
{
temp = new Node;
temp->data = insert;
temp->next = current->next;
current->next = temp;
}
}

void List::search(int search)
{
current=head;
while (head->next != 0) //Cycle through the list, and if the number is found, say so
{
if (current->data = search)
cout<<"Number found."<<endl;
else
cout<<"Number not found."<<endl;
}
}

void List::remove(int remove)
{
if (head == null)
cout <<"Error. No List."<<endl;
else if (head->next == null)
{
num = head->data;
delete head;
head=null;
current=null;
}
else if (head == current)
{
temp = head->next;
num = head->data;
delete head;
head=temp;
current=temp;
}
else
{
temp = current->next;
num = current->data;
delete current;
precurrent->next = temp;
current = temp;
}
}

列表.h

//CLASS PROVIDED:  list                                 
//
// CONSTRUCTOR for the list class:
// list()
// Description: Constructor will initialize variables
// Preconditions: None
// Postcondition: int insert = ""
// int search = ""
// int remove = ""
// ~list()
// Description: Destructor destroys variables
// Preconditions: None
// Postcondition: variable deleted
//
// MEMBER FUNCTIONS for the list class
//
// string insert(int)
// Description: Inserts an integer into a linked list
// Precondition: none
// Postcondition: function returns Success/Error message.
//
// string search(int);
// Description: Searches for certain linked list member and returns int to set current variable
// Precondition: none
// Postcondition: function returns int
//
// string remove(int);
// Description: removes linked list member
// Precondition: user sends int to be deleted
// Postcondition: function returned string sddress
//
// void display(void);
// Description: displays entire linked list
// Precondition: none
// Postcondition: function returns screen output
//
// void quit(void);
// Description: closes program
// Precondition: none
// Postcondition: none
//


#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

class list
{
public:
//CONSTRUCTOR/DESTRUCTOR---------------------
list();
~list();

//GETS---------------------------------------
void insert(int);
string search(int);
string remove(int);
void display(void);
void quit(void);

private:

int insert;
int search;
int remove;


};




#endif

最佳答案

您需要将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

Type 也未定义(除非它在您尚未发布的代码中的某处)。如果您的 List 是一个模板,您还需要让您的运算符重载一个模板。

请初始化变量而不是声明它们然后分配给它们。这:

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;

关于c++ - 通过重载格式化链表输出 <<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9356055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com