- A linked list is a linear type of data structure.
- It is a set of data which has data along with the address stored in it.
- Linked list store the data and address in a node.
- Node:- Node is a combination of data and the next (address of next node).
A linked list is a collection of a node having the following format.
Above node contain two fields of a different type in a node. Therefore to implement a node we use a type of structure to form a node.
Struct node * next;
In which data stores int type data in a node and the next pointer points next node of the same type structure.
Now we will see how exactly the linked list be implemented in C.
Linked list program in c
In this we simply create a linked list with 3 nodes P1, P2, P3 As given below.
Struct node *next;
Struct node *p1,*p2,*p3;
Struct node *start,*temp;
p1=(Struct node*)malloc(sizeof(struct node));
p2=(Struct node*)malloc(sizeof(struct node));
p3=(Struct node*)malloc(sizeof(struct node));
//Display Linked List
In an above linked list, We simply create nodes in c and for creating and allocating an address to the node we used malloc() which allocates memory dynamically and after that, we linked the nodes.
p1 –> next = p2, p1 stores address of p2 which forms a link.
In this way, we formed a linked list and stored data in it with the address.
Types of Linked List
Operations on linked list
1. Insert at beginning
This operation state that to insert the node at beginning of a linked list.
Example: Insert 10 at the beginning.
Insert 7 at the beginning.
2. Insert at end
This operation is to add a new node at last of LL.
3. Insert before
This operation is to add a new node before any previously given node.
4. Deletion of node
This operation is to delete modes of the LL.
To delete complete LL.