2013-11-17 4 views
0

У меня есть следующий код, но он дает ошибку в строке - e = list.first(); (в конце кода). В нем говорится, что он не может преобразовать Node в char, может кто-то сказать мне, как вернуть первое значение из связанного списка в переменной e.C++ возвращает одно значение из связанного списка

Спасибо за помощь :)

// 
// main.cpp 
// cprogram1 
// 

#include <iostream> 

using namespace std; 
class Node { 
    char data; 
    Node* next; 

public: 
    Node() {}; 
    void SetData(int aData) { data = aData; }; 
    void SetNext(Node* aNext) { next = aNext; }; 
    char Data() { return data; }; 
    Node* Next() { return next; }; 
}; 

// List class 
class List { 
    Node *head; 
public: 
    List() { head = NULL; }; 
    void Print(); 
    void Append(int data); 
    void Delete(int data); 
    Node * First() const; 
}; 

/** 
* Print the contents of the list 
*/ 
void List::Print() { 

    // Temp pointer 
    Node *tmp = head; 

    // No nodes 
    if (tmp == NULL) { 
     cout << "EMPTY" << endl; 
     return; 
    } 

    // One node in the list 
    if (tmp->Next() == NULL) { 
     cout << tmp->Data(); 
     cout << " --> "; 
     cout << "NULL" << endl; 
    } 
    else { 
     // Parse and print the list 
     do { 
      cout << tmp->Data(); 
      cout << " --> "; 
      tmp = tmp->Next(); 
     } 
     while (tmp != NULL); 

     cout << "NULL" << endl; 
    } 
} 

/** 
* Append a node to the linked list 
*/ 
void List::Append(int data) { 

    // Create a new node 
    Node* newNode = new Node(); 
    newNode->SetData(data); 
    newNode->SetNext(NULL); 

    // Create a temp pointer 
    Node *tmp = head; 

    if (tmp != NULL) { 
     // Nodes already present in the list 
     // Parse to end of list 
     while (tmp->Next() != NULL) { 
      tmp = tmp->Next(); 
     } 

     // Point the last node to the new node 
     tmp->SetNext(newNode); 
    } 
    else { 
     // First node in the list 
     head = newNode; 
    } 
} 

/** 
* Delete a node from the list 
*/ 
void List::Delete(int data) { 

    // Create a temp pointer 
    Node *tmp = head; 

    // No nodes 
    if (tmp == NULL) 
     return; 

    // Last node of the list 
    if (tmp->Next() == NULL) { 
     delete tmp; 
     head = NULL; 
    } 
    else { 
     // Parse thru the nodes 
     Node *prev; 
     do { 
      if (tmp->Data() == data) break; 
      prev = tmp; 
      tmp = tmp->Next(); 
     } while (tmp != NULL); 

     // Adjust the pointers 
     prev->SetNext(tmp->Next()); 

     // Delete the current node 
     delete tmp; 

    } 
} 

Node * List::First() const { 
    Node *tmp = head; 
    return head; 
} 

int main() 
{ 
    char c; 
    int t = 0; 
    char e; 
    List list; 
    while(t==0) 
    { 
     cout << "Please enter your command"; 
     cin >> c; 
     if(c=='c') 
     { 
      cout << "You will need to enter 6 letters, one after the other"; 
      cout << "Please enter the first letter"; 
      cin >> e; 
      list.Append(e); 
      cout << "Please enter the second letter"; 
      cin >> e; 
      list.Append(e); 
      cout << "Please enter the third letter"; 
      cin >> e; 
      list.Append(e); 
      cout << "Please enter the fourth letter"; 
      cin >> e; 
      list.Append(e); 
      cout << "Please enter the fifth letter"; 
      cin >> e; 
      list.Append(e); 
      cout << "Please enter the sixth letter"; 
      cin >> e; 
      list.Append(e); 
      list.Print(); 
      list.Delete('b'); 
      list.Print(); 
      e = list.First(); 


     } 
    } 
} 

ответ

1

Давайте посмотрим на вашу программу: tested here:

#include <iostream> 

using namespace std; 

