forked from starrohan999/Hacktoberfest-Accepted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppendLinkedList.java
61 lines (53 loc) · 1.42 KB
/
AppendLinkedList.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
import java.util.*;
class AppendLinkedList{
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();
}
static class Node{
int data;
Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}
static class LinkedList{
static Node head;
Node next,temp;
public LinkedList(){
head=null;
}
public void addNode(int data){
Node newnode=new Node(data);
if(head==null){
head=newnode;
}
else{
temp=head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=newnode;
}
}
public void display(){
temp=head;
while(temp!=null){
System.out.print(temp.data+" ");
temp=temp.next;
}
}
}
}