java实现无头单链表的基本操作(新手向)
最好懂的单链表操作
public class SingleLinkedList {
/** 链表小问题
* while(cur.next != null) 尾插法使用 遇到打印或者是计数等 最后一个cur节点会被忽略 因为进不到循环里
* while(cur !=null ) 打印 计数时使用
*/
static class ListNode{
public int val;//数据域
public ListNode next;//指针域
public ListNode(int val){//构造方法 初始化val
this.val = val;
}
}
public ListNode head;//头结点
//简单构造链表
public void createList(){
ListNode listNode1 = new ListNode(12);
ListNode listNode2 = new ListNode(23);
ListNode listNode3 = new ListNode(34);
ListNode listNode4 = new ListNode(45);
ListNode listNode5 = new ListNode(56);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
this.head = listNode1;
}
//头插法 新节点的next指向head head 移动到新节点上
public void addFirst(int data){
ListNode node = new ListNode(data);
node.next = this.head;
this.head = node;
}
//尾插法 定义cur 找到cur.next == null cur为最后一个节点 cur.next指向新节点
public void addLast(int data) {
ListNode node = new ListNode(data);
if(this.head == null){
this.head = node;
}else{
ListNode cur = head;
while(cur.next != null){
cur = cur.next;
}
cur.next = node;
}
}
//任意位置的前面插入,第一个数据节点为0号下标
public boolean addIndex(int index,int data) throws MyAddIndexExcpetion {//异常声明
if(index < 0 || index > size()){
throw new MyAddIndexExcpetion("下标越界");//抛出一个指定的自定义异常
}
if(index == 0){
addFirst((data));
return true;
}
if(index == size()){
addLast(data);
return true;
}
ListNode node = new ListNode(data);
ListNode cur = findIndexSubOne(index);
//cur.next=index下标位置的节点 在index下标前插入
node.next = cur.next;
cur.next = node;
return true;
}
//找到index的位置节点
public ListNode findIndexSubOne(int index){
ListNode cur = this.head;
//index下标的节点
while(index != 0){//3个节点 index=1 就是下标为1的节点 是第二个元素
cur = cur.next;//while进入一次 cur指向第二个元素
index--;
}
return cur;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
ListNode cur = this.head;
while(cur.next != null){
if(cur.val == key){
System.out.println(key+"存在");
return true;
}
cur = cur.next;
}
return false;
}
//删除第一次出现关键字为key的节点
public void remove(int key){
ListNode cur = this.head;
if(this.head == null){
System.out.println("链表为空 不能删除");
return;
}
if(this.head.val == key){
this.head = this.head.next;
return;
}
while(cur.next != null){
if( cur.next.val == key){
break;//返回cur cur.next是key节点
}
cur = cur.next;
}
//跳过了cur.next节点 指向了cur.next.next
cur.next = cur.next.next;
}
//删除所有值为key的节点
public void removeAllKey(int key){
if(this.head == null){
return;
}
ListNode cur = this.head.next;
ListNode prev = this.head;//可以记录成cur的前驱
//while(cur.next) cur.next如果为key 则不进入循环
while(cur != null){
if(cur.val == key){
prev.next = cur.next;
cur = cur.next;
}else{
prev = cur;
cur = cur.next;
}
//cur = cur.next;
}
//处理head为key的情况
if(this.head.val == key){
head = head.next;
}
}
//得到单链表的长度
public int size(){
int count = 0;
ListNode cur = this.head;
if(this.head == null){
return count;
}
//两个节点 条件为cur.next!=null count只++一次
while(cur != null){
count++;
cur = cur.next;
}
return count;
}
public void display(){
ListNode cur = this.head;
if(this.head == null){
System.out.println("null");
}
while(cur != null){
System.out.println(cur.val);
cur = cur.next;
}
System.out.println();
}
public void clear(){
//this.head = null;//暴力 没有了引用 就都为null;内存回收交给JVM
ListNode cur = this.head;
ListNode curNext = null;//记录
while(cur != null){
curNext = cur.next;
//没有了引用
cur.next = null;
cur = curNext;
}
//最后把头结点的引用置空
this.head = null;
}
}
自定义异常
public class MyAddIndexExcpetion extends Exception{
public MyAddIndexExcpetion(String message){
super(message);
}
}