Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in linkedlist why we write (struct node *root) rather than (struct node root) . idont under stand this pls tell .. or in append case (struct node *temp) rather than (struct node temp)


What I have tried:

i dont get any justified answer in tutorials so came here.
Posted
Updated 31-May-20 6:44am

This defines a structure that contains a node (whatever that happens to be).
C++
struct node root; // root is a node struct so may contain many variables.

This defines a pointer to a node structure.
C++
struct node *root;  // root is a pointer to a node struct

In a linked list each item has to link to the next (and/or previous) item. The way to do this is by using a pointer to the item. You can then walk the list by taking each pointer in turn. For example something like the following code:
C++
struct node* next = head; // head points to the first item in the list
while (next != NULL)
{
    next = next->root; // get the address of the next node in the list
    // if it is null (i.e. the end of the list) the while loop will terminate.
}
 
Share this answer
 
Comments
Member 14849148 31-May-20 12:50pm    
thanks sir very much clear to me now.
Richard MacCutchan 31-May-20 13:01pm    
You are welcome.
A linked list can have its data linked in a variety of ways. The links can be indexes into an array or they can be pointers to other nodes and there are other ways. They are most often implemented as pointers. In this case the head of the list is saved as a pointer to the root entry. This makes it very simple to replace the root of the list - it is just a pointer assignment operation. This is the primary advantage of pointer-based linked lists : it is fast and easy to insert and remove items from the list. Sorting is also fast because no data must be moved or copied - only pointers are reassigned.

The primary reason a pointer is used instead of an instance is because maintaining the links of the list is much easier with pointers instead of instances. One does not have to move or copy data around as you would if an instance were used. Consider a question - what does an empty list look like if an instance was used as the root element? With pointers an empty list is a null root pointer. With an instance, generically indicating non-existence can be problematic.
 
Share this answer
 
The * means that it's a pointer to the struct, not a copy of that entire struct. Each element in the list is a struct that also has a pointer to the next struct in the list.

For an append function, the item that is currently last needs a pointer to the struct that will be appended (thus the *), not a copy of it (which is what it would receive if the * was omitted).
 
Share this answer
 
v2
Comments
Member 14849148 31-May-20 12:46pm    
thanks sir.
Greg Utas 31-May-20 13:11pm    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900