【问题标题】:Print Function Linked Lists C++打印函数链表 C++
【发布时间】:2014-02-19 22:13:09
【问题描述】:

我目前正在学习 C++ 中的链接列表,但我无法编写打印出列表元素的打印函数;我的意思是我写了这个函数,但它不能正常工作。

#include <iostream>

using namespace std;

struct node
{
    char name[20];
    node* next;
};

node* addNewPerson(node* head)
{
    node* person = new node;

    cout << "Name: ";
    cin >> person->name;

    person->next = NULL;

    if (head == NULL) //is empty
    {
        head = person;
    }

    else
    {
        person = person->next;
    }

    return head;
}

void printList(node* head)
{
    node* temp = head;

    cout << temp->name << endl;
}

int main()
{
    node* head = NULL;

    node* temp = head;

    unsigned short people = 0;

    cout << "How many people do you want to invite to the party?" << endl;
    cout << "Answer: ";
    cin >> people;

    cout << endl;

    for (unsigned short i = 1; i <= people; i++)
    {
        addNewPerson(head);

        cout << endl;
    }

    cout << "LIST: " << endl;
    cout << endl;

    while (temp != NULL)
    {
        printList(temp);
        temp = temp->next;
    }

    cin.get();
}

我想知道我做错了什么,请帮助我!

【问题讨论】:

  • '但它不能正常工作。''我想知道我做错了什么' 模糊地诊断你的问题,对不起!请编辑您的问题,而不是试图用 cmets 澄清...

标签: c++ function printing linked-list


【解决方案1】:

很明显addNewPerson函数是错误的。

node* addNewPerson(node* head)
{
    node* person = new node;

    cout << "Name: ";
    cin >> person->name;

    person->next = NULL;

    if (head == NULL) //is empty
    {
        head = person;
    }

    else
    {
        person = person->next;
    }

    return head;
}

你分配了新的节点人。

    node* person = new node;

将其字段设置为 NULL

    person->next = NULL;

然后如果 head 不等于 NULL 你将 person 设置为 person->next

        person = person->next;

由于 person->next 设置为 NULL,这意味着现在 person 也将等于 NULL。

此外,该函数返回可以在函数中更改的头部。但是,您忽略 main 中的返回值

addNewPerson(head);

至少应该有

head = addNewPerson(head);

有效的函数 addNewPerson 如下所示

node* addNewPerson(node* head)
{
    node* person = new node;

    cout << "Name: ";
    cin >> person->name;

    person->next = head;
    head = person;

    return head;
}

主要是你必须写

  for (unsigned short i = 1; i <= people; i++)
  {
    head = addNewPerson(head);

    cout << endl;
  }

函数 printList 不会输出整个列表。它只输出头节点的数据成员名称。

void printList(node* head)
{
    node* temp = head;

    cout << temp->name << endl;
}

它应该如下所示

void printList(node* head)
{
  for ( ; head; head = head->next )
  {
    cout << head->name << endl;
  }
}

最后 main 应该是这样的

int main()
{
    node* head = NULL;

    unsigned short people = 0;

    cout << "How many people do you want to invite to the party?" << endl;
    cout << "Answer: ";
    cin >> people;

    cout << endl;

    for ( unsigned short i = 0; i < people; i++ )
    {
        head = addNewPerson(head);

        cout << endl;
    }

    cout << "LIST: " << endl;
    cout << endl;

    printList( head );

    cin.get();
}

【讨论】:

  • 打印列表中的 temp 是什么?
  • @Damian 这是一个错字。:)
【解决方案2】:

在你的 addNewPerson 函数中 person->next 永远不会被设置,因为 head 总是 NULL。

【讨论】:

    【解决方案3】:

    addNewPerson 方法

    您的 addNewPerson 方法从未将新人添加到列表中,它只是将头部设置为新人。

    node* addNewPerson(node* head)
    {
      node* person = new node;
    
      cout << "Name: ";
      cin >> person->name;
    
      person->next = NULL;
    
      if (head->next == NULL) //is empty
      {
        head->next = person;
      }
      else
      {
        person->next = head->next;
        head->next = person;
      }
    
      return head;
    }
    

    printList 方法

    您的方法printList 应该按照它说的去做并打印列表,而不是一次只打印一个人。

    void printList(node* head)
    {
      node* tmp = head;
    
      while(tmp->next != NULL) {
        tmp = tmp->next;
        cout >> tmp->name >> endl;
      }
    }
    

    主要方法

    用新的 printList 方法更新了您的 main 方法。

    int main()
    {
      node* head = new node;
    
      unsigned short people = 0;
    
      cout << "How many people do you want to invite to the party?" << endl;
      cout << "Answer: ";
      cin >> people;
    
      cout << endl;
    
      for (unsigned short i = 1; i <= people; i++)
      {
        addNewPerson(head);
    
        cout << endl;
      }
    
      cout << "LIST: " << endl;
      cout << endl;
    
      cout << printList(head);
    
      cin.get();
    }
    

    另外你为什么使用unsigned shortchar 数组?你的内存有限吗?为什么不直接使用intstring

    【讨论】:

      【解决方案4】:

      当你添加一个新人时,它实际上并没有被放入链表,因为你从来没有调用过类似head -&gt; next = person的东西。

      addNewPerson 例程中的部分应该类似于

      if (head == NULL) {
          head = person;
      } else {
          node * end = head;
          while (end -> next != NULL) // find the end of the list to insert new person
              end = end -> next;
          end -> next = person;
      }
      

      而且一开始你不能只将head 传递给addNewPerson,因为它是一个空值指针,addNewPerson 方法按值接受指针(它指向的内存地址的值) 而不是对head 的引用。所以你需要使用

      node* addNewPerson(node* &head) { ... }
      

      相反。最后,当您声明temp 时,它首先也会收到head 的值,因此稍后访问temp 只会再次给您NULL。您需要将其更改为

      node* &temp = head;
      

      【讨论】:

        【解决方案5】:

        我认为问题在于如何将链接列表添加到 addNewPerson 中。

        addNewPerson 的输入是 'head',所以每次在向链表添加新节点之前,我们需要一直遍历链表直到最后一个节点,然后才能将 'person' 附加到链表。

        【讨论】:

          【解决方案6】:
          struct node
          {
              char name[20];
              node* next;
          };
          
          node* addNewPerson(node* head)
          {
              node* person = new node;
          
              //cout << "Name: ";
              cin >> person->name;
          
              person->next = head;
              head = person;
          
              return head;
          }
          
          void PrintLL(node *head)
          {
              while (head!=NULL){
                  cout<< head->name <<endl;
                  head=head->next;
              }
          
          }
          
          int main() {
          
              node* head = NULL;
              node* temp = head;
              int num_ppl;
              cout << "How many people do you want to invite to the party?" << endl;
              cout << "Answer: ";
              cin >> num_ppl;
          
              for(int arr_i = 0; arr_i < num_ppl; arr_i++){
          
                head = addNewPerson(head);
          
              }
              PrintLL(head);
              return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-01-21
            • 2021-02-15
            • 2016-06-15
            • 1970-01-01
            • 2019-05-14
            • 2014-05-25
            • 2014-05-02
            • 1970-01-01
            相关资源
            最近更新 更多