-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertionBegin.java
39 lines (36 loc) · 1.07 KB
/
insertionBegin.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
import java.util.Scanner;
class Node1{
int data;
Node1 link;
}
class insertionBegin {
public static void main(String[] args) {
Node1 head = null, newnode = null, temp = null;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Enter the element You want to insert or press -1:");
int element = sc.nextInt();
if (element == -1) {
break;
} else {
newnode = new Node1();
newnode.data = element;
newnode.link = null;
if (head == null) {
head = newnode;
temp = head;
} else {
temp.link = newnode;
temp = newnode;
}
}
}
System.out.println("Elements of linked List are:");
temp = head;
while (temp != null) {
System.out.println(temp.data);
temp = temp.link;
}
sc.close();
}
}