Linked Lists
Instead of using a basic array to implement linear data structures such as queues or stacks, you can actually use something called a linked list
A Linked List is basically a way of formulating data inside of a program. The way that it works is you have different nodes that are scattered throughout memory. Each of these nodes is a class or a union or whatever it might be that stores this memory next to the other values inside of RAM right. But basically each of these nodes holds two values, generally both integers but not necessarily. This being the actual value that belongs to that node, as well as then a pointer to the next node in the linked list.
There are many variations of linked lists, in that there are those who have two pointers, one pointing up the chain and one down the chain, but either way a linked list is a bunch of different nodes scattered in memory and connected by pointers.
Why would someone do this? Well because it allows for a much easier time with allocation of new items in the linked list, with a dynamic array, if you want to have your items stored in a continuous chunk of memory you need to pre allocate it to a given size, and then grow it and copy over the array whenever you run out of room, not to mention you cannot store different values either.
This is fixed inside of a linked list because you can directly shrink or grow the list by editing the places that the nodes point to, you can append an item to the list by creating a new node, populating it with a value, and then having the previous node in the chain point over to the new node. You can then take this node away simply by just nullifying the pointer that points to it.
They can be really good for things like stacks and queues where you might want to dynamically allocate the items for these data structures, the only issue being that the under the hood implementation with them is harder in the code because of the fact that you need to deal with pointers pointing at objects, it is still basically the same as a regular array, but the fact that the data is scattered across RAM means that you cannot just do simple pointer math to get to your given item
Traversing around a linked list
Well so the way that you traverse around a linked list would be to have something like a temporary pointer variable, basically an int, and you would then have temp be set to something like the head of the linked list right.
You deference the temp pointer, aka the head pointer, and then you get the first node in the linked list. With this you can access the data at this node, this being the actual data value as well as the next pointer to the next node. So basically your temp now becomes the pointer to the next node, and then you deference and the whole process starts again.
You can then know that you have reached the end of the linked list if whenever you are deferencing the node and it’s pointer field is null, that means there are no more nodes in the list and you’ve reached the end
Editing a Linked list
At the Start
So editing a linked list, or well, adding in new data is a lot more simple than how it might be given a static array. If you want to add a new item at the front for example, all that you do is create the node, assign the value you want it to hold, and for the pointer field you just make it equal to the current head
You then basically create a new pointer to the node that you just created, and then boom, that becomes the new start of your linked list.
At the End
If you want to add a node to the end of the linked list, then the time complexity kind of depends, if you have a traditional single linked list with no tail pointer, then you have to traverse the entire list to get to the ending point in null and then create your new node and point the end to it
However this changes if you have something like a tail pointer, because then you just access the end of the list directly, create the node, add the value and make the pointer null, and then make the current tail node point to the new tail node and then update your tail pointer
Somewhere in the middle
Adding a pointer somewhere in the middle is most often going to be a O(n) operation, you traverse the linked list until you end up hitting the index that you wanted to hit, and then you create your node right, what you do is you give it the value that it has, copy over the pointer of the node before where you want it to be, so it points to the correct next node, and then create a new pointer to the created node for the node to the left of it so you have the correct order and there is no break in the list
