Site icon Study Algorithms

What is a Linked List?

Linked List is a data structure used for storing collections of data. Linked list has the following properties.

A Linked List can be thought of as an Abstract Data Type (ADT). An Abstract Data Type is a user defined data type and it can be created by the user as per his needs. A Linked List is an ADT, because we are defining a separate type of data structure. In this data structure, we have one storage for our data, and the other one for our next memory location. That means we need to store the location of the next memory address as well.

Basic Structure:

A Linked List can be pictorially represented as:-

Fig: Structure of a linked list

The HEAD node is of prime importance as it signifies the starting address of our Linked List. What we have here is an integer as the data, and another storage that points to the next memory location.
Its something like:-

HEAD points at -> 7
The NEXT of HEAD points at -> 15
The NEXT of 15 points at -> 42
The NEXT of 42 points at ->30
The NEXT of 30 points at -> NULL

So, we can conclude that HEAD signifies the start of a link list and NULL signifies that we have reached the end of our Linked List.

Thus, we can define the basic data structure of our Linked List as:

struct node
{
  // this is the data type that we want to store
  int data;

   // a pointer to the next node, same type
  struct node * next;
};
Code language: C/AL (cal)

That means when we want to go to the next address we can do an operation that is similar to:

list = list->next;
printf("The data element is:- %d", list->data);
Code language: C/AL (cal)

Why Linked Lists?

Now, the question arises why should we use Linked List, when there are so many other structures available. One of them is ARRAYS. Both of them are used to store data and serve the same purpose. But there can be some scenarios in which using a Linked List can be beneficial than arrays.

ARRAYS

One memory block is allocated for the entire array to hold the elements of the array. The array elements can be accessed in constant time using the index.

Here we can easily access array elements in constant time. For example if we want to access the 4th element we can simply do it by:

int arr[9] = { 3, 2, 1, 2, 2, 2, 3, 56, 12 };
int elem = arr[3];
printf("The 4th element is:- %d", elem);    // prints the 4th element
Code language: C/AL (cal)

Advantages of Arrays:-

Disadvantages of Arrays:-

Linked Lists:

Thus, we come to the use of Linked Lists. It also has its own advantages and disadvantages.

Comparison of Linked List and arrays and different types of operations:

ParameterLinked ListArray
IndexingO(n)O(1)
Insertion/Deletion at BeginningO(1)
Insertion/Deletion at EndO(n)
Insertion/Deletion in middleO(n)
Wasted SpaceO(n)0
Table : Speed of operations

Video Explanation:

Exit mobile version