-2

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.

  • 3
    You are dereferencing uninitialized pointers in the constructor. – Baum mit Augen Dec 24 '18 at 00:41
  • 1
    Also, this does not compile as it stands; and when you fix the typo, the compiler does warn you with proper settings. At least gcc does. https://wandbox.org/permlink/7pl4kRUZ6de8ulEw – Baum mit Augen Dec 24 '18 at 00:42
  • Baum Mit Augen- That's strange, compiles fine over here. Although I'm confused why you're using gcc- does that compile c++ as well? And how would I fix the dereferencing? – lunchboxninja Dec 24 '18 at 00:45
  • It should not compile; you probably messed up while transferring it to the page. And I used "gcc" as abbreviation for the GNU compiler collection here; you'd invoke it with the g++ command for C++ code of course. Last, you can fix the derefencing by not doing it. Rethink your code's logic. – Baum mit Augen Dec 24 '18 at 00:52
  • I does not compile because you used class `List` then named your constructor `list`. `c++` is case sensitive so these are different things. – drescherjm Dec 24 '18 at 00:57
  • Your constructor probably should be `List() {head=tail=NULL;}` – drescherjm Dec 24 '18 at 00:59
  • BTW, in `c++` you don't need the `struct` in `struct Node *head;`. `Node *head;` is sufficient and preferred. – drescherjm Dec 24 '18 at 02:42
  • This post would be helpful, https://stackoverflow.com/questions/4285895/where-exactly-does-c-standard-say-dereferencing-an-uninitialized-pointer-is-un . – Hiroki Dec 24 '18 at 02:56

1 Answers1

0

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;
}
mss
  • 1,423
  • 2
  • 9
  • 18