A singly linked list defined as all nodes are linked together in a few sequential manners, hence, it also knows as a linear linked list.
therefore, clearly it has the beginning and the end. the main problem which comes with this list is that we cannot access the predecessor of the node from the current node.
therefore, we can say that a singly linked list is a dynamic data structure because it may shrink or grow. hence, the shrinking and growing depending on the operation made.
let’s start a singly list by first creating it. I hope you know very well, the linked list is created for using structures, pointers and dynamic memory allocation function
furthermore, it considers the head as an external pointer. this will help us for creating and accessing other nodes in the linked list.
let’s see the following structures definition and head creation.
typedef struct node NODE;
start=(node *)malloc(size of(NODE));
WHEN THE STATEMENT
hence, as you have seen above, it is executed a block of memory sufficient for the store the node is allocated.
therefore its assigns head as the starting address of the node. hence, these all activities can be pictorially shown as given figures.
In C program – the Basic operation on a linked List
typedef struct node NODE;
NODE *start, *first, *temp;
int count = 0;
first = NULL;
start = (NODE*) malloc(size of (NODE));
printf("enter the data item \n");
scanf("%d", &head -> num);
if (first! = NULL)
temp -> ptr=head;
temp = head;
printf("are you want to continue(type y or Y)");
while(choice=='y') || (choice == 'Y');
therefore, this is the basic opeartion on a linked list hence, this is a function
which will create a single linked list.
Suppose that if you want to insert 35 after 30 in the given list as figure 1.1
First of all, create a node 35
therefore, after that Search for the node 30
then, 35 will be insert in the list
hence, this process can be done through the following steps:
New next = temp next
therefore, thus finally the desired node gets inserted.
Here, similary while deleting the desired node
let’s suppose here we want to delete 40, the the first we search the node 40, which want to delete, we also search its previous node, 30
therefore, after that prev next = temp next
furthermore, we make temp node free, hence, the final list will look like this
Advantages of Singly Linked List
there are some advantages of singly Linked List
- it is very easier for the accessibility of a node in the forward direction.
- the insertion and deletion of a node are very easy.
- the Requirement will less memory when compared to doubly, circular or doubly circular linked list.
- the Singly linked list is the very easy data structure to implement.
- During the execution, we can allocate or deallocate memory easily.
- Insertion and deletion of elements don’t need the movement of all the elements when compared to an array.
Disadvantages of Singly Linked List
the disadvantages of singly Linked List are following
- therefore, Accessing the preceding node of a current node is not possible as there is no backward traversal.
- the Accessing of a node is very time-consuming.