class Node { 
    char data; 
    Node* next; 

public: 
    Node() {}; 
    void SetData(int aData) { data = aData; }; 
    void SetNext(Node* aNext) { next = aNext; }; 
    char Data() { return data; }; 
    Node* Next() { return next; }; 
}; 

// List class 
class List { 
    Node *head; 
public: 
    List() { head = NULL; }; 
    void Print(); 
    void Append(int data); 
    void Delete(int data); 
    Node * First() const; 
    }; 

    /** 
    * Print the contents of the list 
    */ 
    void List::Print() { 

    // Temp pointer 
    Node *tmp = head; 

    // No nodes 
    if (tmp == NULL) { 
     cout << "EMPTY" << endl; 
    return; 
    } 

    // One node in the list 
    if (tmp->Next() == NULL) { 
    cout << tmp->Data(); 
    cout << " --> "; 
    cout << "NULL" << endl; 
    } 
    else { 
    // Parse and print the list 
    do { 
    cout << tmp->Data(); 
    cout << " --> "; 
    tmp = tmp->Next(); 
    } 
    while (tmp != NULL); 

    cout << "NULL" << endl; 
    } 
} 

/** 
* Append a node to the linked list 
*/ 
void List::Append(int data) { 

    // Create a new node 
    Node* newNode = new Node(); 
    newNode->SetData(data); 
    newNode->SetNext(NULL); 

    // Create a temp pointer 
    Node *tmp = head; 

    if (tmp != NULL) { 
     // Nodes already present in the list 
     // Parse to end of list 
     while (tmp->Next() != NULL) { 
      tmp = tmp->Next(); 
     } 

     // Point the last node to the new node 
     tmp->SetNext(newNode); 
    } 
    else { 
     // First node in the list 
     head = newNode; 
    } 
} 

/** 
* Delete a node from the list 
*/ 
void List::Delete(int data) { 

    // Create a temp pointer 
    Node *tmp = head; 

    // No nodes 
    if (tmp == NULL) 
     return; 

    // Last node of the list 
    if (tmp->Next() == NULL) { 
     delete tmp; 
     head = NULL; 
    } 
    else { 
     // Parse thru the nodes 
     Node *prev; 
     do { 
      if (tmp->Data() == data) break; 
      prev = tmp; 
      tmp = tmp->Next(); 
     } while (tmp != NULL); 

     // Adjust the pointers 
     prev->SetNext(tmp->Next()); 

     // Delete the current node 
     delete tmp; 

     } 
} 

Node * List::First() const { 
    Node *tmp = head; 
    return head; 
} 

int main() 
{ 
    char c; 
    int t = 0; 
    char e; 
    List list; 
    while(t==0) 
    { 
     cout << "Please enter your command"; 

     c = 'c'; 
     //cin >> c; 
     if(c=='c') 
    { 
    cout << "You will need to enter 6 letters, one after the other"; 
    cout << "Please enter the first letter"; 

    list.Append('e'); 
    cout << "Please enter the second letter"; 
    //cin >> e; 
    list.Append('a'); 
    cout << "Please enter the third letter"; 
    //cin >> e; 
    list.Append('b'); 
    cout << "Please enter the fourth letter"; 
    //cin >> e; 
    list.Append('f'); 
    cout << "Please enter the fifth letter"; 
    //cin >> e; 
    list.Append('h'); 
    cout << "Please enter the sixth letter"; 
    //cin >> e; 
    list.Append(e); 
    list.Print(); 
    list.Delete('b'); 
    list.Print(); 
    e = list.First()->Data(); 
    t=1; 
} 
} 

} 

Как вы можете видеть, e = list.First() возвращает указатель на Node, а не char. Вы внедрили функцию, возвращающую char, которая равна Data(). Таким образом, вы должны использовать e = list.First()->Data();.

Кроме того, ваша петля бесконечна, потому что t всегда 0. Я просто положил t=1, чтобы остановить ваш цикл и увидеть результат. (если вы используете этот код, не забудьте расколоть cin и удалите c = 'c')

Надеюсь, что вам поможет.

1

Когда я запускаю свой код, я получаю ошибку:

main.cpp: error: assigning to 'char' from incompatible type 'Node *' 
      e = list.First(); 
      ^~~~~~~~~~~~~ 

который я думаю, это объясняет.

Смежные вопросы