cs50x - How to properly free memory in the following W3Schools c programming code demonstrating Queue Implementation using Linked Lists - CS50 Stack Exchange
Learning DSA from scratch : The Ultimate Guide
Is it worth learn Data structures and algorithms in c?
Need advice; fairly good at C, looking to learn DSA.
Videos
DSA can be seen as three step process, A. Learn a language, B. Learn a data structure, C. Apply it in algorithms. Let's dig in shall we?
Learning a language is THE MOST asked question so, here's an answer. What language do I pick, short answer is it depends, the long of it is this. You have primarily three options,
-
C/C++
-
Java
-
Python
What to pick and why? Simply speaking, most programming languages have the same skeleton just different ways of doing it. Therefore, they are used in different ways.
C++ is a powerful language which gives most of the power in your hands, this includes handling how much power or resource you would demand from a system. What kind of system memory you wish to utilise. If you have a timeline of over a year or more this is the best option for you. Though this has a long learning curve and does take time, it helps a lot in the future since almost every other language on this list has features taken from C++. Lastly, if you wish to join Competitive Programming this is the way to go.
Java is a language that falls somewhere in the middle where it's not too easy but not too frustrating as well. Most service based companies who are in the Asian belt prefer Java as a language in many of their projects. Bearing that in mind. If you're aiming for a job in a service based firm this is the way to go. The learning curve is a bit less than C++ but again the concepts are quite empowering for a programmer in their career.
Python is the kingpin that runs the Wild West that's IT these days, Python would be the ideal choice if you're a short deadline and need a job ASAP. The concepts are fewer and the language is less verbose.
Resources to learn the languages:
For C++
1 First two lectures on Harvard's CS50 (David J Malan is one of the best explainers of Intro To Programming I've ever seen)
2. W3Schools www.w3schools.com (Works great as a course material and reference)
3. www.learncpp.com (Frankly, the best place to learn imho)These should be enough for learning C++
For Java
Derek Banas has a fantastic tutorial on YT. Tim Bulchakaโs Java Masterclass course (Paid Resource)Coding With John is another fabulous resource.
For Python
Corey Schafer on YT
W3Schools (Works great as a course material and reference)
The Python Tutorial on Python.Org is a good reference to work as a pair with any of the above listed resources.
So, what all must you know from a language agnostic view point,The basics - Variables, if-else, strings, loops, functions.OOPs (Object Oriented Programming) - Classes, Methods, instances, etc.
At this stage you can move on to learning Data Structures, I'll be listing the most common ones and what approaches are necessary. This is not an exhaustive list nor it is a rulebook for solving problems. Tweak and learn as per your need and adapt.
I would suggest to go through these data structures.
- Linked lists
- Stacks
- Queues
- Trees
- Graphs
- Heaps
- Hash tables
These would allow you to clear any interview or start solving competitive programming problems.
A playlist that helped me a lot for data structures was William Fiset's video
If you have taken Java as a language Princeton University's Algorithms would be the go-to resource.
Tech Interview Handbook is another resource that would be helpful.
Abdul Bari is a fantastic resource for Algorithms. His explanations are top tier.
Though in JavaScript this resource for it's logic explanations are great.
# EDIT :
MOOC is a resource I missed out on thanks to u/WingsOfReason for suggesting.
Simultaneous to this would be recommended to solve problems from sites such as HackerRank, LeetCode.
In the case for LeetCode go for Easy Problems at first then go to medium problems. Hard Problems are better suited for Competitive Problems only.
The way I used to solve problems was this, I set a timer of 20 minutes and read the problem trying to solve the problem. After the 20 mins were over, regardless of if I had solved the problem or not reading through the editorials or looking through on Google for solutions helped me see methods or logics I hadn't thought of before.
Form a habit of solving at least 2 problems a day, which helps your mind work everyday and allow you to go.
Some Tips:
Getting an error is the rule, the program running perfect is the exception. This is a mindset which would allow you to get over the hesitation of feeling incompetent and giving up. StackOverFlow, Reddit and other such resources have millions of people solving, asking problems. Which simply means you're not alone.
You can always edit bad code, a blank page is depressing anyway, Write the code once you've got a solution. You can then edit it and make it better. Writing on paper is also a great habit to have.
The better programmer keeps going one more time than the person before them.
Even the greatest programmer today once didn't know how to declare a variable.Good luck!
Don't modify any of their methods.
The fix is entirely in main
You free the memory createQueue allocated into newQueue by free'ing newQueue at the end. Before you do that you use the dequeue function repeatedly which will free the node's in the queue that were created by enqueue
Shame on them for providing code which leaks memory.
Your assumptions about efficiency are 100% wrong. Your assumption about you shouldn't free from those functions is correct but not for any reason you mention.
You don't want to free memory in those functions because it would defeat the purpose of the functions and it would break the program.
If you delete the memory for the queue after you created it you can't use the queue.
If you delete the memory for an item you put into the (now invalid queue) then the pointer points to free'd memory....
It has nothing to do with efficiency, it has to do with purpose.
You don't free something until you are done using it
That you don't understand this tells me that you need to go back and understand how pointers and memory work, you don't have the foundation to understand data structures based on this question. Also go back and re-read the entire lesson on data structures. not just looking at the code, watch the lecture read the notes.
I don't say this to sound rude, but to inform you; it doesn't sound like you understand or are able to apply almost anything you have learned so far. I would suggest going back over everything and explaining to yourself what each line of does and why it is where it is.
Queue Memory Management In A Nutshell:
- Before every enqueue memory must be allocated, either user does it the enqueue function does it, or some of both ( this depends on what you are putting into the container).
- Creating the queue could be done with or without a pointer, if you do it with a pointer you have to allocate it.
- Anything you allocate, you need to destroy when you are done with it. AKA Clean up your own mess. Generally that is roughly when you remove them from the list/queue but this will vary based on the object contained in the queue. Put simply, every malloc should have a corresponding free somewhere.
If this answers your question, please click on the check mark to accept it. Please consider upvoting the answer as well if it was useful. This helps with site maintenance.
Noicely Done.Well Appreciated.