主代码
1、定义单链表类
public class MySignal2 {
private ListNode2 head;
MySignal2() {
this.head = null;
}
public ListNode2 getHead() {
return head;
}
public void setHead(ListNode2 head) {
this.head = head;
}
public void display() {
ListNode2 listNode2 = this.head;
//MySignal2 mySignal21 = mySignal2;
while (listNode2 != null) {
System.out.print(listNode2.getDate() + " ");
listNode2 = listNode2.next;
}
System.out.println();
}
}
/*节点类*/
class ListNode2 {
private double date;
ListNode2 next;
ListNode2(double date) {
setDate(date);
setNext(null);
}
public ListNode2 getNext() {
return next;
}
public void setNext(ListNode2 next) {
this.next = next;
}
public double getDate() {
return date;
}
public void setDate(double date) {
this.date = date;
}
}
}
插入
1、头插法
2、尾插法
3、任意节点插入
/*头插法*/
public void addFirst(double data) {
ListNode2 listNode = new ListNode2(data);
if (head == null) {
this.head = listNode;
} else {
listNode.setNext(head);
this.head = listNode;
}
}
/*尾插法*/
public void addLast(double data) {
ListNode2 listNode2 = new ListNode2(data);
ListNode2 node2 = head;
if (head == null) {
head.next = listNode2;
}
while (node2.next != null) {
node2 = node2.next;
}
node2.next = listNode2;
}
/*求单链表长度*/
public int getLength() {
int count = 0;
ListNode2 listNode2 = this.head;
while (listNode2 != null) {
listNode2 = listNode2.next;
count++;
}
return count;
}
/*任意位置插入*/
private ListNode2 initPos(int init) {
ListNode2 listNode2 = this.head;
for (int i = 1; i < init; i++) {
listNode2 = listNode2.next;
}
return listNode2;
}
public void addInsert(int init, double data) {
ListNode2 listNode2 = new ListNode2(data);
if (init < 0 | init > this.getLength()) {
System.out.println("插入位置不合法!");
return;
} else if (init == 0) {
listNode2.next = this.head;
} else {
listNode2.next = this.initPos(init).next;
this.initPos(init).next = listNode2;
}
}
删除
1、删除第一个key节点
2、删除所有key节点
/*查找key节点是否在单链表内*/
public boolean seachKey(double key) {
ListNode2 listNode2 = this.head;
while (listNode2 != null) {
if (listNode2.getDate() == key) {
return true;
}
}
return false;
}
/*删除key节点*/
public void remove(double key) {
ListNode2 listNode2 = this.head;
int count = 0;
if (this.head == null) {
System.out.println("单链表为空");
return;
}
if (listNode2.getDate() == key) {
this.head = this.head.next;
}
while (listNode2.getDate() != key) {
listNode2 = listNode2.next;
count++;
}
this.initPos(count).next = listNode2.next;
}
/*删除所有key值*/
public void removeAllkey(double key) {
/*ListNode2 listNode2 = this.head ;
ListNode2 listNode3 = this.head.next;
while (listNode3!=null){
if (listNode2.next.getDate() == key){
listNode2.next = listNode3.next;
listNode3 = listNode3.next;
}else {
listNode2 = listNode3;
listNode3 = listNode3.next;
}
if (this.head.getDate() == key){
this.head = this.head.next;
}
}*/
ListNode2 listNode2;
ListNode2 listNode3;
if (this.head == null) {
System.out.println("单链表为空");
return;
}
while (this.head.getDate() == key) {
this.head = this.head.next;
}
listNode2 = this.head;
listNode3 = this.head.next;
while (listNode2.getDate() != key && listNode3 != null) {
//System.out.println("11");
//listNode3 = listNode2.next;
if (listNode3.getDate() == key) {
listNode2.next = listNode3.next;
listNode3 = listNode3.next;
} else {
listNode2 = listNode3;
listNode3 = listNode3.next;
}
}
}