How to properly insert an element at the end of a linked list in C++?

  • Thread starter rootX
  • Start date
  • Tags
    List Push
In summary, the conversation discusses the process of inserting a new element at the end of a list and the concept of deleting nodes in this process. It is determined that deleting the node is unnecessary and could potentially cause a memory leak. The conversation also touches on the importance of properly managing memory allocation and deletion to avoid memory leaks.
  • #1
rootX
479
4
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:
//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++;
}
 
Technology news on Phys.org
  • #2
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!
 
  • #3
HallsofIvy said:
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!

For
Code:
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] - >0Step 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
 
  • #4
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.
 
  • #5
Defennder said:
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.

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?
 
  • #6
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.
 
  • #7
Defennder said:
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.

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.
 
  • #8
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.
 

1. What is a linked list push at the end?

A linked list push at the end is an operation that adds a new node to the end of a linked list. This new node becomes the new tail of the linked list.

2. Why is a linked list push at the end useful?

A linked list push at the end allows for efficient insertion and deletion of nodes at the end of the linked list. It also maintains the order of the elements in the linked list.

3. How does a linked list push at the end work?

In a linked list push at the end, the new node is created and its pointer is set to the last node in the linked list. The previous tail node's pointer is then updated to point to the new node, making it the new tail.

4. What is the time complexity of a linked list push at the end?

The time complexity of a linked list push at the end is O(1), as it only involves updating the pointers of two nodes regardless of the size of the linked list.

5. Are there any disadvantages to using a linked list push at the end?

A disadvantage of using a linked list push at the end is that accessing a specific node in the linked list can be inefficient, as the linked list must be traversed from the beginning to reach the desired node. Additionally, a linked list can only be traversed in one direction, so deleting nodes from the end of the list can also be inefficient.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
8K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
6
Views
12K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top