Singly Linked List, Deleting, and Destructors

Please Note: This is for a homework assignment, however it was due ~3 weeks ago and I'm only doing it for the learning aspect.

I'm learning about Singly linked lists online and I've gained a basic understanding of how to do them. However, when I was reading this tutorial:
https://www.codementor.io/codementorteam/a-comprehensive-guide-to-implementation-of-singly-linked-list-using-c_plus_plus-ondlm5azr
It had the following example to demonstrate how to delete a node from a particular position in the list:
1
2
3
4
5
6
7
8
9
10
11
12
  void delete_position(int pos)
  {
    node *current=new node;
    node *previous=new node;
    current=head;
    for(int i=1;i<pos;i++)
    {
      previous=current;
      current=current->next;
    }
    previous->next=current->next;
  }

I noticed that none of the lines delete the node. They just remove it from the list. Isn't this bad? So I was wondering how to delete the node rather than just removing it from the list, unless I'm misunderstanding this and it was deallocated from memory already.

I was thinking "delete previous->next;" may fix it if I were to add that after line 9. But I looked it up and "delete" will call the destructor and I don't have a destructor defined. So then I looked up about destructors and it said if you don't have a destructor defined, that's generally okay because it will create it's own, however, if you are using pointers in a class or something then you need to declare your own. Which means I need to make my own destructor but after doing more research on destructors it seems I can only delete the things stored in the private: section of the class and not certain variables from member functions.

I think by now you've probably caught on that I'm thoroughly confused haha.

Basically, I have two questions.
1. Is the example code from the website link above okay? Or does it cause memory problems?
2. When using linked lists, do I need a user defined destructor and if so how do I make one properly?


I doubt you need to look at my code but if you do, then:
Main.cpp: http://cpp.sh/9kekc
SinglyLinkedList.h: http://cpp.sh/5e2asr
It's probably not great at the moment.


Main Sources:
https://www.codementor.io/codementorteam/a-comprehensive-guide-to-implementation-of-singly-linked-list-using-c_plus_plus-ondlm5azr

https://docs.microsoft.com/en-us/cpp/cpp/destructors-cpp?view=vs-2019

https://www.includehelp.com/cpp-tutorial/difference-between-delete-and-free.aspx


Help would be greatly appreciated, thanks!
Last edited on
This is one of the problems with relying on online material alone.

1. Never rely on a single source. Especially when the source is prepared by a student. From the bio at the end of that link:
Kamal Choudhary is a tech geek who writes about c++ programming tutorials on C++ Beginner. He is a student of computer science in University of Gujrat, pakistan.

Highlight mine. Two things here, students coming from India and Pakistan seem to be taught with outdated software and systems, more important is the fact that this is a student.

2. IMO, you should look into getting an actual book that has received good reviews.

3. Prefer material that comes from an accredited qualified source. While your source is accredited I question the qualifications.

4. Read the comments at the bottom of the post for you primary question.

Just a snippet of one comment:

Fourth issue: I count nine distinct memory leaks in the seven functions presented, and that ignores the leak from the lack of a destructor. There are 1 in display, 2 in insert_position, 1 in delete_first, 2 in delete_last, and 3 in delete_position.




jlb wrote:
This is one of the problems with relying on online material alone.

1. Never rely on a single source. Especially when the source is prepared by a student. From the bio at the end of that link:

... (rest of msg)..

Ah okay, thanks for the information. Wish I hadn't spent so much time on it haha.

I was thinking about getting a book actually, but I was trying to wait until I went back to California (after Finals on ~May 20th) for the summer because I can only take a suitcase and backpack worth of stuff back with me, and I'll likely have a LOT I need to take back. So a large textbook or two is just going to take up more space/weight. I'm planning to buy:
- "Programming: Principles and Practice Using C++ (2nd Edition)"
- Then "The C++ Programming Language (hardcover) (4th Edition)"

I read: https://isocpp.org/wiki/faq/how-to-learn-cpp#start-learning

I am admittedly hesitant to switch to textbooks because online formats have many benefits such as finding a particular word on a page, chapter, or the entire book faster and you don't need to carry the book everywhere. You could have 3 books or so worth of information on 1 laptop. Also some online books offer audio which is good for incredibly slow readers like me. Helps me read along and stay on track.

- - -
Anyways, do you know of any decent online sources for beginners I could use until I move?
Last edited on
Well those two books you listed do have Kindle versions if you would rather have an electronic copy.

Regarding the destructor, you need one if instances of your class need to release resources of some sort when they are destroyed. When your class has a raw pointer, you need a destructor to delete the pointer if the class object owns the object pointed to. This is something that you need to decide as part of your design. Linked lists are usually designed so that the list itself owns the items within it, not the individual nodes. So no, you probably don't need a destructor.
jlb wrote:
Well those two books you listed do have Kindle versions if you would rather have an electronic copy.

Thanks for your reply. :)

Unfortunately, I checked the reviews and "The C++ Programming Language" has reviews saying to not buy the Kindle version. They explain the book itself is great but don't get the Kindle version. This is shown here:
https://www.amazon.com/Programming-Language-hardcover-4th/product-reviews/0321958322/ref=cm_cr_othr_d_show_all_btm?ie=UTF8&filterByKeyword=kindle+version#reviews-filter-bar

The first book I could only find one review that said such a comment though because it doesn't allow filtering the reviews for only Kindle format. And that single review said there are no page numbers for the Kindle version but aside from that it's a good book.

So I may buy the first book in Kindle and the second as a hardcover. Plus I suppose that'd let me determine which format I prefer. :) Each have their pros and cons.

Anyways, thanks for reminding me that is an option!

Edit: I bought the first book using Kindle and there are page numbers for PC edition. I am guessing the reviewer either wasn't using PC edition, was using an older version, or didn't see the pages because they weren't actually on the pages but found somewhere else.


dhayden wrote:
Regarding the destructor, you need one if instances of your class need to release resources of some sort when they are destroyed. When your class has a raw pointer, you need a destructor to delete the pointer if the class object owns the object pointed to. This is something that you need to decide as part of your design. Linked lists are usually designed so that the list itself owns the items within it, not the individual nodes. So no, you probably don't need a destructor.

Ahh okay, thank you for explaining that!
Last edited on
Topic archived. No new replies allowed.