首先创建接口
public interface ILinked {
//头插法
void addFirst(int data);
//尾插法
void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
boolean addIndex(int index,int data);
//关键字查找是否包含key是否在单链表当中
boolean contains(int key);
//删除第一次出现关键字为key的节点3.3 链表面试题
int remove(int key);
//删除所有值为key的节点
void removeAllKey(int key);
//得到单链表的长度
int getLength();
void display();
void clear();
}
然后具体实现
public class MySingleList implements ILinked {
class Node {
public int getData() {
return data;
}
private int data;
public Node getNext() {
return next;
}
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public Node getHead() {
return head;
}
private Node head;
public MySingleList() {
this.head = null;
}
@Override
public void addFirst(int data) {
Node node = new Node(data);
//第一次插入的时候,没有任何的数据节点
if (this.head == null) {
this.head = node;
} else {
node.next = this.head;
this.head = node;
}
}
@Override
public void addLast(int data) {
Node node = new Node(data);
if (this.head == null) {
this.head = node;
} else {
Node cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
//cur所指向的位置就是尾节点
cur.next = node;
}
}
//检查index合法性
private void checkIndex(int index) {
if (index < 0 || index > getLength()) {
throw new UnsupportedOperationException("Index不合法");
}
}
//找到index-1的位置,函数返回该位置的节点引用
private Node searchIndex(int index) {
int count = 0;
Node cur = this.head;
while (count < index - 1) {
cur = cur.next;
count++;
}
return cur;
}
@Override
public boolean addIndex(int index, int data) {
checkIndex((index));
//头插法
if (index == 0) {
addFirst(data);
return true;
} else {
Node node = new Node(data);
Node cur = searchIndex(index);
node.next = cur.next;
cur.next = node;
}
return true;
}
@Override
public boolean contains(int key) {
Node cur = this.head;
/*if(this.head==null){
return false;
}*/
while (cur != null) {
if (cur.data == key) {
return true;
}
cur = cur.next;
}
return false;
}
//查找关键字key的前驱
//如果找不到,返回null
private Node searchPre(int key) {
Node pre = this.head;
//头结点是要删除的数据节点
if (pre.data == key) {
return this.head;
}
while (pre.next != null) {
if (pre.next.data == key) {
return pre;
}
pre = pre.next;
}
return null;
}
@Override
public int remove(int key) {
int oldData = 0;
Node pre = searchPre(key);
if (pre == null) {
throw new UnsupportedOperationException("不存在key节点");
}
if (pre == head && pre.data == key) {
oldData = this.head.data;
this.head = this.head.next;
return oldData;
}
Node del = pre.next;
oldData = del.data;//先保存
pre.next = del.next;//删除节点
return oldData;
}
@Override
public void removeAllKey(int key) {
if (this.head == null) {
return;
}
Node pre = this.head;
Node cur = this.head.next;
while (cur != null) {
if (cur.data == key) {
pre.next = cur.next;
cur = cur.next;
} else {
pre = cur;
cur = cur.next;
}
}
if (this.head.data == key) {
this.head = this.head.next;
}
}
@Override
public int getLength() {
Node cur = this.head;
int length = 0;
while (cur != null) {
cur = cur.next;
length++;
}
return length;
}
@Override
public void display() {
Node cur = this.head;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
@Override
public void clear() {
Node cur = this.head;
while (this.head != null) {
this.head.next = null;
this.head = cur;
cur = cur.next;
}
}
测试类
public class Test2 {
public static void main(String[] args) throws InterruptedException {
MySingleList mySingleList = new MySingleList();
/*mySingleList.addFirst(10);
mySingleList.addFirst(20);
mySingleList.addFirst(30);
mySingleList.addFirst(40);
mySingleList.addFirst(50);
mySingleList.display();*/
/* mySingleList.addLast(10);
mySingleList.addFirst(88);
mySingleList.addLast(20);
mySingleList.addLast(30);
mySingleList.addFirst(99);
mySingleList.addLast(40);
mySingleList.display();*/
mySingleList.addIndex(0,10);
mySingleList.addIndex(1,99);
mySingleList.addIndex(2,20);
mySingleList.addIndex(3,30);
mySingleList.addIndex(4,88);
mySingleList.addLast(40);
mySingleList.addIndex(6,88);
mySingleList.display();//10 99 20 30 88 40 98
MySingleList.Node res = mySingleList.reverseList();
mySingleList.show(res);
/*mySingleList.show(mySingleList.reverseList());*/
/* System.out.println(mySingleList.contains(20));
System.out.println(mySingleList.contains(21));*/
/* mySingleList.remove(10);
mySingleList.display();//99 20 30 88 40 98*/
/* mySingleList.removeAllKey(88);
mySingleList.display();
*/
/*mySingleList.clear();*/
/* Thread.sleep(1000);
System.out.println("Hello bit");*/
/* mySingleList.addFirst(10);
mySingleList.addFirst(20);
mySingleList.addFirst(30);
mySingleList.addFirst(40);
System.out.println(mySingleList.getLength());*/
/*mySingleList.addLast(10);
mySingleList.addLast(20);
mySingleList.addLast(30);
System.out.println(mySingleList.contains(20));
*/
/* mySingleList.addFirst(10);
mySingleList.addFirst(20);
mySingleList.addFirst(30);
mySingleList.addFirst(40);
mySingleList.addFirst(50);
mySingleList.clear();
mySingleList.display();*/
}
}