-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathname.java
More file actions
48 lines (40 loc) · 1.01 KB
/
name.java
File metadata and controls
48 lines (40 loc) · 1.01 KB
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
// node created
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
public class name {
Node head;
name() {
head = null;
}
// add the element at the beginning of the linked list
void add_first(int data) {
Node newnode = new Node(data);
newnode.next = head;
head = newnode;
}
// display linked list
void display() {
Node currentnode = head;
if (currentnode == null) {
System.out.println("List is empty.");
return;
}
while (currentnode != null) {
System.out.print(currentnode.data + " -> ");
currentnode = currentnode.next;
}
System.out.println("null");
}
public static void main(String[] args) {
name l = new name();
l.add_first(6);
l.add_first(8);
l.display(); // Output should be: 8 -> 6 -> null
}
}