Linked List Advantages Over Array
Linked List Advantages Over Array
Linked List Advantages Over Array
1) Dynamic size
2) Ease of insertion/deletion
1) Random access is not allowed. We have to access elements sequentially starting from the first
node. So we cannot do binary search with linked lists.
2) Extra memory space for a pointer is required with each element of the list.
3) Arrays have better cache locality that can make a pretty big difference in performance.
Inserting data in a linked list at beg,mid and end using insert functions
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
int main()
insert(&start,10);
insert(&start,20);
insert(&start,12);
insertatbeg(&start,2);
insertatlast(&start,99);
insertatloc(&start,1,50);
display(start);
return 0;
temp->data=num;
int i=0;
while(i!=loc)
enter_loc=enter_loc->next;
i++;
enter_loc->next=temp;
temp->next=temp2;
temp->data=num;
temp->next=NULL;
while(last->next!=NULL)
last=last->next;
last->next=temp;
temp2->data=num;
temp2->next=temp;
*list=temp2;
struct node *last=*list; /* we have taken last pointer to get the starting
address of list and traverse till end otherwise *list->next would have given an error */
temp->data=num;
temp->next=NULL;
if(*list==NULL)
*list=temp;
else
while(last->next!=NULL)
last=last->next;
last->next=temp;
while(head!=NULL)
printf("\t %d",head->data);
head=head->next;
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
};
int main()
insert(&start,10);
insert(&start,17);
insert(&start,8);
insert(&start,11);
display(start);
delete(&start,8);
delete(&start,11);
insert(&start,25);
display(start);
return 0;
if(temp!=NULL && temp->data==num) /* if the first element to be deleted just start list from
second element */
*list=temp->next;
free(temp);
return;
while(temp!=NULL && temp->data!=num) /*find prev and next loc too delete the num */
prev=temp;
temp=temp->next;
if(temp==NULL)
return;
temp->data=num;
temp->next=NULL;
if(*list==NULL)
*list=temp;
else
while(last->next!=NULL)
last=last->next;
last->next=temp;
while(list!=NULL)
printf("\t %d",list->data);
list=list->next;
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *next;
int data;
};
int main()
insert(&start,10);
insert(&start,13);
insert(&start,6);
insert(&start,19);
display(start);
delete(&start,0);
display(start);
return 0;
temp->data=num;
temp->next=NULL;
if(*list==NULL)
{
*list=temp;
else
while(last->next!=NULL)
last=last->next;
last->next=temp;
int i=0;
*list=temp->next;
free(temp);
return;
while(i!=loc)
prev=temp;
temp=temp->next;
i++;
return;
prev->next=temp->next;
free(temp);
while(list!=NULL)
printf("\t %d",list->data);
list=list->next;
Qus. If we need to allocate array memory from Heap section (i.e. at run time) and linked list
memory from Data/Stack section.Is it possible?
The case when we need to store certain data in array but we dont know the total size apriori. One
possibility is to allocate memory of this array from Heap at run time.
For example,
/*At run-time, suppose we know the required size for integer array (e.g. input size from user). Say,
the array size is stored in variable arrSize. Allocate this array from Heap as follows*/
int * dynArr = (int *)malloc(sizeof(int)*arrSize);
Though the memory of this array is allocated from Heap, the elements can still be accessed
via index mechanism e.g. dynArr[i].
Basically, based on the programming problem, we have combined one benefit of array (i.e.
random access of elements) and one benefit of linked list (i.e. delaying the memory allocation
till run time and allocating memory from Heap).
Another advantage of having this type of dynamic array is that, this method of allocating
array from Heap at run time could reduce code-size
Allocating Linked List memory from Stack Section
The case when we need to store data in a linked list but we arent allowed to get this memory from
Heap again and again for each node. Eg. Malloc() is an expensive operation for embedded systems.
If we initialize this linked list (which is actually an array), it would look as follows in memory:
[(0),-1] [(0),-1] [(0),-1] [(0),-1] [(0),-1]
0x500 0x508 0x510 0x518 0x520
This (i.e. -1) is done to denote that the each node of the linked list is empty as of now.
This linked list is denoted by head index 0
res=search_rec(start,9);
if(res==1)
printf("\t YES");
else
printf("\t NO");
return 0;
}
void insert(struct node **list,int num)
{
struct node *last=*list;
struct node *temp=(struct node *)malloc(sizeof(struct node *));
temp->data=num;
temp->next=NULL;
if(*list==NULL)
{
*list=temp;
}
else
{
while(last->next!=NULL)
{
last=last->next;
}
last->next=temp;
}
}
void display(struct node *list)
{
while(list!=NULL)
{
printf("\t %d",list->data);
list=list->next;
}
}
int search(struct node *list,int key)
{
while (list!= NULL)
{
if (list->data == key)
return 1;
list = list->next;
}
return 0;
}
int search_rec(struct node *list,int key)
{
if(list==NULL)
return 0;
if(list->data==key)
return 1;
return search_rec(list->next,key);
}