Re: linked list help please.

From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 03/31/04


Date: Wed, 31 Mar 2004 19:02:29 +0200

fighterman wrote:

> I have a class
>
> template <typename T>
> class node
> {
> public:
> T nodevalue; //store value
> node<T> *next;

When the current node gets destroyed, who is responsible for destroying the
object pointed to by next?

>
> node(){next=NULL;}
> node( T value, node<T> * ptr = NULL): nodevalue(value),next(ptr)
> {}
> };
>
> this is insertrear function to insert node at the back of the linked list
>
> template <typename T>
> void insertrear( T value, node<T> * ptr)
> {
> node<T> *curr = *ptr;

Are you sure that the code compiles without warnings/errors and that you
copy/pasted the code into the message?
The line above should not compile, because you try to initialise a pointer
to a node<T> with a node<T> object (the object that ptr points to).

> node<T> * temp= new node<T>(value);
>
> if(curr=NULL)
               ^
This is an assignment, not a test for equality.

> {
>
> temp->next=curr;
> curr=temp;
This assignment does not affect the passed in pointer in any way.

>
> }
> else{
> while(curr->next!=NULL)
> {
>
> curr=curr->next;
> }
>
> temp->next=curr->next;
> curr->next=temp;
> }
>
> delete temp;

Here you destroy the node, whose address you just stored in the list.

> }
>
> And in the main function
>
> int main()
> {
> node<int> *head;

head should point to an existing node, or it should have the value NULL.

> for (int i=9; i>=0; i--)
> insertrear( i , head);
>
> return 0;
> }
>
> the problem is why when i run it, there is an pop up window said debug
> error, run time check #3, head is using without defined.
>
> I think when I declare head it will call the default constructor which is
> next = NULL

head is a _pointer_, so the default constructor for pointers (which does
exactly nothing) is invoked.

Bart v Ingen Schenau

-- 
a.c.l.l.c-c++ FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/


Relevant Pages

  • Re: Still more linked list magic :-)
    ... if pointer deallocation succeeded ... TYPE(my_pointer), POINTER:: current! ... Check if the head of the list is allocated, ... print *, 'before calling dealloc_list:' ...
    (comp.lang.fortran)
  • Still more linked list magic :-)
    ... if pointer deallocation succeeded ... TYPE(my_pointer), POINTER:: current! ... Check if the head of the list is allocated, ... print *, 'before calling dealloc_list:' ...
    (comp.lang.fortran)
  • [RFC][PATCH] Improve readability by hiding read_barrier_depends() calls
    ... it is sometimes difficult to figure out which pointer is ... extern void rcu_check_callbacks(int cpu, int user); ... #define list_for_each_safe_rcu(pos, n, head) \ ... * Double linked lists with a single pointer list head. ...
    (Linux-Kernel)
  • Re: LinkedList Pointer (REPOST - diff version)
    ... > Here's the original problem. ... > struct node * next; ... > No problem until you are dealing with a pointer variable. ... > node** pointer to the head pointer), add a new node at the head of the ...
    (comp.lang.c)
  • Re: Cannot deallocate a linked list
    ... || argument to the subroutine to indicate the list. ... I make use of module variables to indicate the head ... with few minor tweaks (Head will remain ... a "dangling" pointer, thus falsely associated, because it's never ...
    (comp.lang.fortran)