From 929e8dd3aa3c747c6cc20fc0248751f62d2884e5 Mon Sep 17 00:00:00 2001 From: rishin09 Date: Wed, 7 Oct 2020 00:01:16 +0530 Subject: [PATCH] Circular Linked List --- .../CircularLinkedListByRishinPandit.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 linked-list/CircularLinkedListByRishinPandit.java diff --git a/linked-list/CircularLinkedListByRishinPandit.java b/linked-list/CircularLinkedListByRishinPandit.java new file mode 100644 index 00000000..fb4c2a13 --- /dev/null +++ b/linked-list/CircularLinkedListByRishinPandit.java @@ -0,0 +1,53 @@ +public class CircularLinkedListByRishinPandit{ + public class Node{ + int data; + Node next; + public Node(int data){ + this.data = data; + } + } + + public Node head = null; + public Node tail = null; + + public void add(int data){ + Node newNode = new Node(data); + + if(head == null){ + head = newNode; + tail = newNode; + newNode.next = head; + } + else { + tail.next = newNode; + tail = newNode; + tail.next = head; + + } + } + + public void display(){ + Node current = head; + if(head == null){ + System.out.println("List is empty"); + } + else{ + System.out.println("Nodes of the circular linked list: "); + do{ + System.out.print(" "+ current.data); + current = current.next; + }while(current != head); + System.out.println(); + } + } + + public static void main(String[] args) { + CircularLinkedListByRishinPandit c1 = new CircularLinkedListByRishinPandit(); + c1.add(10); + c1.add(20); + c1.add(30); + c1.add(40); + + c1.display(); + } +} \ No newline at end of file