You are on page 1of 4

Array is a datatype which is widely implemented as a default type,

in almost all the modern programming languages, and is used to store data of
similar type.
But there are many usecases, like the one where we don't know the quantity of data
to be stored,
for which advanced data structures are required, and one such data structure is
linked list.

Array :

1)Array is a collection of elements of similar data type.


2)Array supports Random Access.
3)Elements can be accessed directly using their index.
4)Array is fast with a constant time complexity of O(1).
5)In an array,elements are stored in contiguous memory location or consecutive
manner in the memory.
6)In array,Insertion and Deletion operation takes more time, as the memory
locations are consecutive and fixed.
7)Memory is allocated as soon as the array is declared,at compile time. It's also
known as Static Memory Allocation.
8)In array, each element is independent and can be accessed using it's index value.
9)Array can be single dimensional, two dimensional or multidimensional.
10)Size of the array must be specified at time of array decalaration.
11)Array gets memory allocated in the Stack section.

Linked List :

1)Linked List is an ordered collection of elements of same type, which are


connected to each other using pointers.
2)Linked List supports Sequential Access.
3)Access any element/node in a linked list, we have to sequentially traverse the
complete linked list, upto that element.
4)To access nth element of a linked list, time complexity is O(n).
5)In a linked list, new elements can be stored anywhere in the memory.
6)Insertion and Deletion operations are fast in linked list.
7)Memory is allocated at runtime, as and when a new node is added. It's also known
as Dynamic Memory Allocation.
8)In case of a linked list, each node/element points to the next, previous, or
maybe both nodes.
9)Linked list can be Linear(Singly) linked list, Doubly linked list or Circular
linked list linked list.
10)Size of a Linked list is variable.It grows at runtime,as more nodes are added to
it.
11)linked list gets memory allocated in Heap section.

**************************************************************
Write a program to reverse the linked list

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

struct node
{
int data;
struct node *next;
}*head;
void List(int n);
void reverseList();
void displayList();

int main()
{
int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
List(n);

printf("\nData in the list \n");


displayList();

printf("\nPress 1 to reverse\n");
scanf("%d", &choice);

if(choice == 1)
{
reverseList();
}

printf("\nData in the list\n");


displayList();

return 0;
}

void List(int n)
{
struct node *newNode, *temp;
int data, i;

if(n <= 0)
{
printf("List size must be greater than zero.\n");
return;
}

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

if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{

printf("Enter the data of node 1: ");


scanf("%d", &data);

head->data = data;
head->next = NULL;
temp = head;

for(i=2; i<=n; i++)


{
newNode = (struct node *)malloc(sizeof(struct node));

if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);

newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}

printf("SINGLY LINKED LIST\n");


}
}

void reverseList()
{
struct node *prevNode, *curNode;

if(head != NULL)
{
prevNode = head;
curNode = head->next;
head = head->next;

prevNode->next = NULL;

while(head != NULL)
{
head = head->next;
curNode->next = prevNode;

prevNode = curNode;
curNode = head;
}

head = prevNode;

printf("REVERSED LIST\n");
}
}
void displayList()
{
struct node *temp;

if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}

You might also like