简体   繁体   中英

I can't figure out why my add method won't work

I can get to where its adding the node, but after that, the program just cuts itself off.

I'm on Windows 10, using VSCode insiders. Using G++ as my compiler, if any of that matters. I've tried just setting nodes' pointers manually, and that works. I can't figure out what's different in the method. The idea is "tail is the last node, so make tail.next the added node and set tail equal to the new node."

#include <iostream>
using namespace std;

struct Node{
    int data;
    struct Node *next;
};
class List{
    private:
        struct Node *head;
        struct Node *tail;
    public:
        list(){
            head->next=tail;
            // I tried initializing tail.next to null but that didn't help
            tail->next=NULL;
        }
        void add(int d){
            printf("I'm entering the add\n");
            struct Node *n=new Node;

            printf("Node created\n");
            n->data=d; 
            printf("Data set %d\n", n->data); 

            // right here is the issue, it seems
            tail->next=n;
            printf("Node added\n");
            tail=n->next;
        }
};
int main(){
   List l;
   l.add(50);
   return 0;
}

I'm expecting it to print 50 (I haven't tried my display method yet as the code breaks before it gets there), but it outputs "Data Set 50" and then crashes. Compiles fine, no warnings.

The Main problem is in your constructor, its name should be same as Class(actually you are using STL list).Then in the constructor, you should initialize both your head and tail to NULL.

Other minor mistakes I have corrected, below is your code.

#include <iostream>
using namespace std;

struct Node{
    int data;
    struct Node *next;
};
class List{
    private:
        struct Node *head;
        struct Node *tail;
    public:
        List(){
            //initialise both head and tail to NULL
            tail=NULL;
            head=NULL;
        }
        void add(int d){
            printf("I'm entering the add\n");
            struct Node *n=new Node;

            printf("Node created\n");
            n->data=d;
            printf("Data set %d\n", n->data);
            n->next=NULL;

            // I think you missed about the first time when you will add a node
            if(head==NULL)//for adding first time , we will have to check as the first will be our last one
            {
                tail=n;
                head=n;
            }
            else// for second time or more we give new node pointer address to our linked list last node next
            {
                tail->next=n;
                tail=n; // then this n becomes our last node
            }
            printf("Node added\n");

        }
};
int main(){
   List l;
   l.add(50);
   l.add(70);
   l.add(90);
   return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM