c ++从链表返回单个值

时间:2013-11-17 01:02:28

标签: c++ linked-list

我有以下代码,但它在行上给出错误 - e = list.first();(在代码的末尾)。它说它无法将Node转换为char,有人可以告诉我如何从变量e中的链表返回第一个值。

感谢所有帮助:)

//
//  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();


        }
    }
}

3 个答案:

答案 0 :(得分:1)

当我运行你的代码时,我收到错误:

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

我认为解释它。

答案 1 :(得分:1)

试试这个:

e = list.First()->Data();

答案 2 :(得分: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'

希望对你有所帮助。