Linked List
Linked List
11 February 2025
12:12
Definition:
A linked list is a linear data structure which can store a collection of
"nodes" connected together via links i.e. pointers. Linked lists nodes
not stored at a contiguous location, rather they are linked using
to the different memory locations. A node consists of the data value
a pointer to the address of the next node within the linked list.
A LL is a DS which can change during execution, meaning it can grow
or shrink in size during execution of a program. It also provides
flexibility in allowing the items to be rearranged efficiently by
providing various operations such as Inserting an element /* Initialize nodes */
end, any specific location), Deleting an element (beginning, end, any struct node *head;
struct node *one = NULL;
specific location). struct node *two = NULL;
struct node *three = NULL;
From <https://www.tutorialspoint.com/data_structures_algorithms/linked_list_algorithms.htm>
/* Allocate memory */
one = malloc(sizeof(struct node));
A linked list is a dynamic linear data structure whose memory size can two = malloc(sizeof(struct node));
be allocated or de-allocated at run time based on the operation three = malloc(sizeof(struct node));
or deletion, this helps in using system memory efficiently. Linked lists /* Assign data values */
can be used to implement various data structures like a stack, queue, one->data = 1;
graph, hash maps, etc. two->data = 2;
three->data=3;
/* Connect nodes */
Representation of Linked List: one->next = two;
two->next = three;
struct node three->next = NULL;
{
int data; /* Save address of first node in head */
struct node *next; head = one;
};
Each struct node has a data item and a pointer to another struct node. Let us create a simple
Linked List with three items to understand how this works.