vurpearl.blogg.se

Kotlin linked list
Kotlin linked list







kotlin linked list

In Array, there is no such node concept as it contains only a set of values in it and is hence used as a default data structure in many modern programming languages. In the Linked list each element is considered as one single node where these nodes are connected using pointers which are the address value of the next node and other than address it also stored data for each node. It is also a data structure that stores a collection of elements with the same data type and it only stores values unlike a linked list it does not have any data and address as its values. It is a data structure of the set of elements that contain two parts in it such as data and address which is a pointer to the next element and each element is of the same data type, unlike simple lists which are generic. Whereas, the time taken by the linked list for inserting and deleting elements is faster than the array as it stores any new element in the first free space which is available in memory and uses separate memory to store its pointers also and hence the space utilization is more compared to the array.

kotlin linked list

In Array, the time taken for inserting and deleting elements is more as it uses contiguous memory location which is always fixed in size at the initialization where it cannot be changed later and therefore the space used is to store the entire array and not single elements which require less space. Whereas in the linked list also the insertion is simple and easy if we know the pointers to each element or the addresses for example if we are inserting an element at 3rd location then we need to change the addresses of the element at the 2nd location which should now contain the address of the new element and the new element address should contain the address of the element of 4th location which was previously in 3rd location. In Array, the insertion of elements is very simple and easy as it uses array indices which starts the indexing with 0, and hence it allows random access to the elements for example if in the array we want to insert an element at 3rd location where the index will be “2” so we just need to move the elements from 3rd element which was in index “2” ad becomes index “3” after insertion of the new element. Some of the key differences between Linked List vs Array are given below: Insertion of Elements

kotlin linked list

Key Differences between Linked List vs Array









Kotlin linked list