Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Linked list push at the end

  1. Sep 19, 2008 #1
    Following inserts new element at the list end.
    The class has list_head, list_tail, and count ...

    Problem: I don't know if node should be deleted or not.

    Code (Text):
    //inserts an element at the end
    template <typename Object>
    void Single_list<Object>::push_back( const Object & obj ) {
        //makes new node with value obj that has next element pointing
        //to null
        Single_node<Object> *node = new Single_node<Object>(obj,0);
        //points the tail's next_node to the new node
        list_tail->next_node = node;
        //points list_tail to the new node
        list_tail = node;

        delete node;
        count++;
    }
     
  2. jcsd
  3. Sep 20, 2008 #2

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    I cannot imagine why you would delete a node when adding new data to a list. Here, you have created a new node, linked it to the list. Deleting that node will delete the data!
     
  4. Sep 20, 2008 #3
    For
    Code (Text):
     
    Step 1: list_tail->next_node = node;
    Step 2:   list_tail = node;
    Step 3: delete node
     
    Originally I had this:
    [element 1] .... [element x] -> [element x->next] -> [element->next->next or list_tail] - >0


    Step 1 (Adding new data):
    [element 1] .... [element x] -> [element x->next] -> [element->next->next or list_tail] -> [list_tail->next or node] ->0

    Step 2 (Changing list_tail pointer):
    [element 1] .... [element x] -> [element x->next] -> [element->next->next] -> [element->next->next->next or list tail or node] ->0

    Step 3 (deleting node):
    [element 1] .... [element x] -> [element x->next] -> [element->next->next] -> [element->next->next->next or list tail] ->0

    I think node is just an extra in step 2 that's why I am deleting it
     
  5. Sep 20, 2008 #4

    Defennder

    User Avatar
    Homework Helper

    As HallsOfIvy said, "delete node" really means "delete *node" ie. the item which the node pointer is assigned to gets deleted. If that is your latest node, then you've just deleted it. If you really wanted to, you could assign *node to point to NULL when you're done with inserting the node. But that is unnecessary. Pointer "node" will cease to exist once the program exits the block of code.
     
  6. Sep 20, 2008 #5
    ook thnx.
    I thought delete pointer; deltes the pointer and *pointer if no other pointer is pointing to the same data.


    Second question:
    If I have
    Complex * object_pointer; // pointing to a complex object
    and I do:

    object_pointer = 0;

    or

    object_pointer = another_complex_object_pointer;

    Does this mean that my *object_pointer would be deleted?
     
  7. Sep 20, 2008 #6

    Defennder

    User Avatar
    Homework Helper

    Hmm, you didn't invoke delete in either case. In the first one you are re-assigning object_pointer to NULL, whereas in the second case you are telling object_pointer to point to whatever another_complex_object_pointer is assigned to.

    In either case, object_pointer no longer points to that which is originally assigned to. Let us denote that as "obj" If no pointer is assigned to "obj" and "obj" is dynamically created using "new" , then you have a memory leak which is simply data which cannot be accessed by your program but which takes up memory. But of course it's still there although you can't access it, which means that it's as good as deleted.
     
  8. Sep 20, 2008 #7
    so whenever I want to change pointer's complex data,
    I should do:

    delete pointer;
    pointer = some_other_pointer;

    I think this is better:
    //don't create new pointer to store new data

    *pointer = new_data; //so the original data is deleted?

    I don't want any memory leaks. Currently, My +10 functions program allocates memory but does not delete all of it.
     
  9. Sep 20, 2008 #8

    Defennder

    User Avatar
    Homework Helper

    I don't follow what you're trying to do. If pointer is assigned to data and you delete it, how then are you going to change it?

    And for your second case, you have to assign a pointer to data created using "new" keyword, or else you have a memory leak.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Linked list push at the end
  1. Linked List (Replies: 9)

  2. Linked list (Replies: 12)

  3. Java linked list (Replies: 1)

Loading...