通过插入节点创建新的链表

问题描述

我是编码的新手,我试图通过插入节点来创建链表。我上了一个类Node,它定义了节点的结构。我认为我的代码很好,并且在编译时没有错误或警告,但是有一些运行时错误。请检查并提供帮助。

#include <iostream>

using namespace std;

class Node 
{
public:
   
   int data ;
   Node *next;
   
   Node *head = NULL;
   
   void display (Node *p){
       while(p!=NULL)
       {
           cout<< p->data << " ";
           p=p->next;
           
       }
   }
   
   void InsertAtLast(Node *p,int index,int x)
   {
       Node *t;
       int i;
       
       if(index < 0 )
           return;
           
       t = new Node ;
       
       t->data = x;
       
       if(index == 0)
       {
           t->next = p ;
           head = t;
       }
       else{
           
           for(i=0 ; i<index-1 ; i++)
               p=p->next;
           t->next = p->next;
           p->next = t;
       }
   }
};


int main()
{
   Node *head = NULL;
   Node obj;
   obj.InsertAtLast(head,150);
   obj.InsertAtLast(head,3,200);
   
   obj.display(head);
   
   return 0;
}

解决方法

这不是我建议实现链接列表的方式。 我的调整:

  1. Node中只有两个字段datanext_node
  2. 始终将next_node初始化为NULL
  3. 要插入到最后一个Node,只需迭代直到您的next_node为空。
  4. 为了插入索引k,请迭代k-1次,并相应地更新当前节点和新节点next_node字段。

好运(:

,

这不是最好的解决方案,但我使您的代码可以正常工作。看看下面。我所做的更改很少,并且在代码中有注释。

#include <iostream>

using namespace std;

class Node
{
        public:
                int data ;
                Node *next;

                Node *head = NULL;

                void Display(){
                        Node *it = head;
                        while(it!=NULL)
                        {
                                cout<< it->data << " ";
                                it=it->next;
                        }
                }

                void InsertAtIndex(int index,int x) /*Name of the function should be InsertAtIndex instead of InsertAtLast */
                {
                        if(index < 0 )
                                return;

                        if(index == 0)
                        {
                                if(head == NULL){ /*if the list is empty*/
                                        head = new Node;
                                        head->data = x;
                                }
                                else {  /*insert at head when the list is not empty*/
                                        Node *tmpHead = head;
                                        Node *node = new Node;
                                        node->data = x;
                                        head = node;
                                        node->next = tmpHead;
                                }
                        }
                        else{
                                Node *node = head;
                                Node *tmp = NULL;
                                int i = 1;
                                while(node){
                                        if(i == index) {
                                                /*create a new node*/
                                                tmp = new Node;
                                                tmp->data = x;

                                                /*backup next node,if it exists*/
                                                Node *nextNode = node->next;

                                                /*insert the node*/
                                                node->next = tmp;
                                                tmp->next = nextNode;
                                                break;
                                        }
                                        node = node->next;
                                        i++;
                                }
                                if(tmp == NULL)
                                        std::cout << "insertion failed at index: " << index << " because a node doesn't exist at index: "<< index - 1 << std::endl;
                        }
                }
};


int main()
{
        Node obj;
        obj.InsertAtIndex(0,150);
        obj.InsertAtIndex(1,151);
        obj.InsertAtIndex(2,152);
        obj.InsertAtIndex(3,200);

        obj.InsertAtIndex(3,201);//Inserting at index 3 again

        obj.InsertAtIndex(0,100);//Inserting at index 0 again

        obj.Display();

        return 0;
}

下面是输出:

rohit@linux:~/cpp$ ./list
100 150 151 152 201 200 rohit@linux:~/cpp$
rohit@linux:~/cpp$

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...