C Program for Insertion at the end in circular linked list
Inserting a new node at the last in circular linked list
Today we will learn how to write a code in C Program for Insertion at the end in circular linked list. For doing so, we will keep the following things in mind.
- If the linked list is blank or empty, then head and tail will point to the newly added node.
- If the linked list is not empty, then the newly inserted node will become the new tail of the circular linked list.
In other words, the new node will become last node (tail) of the list, and the previous tail will be the second last node.
Algorithm For Ending New Node At The End
- Step 1: IF HEAD = NULL (END OF IF AND GO TO STEP 8)
- Step 2: ASSIGN POINTER = HEAD
- Step 3: WHILE POINTER -> NEXT != HEAD (REPEAT STEP 4 AND 5)
- Step 4: ASSIGN PREVIOUS_POINTER = POINTER
- Step 5: ASSIGN POINTER = POINTER -> NEXT (END OF WHILE LOOP)
- Step 6: ASSIGN PREVIOUS_POINTER -> NEXT = HEAD
- Step 7: REMOVE POINTER
- Step 8: EXIT
Working require for inserting node at the last
- Make a new node.
- Assign the new node next to circular list.
- If the list is empty then return new node.
- Assign the new node next to the front of the list.
- Assign tail next to the new node.
- Return the end node of the circular linked list.
Structure of Node In Circular Linked List
struct node { int data; struct node* next; };
Code For Insertion At The End In Circular Linked List
Run
#include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node *next; }; void insertLast (struct Node **head, int data) { struct Node *newNode = (struct Node *) malloc (sizeof (struct Node)); newNode->data = data; // if its the first node being entered if (*head == NULL) { *head = newNode; (*head)->next = *head; return; } // if LL already as >=1 node struct Node *curr = *head; // traverse till last node in LL while (curr->next != *head) { curr = curr->next; } // assign LL's current last node's next as this new node curr->next = newNode; // assign this new node's next as current head of LL newNode->next = *head; } void display (struct Node *head) { // if there are no node in LL if (head == NULL) return; struct Node *temp = head; //need to take care of circular structure of LL do { printf ("%d ", temp->data); temp = temp->next; } while (temp != head); printf ("\n"); } int main () { struct Node *head = NULL; printf("Linked List: "); insertLast (&head, 0); insertLast (&head, 10); insertLast (&head, 20); insertLast (&head, 30); insertLast (&head, 40); display (head); return 0; }
Output
Linked List: 0 10 20 30 40
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