it would be best if you learn to learn from the web, but ok, why not.
the backbone of a linked list is the idea that a container can contain a pointer to its own type. That is a mouthful, so stop here and re-read that about 20 times until it sinks in. Here is an example of exactly that:
1 2 3 4 5
|
struct list
{
list * next;
int data;
};
|
that is really 90% of it.
This is sort of like looking into a pair of mirrors. a list has a pointer to a list that has inside it a pointer to a list which has inside it a pointer to a list... it can go on forever (in theory. computers have limits).
now you just "link" up a list of them. Doing that requires you to fully understand pointers, dynamic memory, and how to code using those things without making a mess. Making a list is a great exercise to drive home managing your pointers and teach you to be very careful when using them. I assume you fully understand pointers, but if you do not, and you pursue writing this, you soon will <g>.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <cstddef>
#define null 0 //online compiler is being stubborn
using namespace std;
struct list
{
list * next;
int data;
};
int main()
{
list *x = null; // an empty list
x = new list; //add a value to the list:
x[0].data = 3;
x[0].next = null; //important, the next that is null marks end of a list
//now we have a list with a single element inside.
//repeat this idea: add would be a function but here I just put the code inline
list * tmp = new list; //create a new 'node' or item in the list
tmp[0].data = 2;
tmp[0].next = x; //the new one is now linked to the original, in front of it.
x = tmp; //the original is replaced by the new chain. 2->1 -> null
tmp = new list; //one more time
tmp[0].data = 1;
tmp[0].next = x; //1 -> 2 -> 3 -> null
x = tmp;
//and we now have a list with 1,2,3 inside.
tmp = x; //you must not 'lose' the list itself, so you use temp pointers to navigate it. this is critical!!!
while(tmp) //while you are not at the null end of list pointer
{
cout << tmp[0].data << endl; //do something to data
tmp = tmp[0].next; //move to next item
}
}
|
I have linked it as a stack, inserting at the top which is the most efficient way. you can insert in the middle or end, but you must manage all the pointers when you do that, and it becomes complex. You should do this once you get the basics working. You also need a delete, where you pull one out, fix the pointer chain, and delete the bad item's memory. You may want a search, to find a particular item. from there you just grow it to do what you need it to do.
Im not going to code all that, but how would you insert a value between 2 and 3?
you have to find the 2, and save a pointer to it.
then you need to have a pointer that holds two's old next, the 3 value item.
then you make a new item, as shown, outside the chain.
at this point you have 2,3 saved in temp pointers and newitem as well.
the new item should point its next to two's old next, the pointer you saved holding the 3 node.
now you have a strange Y shaped setup where 1->2->3 is a chain AND newitem->3 is a second chain. This is just an intermediate step so its ok.
Now you need to set two's next to the new one.
this completes it, the chain becomes
1->2->new->3
notes: you should probably use nullptr when you do this.
-> can be used instead of array syntax. So can *var syntax. Pointers unfortunately have many syntaxes to do the same thing.
give it a try now, if you get stuck post code and ask.
utoob has a comment section so you MAY be able to ask there. I wouldn't, but its possible. The obsession with videos is amusing to me as well, they present 5 seconds of info over 5 min. It is not a good way to teach code. Its a good way to teach how to change the oil in your car, so you can see the stuff being talked about.