数据结构-链表带哨兵

这篇具有很好参考价值的文章主要介绍了数据结构-链表带哨兵。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一.链表带哨兵

import java.util.Iterator;
import java.util.function.Consumer;
//带哨兵
public class shuju02 implements Iterable<Integer> {//整体
    private Node head=new Node(666,null);//头指针

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

​        @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) throws IllegalAccessException {
​        //1.链表为空
​        // head=new Node(value,null);
​        //2.链表非空(头插)
​       /* head = new Node(value, head);*/
​        insert(0,value);
​    }

​    //遍历链表
​    //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);
​        }
​    }
​    //遍历链表3(递归遍历)
​    //Params:consumer-要执行的操作
​    public void loop3(Consumer<Integer>before,//没有哨兵
​                      Consumer<Integer>after){
​        recursion(head,before,after);
​    }
​    private void recursion(Node curr,//当前节点
​                           Consumer<Integer>before,Consumer<Integer>after){//某个节点要进行的操作
​        if(curr==null){
​            return;
​        }
​        before.accept(curr.value);
​        recursion(curr.next,before,after);//放前边倒叙,放后面顺序->指这句话
​        after.accept(curr.value);
​    }

​    private Node findLast(){
​        Node p;
​        for(p=head;p.next!=null;p=p.next){

​        }
​        return p;
​    }
​    public void addLast(int value){
​        Node last=findLast();
​        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=-1;
​        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 {
​        Node prev=findNode(index-1);//找到上一个节点
​        if(prev==null){
​            illegalIndex(index);
​        }
​        prev.next=new Node(value,prev.next);

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

​    }
}


二.双向链表带哨兵

import java.util.Iterator;

//双向链表,带哨兵
public class shuju03 implements Iterable<Integer>{
static class Node{
    Node prev;//上一个节点指针
    int value;
    Node next;//下一个节点指针

    public Node(Node prev, int value, Node next) {
        this.prev = prev;
        this.value = value;
        this.next = next;
    }
}
private Node head;//头哨兵
private Node tail;//尾哨兵

    public shuju03(){
        head=new Node(null,666,null);
        tail=new Node(null,666,null);
       head.next=tail;
       tail.prev=head;
    }

    private Node findNode(int index){
        int i=-1;
        for(Node p=head;p!=tail;p=p.next,i++){
            if(i==index){
                return p;
            }
        }
        return null;
    }

    public void addFirst(int value) throws IllegalAccessException {
        insert(0,value);
    }
    public void removeLast() throws IllegalAccessException {
        Node removed=tail.prev;
        if(removed==head){
            illegalIndex(0);
        }
        Node prev=removed.prev;
        prev.next=tail;
        tail.prev=prev;
    }


    public void addLast(int value){
     Node last=tail.prev;
     Node added=new Node(last,value,tail);
     last.next=added;
     tail.prev=added;
    }

    public void insert(int index,int value) throws IllegalAccessException {
        Node prev=findNode(index-1);
        if(prev==null){
           illegalIndex(index);
        }
        Node next=prev.next;
        Node inserted=new Node(prev,value,next);
        prev.next=inserted;
        next.prev=inserted;
    }

    public void remove(int index) throws IllegalAccessException {
        Node prev=findNode(index-1);
        if(prev==null){
            illegalIndex(index);
        }
        Node removed=prev.next;
        if(removed==tail){
            illegalIndex(index);
        }
        Node next=removed.next;

       prev.next=next;
       next.prev=prev;

    }
    private static void illegalIndex(int index) throws IllegalAccessException {
        throw new IllegalAccessException(
                String.format("index[%d] 不合法%n", index)
        );
    }
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p=head.next;
            @Override
            public boolean hasNext() {
                return p!=tail;
            }

            @Override
            public Integer next() {
                int value=p.value;
                return value;
            }
        };
    }
}

三.双向链表

import java.util.Iterator;

public class shuju04 implements Iterable<Integer> {
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p=sentinel.next;
            @Override
            public boolean hasNext() {
                return p!=sentinel;
            }

            @Override
            public Integer next() {
                int value= p.value;
                p=p.next;
                return value;
            }
        };
    }

    /*
           s->1->2->3->1->s
             */
    private static class Node{
        Node prev;
        int value;
        Node next;

            public Node(Node prev, int value, Node next) {
                this.prev = prev;
                this.value = value;
                this.next = next;
            }
        }
        private Node sentinel=new Node(null,-1,null);

        public shuju04(){
            sentinel.prev=sentinel;
            sentinel.next=sentinel;
        }
        //添加到第一个
    //Params value-待添加值
    public void addFirst(int value){
      Node a=sentinel;
      Node b=sentinel.next;
      Node added=new Node(a,value,b);
      a.next=added;
      b.prev=added;
    }
    //添加到最后一个
    //Params:value-待添加值
    public void addLast(int value){
           Node a=sentinel.prev;
           Node b=sentinel;
           Node added=new Node(a,value,b);
           a.next=added;
           b.prev=added;
    }
    //删除第一个
    public void removeFirst() {
            Node removed=sentinel.next;
            if(removed==sentinel){
                throw new IllegalArgumentException("非法");
            }
            Node a=sentinel;
            Node b=removed.next;
            a.next=b;
            b.prev=a;
    }
    //删除最后一个
    public void removeLast(){
            Node removed=sentinel.prev;
            if(removed==sentinel){
                throw  new IllegalArgumentException("非法");
            }
            Node a=removed.prev;
            Node b=sentinel;

            a.next=b;
            b.prev=a;
    }
  //根据值删除
   // Params:value-目标值
    public void removeByValue(int value){
      Node removed=findByValue(value);
      if(removed==null){
          return;//不用删
      }
      Node a=removed.prev;
      Node b=removed.next;
      a.next=b;
      b.prev=a;
    }
    private Node findByValue(int value){
           Node p=sentinel.next;
           while(p!=sentinel){
               if(p.value==value){
                   return p;
               }
               p=p.next;
           }
           return null;
    }



}

