Linked List
Warning: This post will contain video game blood.
OK so this time I’ll be talking about how to make an linked list from scratch.
I’ll be using VS 2014 with c++ syntax(might be some C involved here).
Linked list in programming terms are(but can be something else!) a series of nodes which carry a data and a pointer.
These nodes make up a list where each node looks in front of them so you can traverse in a line to find what you need to add, delete or look for.
Linked list for me can be explained like this:
“There is a line of people. Each person knows their own age and can only see who the person in front of them are. They are allowed to ask the person in front of them their age and they always answer with the truth.”
-To remove a specific person in the line:
To remove, lets say #14 in this line of people. You simply ask the first one in line to tell where the second one is and repeat this until you reach the 13th in line. When that person points to the 14th you kick that person to the face and remove them from the line.
You then tell #13 in the line to start looking at #15.
-To remove the first person in the line
You simply ask the cashier (which always looks at the first person in the line and can ask about the people their age) who is in front. You then proceed to:
You then tell the cashier that the second person in line is the first one now.
-To remove the last person is just as easy.
You tell the cashier to ask the first person to see who’s next in line until you get the question “nobody” then:
You then tell the second last person that nobody is after them.
-To add people to the front of the line.
You simply put someone in front of the first person in line like so:
So now you got tennis dude in line. You tell the cashier that the first person in line is the tennis dude and the tennis lady that the angry lady in behind you, Voilá.
-To add someone at the back!
You tell the cashier to ask the first person to see who’s next in line until you get the question “nobody” then:
Place grumpy guy to the back of the line, tell overweight Mario that grumpy guy is the last guy in line now.
-Now to find a specific person you simply tell the cashier to ask the first person in line his age, if its not the age you were looking for that person will ask the second person until you’ve found you guy. You then do whatever you want with him like so:
Now that’s about it really. You can do so much more with a linked list and its possibilities are actually good. It does have some bad sides I’ll give it that but in the end if you are in need for a straight up list of values this is the way to go.
I would recommend using binary search trees instead =).
So to summarize:
Linked list is a series of nodes which contain a value and a pointer to the next value. You have a head node which looks at the first node in the line. This head node will traverse the list with the help of all the nodes because they all are looking to the guy after them. You can delete the node in front, the last in line and any inside the list.
*The one in front by deleting the one the head is currently looking at, then tell the head to look at the second node.
*The one in the back by traversing through the list until the node that says “the guy after me is a NULL/nullptr” then delete that node and tell the node before that there are no nodes after him.
*to take away a node in the middle you have a temporary node that looks at the node before the one you want to delete. The head node find the one you want to delete. You proceed to tell the temporary node that the node AFTER the one you WANT to delete is the one after him now and then proceed to delete the node you wanted to delete.
These are displayed as the examples I just wanted to show these in a simple matter for people to understand how the three ways work. This can also be the same way you add a node with simple alterations(the first examples already explains it).
A few good links to further understand linked list:
http://www.cplusplus.com/articles/LACRko23/
http://www.cprogramming.com/tutorial/lesson15.html
http://www.bogotobogo.com/cplusplus/quiz_linkedlist.php
http://www.bogotobogo.com/cplusplus/linkedlist.php#linkedlistexample10
http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-using-C-C
http://en.wikipedia.org/wiki/Linked_list
Remember this is how I interpreted linked list as. This might be in no way a definite way to look at it. There is no code here simply an explanation on how to think linked list can work.
Stay sharp.