Array vs. Linked List

Array vs. Linked List

Array is a data chunk allocated in a specific address on heap, while its start address (address of first item) is stored on stack. Items of an array are stored next to each other so they can be accessed by calculating item address by adding number of bytes to the start address (ex. 4 bytes for int) multiplied by required index.


Example:

int[] arr = new int[] {1, 2, 3};

If address of arr is 0x1000

then address of arr[1] is 0x1000 + (4 * 1) = 0x1004


On the other hand Linked List is a set of data chunks allocated using dynamic memory allocation (one by one, not like array in which all items are allocated when it is initialized). It is also stored on heap and address of first item is stored on stack. Items of linked list are not stored next to each other in memory, so they need a kind of link between its items to be accessed and not get lost in memory. In a simple linked list it is a pointer to the next item and items can be accessed by traversing from the start address (address of first item) until the required item is reached, which is slower than access mechanism of array because it fetches many addresses while in array it is just a simple calculation and one direct fetch.


Conclusion:

Array is faster than Linked List in read/access operation.


Try to think about these operations too:

– Adding an item

– Deleting an item

– Updating an item

要查看或添加评论,请登录

Ahmed Abdelkarim的更多文章

  • Stack & Queue

    Stack & Queue

    Two of the most popular data structures. They're used heavily by developers, and operating systems as well.

  • IoT (PIC, Arduino, and Raspberry Pi)

    IoT (PIC, Arduino, and Raspberry Pi)

    Recently there has been a so-called Internet of Things (IoT) which is a way to control hardware remotely or automate…

  • Compile and Run C# code in string variable at runtime

    Compile and Run C# code in string variable at runtime

    If you have a string variable contains C# code, and you want to compile that code to an executable application then run…

    2 条评论
  • Brokered Windows Runtime Component in Windows Store apps (Access Serial COM Port)

    Brokered Windows Runtime Component in Windows Store apps (Access Serial COM Port)

    You're welcome and welcome. I'm here to help.

    6 条评论
  • GOOD HR

    GOOD HR

    To be a good HR specialist: Dive into every single human, get own needs, merge needs, produce an action or an activity…

  • SUCCESS

    SUCCESS

    Success is a journey, it is not a destination.

社区洞察

其他会员也浏览了