首页 > 解决方案 > 通过插入节点创建一个新的链表

问题描述

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

#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 , 0 , 150);
   obj.InsertAtLast(head , 3 , 200);
   
   obj.Display(head);
   
   return 0;
}

标签: c++data-structurescodelite

解决方案


不是最好的解决方案,但我使您的代码正常运行。看看下面。我做了一些更改,并在代码中进行了注释。

#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$

推荐阅读