接口方法:
public interface ILinked {
//头插法
void addFirst(int data);
//尾插法
void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
void 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 MySingleLinkedListImpl implements ILinked {
class Node {
private int data;
private Node next;
//private Node size;
public Node(int data) {
this.data = data;
this.next = null;
//this.front = null;
//this.length = 0;
}
}
private Node head;
public MySingleLinkedListImpl() {
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);
Node cur = this.head;
if(this.head == null){
this.head = node;
}
else {
while (cur.next != null){
cur = cur.next;
}
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){
Node cur = this.head;
for (int i = 0; i < index-1; i++) {
cur = cur.next;
}
return cur;
}
@Override
public void addindex(int index, int data) {
checkIndex(index);
Node node = new Node(data);
if (head == null) {
this.head = node;
//return false;
}
if (index == 0) {
addFirst(data);
//node.next = head;
//head = node;
//return true;
}
Node cur = searchIndex(index);
node.next = cur.next;
cur.next = node;
//return true;
}
public Node select(int data){
Node node = head;
while (node != null && node.data != data){
node = node.next;
}
return node;
}
@Override
public boolean contains(int key) {
Node node = this.select(key);
if (node == null){
System.out.println("该节点不存在!");
return false;
}
Node cur = head;
if(head.data == key){
return true;
}
while (cur != null && cur.data != key){
cur = cur.next;
if (cur.data == key){
return true;
}
}
return false;
}
//查找key的前驱
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 pre;
}
@Override
public int remove(int key) {
Node pre = searchPre(key);
if (pre == null) {
throw new UnsupportedOperationException("要删除的节点不存在!");
}
int oldData;
if(pre == head){
oldData = pre.data;
head = head.next;
return oldData;
}
oldData = pre.next.data;
pre.next = pre.next.next;
return oldData;
}
@Override
public void removeAllKey(int key) {
if (this.head== null) {
throw new UnsupportedOperationException("要删除的节点不存在!");
}
Node cur = this.head;
Node pre = searchPre(key);
//Node cur = this.head;
//Node pre = this.head.next;
if (head.data == key) {
head = head.next;
}
while(cur != null) {
if(cur.data == key) {
pre.next = cur.next;
}
pre = cur;
cur = cur.next;
}
}
@Override
public int getLength() {
int length = 0;
Node cur = this.head;
while (cur != null){
length++;
cur = cur.next;
}
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() {
//不建议this.head = null;
if(this.head == null){
throw new UnsupportedOperationException("单链表为空");
}
while (this.head != null){
/*Node cur = this.head;
cur.next = null;
this.head = cur;*/
Node cur = this.head.next;
this.head.next = null;
this.head = cur;
}
}
public Node reverseList(){
Node cur = this.head;
Node pre = null;
Node reverseHead = null;
while(cur != null){
Node curNext = cur.next;
if (curNext == null){
reverseHead = cur;
}
cur.next = pre;
pre = cur;
cur = curNext;
}
return reverseHead;
}
public void show(Node revList){
Node cur = revList;
while(cur != null) {
System.out.print(cur.data+" ");
cur = cur.next;
}
System.out.println();
}
}
测试用例:
public class TestMain {
public static void main(String[] args) {
MySingleLinkedListImpl mySingleLinkedListList = new MySingleLinkedListImpl();
System.out.println("头插法");
mySingleLinkedListList.addFirst(10);
mySingleLinkedListList.display();
mySingleLinkedListList.addFirst(20);
mySingleLinkedListList.display();
mySingleLinkedListList.addFirst(30);
mySingleLinkedListList.display();
mySingleLinkedListList.addFirst(40);
mySingleLinkedListList.display();
mySingleLinkedListList.addFirst(50);
mySingleLinkedListList.display();
System.out.println("===========================================================");
System.out.println("尾插法");
mySingleLinkedListList.addLast(10);
mySingleLinkedListList.display();
mySingleLinkedListList.addLast(20);
mySingleLinkedListList.display();
mySingleLinkedListList.addLast(30);
mySingleLinkedListList.display();
mySingleLinkedListList.addLast(40);
mySingleLinkedListList.display();
mySingleLinkedListList.addLast(50);
mySingleLinkedListList.display();
System.out.println("===========================================================");
System.out.println("任意位置插入,第一个数据节点为0号下标");
//任意位置插入,第一个数据节点为0号下标
mySingleLinkedListList.addFirst(10);
mySingleLinkedListList.addFirst(20);
mySingleLinkedListList.addFirst(30);
mySingleLinkedListList.addFirst(40);
//System.out.println(mySingleLinkedListList.addindex(2,110));
mySingleLinkedListList.addindex(2,666);
mySingleLinkedListList.display();
System.out.println("===========================================================");
System.out.println("查找是否包含关键字key是否在单链表当中");
//查找是否包含关键字key是否在单链表当中
System.out.println(mySingleLinkedListList.contains(10));
System.out.println("===========================================================");
System.out.println("删除第一次出现关键字为key的节点");
//删除第一次出现关键字为key的节点
mySingleLinkedListList.remove(666);
mySingleLinkedListList.display();
System.out.println("===========================================================");
System.out.println("删除所有值为key的节点");
//删除所有值为key的节点
mySingleLinkedListList.removeAllKey(30);
mySingleLinkedListList.display();
System.out.println("===========================================================");
System.out.println("得到单链表的长度");
//得到单链表的长度
System.out.println(mySingleLinkedListList.getLength());
System.out.println("===========================================================");
//System.out.println("清空单链表");
//mySingleLinkedListList.clear();
//mySingleLinkedListList.display();
System.out.println("===========================================================");
System.out.println("逆置单链表");
mySingleLinkedListList.show(mySingleLinkedListList.reverseList());
}
}