r/Cplusplus • u/Delicious_Wrap_1041 • 13d ago
Question what would you say to someone that is struggling to implement linked lists in c++ even though they understand the basic concept of the data structure itself (i think my problem is with the syntax )
what would you say to someone that is struggling to implement linked lists in c++ even though they understand the basic concept of the data structure itself (i think my problem is with the syntax )
•
u/BitOBear 13d ago
Do you understand the heap?
Is C++ your first language or did you have Java inflicted upon you beforehand? Or did you come from like Python and things like that?
When you fully understand the basic concept of the linked list and the companion concept about what can be called dynamic scope you should end up having no problem with the link list.
The reason we teach people these weird painfully tiny concepts is that they are so too fundamental.
And when most people have a problem with it it is because the concept is too simple compared to what they're trying to make it.
Do you understand what a pointer is? Like that it is simply something that points. Having a pointer doesn't mean you have something to point at. The little piece of wooden dowel in your hand does not imply the existence of a chalkboard to point at nor anything written upon that board.
That the pointer is also something that comes into existence and banishes is one of the places where people get the most messed up.
For instance if you got a link list from A to B to C and you decide to go down the list deleting it. People will frequently make the mistake of deleting the pointer to a and then trying to follow the A.next pointer to deal with B. But if you've already deleted A then A.next no longer exists.
So understanding the layout of pointers pointing at things and having to allocate and delegate the things they point at is the first level of understanding
But understanding where you have to take copies of pointers so that you can delete the nodes and resell the lists together or destroy the list outright is where they start getting complicated.
Imagine a link list laid out pointer to thing contains pointer thing contains pointer to thing is simple keeping track of what you need to know in order to take the middle thing out and splice the list back together for example is not just a drawing of state but a series of State transitions. Of holding on to things while you juggle other things around.
You do these tasks all the time in the real world, ask anybody who's ever worked in a bar and had to balance drinks on a tray about staging and ordering operations carefully.
But until you really get it you don't really get it at all.
I would suggest getting some physical objects like Lego blocks and string and physically reenacting your list operations using the physical operations to see the task physically be in front of you. That will help you within see the difference between the task as you physically understand it in the task as you are executing it in your code.
It sounds dumb to start playing with blocks but it's super helpful when you're dealing with this sort of thing.
•
u/mighty_Ingvar 12d ago
or did you have Java inflicted upon you beforehand?
I get the feeling you don't like Java.
•
u/BitOBear 12d ago
I don't like Java as a first computer science language. Having dealt with a number of students in a number of generations and new hires at various companies that work at I find that people learn Java as a first language have a huge amount of trouble moving to lower level computer science languages.
One of its greatest flaws is that everything complicated is used by a pointer and then they go around acting like they don't have pointers in the language at all because of the way they rely on reference counting and Eden Memory and that sort of thing.
Trying to teach somebody computing primitives and dynamic scope after they've come from java can be brutal on all parties involved.
I had to teach a kid who had a four year computer science degree what did masking was about 6 months ago.
It probably sounds curmudgeonly to hear me say it but teaching somebody how computers actually work and then letting them escape into a higher level language produces somebody better able to consider the algorithm concepts.
Starting somebody off with a high level language and then backing them down to the hardware is harder on them psychologically and intellectually.
The job is some sort of weird mashup where they use the shape of lower level concepts while hiding implementation too much.
Would you take somebody from java down to actual hardware registers and dealing with physical devices and file system layouts and persisted data structures you need for certain kinds of things it's almost cruel.
They've been showing the shape of something but they haven't been given the understanding, and trying to make them understand that the shape thing understood isn't the function they think is happening just can take a lot of words and a lot of effort that everybody could have been spared.
Giving somebody a good and authoritative teacher and teaching them stuff, and then requiring them to unlearn that stuff so that they can learn what's actually underneath the stuff they just learned is kind of unfair to the student.
•
u/Rich-Engineer2670 13d ago
Can you implement them in C rather than C++ -- that means you have a block with the more complex C++ syntax. If C is.a problem, then it's memory management and pointers I suspect.
•
u/deadbeef_enc0de 13d ago
If you don't know C++ it might be how C++ works (particularly memory management whether it's raw memory allocation or shared pointers), try writing it in a language you are more familiar with. Something like Java or Python which have references to objects/data will work much the same as pointers.
If you know C++ maybe you don't know the memory systems well enough or are having a hard time translating the concepts to the code for that language, just have to keep trying.
If you can't do this in any language this is a bigger problem to overcome because it sounds like you can translate the "whiteboard" version of how the structure works to working code. I would recommend going step by step on the white board (or pencil and paper) on how to do various operations writing in plain English (or whatever your primary language is) what to do. Then try translating that to the programming language you know best.
Lastly depending on where you are in experience (just started learning, near the end of college/university, or looking for a job) will impact how much of a problem this may be for you. If you are just starting out, keep working at it and don't fret too much, sometimes it can be hard to grasp.
•
u/Prudent-Bluebird1432 13d ago
I would suggest a little direct mentoring. Have someone look at the code and show the student their error.
•
u/WikiBox 13d ago
I'd say they need to continue their struggle. At least if they feel a need to implement their own linked lists and not use an already existing linked list implementation.
Implementation of linked lists is a very good C++ training exercise. It is a very good way to learn syntax, memory management and about pointers. I think every C++ programmer should do that. And then use std::list.
•
u/MADCandy64 13d ago
If you understand the basic concept and data structure then your issue is with the algorithm itself. Linked lists can seem simple in concept. They have a head and nodes. The first difficult choice happens here. This will affect the entire algorithm. Do you use the head as a node or not? Either answer is correct. The next aspect of the algorithm which is difficult to do but easy to understand are the parts that add, delete, or reorder nodes. Here paper and pencil will serve you well. Draw out a head node, then step by step draw another node and then your directional lines. Is this a forward only list, a bidirectional list, or something else? Those directional lines visualize that for you. Just spend an hour or so with pencil and paper drawing linked lists and modeling different operations. That will guide your implementation when it starts to look muddy with lines of code about next and previous.
•
u/No-Newspaper8619 13d ago
Practice more. Write the algorithm, then delete it all and write it again from scratch, over and over again.
•
u/mredding C++ since ~1992. 12d ago
I would suspect you're a student and all this is new to you. Keep working at it. Thinking in a structured, disciplined way is not a natural or intuitive skill. Most people don't have an innate understanding of software engineering. You are - so far, normal. Typically it clicks at some point, and you start to see some progress. Progress can come in spurts after breakthroughs - it's rarely a linear progression.
There is a point where one might suggest programming isn't for everyone, but you're very likely nowhere near that point yet. Don't sweat it. Keep g oing.
I also knew a Ph.D whose thesis was on singly-linked lists who couldn't write one in C, which he claimed basic competence in.
•
u/Direct_Chemistry_179 13d ago
Check out my code academy tutorial for dsa in c/c++. Also practice some leetcode problems or make a project that would need a list and implement it yourself.
•
u/blue-cheeso 8d ago
Try drawing the operations on paper. There this moment when you just end staring at the keyboard, trying to think how to implement the algorithm or data structure you just studied. It helps trying to visualize them on literal paper.
•
u/AutoModerator 13d ago
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
When posting a question or homework help request, you must explain your good faith efforts to resolve the problem or complete the assignment on your own. Low-effort questions will be removed.
Members of this subreddit are happy to help give you a nudge in the right direction. However, we will not do your homework for you, make apps for you, etc.
Homework help posts must be flaired with Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.