-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLink.java
More file actions
84 lines (67 loc) · 1.84 KB
/
Link.java
File metadata and controls
84 lines (67 loc) · 1.84 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// node created
class Node{
int data;
Node next;
Node(int data){
this.data = data;
next = null;
}
}
public class Link{
Node head;
Node currentNode;
Link(){
head = null;
}
//add the element at the beginsning of the linked list
void add_first(int data){
// Node newnode = new Node(data);
// newnode.next = head;
// head = newnode;
Node newnode = new Node(data);
if (head == null) {
head = newnode;
}
else{
newnode.next = head;
head = newnode;}
}
// add element at end
// void add_last(int data){
// try {
// Node newnode = new Node(data);
// currentNode = head;
// if (currentNode == null) {
// currentNode = newnode;
// }
// else{
// while (currentNode != null) {
// currentNode = currentNode.next;
// }
// currentNode = newnode;
// }
// } catch (Exception e) {
// System.out.println("error");
// }
// }
// display linked list
void display(){
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) {
Link l = new Link();
l.display();
l.add_first(7);
l.add_first(6);
l.display();
}
}