C Program To Insert Element In Singly Linked List ( Insert at last, Insert at beginning and Insert at some position )
Linked List is a sequential data structure in which elements are stored via link. Every node is connect with other node.
Generally Linked List are three type:
- Singly Linked List
- Doubly Linked List
- Circular Linked List
In this article we will cover how we can insert element in the singly linked list at the different position
Singly Linked List
In singly linked list data structure there are mainly two part first is link part and second is data part.
In the link part of singly linked list the base address of the next node is stored.
And in the data part actual data is stored.
1->2->3->4->5->NULL
Link part of the last node of the singly linked list contain NULL.
Insert Data At Beginning In The Singly Linked List
If linked list have already some nodes then make newNode->next = head and head = newNode.
Insert Data At End In The Singly Linked List
First check linked list is empty or not if empty then make the new node as the head node.
If Linked List is not empty then traverse the linked list while (current->next!=NULL).
when we traverse the linked list at the end then:
newNode->next = NULL;
current->next = newNode;
Insert Data At Position In The Singly Linked List
In this operation we create a function as usual but the function will create two parameter first is value which we want to insert and second is the another value after that we want to insert our data.
Now check linked list is empty or note if empty print "Linked List is Empty" and return.
If linked list is not empty the use the while loop "while(current->data!=value);" if the key value is found
then :
newNode->next = current->next;
current->next = newNode;
Also use a "if " condition if key value does not match with any data of the linked list then print the message that key value does note match with any data.
C Program To Insert Element In Singly Linked List ( Insert at last, Insert at beginning and Insert at some position )
#include<stdio.h>
#include<stdlib.h>
// Creating the Node
struct node
{
int data;
struct node *next;
};
struct node *current = NULL;
struct node *head = NULL;
// Function to Print Singly Linked List
void printLinkedList()
{
printf("[");
while (head != NULL)
{
printf("%d ", head->data);
head = head->next;
}
printf("]");
}
//Function to Insert Data at beginning in singly Linked List
void addAtFirst(int value)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
if (head == NULL)
{
newNode->data = value;
newNode->next = NULL;
head = newNode;
}
else
{
newNode->data = value;
newNode->next = head;
head = newNode;
}
}
// Function to insert the data in singly linked list at end
void addAtLast(int value)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
if (head == NULL)
{
newNode->data = value;
newNode->next = NULL;
head = newNode;
}
else
{
current = head;
while (current->next != NULL)
{
current = current->next;
}
newNode->data = value;
newNode->next = NULL;
current->next = newNode;
}
}
// Function to insert the data in singly linked list at some position
void addAtPosition(int value, int key)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
current = head;
if (head == NULL)
{
printf("\nLinked List is Empty !");
}
else
{
while (current->data != key)
{
if (current->next == NULL)
{
printf("No Data Found \n");
return;
}
current = current->next;
}
if (current->data == key)
{
newNode->data = value;
newNode->next = current->next;
current->next = newNode;
}
}
}
// Execution of program will start from the main() function
void main()
{
addAtLast(21);
addAtFirst(17);
addAtLast(53);
addAtFirst(18);
addAtLast(34);
addAtLast(72);
addAtFirst(43);
addAtPosition(12, 15);
printLinkedList();
}
Comments
Post a Comment
DON'T COMMENT LINK.