1

I want to know how a node* variable NODE can be assigned to the data inside the structure?

struct node
{
   int info;
   struct node *link;
};

 typedef struct node* NODE;

//IN SOME OTHER FUNCTION
NODE temp,first;
temp = first;
first = first->link; // I WANT HELP WITH THIS LINE.
Ashwani Jha
  • 355
  • 5
  • 11
  • you assign first to point to the next item in the list, whatever that means in your implementation. First needs not to be null – k_kaz Oct 14 '16 at 08:55
  • 1
    [malloc](http://man7.org/linux/man-pages/man3/malloc.3.html) can be your friend...or enemy.... – LPs Oct 14 '16 at 08:56
  • There is "tutorial-style" [answer here](http://stackoverflow.com/a/23280743/4142924) – Weather Vane Oct 14 '16 at 08:56
  • 3
    One thing it means is that someone made your analysis task more difficult by [hiding a pointer-type in a typedef alias](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) – WhozCraig Oct 14 '16 at 08:59
  • 2
    `first` is uninitialized, so `first = first->link` is wrong. – interjay Oct 14 '16 at 09:00

2 Answers2

0

In a linked list you have a node with some information. In every node you have the address of the next node.

struct node
{
   int info;           //Information or data which is stored at every node
   struct node *link;  // Address of next node 
};

first = first->link; // I WANT HELP WITH THIS LINE.

If you're done with current node then you may require to access the next node. In this line you have accessed the next node (first->link) of the linked list and you are making your next node as first (top node).

The Vee
  • 11,420
  • 5
  • 27
  • 60
Mohan
  • 1,871
  • 21
  • 34
-1

first is of type struct node *

link is also a struct node *

So you can assign one to the other and vice versa.

In this example the original value of first is stored in another variable temp and then first is replaced with what was the value of link in the original first. You could equivalently write

first = temp->link

instead of the last line, hopefully that will make it less confusing.

The Vee
  • 11,420
  • 5
  • 27
  • 60
Secto Kia
  • 990
  • 4
  • 12
  • Huh, what undefined behaviour? `NODE` is a `struct node*` (by `typedef`), so it's only pointers assigned to pointers everywhere. – The Vee Oct 15 '16 at 21:23