無頭結點的單鏈表(java實現)
阿新 • • 發佈:2019-01-25
import java.util.Random;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
class List {
Node first = null;
List(int n) {
Random rand = new Random(96);
for (int i = 0; i < n; i++) {
insert(rand.nextInt() %100);
}
}
public void insert(int data) {
Node node = new Node(data);
Node tmp;
if (first == null) {
first = node;
return;
}
if (first.data < data) {
node.next = first;
first = node;
return;
}
tmp = first;
while (tmp.next != null) {
if (data > tmp.next.data)
break;
tmp = tmp.next;
}
node.next = tmp.next;
tmp.next = node;
}
public void delete(int data) {
Node tmp;
if (data == first.data) {
first = first.next;
return;
}
tmp = first;
while (tmp.next != null) {
if (data == tmp.next.data) {
tmp.next = tmp.next.next;
return;
}
tmp = tmp.next;
}
}
public void reverse1(){
Node pre, cur, last;
pre = null;
cur = first;
while (cur != null) {
last = cur.next;
cur.next = pre;
pre = cur;
cur = last;
}
first = pre;
}
public void reverse2(){
Node cur, last;
cur = first;
if (cur == null || cur.next == null)
return;
while (cur.next.next != null)
cur = cur.next;
last = cur.next;
cur.next = null;
reverse2();
last.next = cur;
first = last;
}
public void reverse3() {
Node cur, last;
cur = first;
if(cur == null || cur.next == null)
return;
last = cur.next;
first = last;
reverse3();
last.next = cur;
cur.next = null;
}
public void show() {
Node tmp;
tmp = first;
while (tmp != null) {
System.out.print(tmp.data + " ");
tmp = tmp.next;
}
System.out.println();
}
}
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
class List {
Node first = null;
List(int n) {
Random rand = new Random(96);
for (int i = 0; i < n; i++) {
insert(rand.nextInt() %100);
}
}
public void insert(int data) {
Node node = new Node(data);
Node tmp;
if (first == null) {
first = node;
return;
}
if (first.data < data) {
node.next = first;
first = node;
return;
}
tmp = first;
while (tmp.next != null) {
if (data > tmp.next.data)
break;
tmp = tmp.next;
}
node.next = tmp.next;
tmp.next = node;
}
public void delete(int data) {
Node tmp;
if (data == first.data) {
first = first.next;
return;
}
tmp = first;
while (tmp.next != null) {
if (data == tmp.next.data) {
tmp.next = tmp.next.next;
return;
}
tmp = tmp.next;
}
}
public void reverse1(){
Node pre, cur, last;
pre = null;
cur = first;
while (cur != null) {
last = cur.next;
cur.next = pre;
pre = cur;
cur = last;
}
first = pre;
}
public void reverse2(){
Node cur, last;
cur = first;
if (cur == null || cur.next == null)
return;
while (cur.next.next != null)
cur = cur.next;
last = cur.next;
cur.next = null;
reverse2();
last.next = cur;
first = last;
}
public void reverse3() {
Node cur, last;
cur = first;
if(cur == null || cur.next == null)
return;
last = cur.next;
first = last;
reverse3();
last.next = cur;
cur.next = null;
}
public void show() {
Node tmp;
tmp = first;
while (tmp != null) {
System.out.print(tmp.data + " ");
tmp = tmp.next;
}
System.out.println();
}
}