Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linked List Advantages Over Array

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 15

Linked List Advantages Over Array

1) Dynamic size

2) Ease of insertion/deletion

Linked List DisAdvantages Over Array

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{

struct node *next;

int data;

};

struct node *temp;

void display(struct node *);

void insert(struct node **,int);

void insertatbeg(struct node **,int);

void insertatlast(struct node **,int);

void insertatloc(struct node **,int,int);

int main()

struct node *start=NULL;

insert(&start,10);

insert(&start,20);
insert(&start,12);

insertatbeg(&start,2);

insertatlast(&start,99);

insertatloc(&start,1,50);

display(start);

return 0;

void insertatloc(struct node **list,int loc,int num)

struct node *enter_loc=*list;

struct node *temp=(struct node *)malloc(sizeof(struct node *));

temp->data=num;

int i=0;

while(i!=loc)

enter_loc=enter_loc->next;

i++;

struct node *temp2=enter_loc->next;

enter_loc->next=temp;

temp->next=temp2;

void insertatlast(struct node **list,int num)

struct node *last=*list;


struct node *temp=(struct node *)malloc(sizeof(struct node *));

temp->data=num;

temp->next=NULL;

while(last->next!=NULL)

last=last->next;

last->next=temp;

void insertatbeg(struct node **list,int num)

struct node *temp=*list;

struct node *temp2;

temp2=(struct node *)malloc(sizeof(struct node *));

temp2->data=num;

temp2->next=temp;

*list=temp2;

void insert(struct node **list,int num)

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=(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 *head)

printf("\n list is \n");

while(head!=NULL)

printf("\t %d",head->data);

head=head->next;

Program to delete node a given value in linked list

#include<stdio.h>
#include<stdlib.h>

struct node{

struct node *next;

int data;

};

void insert(struct node **,int);

void display(struct node *);

void delete(struct node **,int);

int main()

struct node *start=NULL;

insert(&start,10);

insert(&start,17);

insert(&start,8);

insert(&start,11);

display(start);

delete(&start,8);

delete(&start,11);

insert(&start,25);

printf("\n after deletion");

display(start);

return 0;

void delete(struct node **list,int num)

struct node *prev;


struct node *temp=*list;

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;

prev->next=temp->next; /* do the mapping from prev to next node*/

free(temp);/* free the temp node */

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)

printf("\n list is");

while(list!=NULL)

printf("\t %d",list->data);

list=list->next;

Program to delete a node at particular location in linked list

#include<stdio.h>

#include<stdlib.h>

struct node{
struct node *next;

int data;

};

void insert(struct node **,int);

void display(struct node *);

void delete(struct node **,int);

int main()

struct node *start=NULL;

insert(&start,10);

insert(&start,13);

insert(&start,6);

insert(&start,19);

display(start);

delete(&start,0);

printf("\n after deletion \n");

display(start);

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 delete(struct node **list,int loc)

struct node *prev;

struct node *temp=*list;

int i=0;

if(loc==0)/* for deleting the first loc or first node*/

*list=temp->next;

free(temp);

return;

while(i!=loc)

prev=temp;
temp=temp->next;

i++;

if(temp==NULL) /* if loc exceeds total number of avalaible nodes*/

return;

prev->next=temp->next;

free(temp);

void display(struct node *list)

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?

Ans: Allocating array memory from Heap Section:

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.

Type of a node in linked list (i.e. underlying array) is declared as follows:


struct sllNode
{
int dataInt;

/*Here, note that nextIndex stores the location of next node in


linked list*/
int nextIndex;
};
struct sllNode arrayLL[5];

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

To Calculate number of nodes using iterative and recursive methods


#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
void insert(struct node **,int);
void display(struct node *);
void count_iter(struct node *);
int count_rec(struct node *);
int main()
{
struct node *start=NULL;
struct node *start2=NULL;
int count;
insert(&start,3);
insert(&start,2);
insert(&start,4);
insert(&start,6);
printf("\n list is \t");
display(start);
count_iter(start);
insert(&start2,5);
printf("\n list is \t");
display(start2);
count=count_rec(start2);
printf("\n recursive operation --> number of nodes = %d",count);
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;
}
}
void count_iter(struct node *list)
{
int count=0;
while(list!=NULL)
{
count++;
list=list->next;
}
printf("\n Iterative process ---> number of nodes = %d",count);
}
int count_rec(struct node *list)
{
if(list==NULL)
{
return 0;
}
else
{
return 1+count_rec(list->next);
}
}

To search a key using iteration and recursive formula


#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *next;
int data;
};
void insert(struct node **,int);
void display(struct node *);
int search(struct node *,int);
int search_rec(struct node *,int);
int main()
{
struct node *start=NULL;
int res;
insert(&start,3);
insert(&start,2);
insert(&start,4);
insert(&start,7);
printf("\n list is ");
display(start);
printf("\n item avalaible ? ");
res=search(start,4);
if(res==1)
printf("YES");
else
printf("NO");

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);
}

You might also like