【问题标题】:Creating a C++ single linked list Implementation创建 C++ 单链表实现
【发布时间】:2019-10-06 22:37:31
【问题描述】:

我是面向对象编程的新手,我正在尝试完成这个涉及从链表中插入和删除数据的硬件任务。我想我已经创建了节点,但我不知道为什么我不能显示它。我想确保在继续之前确实添加了节点。任何帮助将不胜感激!

#include <iostream>
using namespace std;

//void addNode(int num);
//void displayList();

struct node{
  //Where the data within the node is intialized
  int data;

  //Where the node advancement pointer is intialized
  node *next;
};

class list{
 private:
  //Where the head and tail pointers are intialized, these will hold the first and last node (in order to not lose track)
  node *head,*tail;

  public:
  //The constructor (for the class)
  list()
  {
    //Where the node header is set to NULL
    head=NULL;
    //Where the node tail is set to NULL
    tail=NULL;
  }

  void addNode(int num)
  {
    //A temp node is made by calling the node struct
    node *temp=new node;

    //The user entered number is added to the data portion of the node in the list
    temp->data=num;
    //The new tail is made and pointed to
    temp->next=NULL;


    if(head == NULL)
      {
        //If the data being entered is the first node
        //First andlast nodes is the data being entered
        head = temp;
        tail = temp;
      }
    else
    {
      //Set node after tail (last node) equal to the data being entered
      tail->next = temp;
      //Set the tail (last node) equal to the node after temp
      tail = tail->next;
    }
  }

  void displayList()
  {
    node *displayNode=new node;
    displayNode=head;

    if(displayNode!=NULL)
    {
      cout<<"display list";
      cout<<displayNode->data<<endl;
      displayNode=displayNode->next;
    }
  }

};

int main() {
  //Creating arguments for the list class
  list first;
  list second;
  list third;

  //Where the class member functions are called with the data 1, 2, and 3
  first.addNode(1);
  second.addNode(2);
  third.addNode(3);

  //Whre the display calss member function is called
  list print;
  print.displayList();
}

【问题讨论】:

    标签: oop c++11 singly-linked-list


    【解决方案1】:

    为了打印列表,您需要在 displayList() 函数中使用具有相同条件的 while。 在列表上运行直到 node-&gt;next 的 while 循环将指向 NULL 值(这意味着这是列表的末尾)。

    main()中你只需要初始化一个列表并将所有的值添加进去。

    另外,一个代码风格推荐是用大写字母开头,每个新词都有一个大写字母,这样区分函数和类很有用,例如:

    class MyClass
    {
    
    };
    
    struct MyStruct
    {
    
    };
    

    代码如下:

    #include <iostream>
    using namespace std;
    
    //void addNode(int num);
    //void displayList();
    
    struct Node{
      //Where the data within the node is intialized
      int data;
    
      //Where the node advancement pointer is intialized
      Node *next;
    };
    
    class List{
     private:
      //Where the head and tail pointers are intialized, these will hold the first and last node (in order to not lose track)
      Node *head,*tail;
    
      public:
      //The constructor (for the class)
      List()
      {
        //Where the node header is set to NULL
        head=NULL;
        //Where the node tail is set to NULL
        tail=NULL;
      }
    
      void addNode(int num)
      {
        //A temp node is made by calling the node struct
        Node *temp=new Node;
    
        //The user entered number is added to the data portion of the node in the list
        temp->data=num;
        //The new tail is made and pointed to
        temp->next=NULL;
    
    
        if(head == NULL)
          {
            //If the data being entered is the first node
            //First andlast nodes is the data being entered
            head = temp;
            tail = temp;
          }
        else
        {
          //Set node after tail (last node) equal to the data being entered
          tail->next = temp;
          //Set the tail (last node) equal to the node after temp
          tail = tail->next;
        }
      }
    
      void displayList()
      {
        Node *displayNode=new Node;
        displayNode=head;
    
        while(displayNode!=NULL)
        {
          cout<<"display list";
          cout<<displayNode->data<<endl;
          displayNode=displayNode->next;
        }
      }
    
    };
    
    int main() {
      //Creating arguments for the list class
      List first;
    
      //Where the class member functions are called with the data 1, 2, and 3
      first.addNode(1);
      first.addNode(2);
      first.addNode(3);
    
      //Whre the display calss member function is called
      first.displayList();
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-03
      • 2015-08-18
      • 1970-01-01
      • 2020-09-29
      • 2011-01-19
      • 1970-01-01
      • 2015-06-03
      • 1970-01-01
      相关资源
      最近更新 更多