单链表的测试
/**
* 单链表的使用:
* 单链表由节点组成
* @author wgsstart
* @creat 2021-03-04 16:24
*/
public class Test {
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addLast(10);
myLinkedList.addLast(12);
myLinkedList.addLast(12);
myLinkedList.addLast(13);
myLinkedList.addLast(16);
myLinkedList.display();
myLinkedList.clear();
System.out.println("hhhhhhhhhhh");
/* boolean flag = myLinkedList.contains(11);
System.out.println(flag);
System.out.println(myLinkedList.size())*/
/* myLinkedList.display();
System.out.println();
myLinkedList.addIndex(0,12);
myLinkedList.display();
System.out.println();*/
/* myLinkedList.remove(12);
myLinkedList.display();
*/
/*myLinkedList.removeAllKey(12);
myLinkedList.display();*/
}
}
单链表的方法
/**
* this代表当前对象的引用。
* 引用:属性/方法
* 实例成员变量
* 静态成员变量
* this()
* this.data
* this.func()
* @author wgsstart
* @creat 2021-03-04 17:09
*/
class Node{
public int data;//两个属性默认值为0
public Node next;//默认值null
public Node(int data){//构造方法
this.data = data;
this.next = null;//先置为空,可以方便下面放在任意处,可以灵活。
}
}
public class MyLinkedList {
public Node head;//保存单链表的头节点的引用
//头插法
public void addFirst(int data){
Node node = new Node(data);//这一行代码之后,我们就已经有了这么一个节点了
if(this.head == null){
//第一次插入节点
this.head = node;
return;
}
node.next = this.head;
this.head = node;
}
//打印单链表
public void display(){
Node cur = this.head;
while(cur!=null){
System.out.print(cur.data+" ");
cur = cur.next;
}
}
//尾插法
public void addLast(int data){
Node node = new Node(data);
if(this.head == null){//如果是第一次插入的话
this.head = node;
return;
}
Node cur = this.head;
while(cur.next!=null ){
cur = cur.next;
}
cur.next = node;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
Node cur = this.head;
while(cur!=null){
if(cur.data == key){
return true;
}
cur = cur.next;
}
return false;
}
//得到单链表的长度
public int size(){
Node cur = this.head;
int count = 0;
while(cur!=null){
count++;
cur = cur.next;
}
return count;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){
if(index == 0){//头插法
addFirst(data);
return;
}
if(index == this.size()){//尾插法
addLast(data);
return;
}
//先找到 index位置的前一个节点的地址
Node cur = searchIndex(index);
//进行插入
Node node = new Node(data);
node.next = cur.next;
cur.next = node;
}
private Node searchIndex(int index){
//1.对index进行合法性检查
if(index < 0||index > this.size()){
throw new RuntimeException("index位置不合法!");
}
Node cur = this.head;//index-1
while(index-1 !=0){
cur = cur.next;
index--;
}
return cur;
}
private Node searchPrev(int key){//找前驱,如果找到了返回前驱,找不到返回null;
Node prev = this.head;
while(prev.next!=null){
if(prev.next.data == key){
return prev;
}else {
prev = prev.next;
}
}
return null;
}
//删除第一次出现关键字为key的节点
public void remove(int key){
if(this.head == null){
return;
}
if(this.head.data == key){//这种情况直接将head往后挪
this.head = this.head.next;
return;
}
Node prev = searchPrev(key);//来找当前key的前驱
if(prev == null){
System.out.println("根本没有这个节点!");
return;
}
Node del = prev.next;
prev.next = del.next;
}
//删除所有值为key的节点
public void removeAllKey(int key){
Node prev = this.head;
Node cur = this.head.next;//
while(cur!=null){
if(cur.data == key){
prev.next = cur.next;
cur = cur.next;
}else{
prev = cur;
cur = cur.next;
}
}
if(this.head.data == key){
this.head = this.head.next;
}
}
public void clear(){
this.head = null;
}
}