Arrays v/s  Linked Lists

Arrays v/s Linked Lists

ยท

2 min read

Array

  • Array is a group of elements having same datatype which means it can only store elements which are having same data type.

  • Elements in array can be easily accessed by using index value.

  • Index value in array refers to unique value where each element has its own index value it starts from 0 and goes till size -1.

  • Array is linear data structure which means each element is stored one after the another.

  • Array uses static memory allocation - size of the array is predetermined before the execution of the program.

  • Insertion and Deletion operations in array are costly.

10.png

Following code snippet is a example of array in c++ -

To create array of n size ->

int n;  // n is the size of array
cin>>n;
int arr[n];   //syntax of creating array - data type arrayname[array size]

Linked List

  • Linked list is a linear collection of data elements , called nodes pointing to the next nodes by means of pointer.

  • It is a collection of nodes where each node is divided into two parts data part which contains value and address part which contains the address of next node.

  • Linked list uses dynamic memory allocation - size is not predefined, memory is allocated during the execution of the program.

  • It is a linear data structure.

  • Execution of Insertion and deletion operations in linked list is easier.

linkedlist.png

Following code snippet is a example of Linked list in c++ -

class Node{      //name of the linked list is Node
    public:
    int data;            //data part of the node
    Node *next;    //address of next element
    Node(int val)  //Constructor to initialize values
    {
        data=val;
        next=NULL;
    }
};

Did you find this article valuable?

Support Sayam by becoming a sponsor. Any amount is appreciated!

ย