Description
To delete a node in the beginning of circular linked list - Case 1: Check if list is empty Show message that list is empty. Case 2: If List has one element Assign NULL to haed. Case 2: If list has elements a) Traverse the list until last node contain the address of first node b) Assign the next field of head to next field of last node c) Assign the last node next field 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 *delete_beg(struct node *); //Declaring node struct node{ int data; struct node *next; }; int main(){ int n; struct node *head; printf("Enter the size of linked list : "); scanf("%d",&n); //Calling function to create node head=createCircularLinkedList(n); //Calling function to display list displayCircularLinkedList(head); //Code to delete a node from beginning head=delete_beg(head); printf("\nThe linked list after deletion of node from beginning - \n"); //Calling function to display list after deletion of node from beginning 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); } } //Function to delete a node from beginning struct node *delete_beg(struct node *head){ //No node is available to delete if(head==NULL){ printf("\nLink list is empty...No node to delete!!"); }else if(head->next==head){ head=NULL; free(head); }else{ struct node *ptr; ptr=head; while(ptr->next!=head){ ptr=ptr->next; } ptr->next=head->next; free(head); head=ptr->next; } return head; }
Java
import java.io.*; import java.util.Scanner; public class DeleteNodeBeginningCircularSinglyLinkedList { //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); DeleteNodeBeginningCircularSinglyLinkedList list = new DeleteNodeBeginningCircularSinglyLinkedList(); 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(); list.delete_beg(); // Calling function to display list after deletion of node from beginning System.out.println("\nAfter deletion of node from beginning - "); list.display(); } private void delete_beg() { //No node is available to delete if(head==null){ System.out.println("Link list is empty...No node to delete!!"); }else if(head.next==head){ head=null; }else{ Node ptr=head; while(ptr.next!=head){ ptr=ptr.next; } ptr.next=head.next; head=ptr.next; } } }
Related Programs
1) Program To Create And Display Circular Singly Linked List2) Program To Add A Node At The Beginning Of Circular Linked List
3) Program To Add A Node At The End Circular Singly Linked List
4) Program To Create And Display Singly Linked List
5) Program To Delete Node From Beginning Singly Linked List
6) Program To Add A Node In Beginning Of Singly Linked List
7) Program To Delete Given Node Singly Linked List
8) Program To Delete Node From End Singly Linked List
9) Program To Delete Node Before Given Node Singly Linked List
10) Program To Delete Node After Given Node Singly Linked List