java 链表操作,删除尾结点,指定位置结点

4 篇文章 0 订阅

这两天随便看了看 java 的链表,发现了一些有意思的事情。
一个单向连边一般只有一个头结点,头结点的结点指向,然后是链表的大小了。

若要对其他结点进行操作,必须对链表进行遍历,找到这个结点,然后进行相关操作。
遍历的代码一般是:

for (Node x = first; x != null; x = x.next){
    // 处理 x.item;
}

链表的相关操作:

  1. 添加新的头结点
  2. 删除尾结点
  3. 删除指定位置结点
  4. 在指定位置添加结点
  5. 查找链表里是否含有某个值

我自己编写的一个单向链表如下:

/** 
* @author chen zhen 
* @version 创建时间:2018年1月17日 上午10:56:50 
* @value 类说明: 链表的一些操作
*/
public class MyLink {
    private Node first; // 一个头结点
    private int N;            // 链表长度
 
    public MyLink() {
        first = null;
        N = 0;
    }
 
    /**
     * 自定义一个节点类
     */
    public static class Node{
        int item;
        Node next;
    }
 
    // 链表表头添加一个新的结点
    public void addNode(int i) {
        Node temp = first;
        first = new Node();
        first.item = i;
        first.next = temp;      
        N++;
    }
 
    // 删除一个尾结点
    public void deleteLastNode() {
        if (N == 0) 
            return;
        if (N == 1) 
            first = null;
        else 
            for (Node x = first; x != null; x = x.next) {
                if (x.next.next == null)
                    x.next = null;
            }   
        N--;
    }
 
    // 删除指定位置的结点
    public void deletePosition(int position) {
        if (position  == 1) {
            first = null;
            return;
        }
        if (position > N) 
            System.out.println("the position is too large");
        else {
            int index = 1; // 记录遍历的位置
            for (Node x = first; x != null; x = x.next) {
                if (index + 1 == position) {
                    x.next = x.next.next;
                    break;
                }
                index++;
            }
        }   
    }
 
    //在指定位置添加结点
    public void addToPosition(int position, int value) {
        if (position > N + 1) 
            System.out.println("the position is too large");
        else {
            int index = 1;
            for (Node x = first; x != null; x = x.next) {
                if (index + 1 == position) {
                    Node temp = new Node();
                    temp.item = value;
                    temp.next = x.next;
                    x.next = temp;
                    break;
                }
                index++;
            }
        }
    }
 
    // 查找链表里是否含有某个值
    public boolean find(int key) {
        for (Node x = first; x != null; x = x.next) {
            if (x.item == key) 
                return true;
        }
        return false;
    }
 
 
    // 定义一个 toString,方便打印
    public String toString() {
        StringBuilder str = new StringBuilder();
        for (Node x = first; x != null; x = x.next) {
            str.append(x.item);
            str.append(" ");
        }
        return str.toString();
    }
 
    public static void main(String[] args) {
        MyLink link = new MyLink();
        link.addNode(1);
        link.addNode(2);
        link.addNode(3);
        link.addNode(4);
 
        System.out.println(link.toString());
 
        link.deleteLastNode();
        System.out.println(link.toString());
 
        link.deletePosition(2);
        System.out.println(link.toString());
 
        link.addToPosition(2, 10);
        System.out.println(link.toString());
 
        System.out.println(link.find(2));
    }
 
}

输出结果:
4 3 2 1
4 3 2
4 2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值