算法(链表链表的创建、插入节点、删除节点、遍历节点及其长度)

                              id:671593      转自:Hitsu

本人在idea中能够正常运行出来(结果3 3 2 1 4)。

链表的创建、插入节点、删除节点、遍历节点及其长度。

Java构建链表两种方式

第一种 (这一种写出来看起来更简洁)

Java构建链表两种方式

第一种 (这一种写出来看起来更简洁)

public class Node {
    int data;
    Node next;
​
    public Node(int data) {
        this.data = data;
​
    }
}

第二种 (这一种是较为规范的,但是其他类不能使用成员变量,所以建议把private去掉来使用,否则的话在写其他相关的方法的时候不好写,本人写不出来)

public class Node {
    private int data;
    private Node next;
​
    public Node(int data) {
        this.data = data;
    }
    public int getData() {
        return data;
    }
    public void setData(int data) {
        this.data = data;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }
}

链表算法的代码

插入节点、删除节点、遍历节点及其长度。

public  class LinkedList {
    Node head;
​
    //在某个节点插入数据
    public  void  insertPosition(int data,int position){
        Node node = new Node(data);
        //当插入节点为头结点时。
        if(position == 1){
            node.next = head;
            head =node;
        }else {
            //非头结点,其他及其他情况
            Node current = head;
            int count= 1;
            while (current != null && count < position-1){
                current = current.next;
                count++;
            }
            if(current != null){
                node.next = current.next;
                current.next = node;
            }else {
                //在非循序范围内的其他情况
                System.out.println("输入的范围不正确");
            }
        }
    }
​
    //遍历数据,在此基础上进行链表长度的输出
    public void printAndLength(){
        int length = 0;
        Node current = head;
        while (current != null ){
            length++;
            System.out.println(current.data);
            current = current.next;
        }
        System.out.println(length);
    }
​
    //删除链表的某个节点
    public void deletePosition(int position){
        //删除节点为头结点时
        if (position == 1){
            head = head.next;
        }else {
            //删除节点为非头节点,以及有其他情况时。
            Node current = head;
            int count = 1;
            while (current != null && count < position - 1) {
                current = current.next;
                count++;
            }
            if (current != null) {
                current.next = current.next.next;
            } else {
                //在非循序范围内的其他情况
                System.out.println("请在允许范围内进行");
            }
        }
​
​
    }
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insertPosition(1,1);
        list.insertPosition(2,1);
        list.insertPosition(3,1);
        list.insertPosition(3,1);
        list.insertPosition(3,1);
        list.deletePosition(1);
        list.printAndLength();
    }
}

             

         另一种情况不在类中,单独写的,需要传递参数head.

需要把类中的Node head;去掉,并把参数写道方法中。

但是本人不能够把其实现出来,希望有大佬能够帮下忙。

(部分代码示例)。

// 在链表中指定位置插入新节点
public void insertAtPosition(Node head,int data, int position) {
    Node newNode = new Node(data);
    if (position == 1) {
        newNode.next = head;
        head = newNode;
    } else {
        Node currentNode = head;
        int count = 1;
        while (count < position - 1 && currentNode != null) {
            currentNode = currentNode.next;
            count++;
        }
        if (currentNode != null) {
            newNode.next = currentNode.next;
            currentNode.next = newNode;
        } else {
            System.out.println("Invalid position");
        }
    }
}

如有错误请指出!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值