主要通过编码实现 单链表 的基础操作:
1 从头插入节点
2 插入 链表 中指定位置的节点
3 根据指定的value来查找节点
4 根据指定的节点索引来查询节点
5 删除第一个节点
6 删除节点指定位置的值
7 删除指定索引的节点
8 打印列表信息
public class Node {
public Node next;//指针域
public long data;//数据域
public Node(long data){
this.data = data;
}
/**
* 显示数据
*/
public void show(){
System.out.print(data + " ");
}
}
class NodeApi {
private Node firstNode;
public NodeApi() {
firstNode = null;
}
/**
* 从头开始插入
* @param value
*/
public void insertFromBeginValue(long value){
Node current Node = new Node(value);
currentNode.next = firstNode;
firstNode = currentNode;
}
/**
* 基于循环遍历插入
* 实现思路 1 从头开始交换 2 交互指针域 3 交互值域
* @param value 指定节点位置的值
* @param index 要插入的位置的索引
*/
private void insertNodeByPostiiton01(int index,long value){
if (index<0){
System.out.println("插入索引节点位置不合法!");
}
else {
Node currentNode = firstNode;
Node insertNode = new Node(value);
while (currentNode!=null){
currentNode = currentNode.next;
index--;
}
insertNode.next = currentNode.next;//指针域
currentNode.next = insertNode;//值域
}
}
/**
* 基于查询思路
* 1 根据索引查找出当前索引对应的节点
* 2 改变节点指针域
* 3 改变节点值域
* @param index
* @param value
*/
private void insertNodeByPostiiton02(int index,long value){
if (index<0){
System.out.println("插入索引节点位置不合法!");
}
else {
Node insertNode = new Node(value);
Node indexNode = findNodeByIndex(index);
insertNode.next = indexNode.next;//指针域
indexNode.next = insertNode;//值域
}
}
/**
* 根据指定的值域查找节点
* @param value
* @return
*/
public Node findNodeByValue(long value){
Node currentNode = new Node(value);
//判断当前节点值和要查找的值是否一致
while(currentNode.data!=value){
if (currentNode.next == null) {
System.out.println("对不起已经查询到最后,请确认你输入的是否正确");
return null;
}
currentNode = currentNode.next;
}
return currentNode;
}
/**
* 根据指定的节点索引查找节点
* @param index
* @return
*/
public Node findNodeByIndex(int index){
Node currentNode = firstNode;
int currentIndex=1;
if (index<0){
System.out.println("对不起你输入的查找索引参数有误!");
return null;
}
else {
while (currentNode != null && index > currentIndex) {
currentIndex++;
currentNode = currentNode.next;
}
}
return currentNode;
}
/**
* 查找第一个节点
* @return
*/
public Node findFirstNode(){
Node currentNode = firstNode;
return currentNode;
}
/**
* 删除第一个节点
* @return
*/
public Node deleteFirstNode(){
Node currentNode = firstNode;
firstNode=currentNode.next;
return currentNode;
}
/**
* 根据指定的值 删除节点
* @param value
* @return
*/
public void deleteNodeByParam(long value){
Node previousNode = firstNode;
Node currentNode = firstNode;
while (currentNode.data!=value){
if (currentNode.next==null){
}
previousNode = currentNode;
currentNode = currentNode.next;
}
//删除第一个节点
if (currentNode==firstNode){
firstNode = firstNode.next;
}
else {
previousNode.next = currentNode.next;
}
}
/**
* 根据指定的索引 删除节点
* @param index
* @return
*/
public void deleteNodeByIndex(int index){
// index =3;
Node currentNode = firstNode;
Node indexNode = findNodeByIndex(index);
if (index<0){
System.out.println("对不起删除的节点索引不合理..");
}
//删除的是第一个节点
else if (index==1){
firstNode = firstNode.next;
}
else{
int len=1;
while (currentNode.next!=null){
if (index == ++len){
currentNode.next = currentNode.next.next;
return;
}
currentNode = currentNode.next;
}
}
}
/**
* 显示列表数据
*/
public void showLinkList(Node node){
Node currentNode = null;
if (node==null){
currentNode = firstNode;
}
else{
currentNode = node;
}
while (currentNode!=null){
currentNode.show();
//修改指针
currentNode = currentNode.next;
}
//换行
System.out.println();
}
公众号分类整理了不少好用的学习资料快去看看吧
