-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkListDemo.java
More file actions
92 lines (72 loc) · 1.38 KB
/
Copy pathLinkListDemo.java
File metadata and controls
92 lines (72 loc) · 1.38 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
85
86
87
88
89
90
91
92
public class LinkListDemo {
public static void main(String[] args) {
LinkList mylist = new LinkList();
mylist.appendNode(3);
mylist.appendNode(5);
mylist.appendNode(7);
mylist.Print();
LinkList mylist2 = new LinkList();
mylist2.appendNode(5);
mylist2.appendNode(6);
mylist2.appendNode(7);
mylist2.Print();
}
}
/*
*
*
*/
class LinkNode {
int val;
LinkNode next;
LinkNode(int x, LinkNode nextNode) {
val = x;
this.next = nextNode;
}
}
class LinkList {
public int listSize;
public LinkNode head;
public LinkList() {
listSize = 0;
}
public void appendNode(int x) {
LinkNode end = new LinkNode(x, null);
LinkNode current = head;
if (current == null) {
current = end;
head = current;
listSize++;
return;
}
while (current.next != null) {
current = current.next;
}
current.next = end;
listSize++;
}
public boolean isEmpty() {
return (head == null);
}
public void Print() {
if (head == null) {
System.out.print("linked list is null");
return;
}
while (head != null) {
System.out.println(head.val);
head = head.next;
}
}
/**
*
* @param x
*/
public void deleteNode(LinkNode x) {
if (head == null) {
System.out.print("link list is empty, nothing can be deleted");
return;
} else {
}
}
}