2012-02-20 2 views
0

각 줄마다 5 개의 노드를 인쇄 할 수 있도록 링크 된 목록의 서식을 지정하려고합니다. 내가 연산자 오버로딩을 처음 접했을 때 어떻게해야할지 모르겠습니다. 다음은 틀에 박힌 생활에 갇혀 내가 시도 뭔가하지만 메신저이며, 캔트 list.h에게 개념오버로드로 연결된 목록 출력 서식 지정 <<

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

코드

의에게

//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

에서 s.head까지 설정해야하며 head이 아니라, 이 멤버가 아닌 연산자 오버로드는 (이름에서 알 수 있듯이) 멤버가 아니기 때문에 정의되지 않았습니다.

포인터를 완전히 앞으로 나아가고 있습니다.

편집 :이 같은 각 반복에 하나 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

관련 문제