Write a program in c to insert a node at the end of a circular linked list.

C Exercises: Insert a node at the end of a circular linked list

Last update on December 20 2021 09:10:11 (UTC/GMT +8 hours)

C Exercises: Insert a node at the beginning of a circular linked list

Last update on December 20 2021 09:10:58 (UTC/GMT +8 hours)

Q. Program to insert a new node at the end of the circular linked list.

Explanation

In this program, we will create a circular linked list and insert every new node at the end of the list. If the list is empty, then head and tail will point to the newly added node. If the list is not empty, the newly added node will become the new tail of the list. The previous tail will point to new node as its next node. Since it is a circular linked list; the new tail will point to head. In other words, the new node will become last node (tail) of the list, and the previous tail will be the second last node.

Write a program in c to insert a node at the end of a circular linked list.

After inserting new node to the end of the list

Write a program in c to insert a node at the end of a circular linked list.

New represents the newly added node. D is the previous tail. When new is added to the end of the list, it will become new tail and D will point to new.

Algorithm

  1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
  2. Define another class for creating the circular linked list and it has two nodes: head and tail. It has two methods: addAtEnd() and display() .
  3. addAtEnd() will add the node to the end of the list:
    1. It first checks whether the head is null (empty list), then it will insert the node as the head.
    2. Both head and tail will point to the newly added node.
    3. If the list is not empty, then the newly added node will become the new tail, and previous tail will point to new node as its next node. The new tail will point to the head.
  4. display() will show all the nodes present in the list.
    1. Define a new node 'current' that will point to the head.
    2. Print current.data till current will points to head again.
    3. Current will point to the next node in the list in each iteration.

C Program for Insertion at the end in circular linked list

July 20, 2020

Write a program in c to insert a node at the end of a circular linked list.
Write a program in c to insert a node at the end of a circular linked list.

Linked List | Set 2 (Inserting a node)

We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.
All programs discussed in this post consider the following representations of linked list.




// A linked list node
class Node
{
public:
int data;
Node *next;
};
// This code is contributed by rathbhupendra




// A linked list node
struct Node
{
int data;
struct Node *next;
};




// Linked List Class
class LinkedList
{
Node head; // head of list
/* Node Class */
class Node
{
int data;
Node next;
// Constructor to create a new node
Node(int d) {data = d; next = null; }
}
}




# Node class
class Node:
# Function to initialize the node object
def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize next as null
# Linked List class
class LinkedList:
# Function to initialize the Linked List object
def __init__(self):
self.head = None




/* Linked list Node*/
public class Node
{
public int data;
public Node next;
public Node(int d) {data = d; next = null; }
}




<script>
// Linked List Class
var head; // head of list
/* Node Class */
class Node {
// Constructor to create a new node
constructor(d) {
this.data = d;
this.next = null;
}
}
// This code is contributed by todaysgaurav
</script>

In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways
1) At the front of the linked list
2) After a given node.
3) At the end of the linked list.

Program for all operations on circular linked list in C

In a Circular linked list, every element has a link to its next element in the sequence and the last element has a link to the first element. A circular linked list is similar to the singly linked list except that the last node points to the first node. Below is the image to illustrate the same:

Write a program in c to insert a node at the end of a circular linked list.

Some common operations of a circular linked list are implemented below:

Insertion at the beginning: Inserting a new node as the first node. The next pointer of last will point to this node and this new node will point to the previous first node.






// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in the list
struct node* last = NULL;
// Function to insert a node in the
// starting of the list
void insertAtFront()
{
// Stores the number to be inserted
int data;
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// Input data
printf("\nEnter data to be "
"inserted: \n");
scanf("%d", &data);
// If the new node is the only
// node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else last node contains the
// reference of the new node and
// new node contains the reference
// of the previous first node
else {
temp->info = data;
temp->next = last->next;
// last node now has reference
// of the new node temp
last->next = temp;
}
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
// While first node is not
// reached again, print,
// since the list is circular
do {
printf("\nData = %d", temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Function Call
insertAtFront();
insertAtFront();
insertAtFront();
// Print list
viewList();
return 0;
}

Output:

Write a program in c to insert a node at the end of a circular linked list.

Insertion at the end: Inserting a new node as the last node. The next pointer of last will point to this node and this new node will point to the first node.




// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in the list
struct node* last = NULL;
// Function to add a new node at the
// end of the list
void addatlast()
{
// Stores number to be inserted
int data;
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// Input data
printf("\nEnter data to be "
"inserted : \n");
scanf("%d", &data);
// If the new node is the
// only node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else the new node will be the
// last node and will contain
// the reference of head node
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d",
temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Function Call
addatlast();
addatlast();
addatlast();
// Print list
viewList();
return 0;
}

Output:

Write a program in c to insert a node at the end of a circular linked list.

Insertion after a specific element: Below is the program to insert a node after a specified node in the linked list.




// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in list
struct node* last = NULL;
// Function to add a new node
// at the end of the list
void addatlast()
{
// Stores number to be inserted
int data;
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// Input data
printf("\nEnter data to be inserted : \n");
scanf("%d", &data);
// If the new node is the
// only node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else the new node will be the
// last node and will contain
// the reference of head node
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
// Function to insert after any
// specified element
void insertafter()
{
// Stores data and element after
// which new node is to be inserted
int data, value;
// Initialize a new node
struct node *temp, *n;
// Input data
printf("\nEnter number after which"
" you want to enter number: \n");
scanf("%d", &value);
temp = last->next;
do {
// Element after which node is
// to be inserted is found
if (temp->info == value) {
n = (struct node*)malloc(sizeof(struct node));
// Input Data
printf("\nEnter data to be"
" inserted : \n");
scanf("%d", &data);
n->info = data;
n->next = temp->next;
temp->next = n;
// If temp is the last node
// so now n will become the
// last node
if (temp == last)
last = n;
break;
}
else
temp = temp->next;
} while (temp != last->next);
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d",
temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Initialize the list
addatlast();
addatlast();
addatlast();
// Function Call
insertafter();
// Print list
viewList();
return 0;
}

Output:

Write a program in c to insert a node at the end of a circular linked list.

Delete first element: Deleting the first node of the linked list. For this, the next pointer of last will point to the second node of the linked list. Below is the program for the same:




// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in list
struct node* last = NULL;
// Function to add a new node
// at the end of the list
void addatlast()
{
// Stores number to be inserted
int data;
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// Input data
printf("\nEnter data to be"
" inserted: \n");
scanf("%d", &data);
// If the new node is the only
// node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else the new node will be the
// last node and will contain
// the reference of head node
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
// Function to delete the first
// element of the list
void deletefirst()
{
struct node* temp;
// If list is empty
if (last == NULL)
printf("\nList is empty.\n");
// Else last node now contains
// reference of the second node
// in the list because the
// list is circular
else {
temp = last->next;
last->next = temp->next;
free(temp);
}
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d",
temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Initialize the list
addatlast();
addatlast();
addatlast();
// Function Call
deletefirst();
// Print list
viewList();
return 0;
}

Output:

Write a program in c to insert a node at the end of a circular linked list.

Delete the last element: Deleting the last node of the linked list. For this, the second last node will point to the first node of the list. Below is the program for the same:




// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in list
struct node* last = NULL;
// Function to add a new node
// at the end of the list
void addatlast()
{
// Stores number to be inserted
int data;
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// Input data
printf("\nEnter data to be inserted : \n");
scanf("%d", &data);
// If the new node is the only
// node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else the new node will be
// last node and will contain
// the reference of head node
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
// Function to delete the last node
// in the list
void deletelast()
{
struct node* temp;
// If list is empty
if (last == NULL)
printf("\nList is empty.\n");
temp = last->next;
// Traverse the list till
// the second last node
while (temp->next != last)
temp = temp->next;
// Second last node now contains
// the reference of the first
// node in the list
temp->next = last->next;
last = temp;
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d",
temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Initialize the list
addatlast();
addatlast();
addatlast();
// Function Call
deletelast();
// Print the list
viewList();
return 0;
}

Output:

Write a program in c to insert a node at the end of a circular linked list.

Delete at a given position: Delete an element from a specified position in the linked list. Below is the program for the same:




// C program for the above operation
#include <stdio.h>
#include <stdlib.h>
// Structure of a linked list node
struct node {
int info;
struct node* next;
};
// Pointer to last node in list
struct node* last = NULL;
// Function to add a new node
// at the end of the list
void addatlast()
{
// Stores number to be inserted
int data;
// Initialize a new node
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
// Input data
printf("\nEnter data to be inserted : \n");
scanf("%d", &data);
// If the new node is the
// only node in the list
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
// Else the new node will be
// last node and will contain
// the reference of head node
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
}
// Function to delete an element
// at a specified index in the list
void deleteAtIndex()
{
// Stores the index at which
// the element is to be deleted
int pos, i = 1;
struct node *temp, *position;
temp = last->next;
// If list is empty
if (last == NULL)
printf("\nList is empty.\n");
// Else
else {
// Input Data
printf("\nEnter index : ");
scanf("%d", &pos);
// Traverse till the node to
// be deleted is reached
while (i <= pos - 1) {
temp = temp->next;
i++;
}
// After the loop ends, temp
// points at a node just before
// the node to be deleted
// Reassigning links
position = temp->next;
temp->next = position->next;
free(position);
}
}
// Function to print the list
void viewList()
{
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
// Else print the list
else {
struct node* temp;
temp = last->next;
do {
printf("\nData = %d", temp->info);
temp = temp->next;
} while (temp != last->next);
}
}
// Driver Code
int main()
{
// Initialize the list
addatlast();
addatlast();
addatlast();
// Function Call
deleteAtIndex();
// Print the list
viewList();
return 0;
}

Output:

Write a program in c to insert a node at the end of a circular linked list.

Write a program in c to insert a node at the end of a circular linked list.




Article Tags :
C Programs
Linked List
Technical Scripter
circular linked list
Linked Lists
Technical Scripter 2020
Practice Tags :
Linked List
circular linked list

inserting a node at the end of a linked list

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




Required knowledge

Basic C programming, Functions, Dynamic memory allocation, Circular linked list

Algorithm to insert a new node at the beginning of a Circular linked list

For inserting new node at the beginning of a circular linked list. We need to connect new node with the first node and re-connect the last node with new node instead of head node.

Algorithm to insert new node at the beginning of Circular linked list %%Input : head {Pointer to first node of the linked list} Begin If (head == NULL) then write ('List is empty') End if Else then alloc (newNode) read (data) newNode.data ← data; newNode.next ← head; current ← head; While (current.next != head) do current ← current.next; End while current.next ← newNode; End if End