文章来源地址https://www.toymoban.com/news/detail-551150.html

到了这里,关于数据结构-链表带哨兵的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 链表的总体涵盖以及无哨兵位单链表实现——【数据结构】

     😊W…Y:个人主页 在学习之前看一下美丽的夕阳,也是很不错的。 如果觉得博主的美景不错,博客也不错的话,关注一下博主吧💕 在上一期中,我们说完了顺序表,并且提出顺序表中的问题 1. 中间/头部的插入删除,时间复杂度为O(N) 2. 增容需要申请新空间,拷贝数据,释

    2024年02月14日
    浏览(35)
  • Tecplot数据结构——结构数据(结构网格)与非结构数据(非结构网格)

    结构数据可以是一维、二维或三维的,下面以二维的数据格式为例。 在记事本中写入以下字符,并将文件以.plt或.dat为后缀命名。 其中数据总数为I*J=20,结构数据顺序为point格式,顺序为:(I,J)=(1,1), (I,J)=(2,1), … (I,J)=(Imax,1), (I,J)=(1,2), (I,J)=(2,2), (I,J)=(Imax,2), … (I,J)=(Imax,Jmax).

    2024年02月15日
    浏览(43)
  • 数据结构与算法——数据结构有哪些,常用数据结构详解

    数据结构是学习数据存储方式的一门学科,那么,数据存储方式有哪几种呢?下面将对数据结构的学习内容做一个简要的总结。 数据结构大致包含以下几种存储结构: 线性表,还可细分为顺序表、链表、栈和队列; 树结构,包括普通树,二叉树,线索二叉树等; 图存储结构

    2024年02月15日
    浏览(45)
  • 算法 数据结构分类 数据结构类型介绍 数据结构线性非线性结构 算法合集 (一)

     数据结构分为:                            a.线性结构                            b.非线性结构  a.线性结构:                       数据与结构存在一对一的线性关系; a . 线性结构 存储 分为:                                   顺序存储

    2024年02月10日
    浏览(36)
  • 结构化数据、非结构化数据、半结构化数据

    结构化的数据一般是指可以使用关系型数据库表示和存储,可以用二维表来逻辑表达实现的数据。例如:需要多少个属性,每个属性什么类型,每个属性的取值范围等等,类似下图所示, 提前定义好了一个二维矩阵的元数据 ,包含有列名称、列的类型、列的约束等:   可见

    2024年02月09日
    浏览(44)
  • 【数据结构】何为数据结构。

    🚩 WRITE IN FRONT 🚩    🔎 介绍:\\\"謓泽\\\"正在路上朝着\\\"攻城狮\\\"方向\\\"前进四\\\" 🔎 🏅 荣誉:2021|2022年度博客之星物联网与嵌入式开发TOP5|TOP4、2021|2022博客之星TOP100|TOP63、阿里云专家博主、掘金优秀创作者、全网粉丝量6w+、全网访问量100w+ 🏅 🆔 文章内容由 謓泽 原创 如需相关

    2024年02月08日
    浏览(50)
  • 【数据结构】什么是数据结构?

    🦄 个人主页 :修修修也 🎏 所属专栏 :数据结构 ⚙️ 操作环境 : Visual Studio 2022 目录 🎏数据结构的定义 🎏结语 数据结构(Data Structure)是计算机 存储 , 组织数据的方式 ,指 相互之间存在一种或多种特定关系的数据元素的集合 . 这么讲可能有些抽象,放一张图大家可能好理解一

    2024年02月07日
    浏览(36)
  • 【数据结构(一)】初识数据结构

    ❣博主主页: 33的博客❣ ▶文章专栏分类: Java从入门到精通◀ 🚚我的代码仓库: 33的代码仓库🚚 🫵🫵🫵 关注我带你学更多数据结构知识 数据结构是计算机存储,组织数据的方式,指相互之间存在一种或多种特定关系的数据元素的集合,从这篇文章开始,我们将一起进入数

    2024年04月09日
    浏览(47)
  • 数据结构和算法——数据结构

    目录 线性结构  队列结构的队列 链表结构的队列 链表的面试题 单向链表应用场景 约瑟夫环问题 栈结构 中缀表达式 前缀表达式 后缀表达式 非线性结构 图 递归解决迷宫问题 递归解决八皇后问题 顺序存储方式,顺序表 常见的顺序存储结构有:数组、队列、链表、栈 链式存

    2024年02月07日
    浏览(40)
  • 【数据结构】数据结构中的栈

    该篇文章来了解数据结构中的 栈 ,栈与队列都为一种线性存储结构,同时栈与队列在逻辑结构上,都只能在头或者尾进行对数据的操作; 栈是一种 LIFO(Last in,First out)的结构 ,翻译过来即是 后进先出的一种结构 ;栈无论是出数据还是入数据都 只能从栈顶位置按顺序进行

    2024年02月05日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包