首页 > 其他分享 >数据结构链表的基本操作

数据结构链表的基本操作

时间:2023-07-10 21:46:39浏览次数:37  
标签:Node index null value next 链表 基本操作 数据结构 public

/*数据结构单向链表基本操作
节点类
 */

import java.util.Iterator;
import java.util.function.Consumer;

public class shujujiegou implements Iterable<Integer> {//整体
    private Node head;//头指针

    @Override
    public Iterator<Integer> iterator() {
        //匿名内部类->带名字的内部类
        return new NodeIterator();
    }
    private class NodeIterator implements Iterator<Integer> {
        Node p=head;

        @Override
        public boolean hasNext() {//是否有下一个元素
            return p!=null;//不空返回真
        }

        @Override
        public Integer next() {//返回当前值,并指向下一个元素
            int v=p.value;
            p=p.next;
            return v;
        }
    }

    private static class Node {
        int value;//值
        Node next;//下一个节点指针

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    public void addFirst(int value) {
        //1.链表为空
        // head=new Node(value,null);
        //2.链表非空(头插)
        head = new Node(value, head);
    }
//遍历链表
    //Params:consumer-要执行的操作
    public void loop(Consumer<Integer> consumer) {
        Node p = head;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
    }
//遍历链表2
    //Params:consumer-要执行的操作
    public void loop2(Consumer<Integer> consumer) {
        for (Node p = head; p != null; p = p.next){
            consumer.accept(p.value);
        }
    }

    private Node findLast(){
        if(head==null){
            return null;
        }
        Node p;
        for(p=head;p.next!=null;p=p.next){

        }
        return p;
    }
    public void addLast(int value){
        Node last=findLast();
        if(last==null){
            addFirst(value);
            return;
        }
        last.next=new Node(value,null);
    }
  /* public void test(){
        int i=0;
        for(Node p=head;p!=null;p=p.next,i++){
            System.out.println(p.value+"索引是:"+i);
        }
        根据索引查找
Params:index-索引
Returns:找到,返回该索引位置节点的值
Throws:IlLegalArgumentException-找不到,抛出index非法异常
   }*/

    private Node findNode(int index){//给定索引位置
        int i=0;
        for(Node p=head;p!=null;p=p.next,i++){
            if(i==index){
                return p;
            }
        }
        return null;//没找到
    }
    public int get(int index) throws IllegalAccessException {
        Node node=findNode(index);
        if(node==null){
            //抛异常
            illegalIndex(index);
        }
        return node.value;
    }
    //异常处理(重点)
    private static void illegalIndex(int index) throws IllegalAccessException {
        throw new IllegalAccessException(
                String.format("index[%d] 不合法%n", index)
        );
    }


    /*
向索引位置插入
 */
    public void insert(int index,int value) throws IllegalAccessException {
        if(index==0){
            addFirst(value);
            return;
        }
        Node prev=findNode(index-1);//找到上一个节点
        if(prev==null){
            illegalIndex(index);
        }
        prev.next=new Node(value,prev.next);

    }
//1.问题
    //删除头节点
    public void removeFirst() throws IllegalAccessException {
        if(head==null) {
             illegalIndex(0);
        }
        head=head.next;
    }
    public void remove(int index) throws IllegalAccessException {
        if(index==0){
            removeFirst();
            return;
        }
        Node prev=findNode(index-1);//上一个节点
        if(prev==null){
            illegalIndex(index);
        }
        Node removed=prev.next;//被删除的节点
        if(removed==null){
            illegalIndex(index);
        }
        prev.next=removed.next;

    }
}

标签:Node,index,null,value,next,链表,基本操作,数据结构,public
From: https://www.cnblogs.com/3-6-8/p/17542399.html

相关文章

  • 数据结构问题
    编写一个时间复杂度为O(n),空间复杂度为O(1)是什么意思时间复杂度为O(n)表示算法的执行时间与输入规模n成正比,即算法的执行时间随着输入规模的增加而线性增长。空间复杂度为O(1)表示算法所需的额外空间是固定的,与输入规模n无关。这意味着算法使用的空间是常数级别的,不随输入规......
  • 【数据结构与算法】队列算法题
    TS实现队列interfaceIQueue<T>{//入队enqueue(item:T):void;//出队dequeue():T|undefined;//队首peek():T|undefined;//是否为空isEmpty():boolean;//大小size():number;}classArrayQueue<T>implementsIQueue<T>{......
  • 【数据结构与算法】栈相关算法题(长期更新)
    TS实现栈interfaceIStack<T>{push(e:T):void;pop():T|undefined;peek():T;isEmpyt():boolean;size():number;}//implements:实现接口,一个类可以实现多个接口classArrayStack<T>implementsIStack<T>{privatedata:T[]=[];//pri......
  • 图解算法数据结构
    算法复杂度1.算法复杂度旨在输入数据量N的情况下,算法的时间和空间使用情况,体现算法运行使用的时间和空间随数据大小N而增大的速度。 算法复杂度主要可以从时间,空间两个角度评价:时间:假设各操作的运行时间为固定常数,统计算法运行的计算操作的数量,以代表算法运行所需时间......
  • 第一节 线性数据结构 STL
    vector容器迭代器vector<int>v{1,0,0,8,6};for(vector<int>::interatorit=v.begin();it!=v.end();it++)cout<<*it<<"";函数push_back(x);//添加元素pop_back();//删除尾元素size();//查询vector长度insert(i......
  • 防火墙基本操作
    防火墙对centos6和7版本操作不同所以第一步要看操作系统的版本1、查看操作系统的版本cat/etc/redhat-release  2、 6版本的相关操作iptables防火墙启动、重启、停止serviceiptablesstartserviceiptablesrestartserviceiptablesstopiptables防火墙开机启......
  • 【技术积累】数据结构中栈与队列及其相关算法【一】
    什么是栈栈是一种特殊的数据结构,它的各个元素按照一定的次序排列,且只能在表的一端(称为栈顶)进行添加和删除数据,这种数据结构遵循后进先出(LIFO)的原则。栈可以简单地理解为一种容器,它在使用时非常方便,因为只需在顶部压入(push)或弹出(pop)元素即可。栈可以直接使用数组或链表等数据结构......
  • LeetCode 206. 反转链表
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr){}*ListNode(intx):val(x),next(nullptr){}*ListNode(intx,ListNode*next):val(x),next(next......
  • 链表补充题目
    packageSecondBrush.LinkedList;/***83.删除排序链表中的重复元素*给定一个已排序的链表的头head,删除所有重复的元素,使每个元素只出现一次。返回已排序的链表。**//***这个题目自己就直接做出来了,只是循环条件少写了一个cur.next!=null*首先这道题目......
  • C语言:数据结构之单链表(二)
    上一篇随笔谈了谈单链表是什么东西,然后进行了初始化,这篇随笔就开始对其进行操作了,首先是增,删,改,查的增。增,顾名思义就是要增加新的元素,单链表是链式的,那就要考虑怎么去加新元素,有三种,从头部添加,从尾部添加,从中间添加。先说说从尾部添加,这个比较好理解,直接在尾部放一个结点......