接口实现
定义一个接口:
public interface ILinked {
//头插法
void addFirst(int data);
//尾插法
void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
boolean addindex(int index,int data);
//查找是否包含关键字key是否在单链表当中
boolean contains(int key);
//删除第一次出现关键字为key的节点
int remove(int key);
//删除所有值为key的节点
void removeAllKey(int key);
//得到单链表的长度
int getLength();
void display();
void clear();
}
实现该接口:
public class MyILinkedImpl implements ILinked {
//定义结点内部类
class Node{
private int data;
private Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
//定义一个头结点
private Node head;
public MyILinkedImpl(){
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.next = node;
}
}
//检查index位置的合法性
public void indexOk(int index){
if ((index < 0)||(index > getLength())){
throw new UnsupportedOperationException("Unsupported Operation");
}
}
//找到index位置的前驱结点
public Node preNode(int index){
Node cur = this.head;
for (int i = 0; i < index-1; i++) {
cur = cur.next;
}
return cur;
}
@Override
//在index位置上插入值为data的节点
public boolean addindex(int index, int data) {
indexOk(index);
if (index == 0){
addFirst(data);
return true;
}
Node node = new Node(data);
//接受index位置的前驱结点
Node pre = preNode(index);
node.next = pre.next;
pre.next = node;
return true;
}
@Override
public boolean contains(int key) {
Node cur = this.head;
while (cur != null){
if (cur.data == key){
return true;
}
cur = cur.next;
}
return false;
}
//查找值为key的前驱结点。在使用该方法时注意返回值有三种情况!
public Node searchPreKey(int key){
if (this.head.data == key){
return this.head;
}
Node cur = this.head;
while (cur.next != null){
if (cur.next.data == key){
return cur;
}
cur = cur.next;
}
return null;
}
@Override
public int remove(int key) {
//接收到值为key的前驱结点,下面判断三种情况
Node preNode = searchPreKey(key);
if (preNode == null){
throw new UnsupportedOperationException("Unsuppotred");
}
int oldData = 0;
//如果前驱结点为头结点且值为key的情况
if (preNode == this.head&&preNode.next.data == key){
oldData = preNode.next.data;
this.head = this.head.next;
}else
//如果前驱结点不是头结点且值为key的情况
if (preNode != this.head&&preNode.next.data == key){
oldData = preNode.next.data;
preNode.next = preNode.next.next;
}
//返回删除结点处的值
return oldData;
}
@Override
public void removeAllKey(int key) {
if (this.head == null){
return;
}
Node pre = this.head;
Node cur = pre.next;
//遍历删除所有的key
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() {
int count = 0;
Node cur = this.head;
while (cur != null){
count++;
cur = cur.next;
}
return count;
}
@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() {
while (this.head.next != null){
Node del = this.head.next;
this.head = del.next;
}
this.head = null;
}
}