2012-02-20 4 views
0

リンクリストを書式設定して各行に5ノードを印刷しようとしています。私は演算子のオーバーロードに慣れていないので、これを行う方法がわかりません。ここで私が試みたものですが、イムはマンネリで立ち往生し、コンセプトコードオーバーロードによるリンクリスト出力の書式設定<<

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

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

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

休憩を把握するように見えるカント

答えて

1

currentからs.headまで設定する必要があります。headではなく、このメンバー以外の演算子のオーバーロードは(その名前が示すように)メンバーではないため、定義されていません。

また、ポインタを完全に間違って進めています。

EDIT:あなたはこのような各反復で1 infoを印刷する必要がありますが、行ごとに5を印刷したい場合は、次の操作を行います。またType

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 

定義されていません(それは、コードあなたの避難所のどこかでない限り」)。 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 @私のコード – dtturner12

+0

の残りの部分を追加して編集しなければなりません。 –

+0

あなたの答えをありがとう。私は前進と印刷が今よりずっと良くなると理解しています。しかし、どのように私は5つのノードのセクションに印刷について行くだろうか?私はリストをこのようなものにしたいと思う1 2 3 4 5 "newline" 6 7 8 9 10 – dtturner12