Program To Add A Node At The Beginning Of Circular Linked List

Description

To add a node in the beginning of circular linked list -
Case 1: Check if list is empty
	a) Assign the address of new node to head 
	b) Assign the head to the next field of new node
Case 2: If list has elements
	a) Traverse the list until last node contain the address of first node
	b) Assign the address of new node to the next of last node
	c) Assign the head to the next field of new node
	d) Assign the new node to head
	
Note: Here head contains the address of first node

C/C++

#include<stdio.h>
#include<malloc.h>

struct node *createCircularLinkedList(int);
void displayCircularLinkedList(struct node *);
struct node *insert_beg(struct node *);

struct node{
    int data;
    struct node *next;
};

int main()
{
    int n;
    struct node *head;

    printf("Enter number of nodes : ");
    scanf("%d",&n);

    //Calling function to create node
    head=createCircularLinkedList(n);

    //Calling function to display list
    printf("\nThe circular linked list is - \n");
    displayCircularLinkedList(head);


    //Function to insert the node at beginning of linked list
    head=insert_beg(head);

    //Calling function to display list after addition of node at beginning
    printf("\nThe circular linked list after addition of node at beginning-\n");
    displayCircularLinkedList(head);

    return 0;
}

struct node *createCircularLinkedList(int n){
    int i;
    struct node *head=NULL;
    struct node *newNode, *ptr;

    for(i=0;i<n;i++){
        //Creating a node
        newNode=(struct node *)malloc(sizeof(struct node));

        //Assigning data to newly created node
        printf("Enter %d node data : ",(i+1));
        scanf("%d",&newNode->data);

        /*If list is empty assign the address of newly created node
        to head*/
        if(head==NULL){
            newNode->next=newNode;
            head=newNode;
        }else{
            /* If list already have few elements then loop through
            list and add newly created node at the end of list*/
            ptr=head;
            while(ptr->next!=head){
                ptr=ptr->next;
            }
            ptr->next=newNode;
            newNode->next=head;
        }
    }
    return head;
}

void displayCircularLinkedList(struct node *head){
    struct node *ptr;

    //If list is empty
    if(head==NULL){
        printf("The Linked List is Empty");
    }else{
        /*If list has elements then loop through the loop and
        print elements one by one in sequential manner*/
        ptr=head;
        while(ptr->next!=head){
            printf("The value of node is %d and their address is %u\n",ptr->data,ptr);
            ptr=ptr->next;
        }
        printf("The value of node is %d and their address is %u\n",ptr->data,ptr);
    }
}

struct node *insert_beg(struct node *head){

    struct node *newNode, *ptr;
    int num;

    newNode=(struct node*)malloc(sizeof(struct node));
    printf("\nEnter the node data of new node : ");
    scanf("%d",&newNode->data);

    //assign the address of newly created node to the head if list is empty
    if(head==NULL){
        head=newNode;
        newNode->next=head;
    }else{
        ptr=head;
        //Traversing list until the last node contain the address of first node
        while(ptr->next!=head){
            ptr=ptr->next;
        }
        ptr->next=newNode;
        newNode->next=head;
        head=newNode;
    }

    return head;
}
Add Node Beginning Circular Singly Linked List
Output

Java

import java.io.*;
import java.util.Scanner;

public class AddNodeBeginningCircularSinglyLinkedList {

    //Declaring node
    class Node{
        int data;
        Node next;
        public Node(int data) {
            this.data=data;
            
            //Assigning null value to the next field
            this.next=null;
        }
    }
    
    public Node head=null;
    
    //Function to add node
    public void addNode(int data) {
        
        //Creating a new node
        Node newNode = new Node(data);
        
        /*If list is empty assign the address of newly created node
        to head*/
        if(head==null) {
            newNode.next=newNode;
            head=newNode;
        }else {
            
            /* If list already have few elements then loop through 
            list and add newly created node at the end of list*/
            Node tail=head;
            while(tail.next!=head) {
                tail=tail.next;
            }
            tail.next=newNode;
            newNode.next=head;
        }
    }
    
    public void display() {
        Node ptr=head;
        
        //If list is empty
        if(ptr==null) {
            System.out.println("The list is empty");
            return;
        }
        System.out.println("The nodes of lists are : ");
        
       /*If list has elements then loop through the loop and 
       print elements one by one in sequential manner*/
        while(ptr.next!=head) {
            System.out.print(ptr.data+" ");
            ptr=ptr.next;
        }
        System.out.print(ptr.data+" ");
    }

    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        AddNodeBeginningCircularSinglyLinkedList list = new AddNodeBeginningCircularSinglyLinkedList();
        
        System.out.println("Enter number of nodes : ");
        int n = scanner.nextInt();
        
        for(int i=0;i<n;i++) {
            System.out.println("Enter value of "+(i+1)+" node : ");
            int val = scanner.nextInt();
            list.addNode(val);
        }
        
        //Calling function to display list
        list.display();
        
        System.out.println("\nEnter the value you want to enter at beginning : ");
        int beg = scanner.nextInt();
        list.insert_beg(beg);
        
        
        //Calling function to display list after addition of node in the beginning
        System.out.println("After addition of node in the beginning - ");
        list.display();
    }
    
    //Function to add node in the beginning
    private void insert_beg(int beg) {
        Node ptr=head;
        
        //Creating new node to be added
        Node nodeToAdd = new Node(beg);
        
        //assign the address of newly created node to the head if list is empty
        if(head==null) {
            head=nodeToAdd;
            nodeToAdd.next=head;
        }else {
            ptr=head;
            //Traversing list until the last node contain the address of first node
            while(ptr.next!=head){
                ptr=ptr.next;
            }
            ptr.next=nodeToAdd;
            nodeToAdd.next=head;
            head=nodeToAdd;
        }
    }
}
Add Node Beginning Circular Singly Linked List
Output

Related Programs

1) Program To Create And Display Circular Singly Linked List
Share Me

Leave a Reply