主頁 >  其他 > 資料結構-鏈表帶哨兵

資料結構-鏈表帶哨兵

2023-07-13 08:22:38 其他

一.鏈表帶哨兵

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 = https://www.cnblogs.com/cgy-chen/archive/2023/07/12/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 consumer) {
?        Node p = head;
?        while (p != null) {
?            consumer.accept(p.value);
?            p = p.next;
?        }
?    }
?    //遍歷鏈表2
?    //Params:consumer-要執行的操作
?    public void loop2(Consumer consumer) {
?        for (Node p = head; p != null; p = p.next){
?            consumer.accept(p.value);
?        }
?    }
?    //遍歷鏈表3(遞回遍歷)
?    //Params:consumer-要執行的操作
?    public void loop3(Consumerbefore,//沒有哨兵
?                      Consumerafter){
?        recursion(head,before,after);
?    }
?    private void recursion(Node curr,//當前節點
?                           Consumerbefore,Consumerafter){//某個節點要進行的操作
?        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 = https://www.cnblogs.com/cgy-chen/archive/2023/07/12/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=https://www.cnblogs.com/cgy-chen/archive/2023/07/12/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= https://www.cnblogs.com/cgy-chen/archive/2023/07/12/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=https://www.cnblogs.com/cgy-chen/archive/2023/07/12/=value){
                   return p;
               }
               p=p.next;
           }
           return null;
    }



}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/557143.html

標籤:其他

上一篇:量子糾纏:超越時空的連接

下一篇:返回列表

標籤雲
其他(162483) Python(38274) JavaScript(25531) Java(18294) C(15241) 區塊鏈(8275) C#(7972) AI(7469) 爪哇(7425) MySQL(7296) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5876) 数组(5741) R(5409) Linux(5347) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4616) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2439) ASP.NET(2404) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) HtmlCss(1998) .NET技术(1987) 功能(1967) Web開發(1951) C++(1942) python-3.x(1918) 弹簧靴(1913) xml(1889) PostgreSQL(1883) .NETCore(1863) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 資料結構-鏈表帶哨兵

    ## 一.鏈表帶哨兵 ```java import java.util.Iterator; import java.util.function.Consumer; //帶哨兵 public class shuju02 implements Iterable {//整體 private Node he ......

    uj5u.com 2023-07-13 08:22:38 more
  • 量子糾纏:超越時空的連接

    量子糾纏是一種特殊的量子態,它涉及到兩個或多個量子系統之間的緊密聯系。當這些系統處于糾纏態時,它們之間的狀態無法獨立地描述,即使它們被物理上分離開來。量子糾纏是量子力學中的非局域現象,可以超越時空的距離,為我們提供了一種超越經典物理的聯系方式。 ......

    uj5u.com 2023-07-13 08:22:31 more
  • 后端性能測驗的型別

    ## 性能測驗的型別 性能測驗:確定軟體產品性能的測驗。 ![image](https://img2023.cnblogs.com/blog/3174021/202307/3174021-20230712162602710-1541606934.png) ### 負載測驗(load testing) ......

    uj5u.com 2023-07-13 08:22:16 more
  • SRS之StateThreads學習

    最近在看SRS的原始碼。SRS是基于協程開發的,底層使用了StateThreads。所以為了充分的理解SRS原始碼,需要先學習一下StateThreads。這里對StateThreads的學習做了一些總結和記錄。 ......

    uj5u.com 2023-07-13 08:22:05 more
  • LEA: Improving Sentence Similarity Robustness to Typos Using

    # LEA: Improving Sentence Similarity Robustness to Typos Using Lexical Attention Bias 論文閱讀 KDD 2023 [原文地址](https://arxiv.org/abs/2307.02912) ## Introd ......

    uj5u.com 2023-07-13 08:21:46 more
  • python實作兩函式通過縮放,平移和旋轉進行完美擬合

    # Curve _fitting 前幾天在作業的時候接到了一個需求,希望將不同坐標系,不同角度的兩條不規則曲線,并且組成該曲線的點集數量不一致,需求是希望那個可以通過演算法的平移和旋轉搞到一個概念里最貼合,擬合態進行比較。 ![image-20230712151728578](https://img2 ......

    uj5u.com 2023-07-13 08:21:22 more
  • 淺析華為云Astro的5大關鍵能力技術

    摘要:本文以技術方案視角,對華為云Astro低代碼平臺的一些核心功能進行簡要介紹。 背景介紹 低代碼開發基于可視化開發的概念,結合了云原生和多終端體驗技術,它可以在大多數業務場景中,幫助企業顯著的提升效率。同時為專業開發者提供了一種全新的高生產力開發方式,讓不懂代碼的人通過“拖拉拽”開發組件來完成應 ......

    uj5u.com 2023-07-13 08:21:11 more
  • 重塑未來的1課:組裝式交付新引擎——智能化低代碼平臺

    摘要:智能化低代碼必修課。 緊跟低代碼技術飛速發展——華為云Astro智能作業流驚艷HDC.Cloud 2023!企業對未來智能化組裝式交付的期待已不是空想。智能化低代碼即將重新定義傳統交付模式,密切連接AI科技與創造力。 在HDC.Cloud 2023華為云Astro分論壇,云計算大咖、行業翹楚科 ......

    uj5u.com 2023-07-13 08:20:43 more
  • 盤古大模型加持,華為云開天aPaaS加速使能千行百業應用創新

    摘要:開天aPaaS,讓優秀快速復制,支撐開發者及伙伴上好云、用好云。 本文分享自華為云社區《盤古大模型加持,華為云開天aPaaS加速使能千行百業應用創新》,作者:開天aPaaS小助手。 7月7-9日,華為開發者大會(Cloud)2023在東莞隆重召開。此次大會,華為云開天aPaaS帶來了主題演講、 ......

    uj5u.com 2023-07-13 08:19:49 more
  • Navicat Premium v16.0.6 綠色破解版

    這里版本:Navicat Premium v16.0.6.0 ,這個是綠色版,不需要安裝,啟動Navicat.exe即可用 破解工具:NavicatKeygenPatch(其它版本也能破解) 1、下載安裝檔案 鏈接:https://pan.baidu.com/s/1_9XLoqulp2EyI2H0G ......

    uj5u.com 2023-07-13 08:18:26 more