In: Computer Science
What is the difference between Array and Linkedlist.
What is Array with example?
What is Linkedlist with example?
What is the difference?
Both Array and Linked list are used for storing data of same type.
Thus, array is collection of elements of same type.
int a[10]; //declare integer array , a containing space for 10 elements.
Indexing in array starts from 0. Thus n elements are stored from index 0 to n-1 in array.
simple program in c:
#include<stdio.h>
void main()
{
int a[100],i,n;
printf("Enter no. of elements\n");
scanf("%d",&n);
printf("Enter elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Elemets of array:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
Linekd list is used to store elements of same type using pointers.
Node structure of singly linked list is as:
struct node
{
int data; //data field storing element
struct node *next; //pointer to next node (link)
};