Linked List Vs Arrays in C
A Linked list is a self-referential class i.e. it contains a reference member and that member contains the address to a class object of the same class type. Basically, a linked list is an array of connected objects, where each object behaves as an independent variable. A Linked List can also be known as a chain where each link in the chain contains the address to the next link.
A linked list is very similar to an array, but unlike an array, the objects are not bound. Say, for example, there’s an array of 10 items and all these items can be referred to by a single variable containing them, whereas in a linked list, all these are as 10 different variables. This seems to be a bit complex, but it ha an advantage that in order to increase the size of the list, simply a new variable is created.
A linked list is very flexible and dynamic. It is best suited to be used at places such as - items in a database index; whose size is not specifically known, or to represent events or units in a game; where the number of turns at any time is almost uncertain. Linked lists have two major problems- firstly; they're more difficult to find elements in it and secondly there is a time overhead in allocating and freeing elements as required or the ones no longer required for the list since most machines use virtual memory in some form to aid a smaller-than-required RAM.
An array is a compound type composed of many variables, all of the same type and organized in a contiguous (sequential) memory location. Like a row of boxes, each box may contain a different value, but all boxes are of the same type.The allocation of memory for an array can be done in two ways – static or dynamic
The allocation of memory for the specific fixed use in a predetermined way controlled by the compiler is called static memory allocation. Static memory means to reserve a certain amount of memory by default inside our program to use for variables. Once such a memory is reserved, no other program can use it, even if it is not being used at that time. The one discussed above is a static array.
The allocation of memory during the running of a program is called dynamic memory allocation.
|