2012-02-20 104 views
0

我想格式化鏈接列表,以便每行打印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; 
    } 

其他地區代碼

list.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; 
} 
} 

list.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 

回答

1

您需要將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; 
+0

我編輯添加我的代碼 – dtturner12 2012-02-20 04:29:15

+0

休息@ dtturner12沒有幫助,因爲我不知道這有什麼錯我的回答你。 – 2012-02-20 04:31:01

+0

謝謝你的回答。我現在理解推進和打印好多了。但是,我會如何將它打印到5個節點部分?我想列表打印這樣的東西1 2 3 4 5「newline」6 7 8 9 10 – dtturner12 2012-02-20 04:34:50