What is difference between array and linked list

ANS:- The similarities of the array and a linked list are few.

They are both structures that can hold data, and the type of data they hold is defined by the programmer. However, an array is navigated by the index of the array, and the array is fixed in size at compile time.

The linked list is navigated by running up and down the 'next' and 'previous' pointers in the elements of the list. These pointers point to the next and previous elements in the list, with the top and the bottom of the list being indicated by the previous pointer of the first element pointing to NULL (nothing comes before the first element) and the next pointer of the last element in the list pointing to NULL (nothing comes after the last element).

Linked lists can have elements removed. you make the next pointer of the element in the list BEFORE the one to be removed point to the element AFTER the one to be removed, and make the previous pointer of the element AFTER the one to be removed point to the element BERFORE the one to be removed. That means no element points to the one to be removed, and then you free up the memory of the one to ber removed.

You can also add elements by allocating the memory for a new element, and you can insert it into the list at any point: start, end or middle, by fixing the pointers in the appropriate elements.

A linked list can contain many types of data in each element, depending on the structure of each element.

No comments:

 
Top Blogs