Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I am making a linked list code library,
i 've designed a function that return the value of the element at any in range given index
now i want to know how to make this function set and get without actually passing the new value as a parameter in the function

here 'is the function
C++
int32 list_element_at(llist list, int32 n)
{
   struct node *temp;
   int32 value;
   
   temp = list.head;
   while(temp != null)
   {
      if(temp -> index != n)
      {
         temp = temp -> next;
         continue;
      }
      value = temp -> data;
      break;
   }
   
   return value;
}


i want to use it like this;
C++
a = list_element_at(listo, 2);(allready done)

and
C++
list_element_at(listo , 2 ) = 50;
printf(list_element_at(listo, 2));

----> 50

Thanks in advance,
z3ngew
Posted

You can't, not without changes to your function - it returns an integer - the value of the node - rather than the node itself, so the code fragment you show:
C++
list_element_at(listo , 2 ) = 50;
Is the equivalent of trying to say:
C++
17 = 50;
Which no modern compiler will allow you to do, for obvious reasons. :laugh:

You can do it, if you make your function return the node, instead of the value of the node:
C++
node* list_element_at(llist list, int32 n)
   {
   struct node *temp;
   temp = list.head;
   while(temp != null)
      {
      if(temp->index == n)
         {
         break;
         }
      temp = temp->next;
      }
   return temp;
   }
(I've taken the liberty of "tightening up" your code a little as well, just to make it a bit more readable)
You can then set the value of the node directly:
C++
list_element_at(listo, 2)->data = 50;
But you will have to watch out for invalid indexes!
 
Share this answer
 
Comments
Menon Santosh 7-Sep-13 4:53am    
my +5
z3ngew 7-Sep-13 6:00am    
Thank you, and about invalid indexes, in the list struct there is a member called count which is similar to the length in arrays
The solution is simple: You just have to return a reference to data member:
C++
int32& list_element_at(llist list, int32 n)
{
   ...
   return temp->data;

Just a single ampersand and you are done :-). Now, what that causes behind the scenes is that the compiler will pass a pointer to the data cell instead of a value as the return value of your function. But as you have marked it as a reference, the compiler remembers that it has to dereference this pointer before using it. In other words, this is like writing:
C++
int32* list_element_at(llist list, int32 n)
{
   ...
   return &temp->data;
}

...
*(list_element_at (listo , 2)) = 50;

and the compiler does away with the two asterisks automatically.

While we are at it, I suggest that you give your variables more meaningful names. "temp" is for example no good choice. How about writing your little function like this:
C++
int32 list_element_at (llist list, int32 index)
{
   struct node *pNode;
   
   pNode = list.head;
   while (pNode )
   {
      if (pNode->index == index)
          return pNode->value;
   }
   return INT_MIN; // this is still a problem!
}

In your current version you return an undefined value if you can't find an element with the given index. But what should your function return instead? You have to find a value that is different from all the values in your nodes. I have chosen INT_MIN, which is the lowest integer value that can be represented in an int. But still this is no good solution. In the version that returns a reference, the problem is even bigger, as you have to return something.

My suggested solution is not to return a value but a node pointer. And in case you can't find the node you would return a null pointer:
C++
struct node* list_element_at (llist list, int32 index)
{
   struct node *pNode;

   pNode = list.head;
   while (pNode )
   {
      if (pNode->index == index)
          return pNode;
   }
   return 0; // no element found
}

That also solves your problem with assigning to the element. You can now write:
C++
struct node* pNode = list_element_at (listo , 2);
if (pNode)
    pNode->data = 50;

That looks a lot better.
<END OF LESSEN MODE>
 
Share this answer
 
Comments
Menon Santosh 7-Sep-13 4:54am    
my +5
nv3 7-Sep-13 8:20am    
Thank you!
z3ngew 7-Sep-13 6:02am    
Thank you, and great explanation
nv3 7-Sep-13 8:20am    
You are welcome.
H.Brydon 7-Sep-13 11:48am    
Oh great poobah
Named 'nv3'
You get a +5
And more respect from me.

Apologies to the Vogons. :-)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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