- 头插法
- 尾插法
- 任意位置插入,第一个数据节点为0下标
- 查找是否包含关键字key在单链表当中
- 删除第一次出现关键字为key的节点
- 删除所有值为key的节点
- 得到链表的长度
class Node{
//一个Java文件可以存在多个class类,但是有且只能有一个可以是public class,并且这个public类的类名和文件名完全一样
public int data;//数据
public Node next=null;//下一个节点的位置,这里是引用,相当于低配指针
public Node(int data) {
super();
this.data = data;
}
}
public class LinkedList {
//每个数据+补充信息=节点(node)
//管理所有的链表节点,只需要记录所有节点的位置
//对于一个单向链表来说(只能找到下一个节点),只要知道第一个节点的位置,就能够获取到所有的节点
//通常就会用第一个节点的位置来表示整个链表,这样的第一个节点就会称为“头节点”
//通常就会使用一个头节点来代指整个链表(借代)
private Node head=null;
public void addFirst(int data) {
//头插
//1、根据data的值,构建一个链表节点(Node对象)
Node node=new Node(data);//调用用构造方法
//2、如果是空链表,head=node,头插就完成了
if(head == null) {
head=node;
return;
}
//3、如果不是空链表
node.next=head;
head =node;
}
public void addLast(int data) {
//尾插
//1、根据data构造一个Node对象
Node node=new Node(data);
//2\如果链表为空
if(head==null) {
head=node;
return;
}
//3、如果里链表非空,需要先找到这个链表末尾的最后一个节点
Node tail=head;//tail和head都是引用,注意:Java中的引用不用解引用
while(tail.next!=null) {
tail=tail.next;
}
//循环结束之后,tail就对应到最后一个节点了
tail.next=node;
}
public void display() {
//将链表中的每个元素都打印出来,
for(Node cur=head;cur!=null;cur=cur.next) {
//cur也是引用,存的是地址
System.out.print(cur.data+" ");
}
System.out.println();
}
public int getSize() {
int size=0;
for(Node cur=head;cur!=null;cur=cur.next){
size++;
}
return size;
}
public boolean addIndex(int index,int data) {
int size=getSize();
//插入成功返回true,不成功返回false
//1、判定index是否有效
if(index<0||index>size) {
//此处需要求size
//index无效,插入失败
return false;
}
//2、如果index为0;相当于头插
if(index==0) {
addFirst(data);
return true;
}
//3、如果index为size,相当于尾插
if(index==size) {
addLast(data);
return true;
}
Node node=new Node(data);
// 4、如果index是一个中间位置
//4.1、先找到index的前一个节点index-1
Node prev=getPos(index-1);
//4.2、接下来就把新节点插入到pre之后
node.next=prev.next;
prev.next=node;
return true;
}
private Node getPos(int index) {
//给定index下标,找到对应节点
Node cur=head;
for(int i=0;i<index;i++) {
//cur.next操作之前,必须要保证cur是非null的
cur=cur.next;
}
return cur;
}
public boolean contains(int toFind) {
for(Node cur=head;cur!=null;cur=cur.next) {
if(cur.data==toFind) {
return true;
}
}
return false;
}
public void remove(int toRemove) {
//1、如果要删除元素是头节点,特殊处理一下
if(head.data==toRemove) {
//头结点要被删除掉
head=head.next;
return;
}
//2、如果要删除元素不是头节点,要找到要删除节点的前一个位置
Node prev=searchPrev(toRemove);
//3、修改引用的指向,完成删除
Node toDelete=prev.next;
prev.next=toDelete.next;
}
private Node searchPrev(int toRemove) {
//找到toRemove的前一个节点,
for(Node cur=head;cur!=null&&cur.next!=null;cur=cur.next) {
if(cur.next.data==toRemove) {
return cur;
}
}
return null;
}
}
代码测试部分:
public class TsetLinkedList {
private static void testAddFirst() {
LinkedList linkedList=new LinkedList();
linkedList.addFirst(1);
linkedList.addFirst(1);
linkedList.addFirst(1);
linkedList.addFirst(1);
}
private static void testAddLast() {
LinkedList linkedList=new LinkedList();
linkedList.addLast(1);
linkedList.addLast(2);
linkedList.addLast(3);
linkedList.addLast(4);
linkedList.display();
}
public static void testAddIndex() {
LinkedList linkedList=new LinkedList();
linkedList.addIndex(0,1);
linkedList.addIndex(1,2);
linkedList.addIndex(1,3);
linkedList.addIndex(1,4);
linkedList.display();
}
private static void testContains() {
LinkedList linkedList=new LinkedList();
linkedList.addLast(1);
linkedList.addLast(2);
linkedList.addLast(3);
linkedList.addLast(4);
System.out.print(linkedList.contains(3));
}
private static void testRemove() {
LinkedList linkedList=new LinkedList();
linkedList.addLast(1);
linkedList.addLast(2);
linkedList.addLast(3);
linkedList.addLast(4);
linkedList.remove(3);
linkedList.display();
}
public static void main(String[] args) {
//testAddFirst();
//testAddLast();
//testAddIndex();
//testContains();
testRemove();
}
}