Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 27, 2021 03:58 pm GMT

Arrays and Lists

TL;DR: Arrays are fast and compact. Linked lists are not as compact, but adding or removing elements from them can be more efficient!

You probably already know how to use arrays and are familiar with methods like push, pop, etc. The purpose of this issue is to give you an understanding of arrays as a data structure and how they differ from lists.

**Why would you care?* For those who are preparing for a technical interview or would like to deepen your knowledge of computer science, this is the article for you.*

Arrays and lists are easy to mix up, so lets try to make sense of what the differences are and how they work!

You can think of memory as a bookshelf:

bookshelf.png

Now imagine you got the first four volumes of Game of Thrones :

4-volumes.png

This is your array. Now you wouldnt want to store these volumes separately: if you find volume 1 you want to be able to find volume 2 easily. So youll find a place were you can put the four of them together. Luckily, our top shelf has room for exactly four volumes:

bookshelf-with-4-volumes.png

You found a spot for these, great. But, surprise! A friend comes over and gives you volume 5

You run to your bookshelf, only to realize, you dont have any room left next to volume 4

Reorganizing the bookshelf to make more room near volume 4 would work, but that seems like a lot of effort

Instead, were going to move the first four volumes to a larger empty space. Now we can add the fifth volume!

volume-5-fits.png

Phew, that wasnt easy! These volumes are heavy, and you dont want to go through the hassle of moving all of them every time you dont have enough room for a new one.

So what are your options here? Well, we tried to do it the array way: put each volume next to each other, and move the entire series when you dont have enough room for another volume. What if we tried to do it the list way instead?

Well need a little bit of setup before getting into this. We are going to assign a letter to each shelf, and a number to each column of book:

bookshelf-with-coordinates.png

Now, each location on our bookshelf has a set of coordinates. For example, the third book of the top shelf is at position A2.

Instead of storing our volumes next to each other, we are going to put the first volume anywhere in the bookshelf:

bookshelf-volume-1.png

And now, were going to do the same with the second volume. Once weve found a location for it, were going to add a note next to the first volume with the coordinates of the second volume:

bookshelf-volumes-1-2.png

We can do this until we reach the fourth volume, where the note will be empty since we dont have our fifth volume yet:

bookshelf-4-linked-books.png

We just made a singly linked list: we have a single note next to each book that tells us where to find the next one.

Now you might be thinking, this is great, but why do I have to waste so much space storing those notes?

And you would be right: linked lists take up more space than arrays! You give up some space, but you dont have to worry about moving things around to add a volume to your series. As long as you have space for a book and a note, everythings fine! Removing a volume in the middle is also a lot simpler: if you want to remove volume 2, just change the note after volume 1 with the coordinates of volume 3.

Key takeaway: trade-offs. When discussing the differences between arrays and lists, it is important to understand when and why to use one or the other knowing this will make you stand out during any technical interview!

[1]: Usually, people use **list* to mean linked list, and more specifically singly linked lists. There are other types of lists, but singly linked lists are the most common type.*

[2]: Technically this might not always be true, because of the way programming languages handles memory allocation for arrays. You can read more about this here.

Tip of the week

You can use git switch - to switch back to your previous branch:

(main) git switch other-branch(other-branch) git switch -(main)

Learn more

What else is going on in tech?

PS: We would love to know how you felt about this article, did you find it helpful, and are there topics youd love to see covered? Tag @nspiredTech on Twitter if theres anything you want to share :-)


Original Link: https://dev.to/nspired/arrays-and-lists-1gc7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To