-1

I just want to know if there is and difference between these two notations:

First example:

    struct node{
      int data;
      node* next;
   };
    void insert(node* list){
      node* n,* last;
      last = list;
      while(last)
         last = last -> next;
      n = new node;
      last = n;
      n -> next = NULL;
      cin >> n -> data;
   }
    int main(){
      node* list = new node;
      list -> next = NULL;
      insert(list);
      return 0;
    }

Second example:

    struct node{
      int data;
      node* next;
   };
    void insert(node* list){
      node* n,* last;
      last = list -> next;
      while(last)
         last = last -> next;
      n = new node;
      last = n;
      n -> next = NULL;
      cin >> n -> data;
   }
    int main(){
      node* list = new node;
      list -> next = NULL;
      insert(list);
      return 0;
    }

So I wonder if there is difference between notation lie: in first example:

last = list;

and in second example

last = lista -> next;

And what does node* list do/represent in void insert(node* list){} Is that just a pointer to whatever list, a pointer that points to the list in the main, or what?

Demigod98
  • 35
  • 7
  • 2
    Obviously, there is a difference. What if `list` is a null pointer? – Evg Jan 09 '21 at 11:27
  • 2
    One thing they certainly have in common - neither example compiles, because `lista` is an undeclared identifier. – Igor Tandetnik Jan 09 '21 at 14:39
  • You're leaking a lot of memory in your code. And try to use RAII. – JHBonarius Jan 09 '21 at 17:41
  • yes, I know I didn't deallocate memory, I just wanted to know the difference but now I think I got it. Thanks for the info @JHBonarius, yours and Evg7's comments are the only useful comments here, others just downvote it, or point out the typos, so thank you so much :) – Demigod98 Jan 09 '21 at 18:40

2 Answers2

0
struct node{
  int data;
  node* next;
};

void insert(node* list){
  node* n,* last;
  last = list;
  while(last -> next)
     last = last -> next;
  n = new node;
  last = n;
  n -> next = NULL;
  cin >> n -> data;
}

int main(){
  node* list = new node;
  list -> next = NULL;
  insert(list);
  return 0;
}

Well, obviously nobody wanted to give me an answer so I've found it myself. This is how to code should look like. The reason why:

last = list -> next;

won't work is the following : list -> next is declared as NULL because in the begining it doesn't point to any element so the obvious answer is:

last = list;

because list contains the address of the head which points to the next element

Demigod98
  • 35
  • 7
0

Your above explanation stands correct but let me also add one more point, Suppose your

list = NULL;

void insert(node* list){
  node* n,* last;
  last = list -> next;
  while(last)
     last = last -> next;
  n = new node;
  last = n;
  n -> next = NULL;
  cin >> n -> data;

}

Then in the above case you are trying to access list->next which is next of 'NULL' thus it will give you runtime error because you cant access next of nullptr as it is not defined.