Friday, April 23, 2021

Insertion at the end of a Linked List

 

The new node will be added at the end of the linked list.

Example:

Input

Linked List : 1➞ 2➞ 3➞ 4➞ NULL

5

Output

Linked list : 1➞ 2➞ 3➞ 4➞ 5➞ NULL 

Shown below is a simple visual representation of the insertion of a node at the end of a linked list...

Insertion at the end will be done in four steps,

1) create a new node i.e. temp

2) the next of temp should be NULL

3) Now, tail➞next = temp

4) Lastly, tail = temp

Also if there is no prior list created after which you want to insert a node, then, head = tail = temp since after inserting in a NULL list, you will be left with only one node, so head will be equal to tail.

Let's create a function that will contain the main logic for insertion.

void end(int n)

The function end() will take an integer argument n which will contain the value we want to insert at the end of a linked list.

Now we will create a new node named temp and assign it the value n.

temp➞data will assign n to the node temp.

node *temp=new node;
temp->data=n;

Now assigning NULL to the next of temp since it will be the last node of our linked list and it will point to NULL.

temp->next=NULL;

Also, let's write a condition for if there is no prior list already created. In that case it will assign head and tail to temp. Although, in our program, this will never happen because we are going to input five elements to create a linked list at the very beginning and then we will insert a new node at the end.

if(head==NULL)
{
head=temp;
tail=temp;
}

 Otherwise if a list is already existing, then we eill assign the next to tail to temp and then assign temp to tail.

else
{
tail->next=temp;
tail=temp;
}

 So, the complete function looks like the one given below,

void end(int n)
{
node *temp=new node;
temp->data=n;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}

 

Now, lets look at the complete program to insert a node at the end.

In the program below we have the following,

1) structure node

2) class linked_list

3) a constructor linked_list() inside the class linked_list to initialize the value of head and tail to NULL at the very beginning

4) a function end() to insert  node at the end

5) a function display() to display the linked list

 

In the main() function, we are taking in 5 elements as an input to kind of create a new node which is though just inserting in the end the elements one after another. But for understanding purpose, assume it is creating a linked list having five nodes or five elements. Later then, we are inputting a value to be inserted at the end and then we are passing the value to the function end() and then displaying it by calling the function display().

#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
}*head,*tail;
class linked_list
{
public:
linked_list()
{
head=NULL;
tail=NULL;
}
void end(int n)
{
node *temp=new node;
temp->data=n;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
void display(node *h)
{
if(h==NULL)
{
cout<<"\tNULL";
}
else
{
cout<<"\t"<<h->data;
display(h->next);
}
}
};
int main()
{
linked_list a;
int info[10],end;
cout<<"\nEnter 5 values for a new node : ";
for(int i=0;i<5;i++)
{
cin>>info[i];
}
for(int i=0;i<5;i++)
{
a.end(info[i]);
}
a.display(head);
     cout<<"\nEnter a value that you want to insert in the end : ";
cin>>end;
a.end(end);
a.display(head);
}

 

 

 

 

Time

  A brief look at time......  

Popular Posts