Briefly distinguish between Arrays and Linked List. Write an algorithm for each as well as develop computer programs that implement each of them. Show the results of your output including your code.
Answer : Given data
Let's understand how array is different from Linked list.
ARRAY | LINKED LIST |
---|---|
Array is a collection of elements of similar data type. | Linked List is an ordered collection of elements of same type, which are connected to each other using pointers. |
Array supports Random Access, which means elements can be
accessed directly using their index, like Hence, accessing elements in an array is fast with a constant
time complexity of |
Linked List supports Sequential Access, which means to access any element/node in a linked list, we have to sequentially traverse the complete linked list, upto that element. To access nthelement of a linked list, time complexity is
|
In an array, elements are stored in contiguous memory locationor consecutive manner in the memory. |
In a linked list, new elements can be stored anywhere in the memory. Address of the memory location allocated to the new element is stored in the previous node of linked list, hence formaing a link between the two nodes/elements. |
In array, Insertion and Deletionoperation takes more time, as the memory locations are consecutive and fixed. |
In case of linked list, a new element is stored at the first free and available memory location, with only a single overhead step of storing the address of memory location in the previous node of linked list. Insertion and Deletion operations are fast in linked list. |
Memory is allocated as soon as the array is declared, at compile time. It's also known as Static Memory Allocation. | Memory is allocated at runtime, as and when a new node is added. It's also known as Dynamic Memory Allocation. |
In array, each element is independent and can be accessed using it's index value. | In case of a linked list, each node/element points to the next, previous, or maybe both nodes. |
Array can single dimensional, two dimensional or multidimensional | Linked list can be Linear(Singly), Doublyor Circular linked list. |
Size of the array must be specified at time of array decalaration. | Size of a Linked list is variable. It grows at runtime, as more nodes are added to it. |
Array gets memory allocated in the Stack section. | Whereas, linked list gets memory allocated in Heaps |
Implementation of Array in a program
algorithm
START Step 1 → Take an array A and define its values Step 2 → Loop for each value of A Step 3 → Add each element to 'sum' variable Step 4 → After the loop finishes, display 'sum'
STOP
IMPLEMENTATION OF ABOVE ALGORITHM IN C PROGRAM
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sum, loop;
sum = 0;
for(loop = 9; loop >= 0; loop--) {
sum = sum + array[loop];
}
printf("Sum of array is %d.", sum);
return 0;
}
The output should look like this −
Sum of array is 45.
Implementation of linked list in a program
Algorithm:
1.first=new node;{create the 1st node of the list pointed by first};
2.Read(Data(first));
3.NEXT(First)=NULL;
4.Far a First; [point Far to the First]
5. For I=1 to N-1 repeat steps 6 to 10
6.X=new node;
7.Read(Data(X))
8.NEXT(X)=NULL;
9.NEXT(Far)=X; {connect the nodes}
10.Far=X;[shift the pointer to the last node of the list]
[end of For Loop]
11.END
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//display the list
void printList() {
struct node *ptr = head;
printf("\n[head] =>");
//start from the beginning
while(ptr != NULL) {
printf(" %d =>",ptr->data);
ptr = ptr->next;
}
printf(" [null]\n");
}
//insert link at the first location
void insert(int data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
//link->key = key;
link->data = data;
//point it to old first node
link->next = head;
//point first to new first node
head = link;
}
int main() {
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);
printList();
return 0;
}
Output
Output of the program should be −
[head] => 56 => 40 => 1 => 30 => 20 => 10 => [null]
_______________THE END________________
Get Answers For Free
Most questions answered within 1 hours.