目录
PosNotLegalException异常(用来判断pos是否合法)
用到的包和接口
IMySingleLinkedList接口
package singlelinkedlist;
public interface IMySingleLinkedList {
void addFirst(int val);
void addLast(int val);
void display();
int size();
void addIndex(int pos,int val);
boolean contains(int val);
void remove(int val);
void removeAllKey(int val);
void clear();
}
PosNotLegalException异常(用来判断pos是否合法)
public class PosNotLegalException extends RuntimeException {
public PosNotLegalException(){}
public PosNotLegalException(String msg){
super(msg);
}
}
MySingleLinkedList类
package singlelinkedlist;
public class MySingleLinkedList implements IMySingleLinkedList {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
//创建头节点head
public ListNode head;
@Override
public void addFirst(int val) {
//首先要申请一个节点
ListNode newNode = new ListNode(val);
newNode.next = head;
head = newNode;
}
@Override
public void addLast(int val) {
ListNode newNode = new ListNode(val);
if (head == null) {
head = newNode;
return;
}
ListNode current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
@Override
public int size() {
ListNode current = head;
int count = 0;
while (current != null) {
count++;
current = current.next;
}
return count;
}
@Override
public void addIndex(int pos, int val) {
//需要判断pos的合法性
try {
checkPosLegal(pos);
} catch (PosNotLegalException e) {
e.printStackTrace();
}
//pos在开头
if (pos == 0) {
addFirst(val);
return;
}
//pos在结尾
if (pos == size()) {
addLast(val);
return;
}
//pos在中间
ListNode newNode = new ListNode(val);
ListNode current = head;
int count = 0;
//找到pos前面一个位置
while(pos-1 >0){
current = current.next;
pos--;
}
//进行插入
newNode.next = current.next;
current.next = newNode;
}
private void checkPosLegal(int pos) throws PosNotLegalException {
if (pos < 0 || pos > size()) {
throw new PosNotLegalException("Pos位置不合法...");
}
}
@Override
public boolean contains(int val) {
ListNode current = head;
while(current!=null){
if(current.val == val){
return true;
}
current = current.next;
}
return false;
}
@Override
public void remove(int val) {
//在头部
if(val == head.val){
head = head.next;
return;
}
//不在头部
ListNode current = head;
while(current.next!=null ){
if(current.next.val == val){
//此时的current已经是想要删除的前一个节点
current.next = current.next.next;
}
current = current.next;
}
}
@Override
public void removeAllKey(int val) {
if(head == null){
return;
}
ListNode prev = head;
ListNode cur = head.next;
while(cur!=null){
if(cur.val == val){
prev.next = cur.next;
}else{
prev = prev.next;
}
cur =cur.next;
}
//处理头节点
if(head.val == val){
head = head.next;
}
}
@Override
public void display() {
ListNode current = head;
while (current != null) {
System.out.print(current.val);
if (current.next != null) {
System.out.print("->");
}
current = current.next;
}
System.out.println();
}
@Override
public void clear() {
ListNode cur = head;
while(cur!=null){
ListNode del = cur;
cur = cur.next;
del.next = null;
}
}
}
Test测试类
public class Test {
public static void main(String[] args) {
IMySingleLinkedList list = new MySingleLinkedList();
/*list.addFirst(1);
list.addFirst(2);
list.addFirst(3);
System.out.println("====addFirst===");
list.display();*/
System.out.println("===addLast===");
list.addLast(3);
list.addLast(2);
list.addLast(3);
list.addLast(3);
list.addLast(3);
list.display();
System.out.println("===size===");
System.out.println(list.size());
/*System.out.println("===addIndex===");
list.addIndex(0,10);
list.addIndex(4,10);
list.display();
list.addIndex(6,10);*/
System.out.println("===contains===");
System.out.println(list.contains(1));
System.out.println(list.contains(3));
/* System.out.println("===remove===");
list.remove(1);
list.remove(1);
list.display();*/
System.out.println("===removeAllKey===");
list.removeAllKey(3);
list.display();
list.clear();
}
}