Problem with My C program

Asked By 7060 points N/A Posted on -
qa-featured

How is an array Different from Linked list in C? Please Answer point wise. Can A array fit into a linked list?

Please Help.

SHARE
Answered By 0 points N/A #82714

Problem with My C program

qa-featured

The problem is very easy. You need point wise the difference.

1. Arrays are of fixed length while Linked Lists are of variable lengths.

2. You can Increase the size of Linked List at runtime easily with lower time complexity than increasing size of an array.

3. Linked List requires more space than array tom store the same data.

4. Memory locations for an array are continuous but for linked lists, it is not continuous.

5. Linked list can both grow and shrink unlike an array.

6. Memory is allocated for linked lists at runtime unlike array which allocates at compile time.

 

Yes it is possible to have an array inside each node of a linked list. Instead of only one value at the node of a linked list, an entire array can be kept in the node and a pointer to the next node.

The easiest way is 

void main(){

struct node{

int *array;

struct node *next;

};

int a[3]={4,6,5};

struct node *nod=(struct node*) malloc(size of (struct node));

nod->array=a;       //array inside node

nod->next=NULL;

//display array

for (i=0;i<3;i++){

print("%d" , *(nod->array+i));

}

}

Related Questions