Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hey guys, I have a small question.
 
I've written this function that is supposed to insert an element at the beginning of the linked list.
 
void insertAtStart(node* head /* pointer to head of the list*/, int data /* value of the new node that is to be inserted*/)
{
    /* perform insertion */
 
    node* temp = new node; // create a temporary node
    temp->next=head; // make this new node point where the head of the list was pointing previously
    temp->data=data; // update value of the temporary node
    head=temp; // make head pointer point to this newly inserted node
}
 
My problems start when I want to invoke above function:
insertAtStart(head,9000);
The thing is when I debug, the address where the "head" is pointing before and after invocation of the
insertAtStart 
function remains the same.
 
Thus basically nothing gets changed. Is it because "temp" node gets destroyed once the insertAtStart function has been called? or the "head" must be declared globally somewhere - if yes, why? (and why isn't current solution working?).
 
Thanks!
Posted 29-Sep-12 0:33am
Edited 29-Sep-12 0:38am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Alter your code to something like this:
void insertAtStart(node** head /* pointer to pointer to head of the list*/, int data /* value of the new node that is to be inserted*/)
{
    if(head == nullptr)
    {
      // Do somthing to indicate that you forgot to pass a valid pointer
    }
    /* perform insertion */
 
    node* temp = new node; // create a temporary node
    temp->next=*head; // make this new node point where the head of the list was pointing previously
    temp->data=data; // update value of the temporary node
    *head=temp; // make head pointer point to this newly inserted node
}
 
Which you can use like this:
node *head = nullptr;
insertAtStart(&head,8000);
insertAtStart(&head,9000);
 
Best regards
Espen Harlinn
  Permalink  
Comments
pasztorpisti at 29-Sep-12 7:45am
   
+5, however this whole stuff looks much like C to me. :-) Instead of if a lot of times a debug runtime check with assert does the job but in this case I would rather use neither if(null) nor assert, since the tag is C++ I would rather go with node*& head and in this case the ref isn't null by definition. If it happens to be null then the error is somewhere else deeper in the call stack.
Espen Harlinn at 29-Sep-12 8:01am
   
Thanks, I guess OP is currently learning this stuff, otherwise he would probably be using std::list
pasztorpisti at 29-Sep-12 8:27am
   
Sure, at the beginning every small piece of info is gold. :-)
Espen Harlinn at 29-Sep-12 10:30am
   
Hmm, wasn't trying to be insulting, and doesn't "trying to dig in how data structures like linked list work" and "learning this stuff" mean the same thing?
Espen Harlinn at 29-Sep-12 10:38am
   
Brilliant :-D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

To: Espen Harlinn
 
This looks neat I will give it a try. However why does not my solution work? Actually I have noticed if I just try to change the value where a pointer is pointing to in a function it does not work. e.g.,
 
void insertAtStart(node* head, int data)
{
    head=NULL;
}
 
Now if I call somewhere:
 
node* head = someOtherPointer;
insertAtStart(head, data);
// after calling this function head still points where it was pointing, instead of pointing now to NULL?? Why? just interesting.
  Permalink  
v2
Comments
Espen Harlinn at 29-Sep-12 7:16am
   
void insertAtStart(node* head, int data)
 
head is just a value, passed on the stack, that points to a node.
 
By altering the code to:
void insertAtStart(node** head, int data)
you pass the address of a pointer to a node on the stack, so now you can manipulate the contents of the pointer to the node.
Espen Harlinn at 29-Sep-12 10:33am
   
That sounds about right :-D
nv3 at 29-Sep-12 11:50am
   
head and data in your function are passed "by value". If you modify them in your function the outside values will not be influenced. To pass a parameter "by reference" you either must use a reference parameter (as the name says) or pass a pointer to the variable which you want to change. As your variable in this case is a pointer itself, you must pass a pointer-to-pointer.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 893
1 OriginalGriff 400
2 CPallini 275
3 George Jonsson 191
4 Richard Deeming 145
0 OriginalGriff 5,450
1 Sergey Alexandrovich Kryukov 4,532
2 CPallini 4,500
3 George Jonsson 3,102
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100