We will be looking at singly linked lists with TypeScript in this post. Linked Lists are a commonly occuring data structure, and this blog will help you understand how to implement one using Typescript.
Data structures lie at the heart of programming, and often come in handy when you least expect them to. And even when they don’t, you do definitely need them to crack a lot of interviews.
Linked Lists are a commonly occuring data structure, and this blog will help you understand how to implement one using typescript.
We will be looking at singly linked lists in this post.
To keep things interesting, we will use a pretty cool use case. Gaming is one of my favourite hobbies, and so we’ll use linked lists to calculate the damage done by a character at the end of an attack sequence.
If we imagine a character attacking another character, then in the best case this particular character should be able to land an indefinite number of hits until the HP of the victim is exhausted.
So we have an uncertain length of attack input that can be received already.
Afterwards, we will need to calculate the entire attack chain for reflecting the aggregate damage, and apply special buffs such as move multipliers if necessary.
Linked lists can come in handy here, as we can use them to accommodate an uncertain amount of nodes that are being inserted.
And these nodes do not have to be contiguous, unlike arrays, so we can distribute them across memory space and link them using pointers instead.
This also helps eliminate the problem of shifting nodes up and down using the same contiguous memory block, or wasting memory as arrays often allocate an upper limit for storing elements. Which might never be reached.
A disadvantage however, is accessing particular nodes to inspect their stats. This could have been faster in arrays, but in our case we only care about the aggregate damage done.
We can recalculate the total damage done each time a node is added or removed.
So, let’s get started.
With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...
You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...