what is the difference between arrays and linked list

Answer Posted / jaroosh

>Array is a simple sequence of numbers which are not
>concerned about each-others positions.
This is not true, what about arrays of objects ? They may be
concerned about each other in some way.

>adding,removing or modifying any array element is very >easy.
This is untrue also, adding, removing elements in array is
significantly more complex than removing or adding elements
in linked list. This is because removing elements from array
causes all elements after it to be shifted back, whereas
with linked list, its merely traversing the list to find the
node, previous node, and the next node and setting pointers,
and inserting element into array will probably call for
1) resizing the whole array size (ie. assingning a new,
bigger memory chunk for it)
2) copying elements from the former smaller array to the new
- bigger one.
This is a huge trade off, provided that in list, its simply
matter of setting the NEXT pointer of one of the nodes.

Main differences between the two are:
1) arrays are RANDOM ACCESS structures, where you can access
elements in random/indexed manner, whereas list is a
sequential access structure. This makes such algorithms like
heap sort or binary search to work much faster on arrays
2) arrays are static/fixed size whereas lists are dynamic
size structures. It means that when creating an array (both
on stack or heap), you HAVE to specify its size. With lists,
you just create an empty list and freely expand it
3) array consist of continuous chunks of memory, ie. nth
element is at the memory location of :
address_of_array + sizeof(array_element_type) * n
this always holds true, that is why following will always work :
for(int i=0;i < ARRAY_SIZE; i++)
cout << *(array++);
List is a sequence of nodes, connected by NEXT pointers, so
consequent nodes may lie WHEREVER in memory

Is This Answer Correct ?    122 Yes 22 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

When would you use a pointer to a function?

590


What is printf () in c?

579


to print the salary of an employee according to follwing calculation: Allowances:HRA-20% of BASIC,DA-45% of BASIC,TA-10%. Deductions:EPF-8% of BASIC,LIC-Rs.200/-Prof.Tax:Rs.200/- create c language program?

1574


What is the difference between near, far and huge pointers?

629


Does c have an equivalent to pascals with statement?

575






Explain the difference between malloc() and calloc() in c?

580


How can I implement sets or arrays of bits?

608


What is hashing in c?

645


What is the use of header files?

607


What is string in c language?

628


How can I manipulate individual bits?

609


Is fortran still used in 2018?

596


What is the difference between struct and union in C?

576


What will be your course of action for a push operation?

669


What is meant by keywords in c?

618