Insertion in circular linked list in C++
What is insertion in circular linked in CPP programming?
Insertion in circular linked in C++ in general words can be understood as insertion of new node in the predefined circular linked list. We can perform three different types of insertion in circular linked list, these different types of insertion are:-
- Insertion at beginning of the circular linked list.
- Insertion at specific position in circular linked list.
- Insertion at end in circular linked list.
Types of insertion in circular linked list
1.) Insertion at beginning:
In this type of insertion in circular linked list we add a new node before the head node of the circular linked list. This newly added node will now act as head of the list.
2.) Insertion at specific position:
If we want to insert a new node at specific position then we need to traverse till that position and insert a new node there shifting the position of remaining nodes by one.
3.) Insertion at end:
In this type of insertion in circular linked list a new node is added after the end node of the list, and this newly added node will now act as tail of the list.
How to perform insertion at beginning in circular linked list?
To perform insertion at beginning in circular linked list we will use the following steps:-- We will create a circular linked list with some data in it.
- We will define a node pointer and this node pointer as head of the list.
- Now we will traverse this node pointer till the end of the list and follow the algorithm for insertion at beginning in circular linked list below.
void insertBeginning(int num) { struct node *newnode, *curNode; if(head == NULL) { cout<<"List is empty"; } else { newnode = (struct node *)malloc(sizeof(struct node)); newnode->num = num; newnode->next = head; curNode = head; while(curNode->next != head) { curNode = curNode->next; } curNode->next = newnode; head = newnode; } }
How to perform insertion at specific position in circular linked list?
To perform insertion at specific position in circular linked list we will use the following steps:-- We will create a circular linked list with some data in it.
- Now we will traverse till that specific position using a node pointer.
- Next we will insert the new node after the node pointing by node pointer using the algorithm below
void insertSpecificPosition(int num, int pos) { struct node *newnode, *curNode; int i; if(head == NULL) { cout<<"List is empty"; } else { newnode = (struct node *)malloc(sizeof(struct node)); newnode->num = num; curNode = head; for(i=0; i<=pos-3; i++) { curNode = curNode->next; } newnode->next = curNode->next; curNode->next = newnode; } }
How to perform insertion at end in circular linked list?
To perform insertion at end in circular linked list we will use the following steps:- After creating a linked list with some data in it we will define:-- We will define a node pointer this will initially point the head of the list and later will be used for traversing till the end of the list
- Now traverse till the end of the list with the help of node pointer.
- Now insert new node after the last node using the algorithm below.
void insertEnd(int num1) { struct node *p; int a; a=num1; struct node *temp=(struct node*)malloc(sizeof(struct node)); temp->num=a; p=head; while(p->next!=head) { p=p->next; } p->next=temp; temp->next=head; }
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Circular Linked List
- Introduction to Circular Linked List
Click Here - Circular Linked List Applications
Click Here - Circular Linked List in –
- Insertion in Circular Linked List –
- Insertion at the beginning–
- Insertion at the end –
- Insertion at nth position –
- Deletion in Circular Linked List –
- Deletion from beginning in Circular Linked List –
- Deletion from nth position in Circular Linked List –
- Deletion from end in Circular Linked List –
- Insertion and Deletion in Circular Linked List – C | C++ | Java
- Split a Circular Linked List in two halves –
- Count nodes in Circular Linked List –
- Sorted Insert In Circular Linked List –
- Insertion in the middle in Circular Linked List –
Circular Linked List
- Introduction to Circular Linked List
- Circular Linked List Applications
- Circular Linked List in – C | C++ | Java
- Insertion in Circular Linked List – C | C++ | Java
- Deletion in Circular Linked List – C | C++ | Java
- Insertion and Deletion in a Circular Linked List – C | C++ | Java
- Split a Circular Linked List in two halves – C | C++ | Java
- Count nodes in Circular Linked List – C | C++ | Java
- Sorted Insert In Circular Linked List – C | C++ | Java
- Insertion in the middle in Circular Linked List – C | C++ | Java
Login/Signup to comment