-
Notifications
You must be signed in to change notification settings - Fork 44
/
Linklist.java
108 lines (92 loc) · 1.58 KB
/
Linklist.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.util.Scanner;
class Linklist {
static Node p = new Node(10);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
add(3);
add(3);
add(9);
add(7);
add(7);
add(8);
add(0);
toStr();
System.out.print("insert = ");
int b = sc.nextInt();
insert(50, b);
toStr();
delete(50);
toStr();
System.out.println("isfull : " + isfull());
System.out.println("isempty : " + isempty());
System.out.println(find(6));
}
public static void add(int d) {
Node p = new Node(d);
p.next = head;
head = p;
}
public static void insert(int d, int b) {
Node p = find(b);
if (p == null) {
add(d);
} else {
Node q = new Node(d);
q.next = p.next;
p.next = q;
}
}
public static Node find(int d) {
Node p = head;
while (p.data != d) {
if (p.next == null) {
return null;
} else
p = p.next;
}
return p;
}
public static Node delete(int d) {
Node r = head;
Node p = head;
while (p.data != d) {
if (p.next == null) {
return null;
} else
r = p;
p = p.next;
}
if (p == head) {
head = head.next;
} else {
r.next = p.next;
}
return p;
}
public static boolean isempty() {
if (head == null) {
return true;
} else
return false;
}
public static void toStr() {
Node p = head;
for (; p != null;) {
System.out.print(p.data + "\t");
p = p.next;
}
System.out.println();
}
public static boolean isfull() {
return false;
}
static class Node {
int data;
Node next;
Node(int d) {
this.data = d;
this.next = null;
}
}
static Node head = null;
}