Common data structures and when to use each
--
Learning data structures could be difficult for someone new to coding. It does help a new coder understand the power of recursion and Object Oriented Programming in driving more efficiency out of your system.
For example, imagine that you installed a new operating system and starting the operating system took 10 minutes for the system to load. If a developer you know can make the operating system start in 30 seconds instead, wouldn’t you be stoked?
Similarly, if you installed a desktop application and it took 5 minutes to do a complex computation and on the next version update, the same operation took only 2 seconds, wouldn’t you be excited?
Efficient algorithm helps make happy customers. For web applications, understanding how to build data structures is not as important as if you were building a new operating system, database, or programming language from scratch. However, some of the principles, such as looking at the time-complexity of the software and knowing what Big O notation your function is operating is still important.
For this article, let me address different common data structures and when each could be used.
Array
Array is a common data structure that’s used very frequently. Searching for a particular value in the array O(N). If you want to insert a new value in the middle of the array, you’ll also need to shift the values. Therefore, insertion or deletion for an array takes additional O(N). This means if you are looking for a specific value in the array and wanting to insert another value right after, or delete that value, total big O for that is O(N) + O(N) = O(2N).
Linked List
Linked list (whether it’s a singly linked list or doubly linked list) is great for adding information sequentially. If you need to build a queue system or a stack system, a linked list is the way to go. It’s also quite simple to implement.
For a linked list, searching for a particular value in the linked list is O(N) as you would need to start from the first item and traverse through each item. Once you find a specific node to delete or where you want to insert a new node, that operation is only O(1). Therefore, if you were searching for a…