Ticker

6/recent/ticker-posts

Header Ads Widget

Linked List Operations: Traverse, Insert and Delete

Linked List Operations: Traverse, Insert and Delete

In this article, you will learn different linked list operations which you can performed on linked list also you will find that operations implementations in this article.

To understand the operations of linked list you need the basic knowledge of linked list so if you don’t have that I prefer you to read are previous article.

 

Two things you need to keep in mind

  •       “Head” points the starting point of linked list
  •      “Null” points the end of the linked list

So mainly there are the operations in linked list and they are

·         Traverse

·         Insert

·         Delete


How to traverse a linked list

Suppose we are using the loop with the help of linked list in are code so you need to just keep in mind that you need to keep move further till the “Null” comes.

Here I saw you the example of that


struct node*temp = head;

printf("\n\nList elements are - \n");

while(temp != NULL)

{
printf("%d --->",temp->data);
temp temp->next;
}

So the output can be:

List elements are - 1 --->2 --->3 --->

 

How to insert a linked list

You can add elements at three places at the starting point at the middle and at the end of the link list

Add to the starting point

When you add linked list in staring point you need to just allocate memory for that new variable and just insert the data and you need to change the “head” and “next” points and then do implementation you can see this in example.

struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = 4;
newNode->next = head;
head = newNode;

 
 

Add to the end

When you add variable at the end at that time you need to allocate memory for new node you need to store and traverse that data and you need to change the last point.

You can see implementation in example

struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = 4;
newNode->next = NULL;

struct node *temp = head;
while(temp->next != NULL){
  temp = temp->next;
}

temp->next = newNode;


Add to the middle

When you add variable to the middle at that time you need to allocate memory and store the new data you need to change “next” points and also you need to include new “node”.

You can see implementation in example

struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = 4;

struct node *temp = head;

for(int i=2; i < position; i++) {
    if(temp->next != NULL) {
        temp = temp->next;
    }
}
newNode->next = temp->next;
temp->next = newNode;



How to delete from a linked list

This process is same as insertion you can do changes at three stage at the beginning, end or middle.

Delete from beginning

Here you need to just make second point as “head”

Example:

head = head->next;

 

Delete from end

You need to make second last point as last point

Example:

struct node* temp = head;
while(temp->next->next!=NULL){
  temp = temp->next;
}
temp->next = NULL;

 

Delete from beginning

You need to change the next points and you need to traverse again

Example:

for(int i=2; i< position; i++) {
    if(temp->next!=NULL) {
        temp = temp->next;
    }
}

temp->next = temp->next->next;

 

So After Reading this full article on linked list operations I hope you get the answer what you want and if you have any query then you can ask me in comment section and do comments and do not forget to share this articles to your friends and thank you for reading this article.

Post a comment

1 Comments