Home » Interview Questions » Interviews » Story Details
Printable Version

Interview Question: Difference between linked list and Arrays

by Kamal Rawat on Dec 28, 2011

What is the difference between linked list and arrays.
Comments: 0    Views: 794

MCN Professionals | Interview Question of the day

MCN Professionals is starting Industrial Training for MCA-2012 Batch. Have a look at our Industrial Training Program and Course Details.
-----------------------------------------------------------------------------------------------------------------------------------------

Today's Question:

What is the difference between Linked List and Arrays?

Solution:

Before talking about the differences, lets see the similarities:

1. Both are collection of elements.

2. Both Linked List and Arrays are homogenous collection of elements. i.e all the elements in a linked list (or an Array) has to be of the same type. All Nodes in linked list have same data type. (You may argue, that we can have an array of void pointers each of which can point to element of different data type, like below:

char ch;
int ivar;
void * arr[2] = {&ch, &ivar};

but in the above case also the data type of both the elements in array is same (void *).

Differences:

1. Array is an indexed collection of elements. i.e arr[i] represents elements at i'th index in the array arr. Linked list's element cannot be accessed using index, but you have to traverse to that particular element. 

Hence, accessing i'th element in the array is constant operation (memory look-up) while accessing i'th element in a linked list requires i steps (moving to the i'th node).

2. All the elements in an array are stored contiguously in memory (as a single block). Elements of linked list may be scattered in memory (that's why need a pointer to access the next element).

3. You can apply sizeof operator on an array to get the size of entire array. No such thing can be applied on Linked List.

4. Array's cannot be expanded. Even if you have an array on heap, it cannot be expanded once the memory is allocated. (You may counter argue, that we have realloc, but believe me that's not what we mean by expanding at the same place).

5. You can apply both linear &  binary search in an array (for binary search, the array must be sorted though). For linked list, you have to rely on linear search only (even if it is sorted).

6. Inserting an element in a Linked List is easy, but in an array it may be time consuming. For example: if we have a sorted array (of size 10) like below (last 4 positions are empty which are represented by _):
    2 4 6 8 10 15 _ _ _ _
and we want to insert 7 in this sorted array, then we have to shift 3 elements by one position. 
    2 4 6 7 8 10 15 _ _ _ 

In case of linked list pointers could have been manipulated and no shifting would have been required.

7. Similar to insertion, deletion of an element in an array is also time consuming O(n).

8. In Linked List extra pointer is required to store the address of head node of the array. In case of array, the name does not take physical memory like pointers.

9. Locality of reference is very good in arrays. Suppose if you are reading an array of size 100, then all the elements will be adjacent to each other, If that array is stored on hard disk, then the head does not need to be moved much, but in case of linked list, the physical location of elements may be scattered and hence lot od head movement may require (for externally stored list).

10. Sorting is easy in arrays, there are multiple algorithms which can be used (even the non-comparison sorting algorithms like counting sort). To sort a lined list, we don't have much optimized options.


---------------------------------------------------------------------------

Interview Questions Archive:

         To see all the questions in the category click here...

Post a Comment
*
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative Suite® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName