Circular Linked List Insertion

inserting a node at the beginning

import code from the create and display nodes and getnode function to assign data click here

  1. create a function inserbeg( )
  2. create a *newnode,*temp pointer
  3. call getnode( ) function to assign data to newnode
inserting a node at the beginning

4.initialize head to temp pointer

initialize head to temp pointer

5.move temp pointer to the last node, by replacing the temp address to the next node, until the condition (temp -> next != head)

move temp pointer to the last node

6.link newnode of next address to head node (newnode->next=head)

Circular Linked List Insertion 1

7.move head pointer to newnode (head=newnode)

Circular Linked List Insertion 2

8.link temp of next address to the head node ,because head is now newnode

Circular Linked List Insertion

Program for inserting a node at the beginning

void cllinsertbeg()
{
	node *newnode, *temp;
	newnode = getnode();
	if(head == NULL)
	{
		head = newnode;
		newnode -> next = head;
	}
	else
	{
		temp = head;
		while(temp -> next != head)
			 temp = temp -> next;
		newnode -> next = head;
		head = newnode;
		temp -> next = head;
	}
	printf("\n Node inserted at beginning..");
}

inserting a node at the ending

import code from the create and display nodes and getnode function to assign data click here

  1. create a function inserend( )
  2. create a *newnode,*temp pointer
  3. call getnode( ) function to assign data to newnode
inserting a node at the ending

4. initialize head to temp pointer

Circular Linked List Insertion 3

5. move temp pointer to the last node, by replacing the temp address to the next node, until the condition (temp -> next != head)

Circular Linked List Insertion 4

6.link next address of temp node to newnode

Circular Linked List Insertion 5

7.link next address of newnode to the head

Circular Linked List Insertion 6

Program for inserting a node at the ending

void cllinsertend()
{
	node *newnode, *temp;
	newnode = getnode();
	if(head == NULL )
	{
		head = newnode;
		newnode -> next = head;
	}
	else
	{
		temp = head;
		while(temp -> next != head)
			temp = temp -> next;
		temp -> next = newnode;
		newnode -> next = head;
	}
	printf("\n Node inserted at end..");
}
Default image
Vishal Devxo
Vishal Devxo is a DevOps engineer and a Backend developer, he spends all his time for creating good tutorials with better visuals and blogging, developed some projects based on Python-Django, some hacking modules and scripts in python