-
Notifications
You must be signed in to change notification settings - Fork 590
/
Copy pathCircular-LinkedList.java
63 lines (60 loc) · 1.35 KB
/
Circular-LinkedList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ch="";
LinkedList list = new LinkedList();
do{
System.out.println("Enter the value");
int n = sc.nextInt();
sc.nextLine();
list.addNode(n);
System.out.println("Do you want to add another node? Type Yes/No");
ch = sc.nextLine();
}while(ch.equals("Yes"));
System.out.print("The elements in the linked list are ");
list.display();
}
}
class LinkedList
{
class Node
{
int data;
Node next;
Node(int data,Node temp)
{
this.data=data;
next=temp;
}
Node(int data){
this.data=data;
this.next=null;
}
}
Node head=null;
Node tail=null;
public void addNode(int data)
{
Node node=new Node(data);
if(head==null){
head=node;
}
else{
tail.next=node;
}
tail=node;
tail.next=head;
}
public void display()
{
Node temp=head;
if(head!=null){
do{
System.out.print(temp.data+" ");
temp=temp.next;
}while(temp!=head);
}
System.out.println();
}
}