mirror of
https://github.com/JasonsGong/JasonsGong.github.io.git
synced 2024-11-24 09:09:35 +08:00
558 lines
527 KiB
HTML
558 lines
527 KiB
HTML
<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>数据结构与算法 | The Blog</title><meta name="author" content="Jason"><meta name="copyright" content="Jason"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="代码仓库的地址:https://gitee.com/JasonsGong/DataStructures 一.经典算法问题字符串匹配 KMP算法 汉诺塔问题 分治算法 八皇后问题 回溯算法 马踏棋盘问题 图的深度优化遍历算法(DFS)和 贪心算法优化 二.数据结构与算法的概述2.1 数据结构与算法的关系(1)数据(data)结构(structure)是一门研究组织数">
|
||
<meta property="og:type" content="article">
|
||
<meta property="og:title" content="数据结构与算法">
|
||
<meta property="og:url" content="https://qingling.icu/posts/40445.html">
|
||
<meta property="og:site_name" content="The Blog">
|
||
<meta property="og:description" content="代码仓库的地址:https://gitee.com/JasonsGong/DataStructures 一.经典算法问题字符串匹配 KMP算法 汉诺塔问题 分治算法 八皇后问题 回溯算法 马踏棋盘问题 图的深度优化遍历算法(DFS)和 贪心算法优化 二.数据结构与算法的概述2.1 数据结构与算法的关系(1)数据(data)结构(structure)是一门研究组织数">
|
||
<meta property="og:locale" content="zh_CN">
|
||
<meta property="og:image" content="https://qingling.icu/img/4.png">
|
||
<meta property="article:published_time" content="2023-03-13T03:31:58.000Z">
|
||
<meta property="article:modified_time" content="2024-04-02T02:52:59.937Z">
|
||
<meta property="article:author" content="Jason">
|
||
<meta property="article:tag" content="数据结构与算法">
|
||
<meta name="twitter:card" content="summary">
|
||
<meta name="twitter:image" content="https://qingling.icu/img/4.png"><link rel="shortcut icon" href="/img/%E5%9B%BE%E6%A0%87.png"><link rel="canonical" href="https://qingling.icu/posts/40445.html"><link rel="preconnect" href="//fastly.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="/cdn/icon/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="/cdn/css/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="/cdn/css/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
|
||
root: '/',
|
||
algolia: undefined,
|
||
localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容:${query}","hits_stats":"共找到 ${hits} 篇文章"}},
|
||
translate: undefined,
|
||
noticeOutdate: undefined,
|
||
highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":400},
|
||
copy: {
|
||
success: '复制成功',
|
||
error: '复制错误',
|
||
noSupport: '浏览器不支持'
|
||
},
|
||
relativeDate: {
|
||
homepage: true,
|
||
post: true
|
||
},
|
||
runtime: '天',
|
||
dateSuffix: {
|
||
just: '刚刚',
|
||
min: '分钟前',
|
||
hour: '小时前',
|
||
day: '天前',
|
||
month: '个月前'
|
||
},
|
||
copyright: undefined,
|
||
lightbox: 'mediumZoom',
|
||
Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#006650","bgDark":"#006650","position":"top-center"},
|
||
source: {
|
||
justifiedGallery: {
|
||
js: 'https://fastly.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
|
||
css: 'https://fastly.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
|
||
}
|
||
},
|
||
isPhotoFigcaption: false,
|
||
islazyload: false,
|
||
isAnchor: true,
|
||
percent: {
|
||
toc: true,
|
||
rightside: false,
|
||
},
|
||
autoDarkmode: true
|
||
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
|
||
title: '数据结构与算法',
|
||
isPost: true,
|
||
isHome: false,
|
||
isHighlightShrink: false,
|
||
isToc: true,
|
||
postUpdate: '2024-04-02 10:52:59'
|
||
}</script><noscript><style type="text/css">
|
||
#nav {
|
||
opacity: 1
|
||
}
|
||
.justified-gallery img {
|
||
opacity: 1
|
||
}
|
||
|
||
#recent-posts time,
|
||
#post-meta time {
|
||
display: inline !important
|
||
}
|
||
</style></noscript><script>(win=>{
|
||
win.saveToLocal = {
|
||
set: function setWithExpiry(key, value, ttl) {
|
||
if (ttl === 0) return
|
||
const now = new Date()
|
||
const expiryDay = ttl * 86400000
|
||
const item = {
|
||
value: value,
|
||
expiry: now.getTime() + expiryDay,
|
||
}
|
||
localStorage.setItem(key, JSON.stringify(item))
|
||
},
|
||
|
||
get: function getWithExpiry(key) {
|
||
const itemStr = localStorage.getItem(key)
|
||
|
||
if (!itemStr) {
|
||
return undefined
|
||
}
|
||
const item = JSON.parse(itemStr)
|
||
const now = new Date()
|
||
|
||
if (now.getTime() > item.expiry) {
|
||
localStorage.removeItem(key)
|
||
return undefined
|
||
}
|
||
return item.value
|
||
}
|
||
}
|
||
|
||
win.getScript = url => new Promise((resolve, reject) => {
|
||
const script = document.createElement('script')
|
||
script.src = url
|
||
script.async = true
|
||
script.onerror = reject
|
||
script.onload = script.onreadystatechange = function() {
|
||
const loadState = this.readyState
|
||
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
|
||
script.onload = script.onreadystatechange = null
|
||
resolve()
|
||
}
|
||
document.head.appendChild(script)
|
||
})
|
||
|
||
win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
|
||
const link = document.createElement('link')
|
||
link.rel = 'stylesheet'
|
||
link.href = url
|
||
if (id) link.id = id
|
||
link.onerror = reject
|
||
link.onload = link.onreadystatechange = function() {
|
||
const loadState = this.readyState
|
||
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
|
||
link.onload = link.onreadystatechange = null
|
||
resolve()
|
||
}
|
||
document.head.appendChild(link)
|
||
})
|
||
|
||
win.activateDarkMode = function () {
|
||
document.documentElement.setAttribute('data-theme', 'dark')
|
||
if (document.querySelector('meta[name="theme-color"]') !== null) {
|
||
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
|
||
}
|
||
}
|
||
win.activateLightMode = function () {
|
||
document.documentElement.setAttribute('data-theme', 'light')
|
||
if (document.querySelector('meta[name="theme-color"]') !== null) {
|
||
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
|
||
}
|
||
}
|
||
const t = saveToLocal.get('theme')
|
||
|
||
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
|
||
const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
|
||
const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
|
||
const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
|
||
|
||
if (t === undefined) {
|
||
if (isLightMode) activateLightMode()
|
||
else if (isDarkMode) activateDarkMode()
|
||
else if (isNotSpecified || hasNoSupport) {
|
||
const now = new Date()
|
||
const hour = now.getHours()
|
||
const isNight = hour <= 8 || hour >= 22
|
||
isNight ? activateDarkMode() : activateLightMode()
|
||
}
|
||
window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
|
||
if (saveToLocal.get('theme') === undefined) {
|
||
e.matches ? activateDarkMode() : activateLightMode()
|
||
}
|
||
})
|
||
} else if (t === 'light') activateLightMode()
|
||
else activateDarkMode()
|
||
|
||
const asideStatus = saveToLocal.get('aside-status')
|
||
if (asideStatus !== undefined) {
|
||
if (asideStatus === 'hide') {
|
||
document.documentElement.classList.add('hide-aside')
|
||
} else {
|
||
document.documentElement.classList.remove('hide-aside')
|
||
}
|
||
}
|
||
|
||
const detectApple = () => {
|
||
if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
|
||
document.documentElement.classList.add('apple')
|
||
}
|
||
}
|
||
detectApple()
|
||
})(window)</script><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script type="text/javascript" src ="/js/welcome.js" ></script><script src="/js/sweetalert.js"></script><link rel="stylesheet" href="/css/sweetalert.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiperstyle.css" media="print" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/loading.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">60</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">39</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><br/><div class="menus_items"><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://www.tutorialspoint.com/compile_java8_online.php"><i class="fa-fw fas fa-code"></i><span> 代码</span></a></div><div class="menus_item"><a class="site-page" href="/notice/"><i class="fa-fw fas fa-stream"></i><span> 公告</span></a></div><div class="menus_item"><a class="site-page" href="/website/"><i class="fa-fw fas fa-list"></i><span> 网址</span></a></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="The Blog"><img class="site-icon" src="/img/logo.png"/><span class="site-name">The Blog</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://www.tutorialspoint.com/compile_java8_online.php"><i class="fa-fw fas fa-code"></i><span> 代码</span></a></div><div class="menus_item"><a class="site-page" href="/notice/"><i class="fa-fw fas fa-stream"></i><span> 公告</span></a></div><div class="menus_item"><a class="site-page" href="/website/"><i class="fa-fw fas fa-list"></i><span> 网址</span></a></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">数据结构与算法</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-03-13T03:31:58.000Z" title="发表于 2023-03-13 11:31:58">2023-03-13</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-04-02T02:52:59.937Z" title="更新于 2024-04-02 10:52:59">2024-04-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">23.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>98分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="数据结构与算法"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><p>代码仓库的地址:<a target="_blank" rel="noopener" href="https://gitee.com/JasonsGong/DataStructures">https://gitee.com/JasonsGong/DataStructures</a></p>
|
||
<h2 id="一-经典算法问题"><a href="#一-经典算法问题" class="headerlink" title="一.经典算法问题"></a>一.经典算法问题</h2><p>字符串匹配 KMP算法 </p>
|
||
<p>汉诺塔问题 分治算法</p>
|
||
<p>八皇后问题 回溯算法 </p>
|
||
<p>马踏棋盘问题 图的深度优化遍历算法(DFS)和 贪心算法优化</p>
|
||
<h2 id="二-数据结构与算法的概述"><a href="#二-数据结构与算法的概述" class="headerlink" title="二.数据结构与算法的概述"></a>二.数据结构与算法的概述</h2><h3 id="2-1-数据结构与算法的关系"><a href="#2-1-数据结构与算法的关系" class="headerlink" title="2.1 数据结构与算法的关系"></a>2.1 数据结构与算法的关系</h3><p>(1)数据(data)结构(structure)是一门研究组织数据方式的学科,有了编程语言也就有了数据结构,学好了数据结构可以编写出更加漂亮,更加有效率的代码。</p>
|
||
<p>(2)程序=数据结构+算法</p>
|
||
<p>(3)数据结构是算法的基础</p>
|
||
<h3 id="2-2解决实际的问题"><a href="#2-2解决实际的问题" class="headerlink" title="2.2解决实际的问题"></a>2.2解决实际的问题</h3><p>五子棋程序 稀疏数组(压缩存档) 二维数组->转化成稀疏数组->存档 读档反之</p>
|
||
<p>约瑟夫问题(丢手帕问题) 单向环形列表</p>
|
||
<p>修路问题 求最小生成树 + 普利姆算法</p>
|
||
<p>最短路径问题 图+弗洛伊德算法</p>
|
||
<h3 id="2-3-数据结构"><a href="#2-3-数据结构" class="headerlink" title="2.3 数据结构"></a>2.3 数据结构</h3><p>线性结构:</p>
|
||
<p> 线性结构作为最常用的数据结构,其特点是数据元素之间存在一对一的线性关系</p>
|
||
<p> 线性结构有两种不同的存储结构,即顺序存储结构和链式存储结构。顺序存储的线性表称为顺序表,顺序表中的存储元素是连续的</p>
|
||
<p> 链式存储的线性表称为链表,链表中的存储元素不一定是连续的,元素节点中存放数据元素以及相邻元素的地址信息</p>
|
||
<p> 线性结构常见的有:数组、队列、链表和栈</p>
|
||
<p>非线性结构:</p>
|
||
<p> 二维数组,多维数组,广义表,树结构,图结构</p>
|
||
<h2 id="三-稀疏数组和队列"><a href="#三-稀疏数组和队列" class="headerlink" title="三.稀疏数组和队列"></a>三.稀疏数组和队列</h2><h3 id="3-1稀疏数组"><a href="#3-1稀疏数组" class="headerlink" title="3.1稀疏数组"></a>3.1稀疏数组</h3><h4 id="3-1-1-基本的介绍"><a href="#3-1-1-基本的介绍" class="headerlink" title="3.1.1 基本的介绍"></a>3.1.1 基本的介绍</h4><p> 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。</p>
|
||
<p>稀疏数组的处理方法是:</p>
|
||
<p> 1)记录数组一共有几行几列,有多少个不同的值</p>
|
||
<p> 2)把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模</p>
|
||
<h4 id="3-1-2使用的场景"><a href="#3-1-2使用的场景" class="headerlink" title="3.1.2使用的场景"></a>3.1.2使用的场景</h4><p> 五子棋程序的存档和退出功能的实现</p>
|
||
<h4 id="3-1-3图解"><a href="#3-1-3图解" class="headerlink" title="3.1.3图解"></a>3.1.3图解</h4><p>第一行记录的是原始的数组有几行几列有几个非零的值 例如下面的数组是一个6行7列有8个非零值的数组</p>
|
||
<p><img src="/pictures/image-20230313124552408.png" alt="image-20230313124552408"></p>
|
||
<h4 id="3-1-5实现的思路"><a href="#3-1-5实现的思路" class="headerlink" title="3.1.5实现的思路"></a>3.1.5实现的思路</h4><p><img src="/pictures/image-20230318100138197.png" alt="image-20230318100138197"></p>
|
||
<h4 id="3-1-5代码实现"><a href="#3-1-5代码实现" class="headerlink" title="3.1.5代码实现"></a>3.1.5代码实现</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.sparsearray;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/3/18</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 稀疏数组的代码实现 稀疏数组和二维数组之间的互转</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SparseArray</span> {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//创建一个原始的二维数组</span></span><br><span class="line"> <span class="comment">//0:表示没有棋子 1:表示黑子 2:表示白子</span></span><br><span class="line"> <span class="type">int</span>[][] chessArr1 = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">11</span>][<span class="number">11</span>];</span><br><span class="line"> chessArr1[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line"> chessArr1[<span class="number">2</span>][<span class="number">3</span>] = <span class="number">2</span>;</span><br><span class="line"> chessArr1[<span class="number">4</span>][<span class="number">5</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="comment">//遍历输出</span></span><br><span class="line"> System.out.println(<span class="string">"原始的二维数组"</span>);</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span>[] row : chessArr1) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> num : row) {</span><br><span class="line"> System.out.print(num + <span class="string">" "</span>);</span><br><span class="line"> }</span><br><span class="line"> System.out.println();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">//将二维数组转化成稀疏数组</span></span><br><span class="line"> <span class="comment">//获取原始二维数组中非零元素的个数</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">sum</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span>[] row : chessArr1) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> num : row) {</span><br><span class="line"> <span class="keyword">if</span> (num != <span class="number">0</span>) {</span><br><span class="line"> sum++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"非零元素的个数:"</span> + sum);</span><br><span class="line"> <span class="comment">//根据非零的元素的个数创建对应的稀疏数组</span></span><br><span class="line"> <span class="type">int</span>[][] sparseArr2 = <span class="keyword">new</span> <span class="title class_">int</span>[sum + <span class="number">1</span>][<span class="number">3</span>];</span><br><span class="line"> <span class="comment">//给稀疏数组赋值</span></span><br><span class="line"> <span class="comment">//第一行的赋值操作</span></span><br><span class="line"> sparseArr2[<span class="number">0</span>][<span class="number">0</span>] = <span class="number">11</span>;</span><br><span class="line"> sparseArr2[<span class="number">0</span>][<span class="number">1</span>] = <span class="number">11</span>;</span><br><span class="line"> sparseArr2[<span class="number">0</span>][<span class="number">2</span>] = sum;</span><br><span class="line"> <span class="comment">//其余行的赋值操作</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">row</span> <span class="operator">=</span> <span class="number">1</span>; <span class="comment">//充当计数器的作用</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < chessArr1.length; i++) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j < chessArr1[i].length; j++) {</span><br><span class="line"> <span class="keyword">if</span> (chessArr1[i][j] != <span class="number">0</span>) {</span><br><span class="line"> sparseArr2[row][<span class="number">0</span>] = i;</span><br><span class="line"> sparseArr2[row][<span class="number">1</span>] = j;</span><br><span class="line"> sparseArr2[row][<span class="number">2</span>] = chessArr1[i][j];</span><br><span class="line"> row++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">//遍历稀疏数组</span></span><br><span class="line"> System.out.println(<span class="string">"得到的稀疏数组如下"</span>);</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span>[] ints : sparseArr2) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> anInt : ints) {</span><br><span class="line"> System.out.print(anInt + <span class="string">" "</span>);</span><br><span class="line"> }</span><br><span class="line"> System.out.println();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">//将稀疏数组转化成二维数组</span></span><br><span class="line"> <span class="comment">//获取原先的二维数组的大小</span></span><br><span class="line"> <span class="comment">//读取第一行 获取原始二维数组的行列值</span></span><br><span class="line"> <span class="type">int</span>[][] sparseArr3 = <span class="keyword">new</span> <span class="title class_">int</span>[sparseArr2[<span class="number">0</span>][<span class="number">0</span>]][sparseArr2[<span class="number">0</span>][<span class="number">1</span>]];</span><br><span class="line"> <span class="comment">//遍历稀疏数组第二行之后的值 赋值给原始的数组 从第二行开始算</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i < sparseArr2.length; i++) {</span><br><span class="line"> sparseArr3[sparseArr2[i][<span class="number">0</span>]][sparseArr2[i][<span class="number">1</span>]] = sparseArr2[i][<span class="number">2</span>];</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//遍历还原之后的二维数组</span></span><br><span class="line"> System.out.println(<span class="string">"稀疏数组还原成二维数组"</span>);</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span>[] ints : sparseArr3) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> anInt : ints) {</span><br><span class="line"> System.out.print(anInt+<span class="string">" "</span>);</span><br><span class="line"> }</span><br><span class="line"> System.out.println();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
|
||
|
||
|
||
|
||
<h3 id="3-2队列"><a href="#3-2队列" class="headerlink" title="3.2队列"></a>3.2队列</h3><h4 id="3-2-1基本的介绍"><a href="#3-2-1基本的介绍" class="headerlink" title="3.2.1基本的介绍"></a>3.2.1基本的介绍</h4><p>先进先出</p>
|
||
<p> 队列是一个有序列表,可以通过数组或者链表实现。</p>
|
||
<p> 遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入队列的数据要后取出。</p>
|
||
<h4 id="3-2-2代码实现"><a href="#3-2-2代码实现" class="headerlink" title="3.2.2代码实现"></a>3.2.2代码实现</h4><p> 使用数组模拟队列 </p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.queue;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/3/18</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 使用数组模拟队列</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayQueueDemo</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//测试一把</span></span><br><span class="line"> <span class="comment">//创建一个队列</span></span><br><span class="line"> <span class="type">ArrayQueue</span> <span class="variable">queue</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ArrayQueue</span>(<span class="number">3</span>);</span><br><span class="line"> <span class="type">char</span> <span class="variable">key</span> <span class="operator">=</span> <span class="string">' '</span>; <span class="comment">//接收用户输入</span></span><br><span class="line"> <span class="type">Scanner</span> <span class="variable">scanner</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);<span class="comment">//</span></span><br><span class="line"> <span class="type">boolean</span> <span class="variable">loop</span> <span class="operator">=</span> <span class="literal">true</span>;</span><br><span class="line"> <span class="comment">//输出一个菜单</span></span><br><span class="line"> <span class="keyword">while</span> (loop) {</span><br><span class="line"> System.out.println(<span class="string">"s(show): 显示队列"</span>);</span><br><span class="line"> System.out.println(<span class="string">"e(exit): 退出程序"</span>);</span><br><span class="line"> System.out.println(<span class="string">"a(add): 添加数据到队列"</span>);</span><br><span class="line"> System.out.println(<span class="string">"g(get): 从队列取出数据"</span>);</span><br><span class="line"> System.out.println(<span class="string">"h(head): 查看队列头的数据"</span>);</span><br><span class="line"> key = scanner.next().charAt(<span class="number">0</span>);<span class="comment">//接收一个字符</span></span><br><span class="line"> <span class="keyword">switch</span> (key) {</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'s'</span>:</span><br><span class="line"> queue.showQueue();</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'a'</span>:</span><br><span class="line"> System.out.println(<span class="string">"输出一个数"</span>);</span><br><span class="line"> <span class="type">int</span> <span class="variable">value</span> <span class="operator">=</span> scanner.nextInt();</span><br><span class="line"> queue.addQueue(value);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'g'</span>: <span class="comment">//取出数据</span></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> queue.getQueue();</span><br><span class="line"> System.out.printf(<span class="string">"取出的数据是%d\n"</span>, res);</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> <span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line"> System.out.println(e.getMessage());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'h'</span>: <span class="comment">//查看队列头的数据</span></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> queue.headQueue();</span><br><span class="line"> System.out.printf(<span class="string">"队列头的数据是%d\n"</span>, res);</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> <span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line"> System.out.println(e.getMessage());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'e'</span>: <span class="comment">//退出</span></span><br><span class="line"> scanner.close();</span><br><span class="line"> loop = <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">default</span>:</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> System.out.println(<span class="string">"程序退出~~"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用数组模拟队列-编写一个ArrayQueue类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ArrayQueue</span> {</span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> maxSize; <span class="comment">// 表示数组的最大容量</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> front; <span class="comment">// 队列头</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> rear; <span class="comment">// 队列尾</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span>[] arr; <span class="comment">// 该数据用于存放数据, 模拟队列</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建队列的构造器</span></span><br><span class="line"> <span class="keyword">public</span> <span class="title function_">ArrayQueue</span><span class="params">(<span class="type">int</span> arrMaxSize)</span> {</span><br><span class="line"> maxSize = arrMaxSize;</span><br><span class="line"> arr = <span class="keyword">new</span> <span class="title class_">int</span>[maxSize];</span><br><span class="line"> front = -<span class="number">1</span>; <span class="comment">// 指向队列头部,分析出front是指向队列头的前一个位置.</span></span><br><span class="line"> rear = -<span class="number">1</span>; <span class="comment">// 指向队列尾,指向队列尾的数据(即就是队列最后一个数据)</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 判断队列是否满</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isFull</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> rear == maxSize - <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 判断队列是否为空</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isEmpty</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> rear == front;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 添加数据到队列</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addQueue</span><span class="params">(<span class="type">int</span> n)</span> {</span><br><span class="line"> <span class="comment">// 判断队列是否满</span></span><br><span class="line"> <span class="keyword">if</span> (isFull()) {</span><br><span class="line"> System.out.println(<span class="string">"队列满,不能加入数据~"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> rear++; <span class="comment">// 让rear 后移</span></span><br><span class="line"> arr[rear] = n;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 获取队列的数据, 出队列</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getQueue</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">// 判断队列是否空</span></span><br><span class="line"> <span class="keyword">if</span> (isEmpty()) {</span><br><span class="line"> <span class="comment">// 通过抛出异常</span></span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"队列空,不能取数据"</span>);</span><br><span class="line"> }</span><br><span class="line"> front++; <span class="comment">// front后移</span></span><br><span class="line"> <span class="keyword">return</span> arr[front];</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 显示队列的所有数据</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">showQueue</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">// 遍历</span></span><br><span class="line"> <span class="keyword">if</span> (isEmpty()) {</span><br><span class="line"> System.out.println(<span class="string">"队列空的,没有数据~~"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < arr.length; i++) {</span><br><span class="line"> System.out.printf(<span class="string">"arr[%d]=%d\n"</span>, i, arr[i]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 显示队列的头数据, 注意不是取出数据</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">headQueue</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">// 判断</span></span><br><span class="line"> <span class="keyword">if</span> (isEmpty()) {</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"队列空的,没有数据~~"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> arr[front + <span class="number">1</span>];</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
|
||
|
||
<h4 id="3-2-3-数组模拟环形队列"><a href="#3-2-3-数组模拟环形队列" class="headerlink" title="3.2.3 数组模拟环形队列"></a>3.2.3 数组模拟环形队列</h4><p>环形队列的思路分析</p>
|
||
<ul>
|
||
<li>环形队列满的条件:(rear + 1) % maxSize == front</li>
|
||
<li>环形队列空的条件:rear == front</li>
|
||
<li>环形队列中有效数据的个数: (rear + maxSize - front) % maxSize</li>
|
||
</ul>
|
||
<p><img src="/pictures/image-20230428124659873.png" alt="image-20230428124659873"></p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.queue;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/4/28</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 模拟环形队列</span></span><br><span class="line"><span class="comment"> * <p></span></span><br><span class="line"><span class="comment"> * 环形队列满的条件:(rear + 1) % maxSize == front</span></span><br><span class="line"><span class="comment"> * 环形队列空的条件:rear == front</span></span><br><span class="line"><span class="comment"> * 环形队列中有效数据的个数: (rear + maxSize - front) % maxSize</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CircleArrayQueueDemo</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//创建一个队列</span></span><br><span class="line"> System.out.println(<span class="string">"测试环形队列"</span>);</span><br><span class="line"> <span class="type">CircleArray</span> <span class="variable">queue</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">CircleArray</span>(<span class="number">3</span>);</span><br><span class="line"> <span class="type">char</span> <span class="variable">key</span> <span class="operator">=</span> <span class="string">' '</span>; <span class="comment">//接收用户输入</span></span><br><span class="line"> <span class="type">Scanner</span> <span class="variable">scanner</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);<span class="comment">//</span></span><br><span class="line"> <span class="type">boolean</span> <span class="variable">loop</span> <span class="operator">=</span> <span class="literal">true</span>;</span><br><span class="line"> <span class="comment">//输出一个菜单</span></span><br><span class="line"> <span class="keyword">while</span> (loop) {</span><br><span class="line"> System.out.println(<span class="string">"s(show): 显示队列"</span>);</span><br><span class="line"> System.out.println(<span class="string">"e(exit): 退出程序"</span>);</span><br><span class="line"> System.out.println(<span class="string">"a(add): 添加数据到队列"</span>);</span><br><span class="line"> System.out.println(<span class="string">"g(get): 从队列取出数据"</span>);</span><br><span class="line"> System.out.println(<span class="string">"h(head): 查看队列头的数据"</span>);</span><br><span class="line"> key = scanner.next().charAt(<span class="number">0</span>);<span class="comment">//接收一个字符</span></span><br><span class="line"> <span class="keyword">switch</span> (key) {</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'s'</span>:</span><br><span class="line"> queue.showQueue();</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'a'</span>:<span class="comment">//添加数据</span></span><br><span class="line"> System.out.println(<span class="string">"输出一个数"</span>);</span><br><span class="line"> <span class="type">int</span> <span class="variable">value</span> <span class="operator">=</span> scanner.nextInt();</span><br><span class="line"> queue.addQueue(value);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'g'</span>: <span class="comment">//取出数据</span></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> queue.getQueue();</span><br><span class="line"> System.out.printf(<span class="string">"取出的数据是%d\n"</span>, res);</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> <span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line"> System.out.println(e.getMessage());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'h'</span>: <span class="comment">//查看队列头的数据</span></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> queue.headQueue();</span><br><span class="line"> System.out.printf(<span class="string">"队列头的数据是%d\n"</span>, res);</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> <span class="comment">// <span class="doctag">TODO:</span> handle exception</span></span><br><span class="line"> System.out.println(e.getMessage());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'e'</span>: <span class="comment">//退出</span></span><br><span class="line"> scanner.close();</span><br><span class="line"> loop = <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">default</span>:</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> System.out.println(<span class="string">"程序退出~~"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">CircleArray</span> {</span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> maxSize; <span class="comment">// 表示数组的最大容量 因为预留了一个空间 实际上maxSize=3 只能存储两个元素</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> front; <span class="comment">// 队列头 初始值是0</span></span><br><span class="line"> <span class="comment">//rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> rear; <span class="comment">// 队列尾</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span>[] arr; <span class="comment">// 该数据用于存放数据, 模拟队列</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 构造方法</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> arrMaxSize 环形队列的最大容量</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="title function_">CircleArray</span><span class="params">(<span class="type">int</span> arrMaxSize)</span> {</span><br><span class="line"> maxSize = arrMaxSize;</span><br><span class="line"> arr = <span class="keyword">new</span> <span class="title class_">int</span>[maxSize];</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断队列是否为满</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isFull</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//因为是环形队列 如果尾部队列的后一个指向队列的头部 那么我们认为队列是满的</span></span><br><span class="line"> <span class="keyword">return</span> (rear + <span class="number">1</span>) % maxSize == front; <span class="comment">//这里是一个理解点</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断队列是否为空</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isEmpty</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> rear == front;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 添加数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addQueue</span><span class="params">(<span class="type">int</span> n)</span> {</span><br><span class="line"> <span class="comment">//先判断队列是否为满 满了就不添加</span></span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">this</span>.isFull()) {</span><br><span class="line"> System.out.println(<span class="string">"队列为满,不能添加数据!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//添加数据</span></span><br><span class="line"> arr[rear] = n;</span><br><span class="line"> <span class="comment">//将人rear向后移</span></span><br><span class="line"> rear = (rear + <span class="number">1</span>) % maxSize; <span class="comment">//这里是一个理解点</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 取出数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getQueue</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//先判断队列是否为空 空了就无法取出数据</span></span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">this</span>.isEmpty()) {</span><br><span class="line"> System.out.println(<span class="string">"队列为空,无法取出数据!"</span>);</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"队列空,不能取数据"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//不为空 取出数据</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> arr[front];</span><br><span class="line"> front = (front + <span class="number">1</span>) % maxSize; <span class="comment">//这里是一个理解点</span></span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 显示对垒数据的方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">showQueue</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//判断队列是否为空 如果队列为空就不遍历</span></span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">this</span>.isEmpty()) {</span><br><span class="line"> System.out.println(<span class="string">"队列为空!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//遍历的方式 从front开始遍历 遍历多少个元素</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> <span class="built_in">this</span>.size();</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> front; i < front + count; i++) {<span class="comment">//主要作用是遍历多少次</span></span><br><span class="line"> System.out.println(<span class="string">"arr["</span> + i % maxSize + <span class="string">"]="</span> + arr[i % maxSize]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 求出当前数列中有效数据的个数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">size</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> (rear + maxSize - front) % maxSize; <span class="comment">//这是一个理解点</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 显示队列的头元素</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">headQueue</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">// 判断</span></span><br><span class="line"> <span class="keyword">if</span> (isEmpty()) {</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"队列空的,没有数据~~"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> arr[front];</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
|
||
|
||
|
||
|
||
<h2 id="四-链表"><a href="#四-链表" class="headerlink" title="四.链表"></a>四.链表</h2><h3 id="4-1-链表-Linked-List-介绍"><a href="#4-1-链表-Linked-List-介绍" class="headerlink" title="4.1 链表(Linked List)介绍"></a>4.1 链表(Linked List)介绍</h3><p>在内存中不是连续存储的</p>
|
||
<h3 id=""><a href="#" class="headerlink" title=""></a><img src="/pictures/image-20230428134234636.png"></h3><p>链表的逻辑结构</p>
|
||
<p><img src="/pictures/image-20230428135032883.png" alt="image-20230428135032883"></p>
|
||
<h3 id="4-2单链表的应用实例-CRUD"><a href="#4-2单链表的应用实例-CRUD" class="headerlink" title="4.2单链表的应用实例(CRUD)"></a>4.2单链表的应用实例(CRUD)</h3><p>使用带head头的单向链表实现 –水浒英雄排行榜管理 完成对英雄人物的增删改查操作</p>
|
||
<h4 id="4-2-1向单链表中添加数据"><a href="#4-2-1向单链表中添加数据" class="headerlink" title="4.2.1向单链表中添加数据"></a>4.2.1向单链表中添加数据</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.linkedlist;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/4/28</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 单链表的实现</span></span><br><span class="line"><span class="comment"> * 使用带head头的单向链表实现 –水浒英雄排行榜管理</span></span><br><span class="line"><span class="comment"> * 完成对英雄人物的增删改查操作</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SingleLinkedListDemo</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">SingleLinkedList</span> <span class="variable">singleLinkedList</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SingleLinkedList</span>();</span><br><span class="line"> singleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">1</span>, <span class="string">"宋江"</span>, <span class="string">"及时雨"</span>));</span><br><span class="line"> singleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">2</span>, <span class="string">"卢俊义"</span>, <span class="string">"玉麒麟"</span>));</span><br><span class="line"> singleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">3</span>, <span class="string">"吴用"</span>, <span class="string">"智多星"</span>));</span><br><span class="line"> singleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">4</span>, <span class="string">"林冲"</span>, <span class="string">"豹子头"</span>));</span><br><span class="line"> <span class="comment">//显示链表</span></span><br><span class="line"> singleLinkedList.list();</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义SingleLinkedList来管理我们的英雄任务</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">SingleLinkedList</span> {</span><br><span class="line"> <span class="comment">//初始化一个头节点 不存储具体的数据</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">HeroNode</span> <span class="variable">head</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">0</span>, <span class="string">""</span>, <span class="string">""</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 添加节点到单向链表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">add</span><span class="params">(HeroNode heroNode)</span> {</span><br><span class="line"> <span class="comment">//找到当前链表的最后一个节点</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="keyword">while</span> (temp.next != <span class="literal">null</span>) {</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> temp.next = heroNode;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 显示链表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">list</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//先判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="keyword">while</span> (temp.next != <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(temp.next);</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义一个heroNode,每个heroNode对象都是一个节点</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">HeroNode</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> no;</span><br><span class="line"> <span class="keyword">public</span> String name;</span><br><span class="line"> <span class="keyword">public</span> String nickname;</span><br><span class="line"> <span class="keyword">public</span> HeroNode next; <span class="comment">//指向下一个节点</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//构造器</span></span><br><span class="line"> <span class="keyword">public</span> <span class="title function_">HeroNode</span><span class="params">(<span class="type">int</span> no, String name, String nickname)</span> {</span><br><span class="line"> <span class="built_in">this</span>.no = no;</span><br><span class="line"> <span class="built_in">this</span>.name = name;</span><br><span class="line"> <span class="built_in">this</span>.nickname = nickname;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"HeroNode{"</span> +</span><br><span class="line"> <span class="string">"no="</span> + no +</span><br><span class="line"> <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", nickname='"</span> + nickname + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-2-2修改单链表中节点的方法"><a href="#4-2-2修改单链表中节点的方法" class="headerlink" title="4.2.2修改单链表中节点的方法"></a>4.2.2修改单链表中节点的方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 修改节点的信息</span></span><br><span class="line"><span class="comment"> * 根据新的节点的编号进行修改</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">update</span><span class="params">(HeroNode newHeroNode)</span> {</span><br><span class="line"> <span class="comment">//判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空,无法修改该节点!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//找到需要修改的节点</span></span><br><span class="line"> <span class="comment">//首先找到头节点的位置</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head.next;</span><br><span class="line"> <span class="type">boolean</span> <span class="variable">idFind</span> <span class="operator">=</span> <span class="literal">false</span>; <span class="comment">//是否找到节点的信息</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="keyword">if</span> (temp == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">break</span>;<span class="comment">//已经到了链表的结尾,就退出循环</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (temp.no == newHeroNode.no) {<span class="comment">//找到修改的节点</span></span><br><span class="line"> temp.name = newHeroNode.name;</span><br><span class="line"> temp.nickname = newHeroNode.nickname;</span><br><span class="line"> idFind = <span class="literal">true</span>; <span class="comment">//找到了节点并且修改了节点的信息</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//做出判断,返回结果</span></span><br><span class="line"> System.out.println(idFind ? <span class="string">"修改成功"</span> : <span class="string">"没有找到该节点的信息"</span>);</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-2-3删除单链表中节点的方法"><a href="#4-2-3删除单链表中节点的方法" class="headerlink" title="4.2.3删除单链表中节点的方法"></a>4.2.3删除单链表中节点的方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 删除链表中的节点</span></span><br><span class="line"><span class="comment"> * 根据节点的编号删除节点的信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">delete</span><span class="params">(<span class="type">int</span> no)</span> {</span><br><span class="line"> <span class="comment">//判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空,无法删除!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//找到要删除的节点的信息</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="type">boolean</span> <span class="variable">isDelete</span> <span class="operator">=</span> <span class="literal">false</span>;<span class="comment">//是否删除</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="keyword">if</span> (temp.next == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//这里的temp指的是要删除节点的上一个节点 temp.next.next指的是要删除节点的下一个节点</span></span><br><span class="line"> <span class="keyword">if</span> (temp.next.no == no) {</span><br><span class="line"> <span class="comment">//将要删除的节点的上一个节点指向要删除节点的下一个节点 实现删除的操作</span></span><br><span class="line"> <span class="comment">//这样要删除的节点没有引用指向,会被垃圾回收机制回收</span></span><br><span class="line"> temp.next = temp.next.next; </span><br><span class="line"> isDelete = <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//做出判断,返回结果</span></span><br><span class="line"> System.out.println(isDelete ? <span class="string">"删除成功"</span> : <span class="string">"没有找到该节点的信息"</span>);</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-2-4完整代码"><a href="#4-2-4完整代码" class="headerlink" title="4.2.4完整代码"></a>4.2.4完整代码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.linkedlist;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/4/28</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 单链表的实现</span></span><br><span class="line"><span class="comment"> * 使用带head头的单向链表实现 –水浒英雄排行榜管理</span></span><br><span class="line"><span class="comment"> * 完成对英雄人物的增删改查操作</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SingleLinkedListDemo</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">SingleLinkedList</span> <span class="variable">singleLinkedList</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SingleLinkedList</span>();</span><br><span class="line"> singleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">1</span>, <span class="string">"宋江"</span>, <span class="string">"及时雨"</span>));</span><br><span class="line"> singleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">2</span>, <span class="string">"卢俊义"</span>, <span class="string">"玉麒麟"</span>));</span><br><span class="line"> singleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">3</span>, <span class="string">"吴用"</span>, <span class="string">"智多星"</span>));</span><br><span class="line"> singleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">4</span>, <span class="string">"林冲"</span>, <span class="string">"豹子头"</span>));</span><br><span class="line"> <span class="comment">//显示链表</span></span><br><span class="line"> singleLinkedList.list();</span><br><span class="line"> <span class="comment">//测试修改节点的功能</span></span><br><span class="line"> singleLinkedList.update(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">2</span>, <span class="string">"卢俊义"</span>, <span class="string">"玉麒麟增强版"</span>));</span><br><span class="line"> <span class="comment">//显示链表的信息</span></span><br><span class="line"> singleLinkedList.list();</span><br><span class="line"> <span class="comment">//测试删除节点</span></span><br><span class="line"> singleLinkedList.delete(<span class="number">2</span>);</span><br><span class="line"> <span class="comment">//显示链表的信息</span></span><br><span class="line"> singleLinkedList.list();</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义SingleLinkedList来管理我们的英雄任务</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">SingleLinkedList</span> {</span><br><span class="line"> <span class="comment">//初始化一个头节点 不存储具体的数据</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">HeroNode</span> <span class="variable">head</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">0</span>, <span class="string">""</span>, <span class="string">""</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 删除链表中的节点</span></span><br><span class="line"><span class="comment"> * 根据节点的编号删除节点的信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">delete</span><span class="params">(<span class="type">int</span> no)</span> {</span><br><span class="line"> <span class="comment">//判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空,无法删除!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//找到要删除的节点的信息</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="type">boolean</span> <span class="variable">isDelete</span> <span class="operator">=</span> <span class="literal">false</span>;<span class="comment">//是否删除</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="keyword">if</span> (temp.next == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (temp.next.no == no) {<span class="comment">//这里的temp指的是要删除节点的上一个节点 temp.next.next指的是要删除节点的下一个节点</span></span><br><span class="line"> <span class="comment">//将当前节点的上一个节点指向当前节点的下一个节点</span></span><br><span class="line"> temp.next = temp.next.next; <span class="comment">//将要删除的节点的上一个节点指向要删除节点的下一个节点 这样要删除的节点没有引用指向 会被垃圾回收机制回收</span></span><br><span class="line"> isDelete = <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//做出判断,返回结果</span></span><br><span class="line"> System.out.println(isDelete ? <span class="string">"删除成功"</span> : <span class="string">"没有找到该节点的信息"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 修改节点的信息</span></span><br><span class="line"><span class="comment"> * 根据新的节点的编号进行修改</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">update</span><span class="params">(HeroNode newHeroNode)</span> {</span><br><span class="line"> <span class="comment">//判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空,无法修改该节点!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//找到需要修改的节点</span></span><br><span class="line"> <span class="comment">//首先找到头节点的位置</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head.next;</span><br><span class="line"> <span class="type">boolean</span> <span class="variable">idFind</span> <span class="operator">=</span> <span class="literal">false</span>; <span class="comment">//是否找到节点的信息</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="keyword">if</span> (temp == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">break</span>;<span class="comment">//已经到了链表的结尾,就退出循环</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (temp.no == newHeroNode.no) {<span class="comment">//找到修改的节点</span></span><br><span class="line"> temp.name = newHeroNode.name;</span><br><span class="line"> temp.nickname = newHeroNode.nickname;</span><br><span class="line"> idFind = <span class="literal">true</span>; <span class="comment">//找到了节点并且修改了节点的信息</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//做出判断,返回结果</span></span><br><span class="line"> System.out.println(idFind ? <span class="string">"修改成功"</span> : <span class="string">"没有找到该节点的信息"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 添加节点到单向链表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">add</span><span class="params">(HeroNode heroNode)</span> {</span><br><span class="line"> <span class="comment">//找到当前链表的最后一个节点</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="keyword">while</span> (temp.next != <span class="literal">null</span>) {</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> temp.next = heroNode;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 显示链表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">list</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//先判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="keyword">while</span> (temp.next != <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(temp.next);</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义一个heroNode,每个heroNode对象都是一个节点</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">HeroNode</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> no;</span><br><span class="line"> <span class="keyword">public</span> String name;</span><br><span class="line"> <span class="keyword">public</span> String nickname;</span><br><span class="line"> <span class="keyword">public</span> HeroNode next; <span class="comment">//指向下一个节点</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//构造器</span></span><br><span class="line"> <span class="keyword">public</span> <span class="title function_">HeroNode</span><span class="params">(<span class="type">int</span> no, String name, String nickname)</span> {</span><br><span class="line"> <span class="built_in">this</span>.no = no;</span><br><span class="line"> <span class="built_in">this</span>.name = name;</span><br><span class="line"> <span class="built_in">this</span>.nickname = nickname;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"HeroNode{"</span> +</span><br><span class="line"> <span class="string">"no="</span> + no +</span><br><span class="line"> <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", nickname='"</span> + nickname + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-2-5-单链表的面试题(新浪,百度,腾讯)"><a href="#4-2-5-单链表的面试题(新浪,百度,腾讯)" class="headerlink" title="4.2.5 单链表的面试题(新浪,百度,腾讯)"></a>4.2.5 单链表的面试题(新浪,百度,腾讯)</h4><h5 id="1-求单链表中有效节点的个数"><a href="#1-求单链表中有效节点的个数" class="headerlink" title="1.求单链表中有效节点的个数"></a>1.求单链表中有效节点的个数</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取有效节点的个数(如果是带头节点的链表,需求不统计头节点)</span></span><br><span class="line"><span class="comment"> * 注意:这里是有头节点的情况下统计节点的个数 (这里我们去除了头节点)</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> heroNode 头节点</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 有效节点的个数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getLength</span><span class="params">(HeroNode heroNode)</span>{</span><br><span class="line"> <span class="comment">//先判断节点是否为空</span></span><br><span class="line"> <span class="keyword">if</span>(heroNode.next == <span class="literal">null</span>){</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//遍历节点进行统计</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">length</span> <span class="operator">=</span> <span class="number">0</span>;<span class="comment">//有效节点的个数</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> heroNode.next;<span class="comment">//这里没有统计头节点</span></span><br><span class="line"> <span class="keyword">while</span> (temp != <span class="literal">null</span>){<span class="comment">//遍历 统计个数</span></span><br><span class="line"> length++;</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> length;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
|
||
|
||
<h5 id="2-查找单链表中的倒数第K个节点-新浪面试题"><a href="#2-查找单链表中的倒数第K个节点-新浪面试题" class="headerlink" title="2.查找单链表中的倒数第K个节点(新浪面试题)"></a>2.查找单链表中的倒数第K个节点(新浪面试题)</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 查找单链表中的倒数第K个节点</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> index 倒数第几</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> heroNode 头节点</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 倒数第K个节点的信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> HeroNode <span class="title function_">getHeroNodeByLastIndex</span><span class="params">(<span class="type">int</span> index, HeroNode heroNode)</span> {</span><br><span class="line"> <span class="comment">//先判断节点是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (heroNode.next == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//首先获取有效节点的个数</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">length</span> <span class="operator">=</span> <span class="built_in">this</span>.getLength(heroNode);<span class="comment">//使用求节点有效个数的方法</span></span><br><span class="line"> <span class="comment">//求倒数第k个节点就是在求正数第(length - index + 1)节点</span></span><br><span class="line"> index = length - index; <span class="comment">//求出正向的索引位置</span></span><br><span class="line"> <span class="keyword">if</span> (index < <span class="number">0</span> || index > length) {</span><br><span class="line"> System.out.println(<span class="string">"所求的在链表中不存在"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> heroNode.next;</span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="keyword">if</span> (count == index) {</span><br><span class="line"> <span class="keyword">return</span> temp;</span><br><span class="line"> }</span><br><span class="line"> count++;</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
|
||
|
||
<h5 id="3-单链表的反转-腾讯面试题"><a href="#3-单链表的反转-腾讯面试题" class="headerlink" title="3.单链表的反转(腾讯面试题)"></a>3.单链表的反转(腾讯面试题)</h5><p><img src="/pictures/image-20230506105453269.png" alt="image-20230506105453269"></p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 单链表的反转</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> heroNode 需要反转单链表的头节点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">reverseHeroNode</span><span class="params">(HeroNode heroNode)</span> {</span><br><span class="line"> <span class="comment">//先判断当前的链表是否为空 或者当前的链表只有一个节点</span></span><br><span class="line"> <span class="keyword">if</span>(head.next == <span class="literal">null</span> || head.next.next == <span class="literal">null</span>){</span><br><span class="line"> System.out.println(<span class="string">"当前链表为空或者只有一个节点,无需反转"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//定义一个辅助的变量 帮助我们遍历原来的链表</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">cur</span> <span class="operator">=</span> head.next;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">next</span> <span class="operator">=</span> <span class="literal">null</span>;<span class="comment">//指向当前节点的下一个节点 我们要在挪动当前节点之前帮当前指针 下移</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">reverseHeroNode</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">0</span>, <span class="string">""</span>, <span class="string">""</span>);<span class="comment">//新链表的头节点</span></span><br><span class="line"> <span class="comment">//遍历原先的列表 确定每一个链表的指向关系</span></span><br><span class="line"> <span class="keyword">while</span> (cur != <span class="literal">null</span>){</span><br><span class="line"> next = cur.next;<span class="comment">//保存当前节点的下一个节点</span></span><br><span class="line"> <span class="comment">//reverseHeroNode.next表示头节点的下一个节点 我们让cur的下一个节点(cur.next)指向头节点的下一个节点 //(reverseHeroNode.next) 就把当前的节点(cur)穿插进去了</span></span><br><span class="line"> <span class="comment">//下一句将头节点和当前节点建立关系reverseHeroNode.next = cur 整个节点就连接起来了</span></span><br><span class="line"> cur.next = reverseHeroNode.next;<span class="comment">//将cur的下一个节点指向新的链表的最前端 相当于在头节点和头节点的下一个节点 //之间穿插了一个节点</span></span><br><span class="line"> reverseHeroNode.next = cur;<span class="comment">//将cur连接到新的链表上</span></span><br><span class="line"> cur = next; <span class="comment">//cur像后移动</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//将head.next指向reverseHeroNode.next 实现单链表的反转</span></span><br><span class="line"> head.next = reverseHeroNode.next;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
|
||
|
||
<h5 id="4-从尾到头打印单链表(百度面试题)"><a href="#4-从尾到头打印单链表(百度面试题)" class="headerlink" title="4.从尾到头打印单链表(百度面试题)"></a>4.从尾到头打印单链表(百度面试题)</h5><p>不改变链表本身的结构(不是通过链表的反转之后再打印的)</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * (通过栈的方式实现单链表的逆序打印)</span></span><br><span class="line"><span class="comment"> * 栈的特点是先入后出 正好可以满足逆序打印</span></span><br><span class="line"><span class="comment"> * 单链表的逆序打印</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">reversePrint</span><span class="params">(HeroNode heroNode)</span> {</span><br><span class="line"> <span class="comment">//先判断当前的链表是否为空 或者当前的链表只有一个节点</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"当前链表为空,无法逆序打印!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//创建一个栈,将各个节点压入栈中</span></span><br><span class="line"> <span class="comment">//先创建一个栈</span></span><br><span class="line"> Stack<HeroNode> stack = <span class="keyword">new</span> <span class="title class_">Stack</span><>();</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">cur</span> <span class="operator">=</span> heroNode.next;</span><br><span class="line"> <span class="comment">//循环将每一个节点添加进栈中</span></span><br><span class="line"> <span class="keyword">while</span> (cur != <span class="literal">null</span>) {</span><br><span class="line"> stack.push(cur);<span class="comment">//将节点压入栈中</span></span><br><span class="line"> cur = cur.next;<span class="comment">//cur后移 压入下一个节点</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//将栈中节点打印</span></span><br><span class="line"> <span class="keyword">while</span> (stack.size() > <span class="number">0</span>) {</span><br><span class="line"> System.out.println(stack.pop());</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
|
||
|
||
<h5 id="5-合并两个有序的单链表,合并之后的链表依然有序"><a href="#5-合并两个有序的单链表,合并之后的链表依然有序" class="headerlink" title="5.合并两个有序的单链表,合并之后的链表依然有序"></a>5.合并两个有序的单链表,合并之后的链表依然有序</h5><p>以下代码有一些bug 后期修改</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 合并两个有序的单链表,合并之后的链表依然有序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">mergeHeroNode</span><span class="params">(HeroNode oneHead, HeroNode twoHead ,HeroNode head)</span> {</span><br><span class="line"> <span class="comment">//先判断两个链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (oneHead.next == <span class="literal">null</span> || twoHead.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"其中有一个(两个)链表为空,无法合并"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//合并</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">oneCur</span> <span class="operator">=</span> oneHead.next;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">twoCur</span> <span class="operator">=</span> twoHead.next;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">oneNext</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">twoNext</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">finalHeroHead</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">0</span>, <span class="string">""</span>, <span class="string">""</span>);</span><br><span class="line"> <span class="keyword">while</span> (oneCur != <span class="literal">null</span> && twoCur != <span class="literal">null</span>) {</span><br><span class="line"> oneNext = oneCur.next;</span><br><span class="line"> twoNext = twoCur.next;</span><br><span class="line"> <span class="keyword">if</span>(oneCur.no <= twoCur.no) {</span><br><span class="line"> oneCur.next = finalHeroHead.next;</span><br><span class="line"> finalHeroHead.next = oneCur;</span><br><span class="line"> oneCur = oneNext;</span><br><span class="line"> }<span class="keyword">else</span> {</span><br><span class="line"> twoCur.next = finalHeroHead.next;</span><br><span class="line"> finalHeroHead.next = twoCur;</span><br><span class="line"> twoCur = twoNext;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> head.next = finalHeroHead.next;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
|
||
|
||
<h5 id="全部代码"><a href="#全部代码" class="headerlink" title="全部代码"></a>全部代码</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.linkedlist;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Stack;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/4/28</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 单链表的实现</span></span><br><span class="line"><span class="comment"> * 使用带head头的单向链表实现 –水浒英雄排行榜管理</span></span><br><span class="line"><span class="comment"> * 完成对英雄人物的增删改查操作</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SingleLinkedListDemo</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">SingleLinkedList</span> <span class="variable">singleLinkedList</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SingleLinkedList</span>();</span><br><span class="line"><span class="comment">// singleLinkedList.add(new HeroNode(1, "宋江", "及时雨"));</span></span><br><span class="line"><span class="comment">// singleLinkedList.add(new HeroNode(2, "卢俊义", "玉麒麟"));</span></span><br><span class="line"><span class="comment">// singleLinkedList.add(new HeroNode(3, "吴用", "智多星"));</span></span><br><span class="line"><span class="comment">// singleLinkedList.add(new HeroNode(4, "林冲", "豹子头"));</span></span><br><span class="line"><span class="comment">// //显示链表</span></span><br><span class="line"><span class="comment">// singleLinkedList.list();</span></span><br><span class="line"><span class="comment">// //测试修改节点的功能</span></span><br><span class="line"><span class="comment">// singleLinkedList.update(new HeroNode(2, "卢俊义", "玉麒麟增强版"));</span></span><br><span class="line"><span class="comment">// //显示链表的信息</span></span><br><span class="line"><span class="comment">// singleLinkedList.list();</span></span><br><span class="line"><span class="comment">// //测试删除节点</span></span><br><span class="line"><span class="comment">// singleLinkedList.delete(2);</span></span><br><span class="line"><span class="comment">// //显示链表的信息</span></span><br><span class="line"><span class="comment">// singleLinkedList.list();</span></span><br><span class="line"><span class="comment">// //统计有效节点的个数</span></span><br><span class="line"><span class="comment">// System.out.println("有效节点的个数" + singleLinkedList.getLength(singleLinkedList.getHead()));</span></span><br><span class="line"><span class="comment">// //查找单链表中的倒数第K个节点</span></span><br><span class="line"><span class="comment">// int index = 2;</span></span><br><span class="line"><span class="comment">// System.out.println("倒数第" + index + "个节点的位置:" + singleLinkedList.getHeroNodeByLastIndex(index, singleLinkedList.getHead()));</span></span><br><span class="line"><span class="comment">// //测试单链表的反转</span></span><br><span class="line"><span class="comment">// singleLinkedList.reverseHeroNode(singleLinkedList.getHead());</span></span><br><span class="line"><span class="comment">// singleLinkedList.list();</span></span><br><span class="line"><span class="comment">// //测试通过栈的方式逆序打印</span></span><br><span class="line"><span class="comment">// System.out.println("测试通过栈的方式逆序打印头节点");</span></span><br><span class="line"><span class="comment">// singleLinkedList.reversePrint(singleLinkedList.getHead());</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//测试合并两个有序的链表</span></span><br><span class="line"> <span class="type">SingleLinkedList</span> <span class="variable">singleLinkedList1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SingleLinkedList</span>();</span><br><span class="line"> singleLinkedList1.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">1</span>, <span class="string">"宋江"</span>, <span class="string">"及时雨"</span>));</span><br><span class="line"> singleLinkedList1.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">3</span>, <span class="string">"卢俊义"</span>, <span class="string">"玉麒麟"</span>));</span><br><span class="line"> singleLinkedList1.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">5</span>, <span class="string">"吴用"</span>, <span class="string">"智多星"</span>));</span><br><span class="line"> singleLinkedList1.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">7</span>, <span class="string">"林冲"</span>, <span class="string">"豹子头"</span>));</span><br><span class="line"> <span class="type">SingleLinkedList</span> <span class="variable">singleLinkedList2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SingleLinkedList</span>();</span><br><span class="line"> singleLinkedList2.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">2</span>, <span class="string">"宋江"</span>, <span class="string">"及时雨"</span>));</span><br><span class="line"> singleLinkedList2.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">4</span>, <span class="string">"卢俊义"</span>, <span class="string">"玉麒麟"</span>));</span><br><span class="line"> singleLinkedList2.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">6</span>, <span class="string">"吴用"</span>, <span class="string">"智多星"</span>));</span><br><span class="line"> singleLinkedList2.add(<span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">8</span>, <span class="string">"林冲"</span>, <span class="string">"豹子头"</span>));</span><br><span class="line"> mergeHeroNode(singleLinkedList1.getHead(),singleLinkedList2.getHead(),singleLinkedList.getHead());</span><br><span class="line"> singleLinkedList.list();</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 合并两个有序的单链表,合并之后的链表依然有序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">mergeHeroNode</span><span class="params">(HeroNode oneHead, HeroNode twoHead ,HeroNode head)</span> {</span><br><span class="line"> <span class="comment">//先判断两个链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (oneHead.next == <span class="literal">null</span> || twoHead.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"其中有一个(两个)链表为空,无法合并"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//合并</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">oneCur</span> <span class="operator">=</span> oneHead.next;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">twoCur</span> <span class="operator">=</span> twoHead.next;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">oneNext</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">twoNext</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">finalHeroHead</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">0</span>, <span class="string">""</span>, <span class="string">""</span>);</span><br><span class="line"> <span class="keyword">while</span> (oneCur != <span class="literal">null</span> && twoCur != <span class="literal">null</span>) {</span><br><span class="line"> oneNext = oneCur.next;</span><br><span class="line"> twoNext = twoCur.next;</span><br><span class="line"> <span class="keyword">if</span>(oneCur.no <= twoCur.no) {</span><br><span class="line"> oneCur.next = finalHeroHead.next;</span><br><span class="line"> finalHeroHead.next = oneCur;</span><br><span class="line"> oneCur = oneNext;</span><br><span class="line"> }<span class="keyword">else</span> {</span><br><span class="line"> twoCur.next = finalHeroHead.next;</span><br><span class="line"> finalHeroHead.next = twoCur;</span><br><span class="line"> twoCur = twoNext;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> head.next = finalHeroHead.next;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义SingleLinkedList来管理我们的英雄任务</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">SingleLinkedList</span> {</span><br><span class="line"> <span class="comment">//初始化一个头节点 不存储具体的数据</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">HeroNode</span> <span class="variable">head</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">0</span>, <span class="string">""</span>, <span class="string">""</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> HeroNode <span class="title function_">getHead</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> head;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * (通过栈的方式实现单链表的逆序打印)</span></span><br><span class="line"><span class="comment"> * 栈的特点是先入后出 正好可以满足逆序打印</span></span><br><span class="line"><span class="comment"> * 单链表的逆序打印</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">reversePrint</span><span class="params">(HeroNode heroNode)</span> {</span><br><span class="line"> <span class="comment">//先判断当前的链表是否为空 或者当前的链表只有一个节点</span></span><br><span class="line"> <span class="keyword">if</span> (heroNode.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"当前链表为空,无法逆序打印!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//创建一个栈,将各个节点压入栈中</span></span><br><span class="line"> <span class="comment">//先创建一个栈</span></span><br><span class="line"> Stack<HeroNode> stack = <span class="keyword">new</span> <span class="title class_">Stack</span><>();</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">cur</span> <span class="operator">=</span> heroNode.next;</span><br><span class="line"> <span class="comment">//循环将每一个节点添加进栈中</span></span><br><span class="line"> <span class="keyword">while</span> (cur != <span class="literal">null</span>) {</span><br><span class="line"> stack.push(cur);<span class="comment">//将节点压入栈中</span></span><br><span class="line"> cur = cur.next;<span class="comment">//cur后移 压入下一个节点</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//将栈中节点打印</span></span><br><span class="line"> <span class="keyword">while</span> (stack.size() > <span class="number">0</span>) {</span><br><span class="line"> System.out.println(stack.pop());</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 单链表的反转</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> heroNode 需要反转单链表的头节点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">reverseHeroNode</span><span class="params">(HeroNode heroNode)</span> {</span><br><span class="line"> <span class="comment">//先判断当前的链表是否为空 或者当前的链表只有一个节点</span></span><br><span class="line"> <span class="keyword">if</span> (heroNode.next == <span class="literal">null</span> || heroNode.next.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"当前链表为空或者只有一个节点,无需反转"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//定义一个辅助的变量 帮助我们遍历原来的链表</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">cur</span> <span class="operator">=</span> heroNode.next;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">next</span> <span class="operator">=</span> <span class="literal">null</span>;<span class="comment">//指向当前节点的下一个节点 我们要在挪动当前节点之前帮当前指针 下移</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">reverseHeroNode</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">HeroNode</span>(<span class="number">0</span>, <span class="string">""</span>, <span class="string">""</span>);<span class="comment">//新链表的头节点</span></span><br><span class="line"> <span class="comment">//遍历原先的列表 确定每一个链表的指向关系</span></span><br><span class="line"> <span class="keyword">while</span> (cur != <span class="literal">null</span>) {</span><br><span class="line"> next = cur.next;<span class="comment">//保存当前节点的下一个节点</span></span><br><span class="line"> <span class="comment">//reverseHeroNode.next表示头节点的下一个节点 我们让cur的下一个节点(cur.next)指向头节点的下一个节点(reverseHeroNode.next) 就把当前的节点(cur)穿插进去了</span></span><br><span class="line"> <span class="comment">//下一句将头节点和当前节点建立关系reverseHeroNode.next = cur 整个节点就连接起来了</span></span><br><span class="line"> cur.next = reverseHeroNode.next;<span class="comment">//将cur的下一个节点指向新的链表的最前端 相当于在头节点和头节点的下一个节点之间穿插了一个节点</span></span><br><span class="line"> reverseHeroNode.next = cur;<span class="comment">//将cur连接到新的链表上</span></span><br><span class="line"> cur = next; <span class="comment">//cur像后移动</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//将head.next指向reverseHeroNode.next 实现单链表的反转</span></span><br><span class="line"> heroNode.next = reverseHeroNode.next;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 查找单链表中的倒数第K个节点</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> index 倒数第几</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> heroNode 头节点</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 倒数第K个节点的信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> HeroNode <span class="title function_">getHeroNodeByLastIndex</span><span class="params">(<span class="type">int</span> index, HeroNode heroNode)</span> {</span><br><span class="line"> <span class="comment">//先判断节点是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (heroNode.next == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//首先获取有效节点的个数</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">length</span> <span class="operator">=</span> <span class="built_in">this</span>.getLength(heroNode);<span class="comment">//使用求节点有效个数的方法</span></span><br><span class="line"> <span class="comment">//求倒数第k个节点就是在求正数第(length - index + 1)节点</span></span><br><span class="line"> index = length - index; <span class="comment">//求出正向的索引位置</span></span><br><span class="line"> <span class="keyword">if</span> (index < <span class="number">0</span> || index > length) {</span><br><span class="line"> System.out.println(<span class="string">"所求的在链表中不存在"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> heroNode.next;</span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="keyword">if</span> (count == index) {</span><br><span class="line"> <span class="keyword">return</span> temp;</span><br><span class="line"> }</span><br><span class="line"> count++;</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取有效节点的个数(如果是带头节点的链表,需求不统计头节点)</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> heroNode 头节点</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 有效节点的个数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getLength</span><span class="params">(HeroNode heroNode)</span> {</span><br><span class="line"> <span class="comment">//先判断节点是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (heroNode.next == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//遍历节点进行统计</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">length</span> <span class="operator">=</span> <span class="number">0</span>;<span class="comment">//有效节点的个数</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> heroNode.next;<span class="comment">//这里没有统计头节点</span></span><br><span class="line"> <span class="keyword">while</span> (temp != <span class="literal">null</span>) {<span class="comment">//遍历 统计个数</span></span><br><span class="line"> length++;</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> length;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 删除链表中的节点</span></span><br><span class="line"><span class="comment"> * 根据节点的编号删除节点的信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">delete</span><span class="params">(<span class="type">int</span> no)</span> {</span><br><span class="line"> <span class="comment">//判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空,无法删除!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//找到要删除的节点的信息</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="type">boolean</span> <span class="variable">isDelete</span> <span class="operator">=</span> <span class="literal">false</span>;<span class="comment">//是否删除</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="keyword">if</span> (temp.next == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (temp.next.no == no) {<span class="comment">//这里的temp指的是要删除节点的上一个节点 temp.next.next指的是要删除节点的下一个节点</span></span><br><span class="line"> <span class="comment">//将当前节点的上一个节点指向当前节点的下一个节点</span></span><br><span class="line"> temp.next = temp.next.next; <span class="comment">//将要删除的节点的上一个节点指向要删除节点的下一个节点 这样要删除的节点没有引用指向 会被垃圾回收机制回收</span></span><br><span class="line"> isDelete = <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//做出判断,返回结果</span></span><br><span class="line"> System.out.println(isDelete ? <span class="string">"删除成功"</span> : <span class="string">"没有找到该节点的信息"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 修改节点的信息</span></span><br><span class="line"><span class="comment"> * 根据新的节点的编号进行修改</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">update</span><span class="params">(HeroNode newHeroNode)</span> {</span><br><span class="line"> <span class="comment">//判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空,无法修改该节点!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//找到需要修改的节点</span></span><br><span class="line"> <span class="comment">//首先找到头节点的位置</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head.next;</span><br><span class="line"> <span class="type">boolean</span> <span class="variable">idFind</span> <span class="operator">=</span> <span class="literal">false</span>; <span class="comment">//是否找到节点的信息</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="keyword">if</span> (temp == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">break</span>;<span class="comment">//已经到了链表的结尾,就退出循环</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (temp.no == newHeroNode.no) {<span class="comment">//找到修改的节点</span></span><br><span class="line"> temp.name = newHeroNode.name;</span><br><span class="line"> temp.nickname = newHeroNode.nickname;</span><br><span class="line"> idFind = <span class="literal">true</span>; <span class="comment">//找到了节点并且修改了节点的信息</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//做出判断,返回结果</span></span><br><span class="line"> System.out.println(idFind ? <span class="string">"修改成功"</span> : <span class="string">"没有找到该节点的信息"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 添加节点到单向链表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">add</span><span class="params">(HeroNode heroNode)</span> {</span><br><span class="line"> <span class="comment">//找到当前链表的最后一个节点</span></span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="keyword">while</span> (temp.next != <span class="literal">null</span>) {</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> temp.next = heroNode;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 显示链表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">list</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//先判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="type">HeroNode</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="keyword">while</span> (temp.next != <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(temp.next);</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义一个heroNode,每个heroNode对象都是一个节点</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">HeroNode</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> no;</span><br><span class="line"> <span class="keyword">public</span> String name;</span><br><span class="line"> <span class="keyword">public</span> String nickname;</span><br><span class="line"> <span class="keyword">public</span> HeroNode next; <span class="comment">//指向下一个节点</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//构造器</span></span><br><span class="line"> <span class="keyword">public</span> <span class="title function_">HeroNode</span><span class="params">(<span class="type">int</span> no, String name, String nickname)</span> {</span><br><span class="line"> <span class="built_in">this</span>.no = no;</span><br><span class="line"> <span class="built_in">this</span>.name = name;</span><br><span class="line"> <span class="built_in">this</span>.nickname = nickname;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"HeroNode{"</span> +</span><br><span class="line"> <span class="string">"no="</span> + no +</span><br><span class="line"> <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", nickname='"</span> + nickname + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
|
||
|
||
<h3 id="4-3-双向链表"><a href="#4-3-双向链表" class="headerlink" title="4.3 双向链表"></a>4.3 双向链表</h3><p>双向的链表的CRUD操作于单向链表的CRUD操作类似</p>
|
||
<p><img src="/pictures/image-20230507100730848.png" alt="image-20230507100730848"></p>
|
||
<p>完整的增删改查代码</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.linkedlist;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/7</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 双向链表的使用</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DoubleLinkedListDemo</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">DoubleLinkedList</span> <span class="variable">doubleLinkedList</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DoubleLinkedList</span>();</span><br><span class="line"> <span class="comment">//测试添加链表的功能</span></span><br><span class="line"> doubleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode2</span>(<span class="number">1</span>, <span class="string">"宋江"</span>, <span class="string">"及时雨"</span>));</span><br><span class="line"> doubleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode2</span>(<span class="number">2</span>, <span class="string">"卢俊义"</span>, <span class="string">"玉麒麟"</span>));</span><br><span class="line"> doubleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode2</span>(<span class="number">3</span>, <span class="string">"吴用"</span>, <span class="string">"智多星"</span>));</span><br><span class="line"> doubleLinkedList.add(<span class="keyword">new</span> <span class="title class_">HeroNode2</span>(<span class="number">4</span>, <span class="string">"林冲"</span>, <span class="string">"豹子头"</span>));</span><br><span class="line"> <span class="comment">//测试显示链表的功能</span></span><br><span class="line"> doubleLinkedList.list();</span><br><span class="line"> <span class="comment">//测试删除双向链表中的节点</span></span><br><span class="line"> doubleLinkedList.delete(<span class="number">2</span>);</span><br><span class="line"> doubleLinkedList.list();</span><br><span class="line"> <span class="comment">//测试修改双向链表中的节点</span></span><br><span class="line"> doubleLinkedList.update(<span class="keyword">new</span> <span class="title class_">HeroNode2</span>(<span class="number">2</span>, <span class="string">"卢俊义修改版"</span>, <span class="string">"玉麒麟"</span>));</span><br><span class="line"> doubleLinkedList.list();</span><br><span class="line"> System.out.println(<span class="string">"--------------"</span>);</span><br><span class="line"> doubleLinkedList.orderAdd(<span class="keyword">new</span> <span class="title class_">HeroNode2</span>(<span class="number">3</span>, <span class="string">"小卢"</span>, <span class="string">"玉麒麟"</span>));</span><br><span class="line"> doubleLinkedList.list();</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">DoubleLinkedList</span> {</span><br><span class="line"> <span class="comment">//初始化一个头节点 不存储具体的数据</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">HeroNode2</span> <span class="variable">head</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">HeroNode2</span>(<span class="number">0</span>, <span class="string">""</span>, <span class="string">""</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//返回头节点的信息</span></span><br><span class="line"> <span class="keyword">public</span> HeroNode2 <span class="title function_">getHead</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> head;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 双向链表的顺序添加</span></span><br><span class="line"><span class="comment"> * 应该有问题 不是参考答案</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">orderAdd</span><span class="params">(HeroNode2 heroNode)</span> {</span><br><span class="line"> <span class="comment">//找到当前链表的最后一个节点</span></span><br><span class="line"> <span class="type">HeroNode2</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="type">boolean</span> <span class="variable">isSuccess</span> <span class="operator">=</span> <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">while</span> (temp.next != <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">if</span> (temp.next.no >= heroNode.no) {</span><br><span class="line"> heroNode.next = temp.next;</span><br><span class="line"> temp.next.pre = heroNode;</span><br><span class="line"> temp.next = heroNode;</span><br><span class="line"> heroNode.pre = temp;</span><br><span class="line"> isSuccess = <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (!isSuccess) {<span class="comment">//没有添加成功 说明当前链表的序号超过了链表里面已有节点序号的最高值 直接添加到最后</span></span><br><span class="line"> <span class="built_in">this</span>.add(heroNode);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 删除双向链表中的一个节点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">delete</span><span class="params">(<span class="type">int</span> no)</span> {</span><br><span class="line"> <span class="comment">//判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空,无法删除!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//找到要删除的节点的信息</span></span><br><span class="line"> <span class="type">HeroNode2</span> <span class="variable">temp</span> <span class="operator">=</span> head.next;</span><br><span class="line"> <span class="type">boolean</span> <span class="variable">isDelete</span> <span class="operator">=</span> <span class="literal">false</span>;<span class="comment">//是否删除</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="keyword">if</span> (temp == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (temp.no == no) {</span><br><span class="line"> temp.pre.next = temp.next;</span><br><span class="line"> <span class="keyword">if</span> (temp.next != <span class="literal">null</span>) {</span><br><span class="line"> temp.next.pre = temp.pre;<span class="comment">//这句话有个条件 temp不能是最后一个节点</span></span><br><span class="line"> }</span><br><span class="line"> isDelete = <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> System.out.println(isDelete ? <span class="string">"删除成功"</span> : <span class="string">"没有找到该节点的信息"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 修改双向链表的信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">update</span><span class="params">(HeroNode2 newHeroNode)</span> {</span><br><span class="line"> <span class="comment">//判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空,无法修改该节点!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//找到需要修改的节点</span></span><br><span class="line"> <span class="comment">//首先找到头节点的位置</span></span><br><span class="line"> <span class="type">HeroNode2</span> <span class="variable">temp</span> <span class="operator">=</span> head.next;</span><br><span class="line"> <span class="type">boolean</span> <span class="variable">idFind</span> <span class="operator">=</span> <span class="literal">false</span>; <span class="comment">//是否找到节点的信息</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="keyword">if</span> (temp == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">break</span>;<span class="comment">//已经到了链表的结尾,就退出循环</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (temp.no == newHeroNode.no) {<span class="comment">//找到修改的节点</span></span><br><span class="line"> temp.name = newHeroNode.name;</span><br><span class="line"> temp.nickname = newHeroNode.nickname;</span><br><span class="line"> idFind = <span class="literal">true</span>; <span class="comment">//找到了节点并且修改了节点的信息</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//做出判断,返回结果</span></span><br><span class="line"> System.out.println(idFind ? <span class="string">"修改成功"</span> : <span class="string">"没有找到该节点的信息"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 向双向链表中添加节点信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">add</span><span class="params">(HeroNode2 heroNode)</span> {</span><br><span class="line"> <span class="comment">//找到当前链表的最后一个节点</span></span><br><span class="line"> <span class="type">HeroNode2</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="keyword">while</span> (temp.next != <span class="literal">null</span>) {</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> temp.next = heroNode;</span><br><span class="line"> heroNode.pre = temp;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 显示链表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">list</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//先判断链表是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (head.next == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"链表为空!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="type">HeroNode2</span> <span class="variable">temp</span> <span class="operator">=</span> head;</span><br><span class="line"> <span class="keyword">while</span> (temp.next != <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(temp.next);</span><br><span class="line"> temp = temp.next;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义一个heroNode,每个heroNode对象都是一个节点</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">HeroNode2</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> no;</span><br><span class="line"> <span class="keyword">public</span> String name;</span><br><span class="line"> <span class="keyword">public</span> String nickname;</span><br><span class="line"> <span class="keyword">public</span> HeroNode2 next; <span class="comment">//指向下一个节点</span></span><br><span class="line"> <span class="keyword">public</span> HeroNode2 pre;<span class="comment">//指向上一个节点</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//构造器</span></span><br><span class="line"> <span class="keyword">public</span> <span class="title function_">HeroNode2</span><span class="params">(<span class="type">int</span> no, String name, String nickname)</span> {</span><br><span class="line"> <span class="built_in">this</span>.no = no;</span><br><span class="line"> <span class="built_in">this</span>.name = name;</span><br><span class="line"> <span class="built_in">this</span>.nickname = nickname;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"HeroNode{"</span> +</span><br><span class="line"> <span class="string">"no="</span> + no +</span><br><span class="line"> <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", nickname='"</span> + nickname + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="4-4-单向环形链表的应用场景-Josephu问题"><a href="#4-4-单向环形链表的应用场景-Josephu问题" class="headerlink" title="4.4 单向环形链表的应用场景(Josephu问题)"></a>4.4 单向环形链表的应用场景(Josephu问题)</h3><p> Josephu 问题为:设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。</p>
|
||
<p> 解决的方案:用一个不带头结点的循环链表来处理Josephu 问题:先构成一个有n个结点的单循环链表,然后由k结点起从1开始计数,计到m时,对应结点从链表中删除,然后再从被删除结点的下一个结点又从1开始计数,直到最后一个结点从链表中删除算法结束。</p>
|
||
<p><img src="/pictures/image-20230508104207815.png" alt="image-20230508104207815"></p>
|
||
<p><img src="/pictures/image-20230508121934697.png" alt="image-20230508121934697"></p>
|
||
<p>完整的代码</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.linkedlist;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/8</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 约瑟夫问题</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Josephu</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//测试构建环形链表</span></span><br><span class="line"> <span class="type">CircleSingleLinkedList</span> <span class="variable">circleSingleLinkedList</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">CircleSingleLinkedList</span>();</span><br><span class="line"> circleSingleLinkedList.addBoy(<span class="number">5</span>);</span><br><span class="line"> <span class="comment">//测试遍历环形单向链表</span></span><br><span class="line"> circleSingleLinkedList.showBoy();</span><br><span class="line"> <span class="comment">//测试生成小孩出圈的序列</span></span><br><span class="line"> circleSingleLinkedList.countBoy(<span class="number">1</span>,<span class="number">2</span>,<span class="number">5</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//创建一个环形的单向链表</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">CircleSingleLinkedList</span> {</span><br><span class="line"> <span class="comment">//创建一个first节点,当前没有编号</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">Boy</span> <span class="variable">first</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 根据用户的输入产生一个出队编号的序列</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> startNo 表示从第几个小孩开始数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> countNum 表示数几下</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> nums 传入几个小孩</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">countBoy</span><span class="params">(<span class="type">int</span> startNo, <span class="type">int</span> countNum, <span class="type">int</span> nums)</span> {</span><br><span class="line"> <span class="comment">//先对输入的数据进行校验</span></span><br><span class="line"> <span class="keyword">if</span> (first == <span class="literal">null</span> || startNo < <span class="number">0</span> || startNo > nums) {</span><br><span class="line"> System.out.println(<span class="string">"参数输入有误,请重新输入!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//创建一个辅助指针</span></span><br><span class="line"> <span class="type">Boy</span> <span class="variable">helper</span> <span class="operator">=</span> first;</span><br><span class="line"> <span class="comment">//将helper指向环形链表的最后一个节点</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="keyword">if</span> (helper.getNext() == first) {</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> helper = helper.getNext();</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//将first和helper移动到指定的位置 考虑从第几个小孩开始数</span></span><br><span class="line"> <span class="comment">//小孩报数前,先让 first 和 helper 移动 k - 1次</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < startNo - <span class="number">1</span>; i++) {</span><br><span class="line"> first = first.getNext();</span><br><span class="line"> helper = helper.getNext();</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//让first和helper移动 m-1次,然后出圈</span></span><br><span class="line"> <span class="comment">//循环操作 直到圈中只有一个小孩节点</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>){</span><br><span class="line"> <span class="keyword">if</span>(helper == first){<span class="comment">//说明圈中只有一个节点</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//让first和helper移动 countNum - 1</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < countNum - <span class="number">1</span>; i++) {</span><br><span class="line"> first = first.getNext();</span><br><span class="line"> helper = helper.getNext();</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//这时first指向的节点就是要出圈的小孩</span></span><br><span class="line"> System.out.printf(<span class="string">"小孩%d出圈\n"</span>,first.getNo());</span><br><span class="line"> <span class="comment">//将first指向的小孩出圈</span></span><br><span class="line"> first = first.getNext();</span><br><span class="line"> helper.setNext(first);</span><br><span class="line"> }</span><br><span class="line"> System.out.printf(<span class="string">"最后留在圈中小孩编号是:%d \n"</span>,helper.getNo());</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 遍历当前的环形链表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">showBoy</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//先判断链表是不是为空</span></span><br><span class="line"> <span class="keyword">if</span> (first == <span class="literal">null</span>) {</span><br><span class="line"> System.out.println(<span class="string">"环形链表为空!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//创建一个辅助指针</span></span><br><span class="line"> <span class="type">Boy</span> <span class="variable">curBoy</span> <span class="operator">=</span> first;</span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> System.out.printf(<span class="string">"小孩的编号:%d \n"</span>, curBoy.getNo());</span><br><span class="line"> <span class="keyword">if</span> (curBoy.getNext() == first) {<span class="comment">//说明已经循环一圈了 就不在循环 退出了</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//向后移动</span></span><br><span class="line"> curBoy = curBoy.getNext();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 添加小孩节点 构建成一个环形的链表</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> nums 传入几个小孩</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addBoy</span><span class="params">(<span class="type">int</span> nums)</span> {</span><br><span class="line"> <span class="comment">//将传入的数据进行一个校验</span></span><br><span class="line"> <span class="keyword">if</span> (nums < <span class="number">1</span>) {<span class="comment">//传入小孩的的数量不能小于一</span></span><br><span class="line"> System.out.println(<span class="string">"输入的值不正确!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="type">Boy</span> <span class="variable">curBoy</span> <span class="operator">=</span> <span class="literal">null</span>;<span class="comment">//辅助指针,帮助构建环形链表</span></span><br><span class="line"> <span class="comment">//使用循环创建我们的环形链表</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i <= nums; i++) { <span class="comment">//要加入几个小孩 就循环几次</span></span><br><span class="line"> <span class="comment">//根据编号创建小孩节点</span></span><br><span class="line"> <span class="type">Boy</span> <span class="variable">boy</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Boy</span>(i);</span><br><span class="line"> <span class="comment">//将小孩加入到环形链表中</span></span><br><span class="line"> <span class="keyword">if</span> (i == <span class="number">1</span>) {<span class="comment">//说明是第一个小孩</span></span><br><span class="line"> first = boy;</span><br><span class="line"> first.setNext(first);<span class="comment">//添加的第一个节点 先自己指向自己 构建成一个环</span></span><br><span class="line"> curBoy = first; <span class="comment">//first节点我们不能动 使用辅助节点</span></span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//这是在循环里面 每次循环之后 小孩节点都不一样</span></span><br><span class="line"> curBoy.setNext(boy);</span><br><span class="line"> boy.setNext(first); <span class="comment">//形成回路</span></span><br><span class="line"> curBoy = boy;<span class="comment">//curBoy每次指向的都是最后一个小孩节点</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//创建一个boy类 表示一个节点</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Boy</span> {</span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> no; <span class="comment">//编号</span></span><br><span class="line"> <span class="keyword">private</span> Boy next;<span class="comment">//指向下一个节点 默认是null</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="title function_">Boy</span><span class="params">(<span class="type">int</span> no)</span> {</span><br><span class="line"> <span class="built_in">this</span>.no = no;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getNo</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> no;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setNo</span><span class="params">(<span class="type">int</span> no)</span> {</span><br><span class="line"> <span class="built_in">this</span>.no = no;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> Boy <span class="title function_">getNext</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> next;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setNext</span><span class="params">(Boy next)</span> {</span><br><span class="line"> <span class="built_in">this</span>.next = next;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
|
||
|
||
|
||
|
||
<h2 id="五-栈"><a href="#五-栈" class="headerlink" title="五.栈"></a>五.栈</h2><h3 id="1-介绍"><a href="#1-介绍" class="headerlink" title="1.介绍"></a>1.介绍</h3><p>1)栈的英文为(stack)</p>
|
||
<p>2)栈是一个<strong>先入后出</strong>(FILO-First In Last Out)的有序列表。</p>
|
||
<p>3)栈(stack)是限制线性表中元素的插入和删除<strong>只能在线性表的同一端</strong>进行的一种特殊线性表。允许插入和删除的一端,为变化的一端,称为<strong>栈顶</strong>(Top),另一端为固定的一端,称为<strong>栈底</strong>(Bottom)。</p>
|
||
<p>4)根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元素最先删除,最先放入的元素最后删除</p>
|
||
<h3 id="2-应用场景"><a href="#2-应用场景" class="headerlink" title="2.应用场景"></a>2.应用场景</h3><p>1)子程序的调用:在跳往子程序前,会先将下个指令的地址存到堆栈中,直到子程序执行完后再将地址取出,以回到原来的程序中。 </p>
|
||
<p>2)处理递归调用:和子程序的调用类似,只是除了储存下一个指令的地址外,也将参数、区域变量等数据存入堆栈中。</p>
|
||
<p>3)表达式的转换[中缀表达式转后缀表达式]与求值(实际解决)。</p>
|
||
<p>4)二叉树的遍历。</p>
|
||
<p>5)图形的深度优先(depth一first)搜索法。</p>
|
||
<h3 id="3-图解"><a href="#3-图解" class="headerlink" title="3.图解"></a>3.图解</h3><p><img src="/pictures/image-20230510101713914.png" alt="image-20230510101713914"></p>
|
||
<p><img src="/pictures/image-20230510101722707.png" alt="image-20230510101722707"></p>
|
||
<h3 id="4-使用数组模拟栈"><a href="#4-使用数组模拟栈" class="headerlink" title="4.使用数组模拟栈"></a>4.使用数组模拟栈</h3><p><img src="/pictures/image-20230510101916230.png" alt="image-20230510101916230"></p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.stack;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/10</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 使用数组模拟栈</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayStackDemo</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">ArrayStack</span> <span class="variable">stack</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ArrayStack</span>(<span class="number">5</span>);</span><br><span class="line"> <span class="comment">//入栈</span></span><br><span class="line"> stack.push(<span class="number">1</span>);</span><br><span class="line"> stack.push(<span class="number">2</span>);</span><br><span class="line"> stack.push(<span class="number">3</span>);</span><br><span class="line"> <span class="comment">//出栈</span></span><br><span class="line"> System.out.println(<span class="string">"出栈的数是:"</span>+stack.pop());</span><br><span class="line"> <span class="comment">//显示栈中所有的数据</span></span><br><span class="line"> stack.list();</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义一个ArrayStack表示栈</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ArrayStack</span> {</span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> maxSize;<span class="comment">//栈的大小</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span>[] stack;<span class="comment">//数组 数组模拟栈 数据放在数组中</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> <span class="variable">top</span> <span class="operator">=</span> -<span class="number">1</span>; <span class="comment">//top表示栈顶 初始化为1</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//构造器</span></span><br><span class="line"> <span class="keyword">public</span> <span class="title function_">ArrayStack</span><span class="params">(<span class="type">int</span> maxSize)</span> {</span><br><span class="line"> <span class="built_in">this</span>.maxSize = maxSize;</span><br><span class="line"> <span class="comment">//完成数组的初始化</span></span><br><span class="line"> stack = <span class="keyword">new</span> <span class="title class_">int</span>[maxSize];</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断是否栈满</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isFull</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> top == maxSize - <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断栈是否为空</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isEmpty</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> top == -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 入栈的操作</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">push</span><span class="params">(<span class="type">int</span> value)</span> {</span><br><span class="line"> <span class="comment">//先判断栈是否为满</span></span><br><span class="line"> <span class="keyword">if</span> (isFull()) {</span><br><span class="line"> System.out.println(<span class="string">"栈满,无法添加数据!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> top++;</span><br><span class="line"> stack[top] = value;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 出栈的操作</span></span><br><span class="line"><span class="comment"> * 将栈顶的数据返回</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">pop</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//先判断栈是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (isEmpty()) {</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"栈空,无法取出数据!"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//取出数据</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">value</span> <span class="operator">=</span> stack[top];</span><br><span class="line"> top--;</span><br><span class="line"> <span class="keyword">return</span> value;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 显示栈的情况</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">list</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//先判断栈是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (isEmpty()) {</span><br><span class="line"> System.out.println(<span class="string">"栈空,无法取出数据!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> top; i >= <span class="number">0</span>; i--) {</span><br><span class="line"> System.out.printf(<span class="string">"stack[%d] = %d \n"</span>, i, stack[i]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="5-栈实现综合计算器"><a href="#5-栈实现综合计算器" class="headerlink" title="5.栈实现综合计算器"></a>5.栈实现综合计算器</h3><p>中缀表达式</p>
|
||
<p><img src="/pictures/image-20230510105251076.png" alt="image-20230510105251076"></p>
|
||
<p><img src="/pictures/image-20230510110403055.png" alt="image-20230510110403055"></p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.stack;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/10</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 综合计算器</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Calculator</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 1. 通过一个 index 值(索引),来遍历我们的表达式</span></span><br><span class="line"><span class="comment"> * 2. 如果我们发现是一个数字, 就直接入数栈</span></span><br><span class="line"><span class="comment"> * 3. 如果发现扫描到的是一个符号, 就分如下情况</span></span><br><span class="line"><span class="comment"> * 3.1 如果发现当前的符号栈为 空,就直接入栈</span></span><br><span class="line"><span class="comment"> * 3.2 如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或者等于栈中的操作符, 就需要从数栈中pop出两个数,在从符号栈中pop出一个符号,进行运算,将得到结果,入数栈,然后将当前的操作符入符号栈, 如果当前的操作符的优先级大于栈中的操作符, 就直接入符号栈.</span></span><br><span class="line"><span class="comment"> * 4. 当表达式扫描完毕,就顺序的从 数栈和符号栈中pop出相应的数和符号,并运行.</span></span><br><span class="line"><span class="comment"> * 5. 最后在数栈只有一个数字,就是表达式的结果</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">expression</span> <span class="operator">=</span> <span class="string">"30+2*6-2"</span>;</span><br><span class="line"> <span class="comment">//创建两个栈 一个是数栈 一个是符号栈</span></span><br><span class="line"> <span class="type">ArrayStack2</span> <span class="variable">numStack</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ArrayStack2</span>(<span class="number">10</span>);</span><br><span class="line"> <span class="type">ArrayStack2</span> <span class="variable">operStack</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ArrayStack2</span>(<span class="number">10</span>);</span><br><span class="line"> <span class="comment">//定义需要的相关变量</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">index</span> <span class="operator">=</span> <span class="number">0</span>;<span class="comment">//用于扫描</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">num1</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> <span class="variable">num2</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> <span class="variable">oper</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="type">char</span> <span class="variable">ch</span> <span class="operator">=</span> <span class="string">' '</span>; <span class="comment">//将每次扫描得到的char保存在ch中</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">keepNum</span> <span class="operator">=</span> <span class="string">""</span>;<span class="comment">//用于拼接多位数的</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="comment">//得到expression的每一个字符</span></span><br><span class="line"> ch = expression.substring(index, index + <span class="number">1</span>).charAt(<span class="number">0</span>);</span><br><span class="line"> <span class="comment">//判断ch是什么 做相应的处理</span></span><br><span class="line"> <span class="keyword">if</span> (operStack.isOper(ch)) {</span><br><span class="line"> <span class="comment">//先符号栈判断是不是为空</span></span><br><span class="line"> <span class="keyword">if</span> (!operStack.isEmpty()) { <span class="comment">//判断符号栈是否为空</span></span><br><span class="line"> <span class="comment">//不为空的情况</span></span><br><span class="line"> <span class="keyword">if</span> (operStack.priority(ch) <= operStack.priority(operStack.peek())) {</span><br><span class="line"> <span class="comment">//如果当前的操作符的优先级小于或者等于栈中的操作符, 就需要从数栈中pop出两个数,</span></span><br><span class="line"> <span class="comment">// 在从符号栈中pop出一个符号,进行运算,将得到结果,入数栈</span></span><br><span class="line"> num1 = numStack.pop();</span><br><span class="line"> num2 = numStack.pop();</span><br><span class="line"> oper = operStack.pop();</span><br><span class="line"> result = numStack.cal(num1, num2, oper);</span><br><span class="line"> <span class="comment">//将运算的结果入数栈</span></span><br><span class="line"> numStack.push(result);</span><br><span class="line"> <span class="comment">//将当前的操作符入符号栈</span></span><br><span class="line"> operStack.push(ch);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//如果当前的操作符的优先级大于栈中的操作符, 就直接入符号栈.</span></span><br><span class="line"> operStack.push(ch);</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//如果为空 直接入栈</span></span><br><span class="line"> operStack.push(ch);</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//如果是数字 就直接入数栈</span></span><br><span class="line"> <span class="comment">//要考虑是多位数的情况</span></span><br><span class="line"> keepNum += ch;</span><br><span class="line"> <span class="comment">//如果ch是表达式的最后一位 就直接入栈</span></span><br><span class="line"> <span class="keyword">if</span> (index == expression.length() - <span class="number">1</span>) {</span><br><span class="line"> numStack.push(Integer.parseInt(keepNum));</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//判断下一个字符是不是数字 如果是数字 就继续扫描 如果是运算符 则入栈</span></span><br><span class="line"> <span class="keyword">if</span> (operStack.isOper(expression.substring(index + <span class="number">1</span>, index + <span class="number">2</span>).charAt(<span class="number">0</span>))) {<span class="comment">//下一位是操作符</span></span><br><span class="line"> <span class="comment">//直接将当前为添加到数栈</span></span><br><span class="line"> numStack.push(Integer.parseInt(keepNum));</span><br><span class="line"> <span class="comment">//清空keepNum</span></span><br><span class="line"> keepNum = <span class="string">""</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//让index + 1 ,并判断是否扫描到表达式的最后</span></span><br><span class="line"> index++;</span><br><span class="line"> <span class="keyword">if</span> (index >= expression.length()) {</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//当表达式扫描完毕,就顺序的从 数栈和符号栈中pop出相应的数和符号,并运行.</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="comment">//如果符号栈为空 则计算结束 数栈中只有 一个数字</span></span><br><span class="line"> <span class="keyword">if</span> (operStack.isEmpty()) {</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> num1 = numStack.pop();</span><br><span class="line"> num2 = numStack.pop();</span><br><span class="line"> oper = operStack.pop();</span><br><span class="line"> result = numStack.cal(num1, num2, oper);</span><br><span class="line"> numStack.push(result);</span><br><span class="line"> }</span><br><span class="line"> System.out.printf(<span class="string">"表达式:%s = %d"</span>, expression, numStack.pop());</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义一个ArrayStack表示栈</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ArrayStack2</span> {</span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> maxSize;<span class="comment">//栈的大小</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span>[] stack;<span class="comment">//数组 数组模拟栈 数据放在数组中</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> <span class="variable">top</span> <span class="operator">=</span> -<span class="number">1</span>; <span class="comment">//top表示栈顶 初始化为1</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//构造器</span></span><br><span class="line"> <span class="keyword">public</span> <span class="title function_">ArrayStack2</span><span class="params">(<span class="type">int</span> maxSize)</span> {</span><br><span class="line"> <span class="built_in">this</span>.maxSize = maxSize;</span><br><span class="line"> <span class="comment">//完成数组的初始化</span></span><br><span class="line"> stack = <span class="keyword">new</span> <span class="title class_">int</span>[maxSize];</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断是否栈满</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isFull</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> top == maxSize - <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断栈是否为空</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isEmpty</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> top == -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 入栈的操作</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">push</span><span class="params">(<span class="type">int</span> value)</span> {</span><br><span class="line"> <span class="comment">//先判断栈是否为满</span></span><br><span class="line"> <span class="keyword">if</span> (isFull()) {</span><br><span class="line"> System.out.println(<span class="string">"栈满,无法添加数据!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> top++;</span><br><span class="line"> stack[top] = value;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 出栈的操作</span></span><br><span class="line"><span class="comment"> * 将栈顶的数据返回</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">pop</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//先判断栈是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (isEmpty()) {</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"栈空,无法取出数据!"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//取出数据</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">value</span> <span class="operator">=</span> stack[top];</span><br><span class="line"> top--;</span><br><span class="line"> <span class="keyword">return</span> value;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 显示栈的情况</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">list</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">//先判断栈是否为空</span></span><br><span class="line"> <span class="keyword">if</span> (isEmpty()) {</span><br><span class="line"> System.out.println(<span class="string">"栈空,无法取出数据!"</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> top; i >= <span class="number">0</span>; i--) {</span><br><span class="line"> System.out.printf(<span class="string">"stack[%d] = %d \n"</span>, i, stack[i]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 返回运算符的优先级</span></span><br><span class="line"><span class="comment"> * 优先级由数字表示</span></span><br><span class="line"><span class="comment"> * 数字大 优先级高</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">priority</span><span class="params">(<span class="type">int</span> oper)</span> {</span><br><span class="line"> <span class="keyword">if</span> (oper == <span class="string">'*'</span> || oper == <span class="string">'/'</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (oper == <span class="string">'+'</span> || oper == <span class="string">'-'</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断是不是一个运算符</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isOper</span><span class="params">(<span class="type">char</span> val)</span> {</span><br><span class="line"> <span class="keyword">return</span> val == <span class="string">'+'</span> || val == <span class="string">'-'</span> || val == <span class="string">'*'</span> || val == <span class="string">'/'</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 计算方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">cal</span><span class="params">(<span class="type">int</span> num1, <span class="type">int</span> num2, <span class="type">int</span> oper)</span> {</span><br><span class="line"> <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">switch</span> (oper) {</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'+'</span>:</span><br><span class="line"> result = num1 + num2;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'-'</span>:</span><br><span class="line"> result = num2 - num1; <span class="comment">//注意顺序</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'*'</span>:</span><br><span class="line"> result = num1 * num2;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'/'</span>:</span><br><span class="line"> result = num2 / num1;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 查看栈顶数据的方法</span></span><br><span class="line"><span class="comment"> * 返回栈顶的值 不是pop出来</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">peek</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> stack[top];</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="6-逆波兰计算器的设计与实现"><a href="#6-逆波兰计算器的设计与实现" class="headerlink" title="6.逆波兰计算器的设计与实现"></a>6.逆波兰计算器的设计与实现</h3><p><strong>逆波兰表达式(后缀表达式)</strong></p>
|
||
<p>1)<strong>输入一个逆波兰表达式(后缀表达式),使用栈(Stack),计算其结果</strong></p>
|
||
<p>2)<strong>支持小括号和多位数整数,因为这里我们主要讲的是数据结构,因此计算器进行简化,只支持对整数的计算。</strong></p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.stack;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Stack;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 逆波兰计算器的设计与实现</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PolandNotation</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//先定义一个逆波兰表达式</span></span><br><span class="line"> <span class="comment">//(3+4)×5-6 的逆波兰表达式是 3 4 + 5 × 6 -</span></span><br><span class="line"> <span class="comment">//为了方便 逆波兰表达式的数字和符号有空格隔开</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">suffixExpression</span> <span class="operator">=</span> <span class="string">"3 4 + 5 * 6 -"</span>;<span class="comment">//中间有空格隔开</span></span><br><span class="line"> <span class="comment">//1.先将suffixExpression放入到List集合中</span></span><br><span class="line"> <span class="comment">//2.将List集合传递给一个方法 配合栈 完成计算</span></span><br><span class="line"> List<String> rpnList = getListString(suffixExpression);</span><br><span class="line"> System.out.println(<span class="string">"计算的结果是:"</span>+calculate(rpnList));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//将一个逆波兰表达式依次将数据和运算符放入到List集合中</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> List<String> <span class="title function_">getListString</span><span class="params">(String suffixExpression)</span> {</span><br><span class="line"> <span class="comment">//将suffixExpression按照空格分割</span></span><br><span class="line"> String[] strings = suffixExpression.split(<span class="string">" "</span>);</span><br><span class="line"> List<String> list = <span class="keyword">new</span> <span class="title class_">ArrayList</span><>();</span><br><span class="line"> <span class="keyword">for</span> (String string : strings) {</span><br><span class="line"> list.add(string);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> list;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 从左至右扫描,将3和4压入堆栈;</span></span><br><span class="line"><span class="comment"> * 遇到+运算符,因此弹出4和3(4为栈顶元素,3为次顶元素),计算出3+4的值,得7,再将7入栈;</span></span><br><span class="line"><span class="comment"> * 将5入栈;</span></span><br><span class="line"><span class="comment"> * 接下来是×运算符,因此弹出5和7,计算出7×5=35,将35入栈;</span></span><br><span class="line"><span class="comment"> * 将6入栈;</span></span><br><span class="line"><span class="comment"> * 最后是-运算符,计算出35-6的值,即29,由此得出最终结果</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">calculate</span><span class="params">(List<String> list)</span> {</span><br><span class="line"> <span class="comment">//创建一个栈</span></span><br><span class="line"> Stack<String> stack = <span class="keyword">new</span> <span class="title class_">Stack</span><>();</span><br><span class="line"> <span class="comment">//遍历list</span></span><br><span class="line"> <span class="keyword">for</span> (String item : list) {</span><br><span class="line"> <span class="comment">//使用正则表达式取出数字</span></span><br><span class="line"> <span class="keyword">if</span> (item.matches(<span class="string">"\\d+"</span>)) { <span class="comment">//匹配的是多位数</span></span><br><span class="line"> <span class="comment">//入栈</span></span><br><span class="line"> stack.push(item);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//弹出两个数 并运算 再入栈</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">num2</span> <span class="operator">=</span> Integer.parseInt(stack.pop());</span><br><span class="line"> <span class="type">int</span> <span class="variable">num1</span> <span class="operator">=</span> Integer.parseInt(stack.pop());</span><br><span class="line"> <span class="comment">//运算</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span> (item.equals(<span class="string">"+"</span>)) {</span><br><span class="line"> res = num1 + num2;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">"-"</span>)) {</span><br><span class="line"> res = num1 - num2;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">"*"</span>)) {</span><br><span class="line"> res = num1 * num2;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">"/"</span>)) {</span><br><span class="line"> res = num1 / num2;</span><br><span class="line"> }<span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"运算符有误!"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//把结果入栈</span></span><br><span class="line"> stack.push(res + <span class="string">""</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//最后留在stack中的数就是运算结果</span></span><br><span class="line"> <span class="keyword">return</span> Integer.parseInt(stack.pop());</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>将中缀表达式转化为后缀表达式(逆波兰表达式)</strong></p>
|
||
<p>解题的步骤:</p>
|
||
<p>1)初始化两个栈:运算符栈s1和储存中间结果的栈s2;</p>
|
||
<p>2)从左至右扫描中缀表达式;</p>
|
||
<p>3)遇到操作数时,将其压s2;</p>
|
||
<p>4)遇到运算符时,比较其与s1栈顶运算符的优先级:</p>
|
||
<p> (1)如果s1为空,或栈顶运算符为左括号“(”,则直接将此运算符入栈;</p>
|
||
<p> (2)否则,若优先级比栈顶运算符的高,也将运算符压入s1;</p>
|
||
<p> (3)否则,将s1栈顶的运算符弹出并压入到s2中,再次转到(4-1)与s1中新的栈顶运算符相比较</p>
|
||
<p>5)遇到括号时:<br> (1) 如果是左括号“(”,则直接压入s1<br> (2) 如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃</p>
|
||
<p>6)重复步骤2至5,直到表达式的最右边</p>
|
||
<p>7)将s1中剩余的运算符依次弹出并压入s2</p>
|
||
<p>8)依次弹出s2中的元素并输出,<strong>结果的逆序即为中缀表达式对应的后缀表达式</strong></p>
|
||
<p><img src="/pictures/image-20230511094011546.png" alt="image-20230511094011546"></p>
|
||
<p>先将中缀表达式转化成list集合</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 将中缀表达式转化成对应的list</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> expression 中缀表达式</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 中缀表达式转化成的一个list集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> List<String> <span class="title function_">toInfixExpressionList</span><span class="params">(String expression)</span> {</span><br><span class="line"> <span class="comment">//定义一个集合,存储中缀表达式对应的list集合</span></span><br><span class="line"> List<String> list = <span class="keyword">new</span> <span class="title class_">ArrayList</span><>();</span><br><span class="line"> <span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>;<span class="comment">//这个是一个指针 用于遍历中缀表达式的字符串</span></span><br><span class="line"> String str;<span class="comment">//做多位数的拼接工作 ,因为我们要考虑多位数的情况</span></span><br><span class="line"> <span class="type">char</span> c;<span class="comment">//每遍历一个字符就放入到c中</span></span><br><span class="line"> <span class="keyword">do</span> {</span><br><span class="line"> <span class="keyword">if</span> ((c = expression.charAt(i)) < <span class="number">48</span> || (c = expression.charAt(i)) > <span class="number">57</span>) { <span class="comment">//如果c是一个非数字的字符需要加入到ls中</span></span><br><span class="line"> list.add(c + <span class="string">""</span>); <span class="comment">//直接将这个字符添加到list集合中</span></span><br><span class="line"> i++;</span><br><span class="line"> } <span class="keyword">else</span> {<span class="comment">//如果是一个数 要考虑多位数的问题</span></span><br><span class="line"> str = <span class="string">""</span>;<span class="comment">//先将str置空</span></span><br><span class="line"> <span class="keyword">while</span> (i < expression.length() && (c = expression.charAt(i)) >= <span class="number">48</span> && (c = expression.charAt(i)) <= <span class="number">57</span>) {</span><br><span class="line"> str += c;</span><br><span class="line"> i++;</span><br><span class="line"> }</span><br><span class="line"> list.add(str);</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">while</span> (i < expression.length());</span><br><span class="line"> <span class="keyword">return</span> list;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>将中缀表达式的集合转化成逆波兰表达式(后缀表达式)的集合</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 将中缀表达式对应的List转化成后缀表达式对应的list</span></span><br><span class="line"><span class="comment"> * tips:</span></span><br><span class="line"><span class="comment"> * 1)初始化两个栈:运算符栈s1和储存中间结果的栈s2;</span></span><br><span class="line"><span class="comment"> * 2)从左至右扫描中缀表达式;</span></span><br><span class="line"><span class="comment"> * 3)遇到操作数时,将其压s2;</span></span><br><span class="line"><span class="comment"> * 4)遇到运算符时,比较其与s1栈顶运算符的优先级:</span></span><br><span class="line"><span class="comment"> * (1)如果s1为空,或栈顶运算符为左括号“(”,则直接将此运算符入栈;</span></span><br><span class="line"><span class="comment"> * (2)否则,若优先级比栈顶运算符的高,也将运算符压入s1;</span></span><br><span class="line"><span class="comment"> * (3)否则,将s1栈顶的运算符弹出并压入到s2中,再次转到(4-1)与s1中新的栈顶运算符相比较</span></span><br><span class="line"><span class="comment"> * 5)遇到括号时:</span></span><br><span class="line"><span class="comment"> * (1) 如果是左括号“(”,则直接压入s1</span></span><br><span class="line"><span class="comment"> * (2) 如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃</span></span><br><span class="line"><span class="comment"> * 6)重复步骤2至5,直到表达式的最右边</span></span><br><span class="line"><span class="comment"> * 7)将s1中剩余的运算符依次弹出并压入s2</span></span><br><span class="line"><span class="comment"> * 8)依次弹出s2中的元素并输出,**结果的逆序即为中缀表达式对应的后缀表达式**</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> ls 中缀表达式对应的list</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 后缀表达式对应的list</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> List<String> <span class="title function_">parseSuffixExpressionList</span><span class="params">(List<String> ls)</span> {</span><br><span class="line"> <span class="comment">//定义栈</span></span><br><span class="line"> Stack<String> s1 = <span class="keyword">new</span> <span class="title class_">Stack</span><>();<span class="comment">//符号栈 s1栈</span></span><br><span class="line"> <span class="comment">//说明:因为思路分析中使用的s2的栈在整个的运算的过程中没有进行pop的操作 我们直接使用list集合代替s2栈 (同时也为了方便后面逆序的输出)</span></span><br><span class="line"> ArrayList<String> s2 = <span class="keyword">new</span> <span class="title class_">ArrayList</span><>();<span class="comment">//用于存储中间结果的list</span></span><br><span class="line"> <span class="comment">//遍历ls</span></span><br><span class="line"> <span class="keyword">for</span> (String item : ls) {</span><br><span class="line"> <span class="comment">//如果是一个数 就加入到s2栈</span></span><br><span class="line"> <span class="keyword">if</span> (item.matches(<span class="string">"\\d+"</span>)) {<span class="comment">//正则匹配</span></span><br><span class="line"> s2.add(item);</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">"("</span>)) { <span class="comment">//如果是s1的话就直接入符号栈</span></span><br><span class="line"> s1.push(item);</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">")"</span>)) {</span><br><span class="line"> <span class="comment">//如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃</span></span><br><span class="line"> <span class="keyword">while</span> (!s1.peek().equals(<span class="string">"("</span>)) {</span><br><span class="line"> s2.add(s1.pop());</span><br><span class="line"> }</span><br><span class="line"> s1.pop();<span class="comment">//将(弹出s1栈,这里的操作就是消除括号</span></span><br><span class="line"> } <span class="keyword">else</span> {<span class="comment">//加减乘除的操作</span></span><br><span class="line"> <span class="comment">//当item的优先级小于栈顶的优先级,</span></span><br><span class="line"> <span class="comment">// 将s1栈顶的运算符弹出并加入到s2中,再次转到(4.1)与s1中新的栈顶运算符相比较</span></span><br><span class="line"> <span class="keyword">while</span> (s1.size() != <span class="number">0</span> && getVal(s1.peek()) >= getVal(item)) {</span><br><span class="line"> s2.add(s1.pop());</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//还需要将item压入栈中</span></span><br><span class="line"> s1.push(item);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//将s1中剩余的运算符依次弹出并加入s2</span></span><br><span class="line"> <span class="keyword">while</span> (s1.size() != <span class="number">0</span>) {</span><br><span class="line"> s2.add(s1.pop());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> s2;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>通过逆波兰表达式(后缀表达式)的集合得到最终的计算结果</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 通过一个后缀表达式的list集合得到表达式最终的计算结果</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> list 后缀表达式的list集合</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 根据后缀表达式的集合计算出的表达式的结果信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">calculate</span><span class="params">(List<String> list)</span> {</span><br><span class="line"> <span class="comment">//创建一个栈</span></span><br><span class="line"> Stack<String> stack = <span class="keyword">new</span> <span class="title class_">Stack</span><>();</span><br><span class="line"> <span class="comment">//遍历list</span></span><br><span class="line"> <span class="keyword">for</span> (String item : list) {</span><br><span class="line"> <span class="comment">//使用正则表达式取出数字</span></span><br><span class="line"> <span class="keyword">if</span> (item.matches(<span class="string">"\\d+"</span>)) { <span class="comment">//匹配的是多位数</span></span><br><span class="line"> <span class="comment">//入栈</span></span><br><span class="line"> stack.push(item);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//弹出两个数 并运算 再入栈</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">num2</span> <span class="operator">=</span> Integer.parseInt(stack.pop());</span><br><span class="line"> <span class="type">int</span> <span class="variable">num1</span> <span class="operator">=</span> Integer.parseInt(stack.pop());</span><br><span class="line"> <span class="comment">//运算</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span> (item.equals(<span class="string">"+"</span>)) {</span><br><span class="line"> res = num1 + num2;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">"-"</span>)) {</span><br><span class="line"> res = num1 - num2;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">"*"</span>)) {</span><br><span class="line"> res = num1 * num2;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">"/"</span>)) {</span><br><span class="line"> res = num1 / num2;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"运算符有误!"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//把结果入栈</span></span><br><span class="line"> stack.push(res + <span class="string">""</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//最后留在stack中的数就是运算结果</span></span><br><span class="line"> <span class="keyword">return</span> Integer.parseInt(stack.pop());</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>完整的代码</strong></p>
|
||
<p>(不支持小数)</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.stack;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Stack;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/11</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 逆波兰计算器的设计与实现</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PolandNotation</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//先定义一个逆波兰表达式</span></span><br><span class="line"> <span class="comment">//(3+4)×5-6 的逆波兰表达式是 3 4 + 5 × 6 -</span></span><br><span class="line"> <span class="comment">//为了方便 逆波兰表达式的数字和符号有空格隔开</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">suffixExpression</span> <span class="operator">=</span> <span class="string">"3 4 + 5 * 6 -"</span>;<span class="comment">//中间有空格隔开</span></span><br><span class="line"> <span class="comment">//1.先将suffixExpression放入到List集合中</span></span><br><span class="line"> <span class="comment">//2.将List集合传递给一个方法 配合栈 完成计算</span></span><br><span class="line"> List<String> rpnList = getListString(suffixExpression);</span><br><span class="line"> System.out.println(<span class="string">"计算的结果是:"</span> + calculate(rpnList));</span><br><span class="line"></span><br><span class="line"> <span class="comment">//测试将中缀表达式转化成逆波兰表达式</span></span><br><span class="line"> <span class="comment">//完成一个将中缀表达式 转换为一个后缀表达式</span></span><br><span class="line"> <span class="comment">//1. 1+((2+3)×4)-5(中缀表达式) -> 1 2 3 + 4 × + 5 –(后缀表达式)</span></span><br><span class="line"> <span class="comment">//2. 将中缀表达式转化成List集合</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">expression</span> <span class="operator">=</span> <span class="string">"1+((2+3)*4)-5"</span>;</span><br><span class="line"> List<String> list = toInfixExpressionList(expression);</span><br><span class="line"> System.out.println(<span class="string">"中缀表达式对应的List:"</span> + list);</span><br><span class="line"> <span class="comment">//3.将得到的中缀表达式对应的List 转换成一个逆波兰表达式(后缀表达式)对应的List</span></span><br><span class="line"> List<String> stringList = parseSuffixExpressionList(list);</span><br><span class="line"> System.out.println(<span class="string">"中缀表达式对应的List:"</span> + stringList);</span><br><span class="line"> System.out.println(<span class="string">"计算的结果:"</span> + calculate(stringList));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 输入一个运算符 返回对应的优先级</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">getVal</span><span class="params">(String operation)</span> {</span><br><span class="line"> <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">switch</span> (operation) {</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"+"</span>:</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"-"</span>:</span><br><span class="line"> res = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"/"</span>:</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"*"</span>:</span><br><span class="line"> res = <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">default</span>:</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 将中缀表达式对应的List转化成后缀表达式对应的list</span></span><br><span class="line"><span class="comment"> * tips:</span></span><br><span class="line"><span class="comment"> * 1)初始化两个栈:运算符栈s1和储存中间结果的栈s2;</span></span><br><span class="line"><span class="comment"> * 2)从左至右扫描中缀表达式;</span></span><br><span class="line"><span class="comment"> * 3)遇到操作数时,将其压s2;</span></span><br><span class="line"><span class="comment"> * 4)遇到运算符时,比较其与s1栈顶运算符的优先级:</span></span><br><span class="line"><span class="comment"> * (1)如果s1为空,或栈顶运算符为左括号“(”,则直接将此运算符入栈;</span></span><br><span class="line"><span class="comment"> * (2)否则,若优先级比栈顶运算符的高,也将运算符压入s1;</span></span><br><span class="line"><span class="comment"> * (3)否则,将s1栈顶的运算符弹出并压入到s2中,再次转到(4-1)与s1中新的栈顶运算符相比较</span></span><br><span class="line"><span class="comment"> * 5)遇到括号时:</span></span><br><span class="line"><span class="comment"> * (1) 如果是左括号“(”,则直接压入s1</span></span><br><span class="line"><span class="comment"> * (2) 如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃</span></span><br><span class="line"><span class="comment"> * 6)重复步骤2至5,直到表达式的最右边</span></span><br><span class="line"><span class="comment"> * 7)将s1中剩余的运算符依次弹出并压入s2</span></span><br><span class="line"><span class="comment"> * 8)依次弹出s2中的元素并输出,**结果的逆序即为中缀表达式对应的后缀表达式**</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> ls 中缀表达式对应的list</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 后缀表达式对应的list</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> List<String> <span class="title function_">parseSuffixExpressionList</span><span class="params">(List<String> ls)</span> {</span><br><span class="line"> <span class="comment">//定义栈</span></span><br><span class="line"> Stack<String> s1 = <span class="keyword">new</span> <span class="title class_">Stack</span><>();<span class="comment">//符号栈 s1栈</span></span><br><span class="line"> <span class="comment">//说明:因为思路分析中使用的s2的栈在整个的运算的过程中没有进行pop的操作 我们直接使用list集合代替s2栈 (同时也为了方便后面逆序的输出)</span></span><br><span class="line"> ArrayList<String> s2 = <span class="keyword">new</span> <span class="title class_">ArrayList</span><>();<span class="comment">//用于存储中间结果的list</span></span><br><span class="line"> <span class="comment">//遍历ls</span></span><br><span class="line"> <span class="keyword">for</span> (String item : ls) {</span><br><span class="line"> <span class="comment">//如果是一个数 就加入到s2栈</span></span><br><span class="line"> <span class="keyword">if</span> (item.matches(<span class="string">"\\d+"</span>)) {<span class="comment">//正则匹配</span></span><br><span class="line"> s2.add(item);</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">"("</span>)) { <span class="comment">//如果是s1的话就直接入符号栈</span></span><br><span class="line"> s1.push(item);</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">")"</span>)) {</span><br><span class="line"> <span class="comment">//如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃</span></span><br><span class="line"> <span class="keyword">while</span> (!s1.peek().equals(<span class="string">"("</span>)) {</span><br><span class="line"> s2.add(s1.pop());</span><br><span class="line"> }</span><br><span class="line"> s1.pop();<span class="comment">//将(弹出s1栈,这里的操作就是消除括号</span></span><br><span class="line"> } <span class="keyword">else</span> {<span class="comment">//加减乘除的操作</span></span><br><span class="line"> <span class="comment">//当item的优先级小于栈顶的优先级,</span></span><br><span class="line"> <span class="comment">// 将s1栈顶的运算符弹出并加入到s2中,再次转到(4.1)与s1中新的栈顶运算符相比较</span></span><br><span class="line"> <span class="keyword">while</span> (s1.size() != <span class="number">0</span> && getVal(s1.peek()) >= getVal(item)) {</span><br><span class="line"> s2.add(s1.pop());</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//还需要将item压入栈中</span></span><br><span class="line"> s1.push(item);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//将s1中剩余的运算符依次弹出并加入s2</span></span><br><span class="line"> <span class="keyword">while</span> (s1.size() != <span class="number">0</span>) {</span><br><span class="line"> s2.add(s1.pop());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> s2;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 将中缀表达式转化成对应的list</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> expression 中缀表达式</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 中缀表达式转化成的一个list集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> List<String> <span class="title function_">toInfixExpressionList</span><span class="params">(String expression)</span> {</span><br><span class="line"> <span class="comment">//定义一个集合,存储中缀表达式对应的list集合</span></span><br><span class="line"> List<String> list = <span class="keyword">new</span> <span class="title class_">ArrayList</span><>();</span><br><span class="line"> <span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>;<span class="comment">//这个是一个指针 用于遍历中缀表达式的字符串</span></span><br><span class="line"> String str;<span class="comment">//做多位数的拼接工作 ,因为我们要考虑多位数的情况</span></span><br><span class="line"> <span class="type">char</span> c;<span class="comment">//每遍历一个字符就放入到c中</span></span><br><span class="line"> <span class="keyword">do</span> {</span><br><span class="line"> <span class="keyword">if</span> ((c = expression.charAt(i)) < <span class="number">48</span> || (c = expression.charAt(i)) > <span class="number">57</span>) { <span class="comment">//如果c是一个非数字的字符需要加入到ls中</span></span><br><span class="line"> list.add(c + <span class="string">""</span>); <span class="comment">//直接将这个字符添加到list集合中</span></span><br><span class="line"> i++;</span><br><span class="line"> } <span class="keyword">else</span> {<span class="comment">//如果是一个数 要考虑多位数的问题</span></span><br><span class="line"> str = <span class="string">""</span>;<span class="comment">//先将str置空</span></span><br><span class="line"> <span class="comment">//这个只要遍历的有一个字符不是数字 就会退出while循环</span></span><br><span class="line"> <span class="keyword">while</span> (i < expression.length() && (c = expression.charAt(i)) >= <span class="number">48</span> && (c = expression.charAt(i)) <= <span class="number">57</span>) {</span><br><span class="line"> str += c;</span><br><span class="line"> i++;</span><br><span class="line"> }</span><br><span class="line"> list.add(str);</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">while</span> (i < expression.length());</span><br><span class="line"> <span class="keyword">return</span> list;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">//将一个逆波兰表达式依次将数据和运算符放入到List集合中</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> List<String> <span class="title function_">getListString</span><span class="params">(String suffixExpression)</span> {</span><br><span class="line"> <span class="comment">//将suffixExpression按照空格分割</span></span><br><span class="line"> String[] strings = suffixExpression.split(<span class="string">" "</span>);</span><br><span class="line"> List<String> list = <span class="keyword">new</span> <span class="title class_">ArrayList</span><>();</span><br><span class="line"> <span class="keyword">for</span> (String string : strings) {</span><br><span class="line"> list.add(string);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> list;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 通过一个后缀表达式的list集合得到表达式最终的计算结果</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> list 后缀表达式的list集合</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 根据后缀表达式的集合计算出的表达式的结果信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">calculate</span><span class="params">(List<String> list)</span> {</span><br><span class="line"> <span class="comment">//创建一个栈</span></span><br><span class="line"> Stack<String> stack = <span class="keyword">new</span> <span class="title class_">Stack</span><>();</span><br><span class="line"> <span class="comment">//遍历list</span></span><br><span class="line"> <span class="keyword">for</span> (String item : list) {</span><br><span class="line"> <span class="comment">//使用正则表达式取出数字</span></span><br><span class="line"> <span class="keyword">if</span> (item.matches(<span class="string">"\\d+"</span>)) { <span class="comment">//匹配的是多位数</span></span><br><span class="line"> <span class="comment">//入栈</span></span><br><span class="line"> stack.push(item);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//弹出两个数 并运算 再入栈</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">num2</span> <span class="operator">=</span> Integer.parseInt(stack.pop());</span><br><span class="line"> <span class="type">int</span> <span class="variable">num1</span> <span class="operator">=</span> Integer.parseInt(stack.pop());</span><br><span class="line"> <span class="comment">//运算</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span> (item.equals(<span class="string">"+"</span>)) {</span><br><span class="line"> res = num1 + num2;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">"-"</span>)) {</span><br><span class="line"> res = num1 - num2;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">"*"</span>)) {</span><br><span class="line"> res = num1 * num2;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (item.equals(<span class="string">"/"</span>)) {</span><br><span class="line"> res = num1 / num2;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"运算符有误!"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//把结果入栈</span></span><br><span class="line"> stack.push(res + <span class="string">""</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//最后留在stack中的数就是运算结果</span></span><br><span class="line"> <span class="keyword">return</span> Integer.parseInt(stack.pop());</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>完整的逆波兰计算器,含小数点的计算</strong></p>
|
||
<p>(老师的代码)</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.stack;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Collections;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Stack;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ReversePolishMultiCalc</span> {</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 匹配 + - * / ( ) 运算符</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">SYMBOL</span> <span class="operator">=</span> <span class="string">"\\+|-|\\*|/|\\(|\\)"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">LEFT</span> <span class="operator">=</span> <span class="string">"("</span>;</span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">RIGHT</span> <span class="operator">=</span> <span class="string">")"</span>;</span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">ADD</span> <span class="operator">=</span> <span class="string">"+"</span>;</span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">final</span> String MINUS= <span class="string">"-"</span>;</span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">TIMES</span> <span class="operator">=</span> <span class="string">"*"</span>;</span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">DIVISION</span> <span class="operator">=</span> <span class="string">"/"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 加減 + -</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">LEVEL_01</span> <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 乘除 * /</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">LEVEL_02</span> <span class="operator">=</span> <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 括号</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">LEVEL_HIGH</span> <span class="operator">=</span> Integer.MAX_VALUE;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="keyword">static</span> Stack<String> stack = <span class="keyword">new</span> <span class="title class_">Stack</span><>();</span><br><span class="line"> <span class="keyword">static</span> List<String> data = Collections.synchronizedList(<span class="keyword">new</span> <span class="title class_">ArrayList</span><String>());</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 去除所有空白符</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> s</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">replaceAllBlank</span><span class="params">(String s )</span>{</span><br><span class="line"> <span class="comment">// \\s+ 匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v]</span></span><br><span class="line"> <span class="keyword">return</span> s.replaceAll(<span class="string">"\\s+"</span>,<span class="string">""</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断是不是数字 int double long float</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> s</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">boolean</span> <span class="title function_">isNumber</span><span class="params">(String s)</span>{</span><br><span class="line"> <span class="type">Pattern</span> <span class="variable">pattern</span> <span class="operator">=</span> Pattern.compile(<span class="string">"^[-\\+]?[.\\d]*$"</span>);</span><br><span class="line"> <span class="keyword">return</span> pattern.matcher(s).matches();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断是不是运算符</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> s</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">boolean</span> <span class="title function_">isSymbol</span><span class="params">(String s)</span>{</span><br><span class="line"> <span class="keyword">return</span> s.matches(SYMBOL);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 匹配运算等级</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> s</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">calcLevel</span><span class="params">(String s)</span>{</span><br><span class="line"> <span class="keyword">if</span>(<span class="string">"+"</span>.equals(s) || <span class="string">"-"</span>.equals(s)){</span><br><span class="line"> <span class="keyword">return</span> LEVEL_01;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span>(<span class="string">"*"</span>.equals(s) || <span class="string">"/"</span>.equals(s)){</span><br><span class="line"> <span class="keyword">return</span> LEVEL_02;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> LEVEL_HIGH;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 匹配</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> s</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> List<String> <span class="title function_">doMatch</span> <span class="params">(String s)</span> <span class="keyword">throws</span> Exception{</span><br><span class="line"> <span class="keyword">if</span>(s == <span class="literal">null</span> || <span class="string">""</span>.equals(s.trim())) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"data is empty"</span>);</span><br><span class="line"> <span class="keyword">if</span>(!isNumber(s.charAt(<span class="number">0</span>)+<span class="string">""</span>)) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"data illeagle,start not with a number"</span>);</span><br><span class="line"></span><br><span class="line"> s = replaceAllBlank(s);</span><br><span class="line"></span><br><span class="line"> String each;</span><br><span class="line"> <span class="type">int</span> <span class="variable">start</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < s.length(); i++) {</span><br><span class="line"> <span class="keyword">if</span>(isSymbol(s.charAt(i)+<span class="string">""</span>)){</span><br><span class="line"> each = s.charAt(i)+<span class="string">""</span>;</span><br><span class="line"> <span class="comment">//栈为空,(操作符,或者 操作符优先级大于栈顶优先级 && 操作符优先级不是( )的优先级 及是 ) 不能直接入栈</span></span><br><span class="line"> <span class="keyword">if</span>(stack.isEmpty() || LEFT.equals(each)</span><br><span class="line"> || ((calcLevel(each) > calcLevel(stack.peek())) && calcLevel(each) < LEVEL_HIGH)){</span><br><span class="line"> stack.push(each);</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>( !stack.isEmpty() && calcLevel(each) <= calcLevel(stack.peek())){</span><br><span class="line"> <span class="comment">//栈非空,操作符优先级小于等于栈顶优先级时出栈入列,直到栈为空,或者遇到了(,最后操作符入栈</span></span><br><span class="line"> <span class="keyword">while</span> (!stack.isEmpty() && calcLevel(each) <= calcLevel(stack.peek()) ){</span><br><span class="line"> <span class="keyword">if</span>(calcLevel(stack.peek()) == LEVEL_HIGH){</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> data.add(stack.pop());</span><br><span class="line"> }</span><br><span class="line"> stack.push(each);</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(RIGHT.equals(each)){</span><br><span class="line"> <span class="comment">// ) 操作符,依次出栈入列直到空栈或者遇到了第一个)操作符,此时)出栈</span></span><br><span class="line"> <span class="keyword">while</span> (!stack.isEmpty() && LEVEL_HIGH >= calcLevel(stack.peek())){</span><br><span class="line"> <span class="keyword">if</span>(LEVEL_HIGH == calcLevel(stack.peek())){</span><br><span class="line"> stack.pop();</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> data.add(stack.pop());</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> start = i ; <span class="comment">//前一个运算符的位置</span></span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>( i == s.length()-<span class="number">1</span> || isSymbol(s.charAt(i+<span class="number">1</span>)+<span class="string">""</span>) ){</span><br><span class="line"> each = start == <span class="number">0</span> ? s.substring(start,i+<span class="number">1</span>) : s.substring(start+<span class="number">1</span>,i+<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span>(isNumber(each)) {</span><br><span class="line"> data.add(each);</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"data not match number"</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//如果栈里还有元素,此时元素需要依次出栈入列,可以想象栈里剩下栈顶为/,栈底为+,应该依次出栈入列,可以直接翻转整个stack 添加到队列</span></span><br><span class="line"> Collections.reverse(stack);</span><br><span class="line"> data.addAll(<span class="keyword">new</span> <span class="title class_">ArrayList</span><>(stack));</span><br><span class="line"></span><br><span class="line"> System.out.println(data);</span><br><span class="line"> <span class="keyword">return</span> data;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 算出结果</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> list</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> Double <span class="title function_">doCalc</span><span class="params">(List<String> list)</span>{</span><br><span class="line"> <span class="type">Double</span> <span class="variable">d</span> <span class="operator">=</span> <span class="number">0d</span>;</span><br><span class="line"> <span class="keyword">if</span>(list == <span class="literal">null</span> || list.isEmpty()){</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (list.size() == <span class="number">1</span>){</span><br><span class="line"> System.out.println(list);</span><br><span class="line"> d = Double.valueOf(list.get(<span class="number">0</span>));</span><br><span class="line"> <span class="keyword">return</span> d;</span><br><span class="line"> }</span><br><span class="line"> ArrayList<String> list1 = <span class="keyword">new</span> <span class="title class_">ArrayList</span><>();</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < list.size(); i++) {</span><br><span class="line"> list1.add(list.get(i));</span><br><span class="line"> <span class="keyword">if</span>(isSymbol(list.get(i))){</span><br><span class="line"> <span class="type">Double</span> <span class="variable">d1</span> <span class="operator">=</span> doTheMath(list.get(i - <span class="number">2</span>), list.get(i - <span class="number">1</span>), list.get(i));</span><br><span class="line"> list1.remove(i);</span><br><span class="line"> list1.remove(i-<span class="number">1</span>);</span><br><span class="line"> list1.set(i-<span class="number">2</span>,d1+<span class="string">""</span>);</span><br><span class="line"> list1.addAll(list.subList(i+<span class="number">1</span>,list.size()));</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> doCalc(list1);</span><br><span class="line"> <span class="keyword">return</span> d;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 运算</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> s1</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> s2</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> symbol</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> Double <span class="title function_">doTheMath</span><span class="params">(String s1,String s2,String symbol)</span>{</span><br><span class="line"> Double result ;</span><br><span class="line"> <span class="keyword">switch</span> (symbol){</span><br><span class="line"> <span class="keyword">case</span> ADD : result = Double.valueOf(s1) + Double.valueOf(s2); <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> MINUS : result = Double.valueOf(s1) - Double.valueOf(s2); <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> TIMES : result = Double.valueOf(s1) * Double.valueOf(s2); <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> DIVISION : result = Double.valueOf(s1) / Double.valueOf(s2); <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">default</span> : result = <span class="literal">null</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//String math = "9+(3-1)*3+10/2";</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">math</span> <span class="operator">=</span> <span class="string">"12.8 + (2 - 3.55)*4+10/5.0"</span>;</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> doCalc(doMatch(math));</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h2 id="六-递归"><a href="#六-递归" class="headerlink" title="六.递归"></a>六.递归</h2><h3 id="1-简单介绍"><a href="#1-简单介绍" class="headerlink" title="1.简单介绍"></a>1.简单介绍</h3><p>递归就是方法自己调用自己,每次调用时传入不同的变量,<strong>递归有助于编程者解决复杂的问题</strong>,同时可以让代码变得简洁</p>
|
||
<p><img src="/pictures/image-20230511123527496.png" alt="image-20230511123527496"></p>
|
||
<h3 id="2-入门案例"><a href="#2-入门案例" class="headerlink" title="2.入门案例"></a>2.入门案例</h3><p><img src="/pictures/image-20230511123245408.png" alt="image-20230511123245408"></p>
|
||
<h3 id="3-递归解决的问题"><a href="#3-递归解决的问题" class="headerlink" title="3.递归解决的问题"></a>3.递归解决的问题</h3><p>1)各种数学问题如: 8皇后问题 , 汉诺塔, 阶乘问题, 迷宫问题, 球和篮子的问题(google编程大赛)</p>
|
||
<p>2)各种算法中也会使用到递归,比如快排,归并排序,二分查找,分治算法等.</p>
|
||
<p>3)将用栈解决的问题–>第归代码比较简洁</p>
|
||
<h3 id="4-递归遵循的规则"><a href="#4-递归遵循的规则" class="headerlink" title="4.递归遵循的规则"></a>4.递归遵循的规则</h3><p>1)执行一个方法时,就创建一个新的受保护的独立空间(栈空间)</p>
|
||
<p>2)方法的局部变量是独立的,不会相互影响, 比如n变量</p>
|
||
<p>3)如果方法中使用的是引用类型变量(比如数组),就会共享该引用类型的数据.</p>
|
||
<p>4)递归必须向退出递归的条件逼近,否则就是无限递归,出现StackOverflowError,死龟了:)</p>
|
||
<p>5)当一个方法执行完毕,或者遇到return,就会返回,遵守谁调用,就将结果返回给谁,同时当方法执行完毕或者返回时,该方法也就执行完毕。</p>
|
||
<h3 id="5-递归的实际应用"><a href="#5-递归的实际应用" class="headerlink" title="5.递归的实际应用"></a>5.递归的实际应用</h3><h4 id="5-1递归解决迷宫问题"><a href="#5-1递归解决迷宫问题" class="headerlink" title="5.1递归解决迷宫问题"></a>5.1递归解决迷宫问题</h4><p><img src="/pictures/image-20230512092144712.png" alt="image-20230512092144712"></p>
|
||
<p><strong>代码实现</strong></p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.recursion;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/12</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 递归解决迷宫问题</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MiGong</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//创建一个二维数组模拟迷宫</span></span><br><span class="line"> <span class="comment">//地图</span></span><br><span class="line"> <span class="type">int</span>[][] map = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">8</span>][<span class="number">7</span>];</span><br><span class="line"> <span class="comment">//使用1表示墙的位置</span></span><br><span class="line"> <span class="comment">//把四周变成墙</span></span><br><span class="line"> <span class="comment">//将上下变成墙</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < <span class="number">7</span>; i++) {</span><br><span class="line"> map[<span class="number">0</span>][i] = <span class="number">1</span>;</span><br><span class="line"> map[<span class="number">7</span>][i] = <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//将左右变成墙</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < <span class="number">8</span>; i++) {</span><br><span class="line"> map[i][<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"> map[i][<span class="number">6</span>] = <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//设置自定义的墙的位置</span></span><br><span class="line"> map[<span class="number">3</span>][<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"> map[<span class="number">3</span>][<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">//输出地图</span></span><br><span class="line"> showMap(map);</span><br><span class="line"> <span class="comment">//使用递归回溯给小球找路</span></span><br><span class="line"> getWay(map, <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line"> System.out.println(<span class="string">"标识过的路"</span>);</span><br><span class="line"> showMap(map);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 遍历输出地图的信息</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> map 地图组成的二维数组</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">showMap</span><span class="params">(<span class="type">int</span>[][] map)</span> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < map.length; i++) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j < map[i].length; j++) {</span><br><span class="line"> System.out.print(map[i][j] + <span class="string">" "</span>);</span><br><span class="line"> }</span><br><span class="line"> System.out.println();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 1.表示墙 2.表示通路 可以走 3.表示该位置已经走过 但是走不通</span></span><br><span class="line"><span class="comment"> * 在就走迷宫的时候我们要确定一个策略 比如下->右->上->左 如果该点走不通 再回溯</span></span><br><span class="line"><span class="comment"> * 使用递归的方法给小球找路</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> map 传进来的地图信息</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> i 从哪个位置开始找</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> j 从哪个位置开始找</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 是否找到路</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">boolean</span> <span class="title function_">getWay</span><span class="params">(<span class="type">int</span>[][] map, <span class="type">int</span> i, <span class="type">int</span> j)</span> {</span><br><span class="line"> <span class="keyword">if</span> (map[<span class="number">6</span>][<span class="number">5</span>] == <span class="number">2</span>) { <span class="comment">//说明通路已经找到了 就直接退出</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">if</span> (map[i][j] == <span class="number">0</span>) {<span class="comment">//如果当前这个点还没有走过</span></span><br><span class="line"> <span class="comment">//按照策略走 下->右->上->左</span></span><br><span class="line"> map[i][j] = <span class="number">2</span>;<span class="comment">//先假定这个点可以走通</span></span><br><span class="line"> <span class="keyword">if</span> (getWay(map, i + <span class="number">1</span>, j)) { <span class="comment">//向下走</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (getWay(map, i, j + <span class="number">1</span>)) {<span class="comment">//向右走</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (getWay(map, i - <span class="number">1</span>, j)) {<span class="comment">//向上走</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (getWay(map, i, j - <span class="number">1</span>)) {<span class="comment">//向左走</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> } <span class="keyword">else</span> {<span class="comment">//四个方向都走不通的话说明不是一个通路 设置成false</span></span><br><span class="line"> map[i][j] = <span class="number">3</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">else</span> {<span class="comment">//不为0 可以是1,2,3</span></span><br><span class="line"> <span class="comment">//可以理解为只走没有走过的</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
|
||
|
||
<h4 id="5-2-递归解决八皇后问题"><a href="#5-2-递归解决八皇后问题" class="headerlink" title="5.2 递归解决八皇后问题"></a>5.2 递归解决八皇后问题</h4><p>使用回溯算法解决 类似于穷举法 后期使用别的算法优化</p>
|
||
<p><strong>问题介绍</strong></p>
|
||
<p><img src="/pictures/image-20230512112050729.png" alt="image-20230512112050729"></p>
|
||
<p><strong>思路分析</strong></p>
|
||
<p><img src="/pictures/image-20230512112456731.png" alt="image-20230512112456731"></p>
|
||
<p><strong>代码实现</strong></p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.recursion;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/13</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 八皇后问题的解题思路及代码实现</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Queue8</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">Queue8</span> <span class="variable">queue8</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Queue8</span>();</span><br><span class="line"> queue8.check(<span class="number">0</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//定义一个max表示共有多少个皇后</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">max</span> <span class="operator">=</span> <span class="number">8</span>;</span><br><span class="line"> <span class="comment">//定义一个一维数组 记录皇后在列上的位置 arr[8] = {0 , 4, 7, 5, 2, 6, 1, 3}</span></span><br><span class="line"> <span class="type">int</span>[] array = <span class="keyword">new</span> <span class="title class_">int</span>[max];</span><br><span class="line"> <span class="comment">//记录摆法的次数</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 编写一个方法 放置n个皇后</span></span><br><span class="line"><span class="comment"> * 会自己回溯</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">check</span><span class="params">(<span class="type">int</span> n)</span> {</span><br><span class="line"> <span class="keyword">if</span> (n == max) { <span class="comment">//说明已经放到了第九个皇后了</span></span><br><span class="line"> print(); <span class="comment">//打印</span></span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//依次放入皇后 判断是否冲突</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < max; i++) {</span><br><span class="line"> <span class="comment">//先把当前的这个皇后n,放到该行的第i列</span></span><br><span class="line"> array[n] = i;</span><br><span class="line"> <span class="comment">//判断当前放置第n个皇后到i列时,与前面放置的皇后是否冲突</span></span><br><span class="line"> <span class="keyword">if</span> (judge(n)) { <span class="comment">//不冲突</span></span><br><span class="line"> <span class="comment">//接着放n+1个皇后</span></span><br><span class="line"> check(n + <span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//如果冲突 就继续执行 array[n] = i 就皇后在本行后移一个位置</span></span><br><span class="line"> <span class="comment">//因为在循环里面 i++会自增 n会后移</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 当我们放置了n个皇后之后 就去检测</span></span><br><span class="line"><span class="comment"> * 该皇后是否和前面已经摆放的皇后冲突</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> n 表示第n个皇后</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">judge</span><span class="params">(<span class="type">int</span> n)</span> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < n; i++) {</span><br><span class="line"> <span class="comment">// array[8] = {0 , 4, 7, 5, 2, 6, 1, 3} 再次注意这里数组记录的是每个皇后在列上的值</span></span><br><span class="line"> <span class="comment">//array[i] == array[i] 判断是不是在同一列</span></span><br><span class="line"> <span class="comment">//Math.abs(n - i) == Math.abs(array[n] - array[i]) 判断是不是在同一斜线上</span></span><br><span class="line"> <span class="comment">//自己的理解:(n -i) 相当于在行上的距离 array[n] - array[i]相当于在列上的距离 行距离等于列距离 说明二者在同一斜线上</span></span><br><span class="line"> <span class="keyword">if</span> (array[i] == array[n] || Math.abs(n - i) == Math.abs(array[n] - array[i])) {<span class="comment">//与前面放置的位置是否冲突</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义一个数组 打印皇后拜访的位置</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">print</span><span class="params">()</span> {</span><br><span class="line"> System.out.println(<span class="string">"第"</span> + (++count) + <span class="string">"中摆法"</span>);</span><br><span class="line"> <span class="type">int</span>[][] arr = <span class="keyword">new</span> <span class="title class_">int</span>[max][max];</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < array.length; i++) {</span><br><span class="line"> arr[i][array[i]] = <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span>[] ints : arr) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i : ints) {</span><br><span class="line"> System.out.print(i + <span class="string">" "</span>);</span><br><span class="line"> }</span><br><span class="line"> System.out.println();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
|
||
|
||
<h2 id="七-排序算法"><a href="#七-排序算法" class="headerlink" title="七.排序算法"></a>七.排序算法</h2><p><strong>十大经典的排序算法:</strong><a target="_blank" rel="noopener" href="https://www.runoob.com/w3cnote/ten-sorting-algorithm.html">菜鸟教程排序算法</a></p>
|
||
<h3 id="1-介绍-1"><a href="#1-介绍-1" class="headerlink" title="1.介绍"></a>1.介绍</h3><p><img src="/pictures/image-20230513115319799.png" alt="image-20230513115319799"></p>
|
||
<h3 id="2-时间复杂度"><a href="#2-时间复杂度" class="headerlink" title="2.时间复杂度"></a>2.时间复杂度</h3><p><strong>度量时间复杂度的两种方法</strong></p>
|
||
<p>事后统计法的不足:</p>
|
||
<ul>
|
||
<li>需要实际的运行程序 比较耗时</li>
|
||
<li>受计算机硬件和软件的影响</li>
|
||
</ul>
|
||
<p><img src="/pictures/image-20230513115606521.png" alt="image-20230513115606521"></p>
|
||
<p><strong>时间频度</strong></p>
|
||
<p> 基本的介绍:一个算法花费的时间与算法中语句的执行次数成正比例,哪个算法中语句执行次数多,它花费时间就多。<strong>一个算法中的语句执行次数称为语句频度或时间频度</strong>。记为T(n)</p>
|
||
<p><strong>时间复杂度介绍</strong></p>
|
||
<p><img src="/pictures/image-20230515093520587.png" alt="image-20230515093520587"></p>
|
||
<p><strong>常见的时间复杂度</strong></p>
|
||
<p><img src="/pictures/image-20230515093753893.png" alt="image-20230515093753893"></p>
|
||
<h3 id="3-空间复杂度"><a href="#3-空间复杂度" class="headerlink" title="3.空间复杂度"></a>3.空间复杂度</h3><p><img src="/pictures/image-20230515095247319.png" alt="image-20230515095247319"></p>
|
||
<p><strong>排序算法的时间和空间复杂度</strong></p>
|
||
<p><img src="/pictures/image-20230516083738747.png" alt="image-20230516083738747"></p>
|
||
<h3 id="4-冒泡排序"><a href="#4-冒泡排序" class="headerlink" title="4.冒泡排序"></a>4.冒泡排序</h3><p>冒泡排序的时间复杂度:o(n^2)</p>
|
||
<p>同一台电脑 8万个数据 十几秒左右</p>
|
||
<h4 id="4-1-基本介绍"><a href="#4-1-基本介绍" class="headerlink" title="4.1 基本介绍"></a>4.1 基本介绍</h4><p><img src="/pictures/image-20230515100345339.png" alt="image-20230515100345339"></p>
|
||
<h4 id="4-2-图解"><a href="#4-2-图解" class="headerlink" title="4.2 图解"></a>4.2 图解</h4><p><img src="/pictures/image-20230515100754827.png" alt="image-20230515100754827"></p>
|
||
<h4 id="4-3-代码实现"><a href="#4-3-代码实现" class="headerlink" title="4.3 代码实现"></a>4.3 代码实现</h4><p>优化之前的代码 </p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.sort;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/15</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 冒泡排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BubbleSort</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">int</span> arr[] = {<span class="number">3</span>, <span class="number">9</span>, -<span class="number">1</span>, <span class="number">10</span>, -<span class="number">2</span>};</span><br><span class="line"> BubbleSort.sort(arr);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">sort</span><span class="params">(<span class="type">int</span>[] arr)</span> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < arr.length - <span class="number">1</span>; i++) {</span><br><span class="line"> <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> <span class="number">0</span>; <span class="comment">//临时变量</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j < arr.length - <span class="number">1</span> - i; j++) {</span><br><span class="line"> <span class="comment">//如果前面的数字大于后面的数字就交换</span></span><br><span class="line"> <span class="keyword">if</span> (arr[j] > arr[j + <span class="number">1</span>]) {</span><br><span class="line"> temp = arr[j];</span><br><span class="line"> arr[j] = arr[j + <span class="number">1</span>];</span><br><span class="line"> arr[j + <span class="number">1</span>] = temp;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"第"</span> + (i + <span class="number">1</span>) + <span class="string">"次排序:"</span> + Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"排序之后的数组:"</span> + Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>优化之后的代码(如果排序的过程中代码有序就不在排序)</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.sort;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/15</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 冒泡排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BubbleSort</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">int</span> arr[] = {<span class="number">3</span>, <span class="number">9</span>, -<span class="number">1</span>, <span class="number">10</span>, -<span class="number">2</span>};</span><br><span class="line"> BubbleSort.sort(arr);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 冒泡排序</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> arr 需要排序的数组</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">sort</span><span class="params">(<span class="type">int</span>[] arr)</span> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < arr.length - <span class="number">1</span>; i++) {</span><br><span class="line"> <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> <span class="number">0</span>; <span class="comment">//临时变量</span></span><br><span class="line"> <span class="type">boolean</span> <span class="variable">flag</span> <span class="operator">=</span> <span class="literal">false</span>; <span class="comment">//表示是否进行过排序</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j < arr.length - <span class="number">1</span> - i; j++) {</span><br><span class="line"> <span class="comment">//如果前面的数字大于后面的数字就交换</span></span><br><span class="line"> <span class="keyword">if</span> (arr[j] > arr[j + <span class="number">1</span>]) {</span><br><span class="line"> flag = <span class="literal">true</span>;</span><br><span class="line"> temp = arr[j];</span><br><span class="line"> arr[j] = arr[j + <span class="number">1</span>];</span><br><span class="line"> arr[j + <span class="number">1</span>] = temp;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"第"</span> + (i + <span class="number">1</span>) + <span class="string">"次排序:"</span> + Arrays.toString(arr));</span><br><span class="line"> <span class="keyword">if</span>(!flag){ <span class="comment">//说明一次都没有交换 说明已经有序</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"排序之后的数组:"</span> + Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
|
||
|
||
<p><img src="/pictures/image-20230516091218716.png" alt="image-20230516091218716"></p>
|
||
<h3 id="5-选择排序"><a href="#5-选择排序" class="headerlink" title="5.选择排序"></a>5.选择排序</h3><h4 id="5-1-基本介绍"><a href="#5-1-基本介绍" class="headerlink" title="5.1 基本介绍"></a>5.1 基本介绍</h4><p>同一台电脑 8万个数据 两秒左右</p>
|
||
<p>选择式排序也属于内部排序法,是从欲排序的数据中,按指定的规则选出某一元素,再依规定交换位置后达到排序的目的</p>
|
||
<p><img src="/pictures/image-20230515105359552.png" alt="image-20230515105359552"></p>
|
||
<h4 id="5-2图解"><a href="#5-2图解" class="headerlink" title="5.2图解"></a>5.2图解</h4><p><img src="/pictures/selectionSort.gif" alt="img"></p>
|
||
<p><img src="/pictures/image-20230516083148055.png" alt="image-20230516083148055"></p>
|
||
<h4 id="5-3代码实现"><a href="#5-3代码实现" class="headerlink" title="5.3代码实现"></a>5.3代码实现</h4><p>两种方法 一个是自己的 一个是老师的</p>
|
||
<p>使用老师的代码 老师的代码验证过</p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.sort;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/16</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 选择排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SelectSort</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">int</span>[] arr = {<span class="number">101</span>,<span class="number">34</span>,<span class="number">119</span>,<span class="number">1</span>};</span><br><span class="line"> <span class="type">int</span>[] arr1 = {<span class="number">3</span>, <span class="number">9</span>, -<span class="number">1</span>, <span class="number">10</span>, -<span class="number">2</span>};</span><br><span class="line"> <span class="type">int</span>[] arr2 = {<span class="number">3</span>, <span class="number">9</span>, -<span class="number">1</span>, <span class="number">10</span>, -<span class="number">2</span>};</span><br><span class="line"> System.out.println(<span class="string">"自己的代码"</span>);</span><br><span class="line"> SelectSort.selectSort(arr2);</span><br><span class="line"> System.out.println(<span class="string">"老师的代码"</span>);</span><br><span class="line"> SelectSort.selectSortByTeacher(arr1);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 选择排序</span></span><br><span class="line"><span class="comment"> * 自己写的 中间的过程和老师的不一样 不确定是不是选择排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">selectSort</span><span class="params">(<span class="type">int</span>[] arr)</span> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < arr.length - <span class="number">1</span>; i++) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> i + <span class="number">1</span>; j < arr.length; j++) {<span class="comment">//从第一个数字开始 依次找到最小值</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(arr[i] > arr[j]) {</span><br><span class="line"> temp = arr[i];</span><br><span class="line"> arr[i] = arr[j];</span><br><span class="line"> arr[j] = temp;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"第次"</span>+(i+<span class="number">1</span>)+<span class="string">"排序的结果:"</span>+Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"最终的结果:"</span>+Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 老师的代码</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">selectSortByTeacher</span><span class="params">(<span class="type">int</span>[] arr)</span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < arr.length - <span class="number">1</span>; i++) {</span><br><span class="line"> <span class="type">int</span> <span class="variable">minIndex</span> <span class="operator">=</span> i;</span><br><span class="line"> <span class="type">int</span> <span class="variable">min</span> <span class="operator">=</span> arr[i];</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> i + <span class="number">1</span>; j < arr.length; j++) {<span class="comment">//从第一个数字开始 依次找到最小值</span></span><br><span class="line"> <span class="keyword">if</span>(min > arr[j]){</span><br><span class="line"> min = arr[j];</span><br><span class="line"> minIndex = j;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//交换</span></span><br><span class="line"> arr[minIndex] = arr[i];</span><br><span class="line"> arr[i] = min;</span><br><span class="line"> System.out.println(<span class="string">"第次"</span>+(i+<span class="number">1</span>)+<span class="string">"排序的结果:"</span>+Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"最终的结果:"</span>+Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><img src="/pictures/image-20230516093143656.png" alt="image-20230516093143656"></p>
|
||
<h3 id="6-插入排序"><a href="#6-插入排序" class="headerlink" title="6.插入排序"></a>6.插入排序</h3><h4 id="6-1-基本介绍"><a href="#6-1-基本介绍" class="headerlink" title="6.1 基本介绍"></a>6.1 基本介绍</h4><p>同一台电脑 8万个数据 五秒左右</p>
|
||
<p>插入式排序属于内部排序法,是对于欲排序的元素以插入的方式找寻该元素的适当位置,以达到排序的目的。</p>
|
||
<p><img src="/pictures/image-20230516100256880.png" alt="image-20230516100256880"></p>
|
||
<h4 id="6-2-图解"><a href="#6-2-图解" class="headerlink" title="6.2 图解"></a>6.2 图解</h4><img src="/pictures/insertionSort.gif" alt="img" style="zoom:150%;" />
|
||
|
||
<h4 id="6-3-代码实现"><a href="#6-3-代码实现" class="headerlink" title="6.3 代码实现"></a>6.3 代码实现</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.sort;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/16</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 插入排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">InsertSort</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">int</span>[] arr = {<span class="number">101</span>, <span class="number">34</span>, <span class="number">119</span>, <span class="number">1</span>,-<span class="number">1</span>,<span class="number">89</span>};</span><br><span class="line"> System.out.println(<span class="string">"插入前的数组:"</span>+Arrays.toString(arr));</span><br><span class="line"> InsertSort.insertSort(arr);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 插入排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">insertSort</span><span class="params">(<span class="type">int</span>[] arr)</span> {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i < arr.length; i++) {</span><br><span class="line"> <span class="comment">//定义待插入的数</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">insertVal</span> <span class="operator">=</span> arr[i];</span><br><span class="line"> <span class="comment">//待插入的数的前一个数的下标</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">insertIndex</span> <span class="operator">=</span> i - <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (insertIndex >= <span class="number">0</span> && insertVal < arr[insertIndex]) {</span><br><span class="line"> arr[insertIndex + <span class="number">1</span>] = arr[insertIndex];</span><br><span class="line"> insertIndex--;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//退出循环之后 代表找到了要插入数据的位置</span></span><br><span class="line"> <span class="comment">//插入数据</span></span><br><span class="line"> arr[insertIndex + <span class="number">1</span>] = insertVal;</span><br><span class="line"> System.out.println(<span class="string">"第"</span> + i + <span class="string">"次插入的结果:"</span> + Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"最终的结果:"</span> + Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><img src="/pictures/image-20230516105517026.png" alt="image-20230516105517026"></p>
|
||
<h3 id="7-希尔排序"><a href="#7-希尔排序" class="headerlink" title="7.希尔排序"></a>7.希尔排序</h3><h4 id="7-1基本介绍"><a href="#7-1基本介绍" class="headerlink" title="7.1基本介绍"></a>7.1基本介绍</h4><p>同一台电脑 8万个数据 十七秒左右(交换法)</p>
|
||
<p>同一台电脑 8万个数据 一秒左右(移动法)</p>
|
||
<p><img src="/pictures/image-20230517091741355.png" alt="image-20230517091741355"></p>
|
||
<p><img src="/pictures/image-20230517091841366.png" alt="image-20230517091841366"></p>
|
||
<h4 id="7-2-图解"><a href="#7-2-图解" class="headerlink" title="7.2.图解"></a>7.2.图解</h4><p><img src="/pictures/Sorting_shellsort_anim.gif" alt="img"></p>
|
||
<img src="/pictures/image-20230517102649272.png" alt="image-20230517102649272" style="zoom:150%;" />
|
||
|
||
<h4 id="7-3-代码实现"><a href="#7-3-代码实现" class="headerlink" title="7.3 代码实现"></a>7.3 代码实现</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.sort;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/17</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 希尔排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ShellSort</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">int</span>[] arr = {<span class="number">8</span>, <span class="number">9</span>, <span class="number">1</span>, <span class="number">7</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">0</span>};</span><br><span class="line"> <span class="comment">//shellSort(arr);</span></span><br><span class="line"> shellSort2(arr);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 希尔排序 使用的是交换法 这个很慢 比直接的插入排序还慢</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">shellSort</span><span class="params">(<span class="type">int</span>[] arr)</span> {</span><br><span class="line"> <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="comment">//用于分组 10个数据 第一次分5组 第二次分2组 第三次分1组 每组分别插入排序</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">gap</span> <span class="operator">=</span> arr.length / <span class="number">2</span>; gap > <span class="number">0</span>; gap /= <span class="number">2</span>) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> gap; i < arr.length; i++) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> i - gap; j >= <span class="number">0</span>; j -= gap) {</span><br><span class="line"> <span class="comment">//如果当前的那个元素大于加上步长后的那个元素,就交换</span></span><br><span class="line"> <span class="keyword">if</span> (arr[j] > arr[j + gap]) {</span><br><span class="line"> temp = arr[j];</span><br><span class="line"> arr[j] = arr[j + gap];</span><br><span class="line"> arr[j + gap] = temp;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"排序第"</span> + (++count) + <span class="string">"轮的结果:"</span> + Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"最终的结果:"</span> + Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用移动法的希尔排序 这个更快 </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">shellSort2</span><span class="params">(<span class="type">int</span>[] arr)</span> {</span><br><span class="line"> <span class="comment">//用于分组 10个数据 第一次分5组 第二次分2组 第三次分1组 每组分别插入排序</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">gap</span> <span class="operator">=</span> arr.length / <span class="number">2</span>; gap > <span class="number">0</span>; gap /= <span class="number">2</span>) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> gap; i < arr.length; i++) {</span><br><span class="line"> <span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> i;</span><br><span class="line"> <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> arr[j];</span><br><span class="line"> <span class="keyword">if</span> (arr[j] < arr[j - gap]) {</span><br><span class="line"> <span class="keyword">while</span> (j - gap >= <span class="number">0</span> && temp < arr[j - gap]) {</span><br><span class="line"> arr[j] = arr[j - gap];</span><br><span class="line"> j -= gap;</span><br><span class="line"> }</span><br><span class="line"> arr[j] = temp; }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"排序第"</span> + (++count) + <span class="string">"轮的结果:"</span> + Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"最终的结果:"</span> + Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="8-快速排序"><a href="#8-快速排序" class="headerlink" title="8.快速排序"></a>8.快速排序</h3><h4 id="8-1-基本介绍"><a href="#8-1-基本介绍" class="headerlink" title="8.1 基本介绍"></a>8.1 基本介绍</h4><p>同一台电脑 8万个数据 不到一秒 800万个数据两秒钟</p>
|
||
<p> <strong>快速排序(Quicksort)是对冒泡排序的一种改进。基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列</strong></p>
|
||
<h4 id="8-2-图解"><a href="#8-2-图解" class="headerlink" title="8.2 图解"></a>8.2 图解</h4><img src="/pictures/quickSort.gif" alt="img" style="zoom:150%;" />
|
||
|
||
<img src="/pictures/image-20230517103139900.png" alt="image-20230517103139900" style="zoom:150%;" />
|
||
|
||
<h4 id="8-3-代码实现"><a href="#8-3-代码实现" class="headerlink" title="8.3 代码实现"></a>8.3 代码实现</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.sort;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/17</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 快速排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">QuickSort</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">int</span>[] arr = {-<span class="number">9</span>, <span class="number">78</span>, <span class="number">0</span>, <span class="number">23</span>, -<span class="number">567</span>, <span class="number">70</span>};</span><br><span class="line"> quickSort(arr, <span class="number">0</span>, arr.length - <span class="number">1</span>);</span><br><span class="line"> System.out.println(Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> arr 需要排序的数组</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> left 左下标</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> right 右下标</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">quickSort</span><span class="params">(<span class="type">int</span>[] arr, <span class="type">int</span> left, <span class="type">int</span> right)</span> {</span><br><span class="line"> <span class="type">int</span> <span class="variable">l</span> <span class="operator">=</span> left;</span><br><span class="line"> <span class="type">int</span> <span class="variable">r</span> <span class="operator">=</span> right;</span><br><span class="line"> <span class="comment">//pivot 中轴</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">pivot</span> <span class="operator">=</span> arr[(left + right) / <span class="number">2</span>];</span><br><span class="line"> <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="comment">//while循环的目的是让比pivot小的放到左边 大的放在右边</span></span><br><span class="line"> <span class="keyword">while</span> (l < r) {</span><br><span class="line"> <span class="comment">//在pivot的左边一直找 找到大于等于pivot的值 才退出</span></span><br><span class="line"> <span class="keyword">while</span> (arr[l] < pivot) {</span><br><span class="line"> l += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//在pivot的左边一直找 找到大于等于pivot的值 才退出</span></span><br><span class="line"> <span class="keyword">while</span> (arr[r] > pivot) {</span><br><span class="line"> r -= <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//如果l >= r成立 说明pivot的左右两边的值 已经是按照左边全部是</span></span><br><span class="line"> <span class="comment">//小于等于pivot值 右边全部是大于等于pivot值</span></span><br><span class="line"> <span class="keyword">if</span> (l >= r) {</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//交换</span></span><br><span class="line"> temp = arr[l];</span><br><span class="line"> arr[l] = arr[r];</span><br><span class="line"> arr[r] = temp;</span><br><span class="line"> <span class="comment">//如果交换完之后发现arr[l] = pivot值</span></span><br><span class="line"> <span class="keyword">if</span> (arr[l] == pivot) {</span><br><span class="line"> r--;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//如果交换完之后发现arr[r] = pivot值</span></span><br><span class="line"> <span class="keyword">if</span> (arr[r] == pivot) {</span><br><span class="line"> l++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//如果l == r 必须l++ r--</span></span><br><span class="line"> <span class="keyword">if</span> (l == r) {</span><br><span class="line"> l += <span class="number">1</span>;</span><br><span class="line"> r -= <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//向左递归</span></span><br><span class="line"> <span class="keyword">if</span> (left < r) {<span class="comment">//左边的数全部有序</span></span><br><span class="line"> quickSort(arr, left, r);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//向右递归</span></span><br><span class="line"> <span class="keyword">if</span> (right > l) {</span><br><span class="line"> quickSort(arr, l, right);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
|
||
|
||
<h3 id="9-归并排序"><a href="#9-归并排序" class="headerlink" title="9. 归并排序"></a>9. 归并排序</h3><h4 id="9-1基本介绍"><a href="#9-1基本介绍" class="headerlink" title="9.1基本介绍"></a>9.1基本介绍</h4><p>同一台电脑 8万个数据 大约一秒钟 800万个数据三秒</p>
|
||
<p><img src="/pictures/image-20230518111233009.png" alt="image-20230518111233009"></p>
|
||
<h4 id="9-2-图解"><a href="#9-2-图解" class="headerlink" title="9.2 图解"></a>9.2 图解</h4><img src="/pictures/mergeSort.gif" alt="img" style="zoom:150%;" />
|
||
|
||
<p><img src="/pictures/image-20230518111454775.png" alt="image-20230518111454775"></p>
|
||
<p><img src="/pictures/image-20230518111536026.png" alt="image-20230518111536026"></p>
|
||
<h4 id="9-3代码实现"><a href="#9-3代码实现" class="headerlink" title="9.3代码实现"></a>9.3代码实现</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.sort;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/18</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 归并排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MergeSort</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">int</span>[] arr = {<span class="number">8</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">6</span>, <span class="number">2</span>};</span><br><span class="line"> <span class="type">int</span>[] temp = <span class="keyword">new</span> <span class="title class_">int</span>[arr.length];</span><br><span class="line"> mergeSort(arr,<span class="number">0</span>, arr.length -<span class="number">1</span>,temp);</span><br><span class="line"> System.out.println(<span class="string">"归并排序之后的数组:"</span>+ Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 分治的过程</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">mergeSort</span><span class="params">(<span class="type">int</span>[] arr, <span class="type">int</span> left, <span class="type">int</span> right, <span class="type">int</span>[] temp)</span>{</span><br><span class="line"> <span class="keyword">if</span>(left < right){</span><br><span class="line"> <span class="type">int</span> <span class="variable">mid</span> <span class="operator">=</span> (left + right) / <span class="number">2</span>;</span><br><span class="line"> <span class="comment">//向左递归分解</span></span><br><span class="line"> mergeSort(arr,left,mid,temp);</span><br><span class="line"> <span class="comment">//向右递归分解</span></span><br><span class="line"> mergeSort(arr,mid+<span class="number">1</span>,right,temp);</span><br><span class="line"> <span class="comment">//合并</span></span><br><span class="line"> merge(arr,left,mid,right,temp);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 合并的方法 治</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> arr 需要排序的数组</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> left 右边有序序列的初始索引</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> mid 中间索引</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> right 右边索引</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> temp 中转的数组</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">merge</span><span class="params">(<span class="type">int</span>[] arr, <span class="type">int</span> left, <span class="type">int</span> mid, <span class="type">int</span> right, <span class="type">int</span>[] temp)</span> {</span><br><span class="line"> <span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> left;<span class="comment">//左边有序序列的初始索引</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> mid + <span class="number">1</span>;<span class="comment">//右边有序序列的初始化索引</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">t</span> <span class="operator">=</span> <span class="number">0</span>;<span class="comment">//指向temp数组的当前索引</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//1.</span></span><br><span class="line"> <span class="comment">//先把左右两边(有序)的数据填充到temp数组</span></span><br><span class="line"> <span class="comment">//直到左右两边的数据有一边处理完毕为止</span></span><br><span class="line"> <span class="keyword">while</span> (i <= mid && j <= right) {<span class="comment">//相当于左右两边的数组都有一个指针</span></span><br><span class="line"> <span class="keyword">if</span> (arr[i] <= arr[j]) {</span><br><span class="line"> temp[t] = arr[i];</span><br><span class="line"> t++;</span><br><span class="line"> i++;</span><br><span class="line"> } <span class="keyword">else</span> {<span class="comment">//反之将右边有序序列的当前元素 填充到temp数组</span></span><br><span class="line"> temp[t] = arr[j];</span><br><span class="line"> t++;</span><br><span class="line"> j++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//2.</span></span><br><span class="line"> <span class="comment">//把有剩余数据的一边的数据依次全部填充到temp</span></span><br><span class="line"> <span class="keyword">while</span> (i <= mid) {<span class="comment">//说明左边的有序序列还有剩余的元素</span></span><br><span class="line"> temp[t] = arr[i];</span><br><span class="line"> t++;</span><br><span class="line"> i++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span> (j <= right) {</span><br><span class="line"> temp[t] = arr[j];</span><br><span class="line"> t++;</span><br><span class="line"> j++;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//3.</span></span><br><span class="line"> <span class="comment">//将temp数组的元素拷贝到arr</span></span><br><span class="line"> <span class="comment">//并不是每次都拷贝</span></span><br><span class="line"> t = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> <span class="variable">tempLeft</span> <span class="operator">=</span> left;</span><br><span class="line"> <span class="keyword">while</span> (tempLeft <= right) {</span><br><span class="line"> arr[tempLeft] = temp[t];</span><br><span class="line"> t++;</span><br><span class="line"> tempLeft++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="10-基数排序"><a href="#10-基数排序" class="headerlink" title="10.基数排序"></a>10.基数排序</h3><p>同一台电脑8万个数据 一秒左右 8千万个数据会报内存不足</p>
|
||
<h4 id="10-1-基本介绍"><a href="#10-1-基本介绍" class="headerlink" title="10.1 基本介绍"></a>10.1 基本介绍</h4><p><img src="/pictures/image-20230525195008102.png" alt="image-20230525195008102"></p>
|
||
<p><img src="/pictures/image-20230526094945578.png" alt="image-20230526094945578"></p>
|
||
<p><img src="/pictures/image-20230526113739081.png" alt="image-20230526113739081"></p>
|
||
<h4 id="10-2-图解"><a href="#10-2-图解" class="headerlink" title="10.2 图解"></a>10.2 图解</h4><p><img src="/pictures/radixSort.gif" alt="img"></p>
|
||
<h4 id="10-3-代码实现"><a href="#10-3-代码实现" class="headerlink" title="10.3 代码实现"></a>10.3 代码实现</h4><p><strong>推导代码</strong></p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.sort;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/26</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 基数排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">RadixSort</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//定义一个待排序的数组</span></span><br><span class="line"> <span class="type">int</span> arr[] = {<span class="number">53</span>, <span class="number">3</span>, <span class="number">542</span>, <span class="number">748</span>, <span class="number">14</span>, <span class="number">214</span>};</span><br><span class="line"> RadixSort.radixSort(arr);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//基数排序</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">radixSort</span><span class="params">(<span class="type">int</span>[] arr)</span> {</span><br><span class="line"> <span class="comment">//1.第一轮(针对每个元素的个位进行排序处理)</span></span><br><span class="line"> <span class="comment">//定义一个二维数组,表示十个桶,每个桶就是一个一维数组</span></span><br><span class="line"> <span class="comment">//为了防止数据的溢出,这里每个桶的大小我们设置的大一些 大小定为arr.length</span></span><br><span class="line"> <span class="type">int</span>[][] bucket = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">10</span>][arr.length];</span><br><span class="line"> <span class="comment">//为了记录每个桶中实际存放了多少个数据,我们定义一个一维数组来记录每个桶中依次放入数据的个数</span></span><br><span class="line"> <span class="comment">//bucketElementCounts[0]记录的就是第0个桶的数据的个数</span></span><br><span class="line"> <span class="type">int</span>[] bucketElementCounts = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">10</span>];</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j < arr.length; j++) {</span><br><span class="line"> <span class="comment">//取出每个元素的个位数的值</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">digitOfElement</span> <span class="operator">=</span> arr[j] % <span class="number">10</span>;</span><br><span class="line"> <span class="comment">//放入对应的桶中</span></span><br><span class="line"> bucket[digitOfElement][bucketElementCounts[digitOfElement]] = arr[j];</span><br><span class="line"> <span class="comment">//[bucketElementCounts[digitOfElement]]++的意思是在digitOfElement对应的桶中数据个数加一</span></span><br><span class="line"> bucketElementCounts[digitOfElement]++;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//按照这个桶中顺序取出数据</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">index</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">k</span> <span class="operator">=</span> <span class="number">0</span>; k < bucketElementCounts.length; k++) {</span><br><span class="line"> <span class="comment">//如果桶中有数据,我们才放入到原数组</span></span><br><span class="line"> <span class="keyword">if</span> (bucketElementCounts[k] != <span class="number">0</span>) {</span><br><span class="line"> <span class="comment">//循环该桶即第K个桶,放入</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">l</span> <span class="operator">=</span> <span class="number">0</span>; l < bucketElementCounts[k]; l++) {</span><br><span class="line"> arr[index++] = bucket[k][l];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> bucketElementCounts[k] = <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"第一轮:"</span> + Arrays.toString(arr));</span><br><span class="line"></span><br><span class="line"> <span class="comment">//2.第2轮</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j < arr.length; j++) {</span><br><span class="line"> <span class="comment">//取出每个元素的十位数的值</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">digitOfElement</span> <span class="operator">=</span> arr[j] / <span class="number">10</span> % <span class="number">10</span>;</span><br><span class="line"> <span class="comment">//放入对应的桶中</span></span><br><span class="line"> bucket[digitOfElement][bucketElementCounts[digitOfElement]] = arr[j];</span><br><span class="line"> <span class="comment">//[bucketElementCounts[digitOfElement]]++的意思是在digitOfElement对应的桶中数据个数加一</span></span><br><span class="line"> bucketElementCounts[digitOfElement]++;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//按照这个桶中顺序取出数据</span></span><br><span class="line"> index = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">k</span> <span class="operator">=</span> <span class="number">0</span>; k < bucketElementCounts.length; k++) {</span><br><span class="line"> <span class="comment">//如果桶中有数据,我们才放入到原数组</span></span><br><span class="line"> <span class="keyword">if</span> (bucketElementCounts[k] != <span class="number">0</span>) {</span><br><span class="line"> <span class="comment">//循环该桶即第K个桶,放入</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">l</span> <span class="operator">=</span> <span class="number">0</span>; l < bucketElementCounts[k]; l++) {</span><br><span class="line"> arr[index++] = bucket[k][l];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> bucketElementCounts[k] = <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"第2轮:"</span> + Arrays.toString(arr));</span><br><span class="line"></span><br><span class="line"> <span class="comment">//3.第3轮</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j < arr.length; j++) {</span><br><span class="line"> <span class="comment">//取出每个元素的百位数的值</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">digitOfElement</span> <span class="operator">=</span> arr[j] / <span class="number">100</span> % <span class="number">10</span>;</span><br><span class="line"> <span class="comment">//放入对应的桶中</span></span><br><span class="line"> bucket[digitOfElement][bucketElementCounts[digitOfElement]] = arr[j];</span><br><span class="line"> <span class="comment">//[bucketElementCounts[digitOfElement]]++的意思是在digitOfElement对应的桶中数据个数加一</span></span><br><span class="line"> bucketElementCounts[digitOfElement]++;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//按照这个桶中顺序取出数据</span></span><br><span class="line"> index = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">k</span> <span class="operator">=</span> <span class="number">0</span>; k < bucketElementCounts.length; k++) {</span><br><span class="line"> <span class="comment">//如果桶中有数据,我们才放入到原数组</span></span><br><span class="line"> <span class="keyword">if</span> (bucketElementCounts[k] != <span class="number">0</span>) {</span><br><span class="line"> <span class="comment">//循环该桶即第K个桶,放入</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">l</span> <span class="operator">=</span> <span class="number">0</span>; l < bucketElementCounts[k]; l++) {</span><br><span class="line"> arr[index++] = bucket[k][l];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"第3轮:"</span> + Arrays.toString(arr));</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>最终代码</strong></p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.sort;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/26</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 基数排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">RadixSort</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//定义一个待排序的数组</span></span><br><span class="line"> <span class="type">int</span> arr[] = {<span class="number">53</span>, <span class="number">3</span>, <span class="number">542</span>, <span class="number">748</span>, <span class="number">14</span>, <span class="number">214</span>};</span><br><span class="line"> RadixSort.radixSort(arr);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//基数排序</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">radixSort</span><span class="params">(<span class="type">int</span>[] arr)</span> {</span><br><span class="line"> <span class="comment">//得到数组中最大数的位数</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">max</span> <span class="operator">=</span> arr[<span class="number">0</span>];</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < arr.length; i++) {</span><br><span class="line"> <span class="keyword">if</span> (arr[i] > max) {</span><br><span class="line"> max = arr[i];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//最大数的位数</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">maxLength</span> <span class="operator">=</span> (max + <span class="string">""</span>).length();</span><br><span class="line"> <span class="comment">//定义一个二维数组,表示十个桶,每个桶就是一个一维数组</span></span><br><span class="line"> <span class="type">int</span>[][] bucket = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">10</span>][arr.length];</span><br><span class="line"> <span class="comment">//为了记录每个桶中实际存放了多少个数据,我们定义一个一维数组来记录每个桶中依次放入数据的个数</span></span><br><span class="line"> <span class="type">int</span>[] bucketElementCounts = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">10</span>];</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>, n = <span class="number">1</span>; i < maxLength; i++, n *= <span class="number">10</span>) {<span class="comment">//循环的是每一轮,第一次是个位。第二次是百位</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j < arr.length; j++) {</span><br><span class="line"> <span class="comment">//取出每个元素的对应位数的值</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">digitOfElement</span> <span class="operator">=</span> arr[j] / n % <span class="number">10</span>;</span><br><span class="line"> <span class="comment">//放入对应的桶中</span></span><br><span class="line"> bucket[digitOfElement][bucketElementCounts[digitOfElement]] = arr[j];</span><br><span class="line"> <span class="comment">//[bucketElementCounts[digitOfElement]]++的意思是在digitOfElement对应的桶中数据个数加一</span></span><br><span class="line"> bucketElementCounts[digitOfElement]++;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//按照这个桶中顺序取出数据</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">index</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">k</span> <span class="operator">=</span> <span class="number">0</span>; k < bucketElementCounts.length; k++) {</span><br><span class="line"> <span class="comment">//如果桶中有数据,我们才放入到原数组</span></span><br><span class="line"> <span class="keyword">if</span> (bucketElementCounts[k] != <span class="number">0</span>) {</span><br><span class="line"> <span class="comment">//循环该桶即第K个桶,放入</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">l</span> <span class="operator">=</span> <span class="number">0</span>; l < bucketElementCounts[k]; l++) {</span><br><span class="line"> arr[index++] = bucket[k][l];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> bucketElementCounts[k] = <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> System.out.println(<span class="string">"第"</span> + (i + <span class="number">1</span>) + <span class="string">"轮:"</span> + Arrays.toString(arr));</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="11-常用排序算法总结和对比"><a href="#11-常用排序算法总结和对比" class="headerlink" title="11.常用排序算法总结和对比"></a>11.常用排序算法总结和对比</h3><p><img src="/pictures/image-20230526114127402.png" alt="image-20230526114127402"></p>
|
||
<h2 id="八-查找算法"><a href="#八-查找算法" class="headerlink" title="八.查找算法"></a>八.查找算法</h2><h3 id="1-简单介绍-1"><a href="#1-简单介绍-1" class="headerlink" title="1.简单介绍"></a>1.简单介绍</h3><img src="/pictures/image-20230526115027518.png" alt="image-20230526115027518" style="zoom:200%;" />
|
||
|
||
<h3 id="2-线性查找算法"><a href="#2-线性查找算法" class="headerlink" title="2.线性查找算法"></a>2.线性查找算法</h3><h4 id="2-1代码实现"><a href="#2-1代码实现" class="headerlink" title="2.1代码实现"></a>2.1代码实现</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.search;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/29</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 线性查找</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SeqSearch</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">int</span>[] arr = {<span class="number">1</span>,<span class="number">9</span>,<span class="number">11</span>,-<span class="number">1</span>,<span class="number">34</span>,<span class="number">89</span>};<span class="comment">//没有顺序的数组</span></span><br><span class="line"> System.out.println(<span class="string">"目标值的下标:"</span>+seqSearch(arr, <span class="number">11</span>));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 找到一个满足条件的值就返回</span></span><br><span class="line"><span class="comment"> * 线性查找是逐一比对,发现有相同的值时就返回这个值的下标</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> arr 需要在这里面找目标值的数组</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> value 目标值</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 目标值的下标</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">seqSearch</span><span class="params">(<span class="type">int</span>[] arr,<span class="type">int</span> value)</span>{</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < arr.length; i++) {</span><br><span class="line"> <span class="keyword">if</span>(arr[i] == value){</span><br><span class="line"> <span class="keyword">return</span> i;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="3-二分查找算法"><a href="#3-二分查找算法" class="headerlink" title="3.二分查找算法"></a>3.二分查找算法</h3><h4 id="3-1-图解"><a href="#3-1-图解" class="headerlink" title="3.1 图解"></a>3.1 图解</h4><img src="/pictures/image-20230529104112317.png" alt="image-20230529104112317" style="zoom:150%;" />
|
||
|
||
<h4 id="3-2-代码实现"><a href="#3-2-代码实现" class="headerlink" title="3.2 代码实现"></a>3.2 代码实现</h4><p><em><strong>递归的方式解决</strong></em></p>
|
||
<p><strong>基本的写法</strong></p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.search;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/29</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 二分查找</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BinarySearch</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">int</span>[] arr = {<span class="number">1</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">89</span>, <span class="number">1000</span>, <span class="number">1234</span>};</span><br><span class="line"> System.out.println(<span class="string">"目标值的下标:"</span> + binarySearch(arr, <span class="number">0</span>, arr.length - <span class="number">1</span>, <span class="number">3</span>));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 二分查找</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> arr 需要在这里面查找目标值的数组</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> left 左边的索引</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> right 右边的索引</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> findVal 要查找的值</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 目标值在数组中的下标,找到就返回下标,没有就返回-1</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">binarySearch</span><span class="params">(<span class="type">int</span>[] arr, <span class="type">int</span> left, <span class="type">int</span> right, <span class="type">int</span> findVal)</span> {</span><br><span class="line"> <span class="comment">//结束递归的条件</span></span><br><span class="line"> <span class="keyword">if</span> (left > right) {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="type">int</span> <span class="variable">mid</span> <span class="operator">=</span> (left + right) / <span class="number">2</span>;</span><br><span class="line"> <span class="type">int</span> <span class="variable">midVal</span> <span class="operator">=</span> arr[mid];</span><br><span class="line"> <span class="keyword">if</span> (findVal > midVal) {<span class="comment">//向右递归</span></span><br><span class="line"> <span class="keyword">return</span> binarySearch(arr, mid + <span class="number">1</span>, right, findVal);</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (findVal < midVal) {<span class="comment">//向左递归</span></span><br><span class="line"> <span class="keyword">return</span> binarySearch(arr, left, mid - <span class="number">1</span>, findVal);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> mid;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>完整的代码</strong></p>
|
||
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.search;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/29</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 二分查找</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BinarySearch</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">int</span>[] arr = {<span class="number">1</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">89</span>, <span class="number">1000</span>, <span class="number">1234</span>};</span><br><span class="line"> System.out.println(<span class="string">"目标值的下标:"</span> + binarySearch(arr, <span class="number">0</span>, arr.length - <span class="number">1</span>, <span class="number">3</span>));</span><br><span class="line"> <span class="type">int</span>[] arr2 = {<span class="number">1</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">89</span>, <span class="number">89</span>, <span class="number">89</span>, <span class="number">1000</span>, <span class="number">1234</span>};</span><br><span class="line"> System.out.println(<span class="string">"目标值在数组中下标的集合:"</span> + binarySearch2(arr2, <span class="number">0</span>, arr2.length - <span class="number">1</span>, <span class="number">89</span>));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 二分查找</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> arr 需要在这里面查找目标值的数组</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> left 左边的索引</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> right 右边的索引</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> findVal 要查找的值</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 目标值在数组中的下标,找到就返回下标,没有就返回-1</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">binarySearch</span><span class="params">(<span class="type">int</span>[] arr, <span class="type">int</span> left, <span class="type">int</span> right, <span class="type">int</span> findVal)</span> {</span><br><span class="line"> <span class="comment">//结束递归的条件</span></span><br><span class="line"> <span class="keyword">if</span> (left > right) {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="type">int</span> <span class="variable">mid</span> <span class="operator">=</span> (left + right) / <span class="number">2</span>;</span><br><span class="line"> <span class="type">int</span> <span class="variable">midVal</span> <span class="operator">=</span> arr[mid];</span><br><span class="line"> <span class="keyword">if</span> (findVal > midVal) {<span class="comment">//向右递归</span></span><br><span class="line"> <span class="keyword">return</span> binarySearch(arr, mid + <span class="number">1</span>, right, findVal);</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (findVal < midVal) {<span class="comment">//向左递归</span></span><br><span class="line"> <span class="keyword">return</span> binarySearch(arr, left, mid - <span class="number">1</span>, findVal);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> mid;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 二分查找查找出所有与目标值相同的数组的下标</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> arr 需要在这里面查找目标值的数组</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> left 左边的索引</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> right 右边的索引</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> findVal 要查找的的值</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 目标值在数组中的下标的集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> List<Integer> <span class="title function_">binarySearch2</span><span class="params">(<span class="type">int</span>[] arr, <span class="type">int</span> left, <span class="type">int</span> right, <span class="type">int</span> findVal)</span> {</span><br><span class="line"> <span class="comment">//结束递归的条件</span></span><br><span class="line"> <span class="keyword">if</span> (left > right) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">ArrayList</span><>();</span><br><span class="line"> }</span><br><span class="line"> <span class="type">int</span> <span class="variable">mid</span> <span class="operator">=</span> (left + right) / <span class="number">2</span>;</span><br><span class="line"> <span class="type">int</span> <span class="variable">midVal</span> <span class="operator">=</span> arr[mid];</span><br><span class="line"> <span class="keyword">if</span> (findVal > midVal) {<span class="comment">//向右递归</span></span><br><span class="line"> <span class="keyword">return</span> binarySearch2(arr, mid + <span class="number">1</span>, right, findVal);</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (findVal < midVal) {<span class="comment">//向左递归</span></span><br><span class="line"> <span class="keyword">return</span> binarySearch2(arr, left, mid - <span class="number">1</span>, findVal);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> List<Integer> resList = <span class="keyword">new</span> <span class="title class_">ArrayList</span><>();</span><br><span class="line"> <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> mid - <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) { <span class="comment">//向左边探测相同值的元素</span></span><br><span class="line"> <span class="keyword">if</span> (temp < <span class="number">0</span> || arr[temp] != findVal) {</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//否则就把temp放入到集合中</span></span><br><span class="line"> resList.add(temp);</span><br><span class="line"> temp--;<span class="comment">//左移</span></span><br><span class="line"> }</span><br><span class="line"> resList.add(mid);</span><br><span class="line"> <span class="comment">//向右边探测</span></span><br><span class="line"> temp = mid + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) { <span class="comment">//向右边探测相同值的元素</span></span><br><span class="line"> <span class="keyword">if</span> (temp > arr.length - <span class="number">1</span> || arr[temp] != findVal) {</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//否则就把temp放入到集合中</span></span><br><span class="line"> resList.add(temp);</span><br><span class="line"> temp++;<span class="comment">//右移</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> resList;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="4-插值查找算法"><a href="#4-插值查找算法" class="headerlink" title="4.插值查找算法"></a>4.插值查找算法</h3><h4 id="4-1-图解"><a href="#4-1-图解" class="headerlink" title="4.1 图解"></a>4.1 图解</h4><p><img src="/pictures/image-20230530101020552.png"></p>
|
||
<img src="/pictures/image-20230530101803392.png" alt="image-20230530101803392" style="zoom:200%;" />
|
||
|
||
<h4 id="4-2-代码实现"><a href="#4-2-代码实现" class="headerlink" title="4.2 代码实现"></a>4.2 代码实现</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.search;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> GongChangjiang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2023/5/30</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> 插值查找算法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">InsertValueSearch</span> {</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//创建一个数组 用于模拟需要查找数据的数组</span></span><br><span class="line"> <span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">100</span>];</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < <span class="number">100</span>; i++) {</span><br><span class="line"> arr[i] = i + <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> System.out.println(Arrays.toString(arr));</span><br><span class="line"> System.out.println(<span class="string">"查找的数的下标为:"</span> + insertValueSearch(arr, <span class="number">0</span>, arr.length - <span class="number">1</span>, <span class="number">30</span>));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 插值查找算法</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> arr 目标数组(有序的)等差序列的最好,1-100有序的数组,找一个数只用找一次就行了</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> left 左边索引</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> right 右边索引</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> findVal 需要查找的值</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 查找的值在数组中的索引</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">insertValueSearch</span><span class="params">(<span class="type">int</span>[] arr, <span class="type">int</span> left, <span class="type">int</span> right, <span class="type">int</span> findVal)</span> {</span><br><span class="line"> System.out.println(<span class="string">"方法被调用了"</span>);</span><br><span class="line"> <span class="comment">//退出的条件和防止数组越界</span></span><br><span class="line"> <span class="keyword">if</span> (left > right || findVal < arr[<span class="number">0</span>] || findVal > arr[arr.length - <span class="number">1</span>]) {</span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//求出mid,这个是插值查找算法的灵魂,自适应的查找</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">mid</span> <span class="operator">=</span> left + (right - left) * (findVal - arr[left]) / (arr[right] - arr[left]);</span><br><span class="line"> <span class="type">int</span> <span class="variable">midVal</span> <span class="operator">=</span> arr[mid];</span><br><span class="line"> <span class="keyword">if</span> (findVal > midVal) { <span class="comment">//应向右递归</span></span><br><span class="line"> <span class="keyword">return</span> insertValueSearch(arr, mid + <span class="number">1</span>, right, findVal);</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (findVal < midVal) {<span class="comment">//向左边递归</span></span><br><span class="line"> <span class="keyword">return</span> insertValueSearch(arr, left, mid - <span class="number">1</span>, findVal);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> mid;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-3-注意事项"><a href="#4-3-注意事项" class="headerlink" title="4.3 注意事项"></a>4.3 注意事项</h4><p><img src="/pictures/image-20230530105652529.png" alt="image-20230530105652529"></p>
|
||
<h3 id="5-斐波那契(黄金分割法)查找算法"><a href="#5-斐波那契(黄金分割法)查找算法" class="headerlink" title="5.斐波那契(黄金分割法)查找算法"></a>5.斐波那契(黄金分割法)查找算法</h3><h4 id="5-1-基本介绍-1"><a href="#5-1-基本介绍-1" class="headerlink" title="5.1 基本介绍"></a>5.1 基本介绍</h4><img src="/pictures/image-20230530110145048.png" alt="image-20230530110145048" style="zoom:150%;" />
|
||
|
||
<h4 id="5-2-原理介绍"><a href="#5-2-原理介绍" class="headerlink" title="5.2 原理介绍"></a>5.2 原理介绍</h4><p><img src="/pictures/image-20230530110511769.png" alt="image-20230530110511769"></p>
|
||
<hr>
|
||
<h2 id="PDF笔记"><a href="#PDF笔记" class="headerlink" title="PDF笔记"></a>PDF笔记</h2>
|
||
|
||
<div class="row">
|
||
<embed src="/pdf/尚硅谷图解Java数据结构和算法.pdf" width="100%" height="550" type="application/pdf">
|
||
</div>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/">数据结构与算法</a></div><div class="post_share"><div class="social-share" data-image="/img/4.png" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="/cdn/css/share.min.css" media="print" onload="this.media='all'"><script src="/cdn/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><br/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="far fa-comment-alt fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="gitalk-container"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/loading.gif'" alt="avatar"/></div><div class="author-info__name">Jason</div><div class="author-info__description">Debug the World!</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">60</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">39</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><a id="card-info-btn"><i class="fab fa-microsoft"></i><span>Ctrl + D 收藏</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/JasonsGong" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=2602183349&website=www.oicqzone.com" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="mailto:2602183349@qq.com" target="_blank" title="Email"><i class="fas fa-envelope-open-text"></i></a><a class="social-icon" href="https://github.com/JasonsGong?tab=repositories" target="_blank" title="代码仓库"><i class="fas fa-database"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">本网站是静态网站,更新页面资源请使用Ctrl+F5;若网站内文章对你有帮助,请使用Ctrl+D收藏该网站!</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80-%E7%BB%8F%E5%85%B8%E7%AE%97%E6%B3%95%E9%97%AE%E9%A2%98"><span class="toc-text">一.经典算法问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95%E7%9A%84%E6%A6%82%E8%BF%B0"><span class="toc-text">二.数据结构与算法的概述</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95%E7%9A%84%E5%85%B3%E7%B3%BB"><span class="toc-text">2.1 数据结构与算法的关系</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2%E8%A7%A3%E5%86%B3%E5%AE%9E%E9%99%85%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-text">2.2解决实际的问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84"><span class="toc-text">2.3 数据结构</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89-%E7%A8%80%E7%96%8F%E6%95%B0%E7%BB%84%E5%92%8C%E9%98%9F%E5%88%97"><span class="toc-text">三.稀疏数组和队列</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1%E7%A8%80%E7%96%8F%E6%95%B0%E7%BB%84"><span class="toc-text">3.1稀疏数组</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-1-%E5%9F%BA%E6%9C%AC%E7%9A%84%E4%BB%8B%E7%BB%8D"><span class="toc-text">3.1.1 基本的介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-2%E4%BD%BF%E7%94%A8%E7%9A%84%E5%9C%BA%E6%99%AF"><span class="toc-text">3.1.2使用的场景</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-3%E5%9B%BE%E8%A7%A3"><span class="toc-text">3.1.3图解</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-5%E5%AE%9E%E7%8E%B0%E7%9A%84%E6%80%9D%E8%B7%AF"><span class="toc-text">3.1.5实现的思路</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-5%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">3.1.5代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2%E9%98%9F%E5%88%97"><span class="toc-text">3.2队列</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-1%E5%9F%BA%E6%9C%AC%E7%9A%84%E4%BB%8B%E7%BB%8D"><span class="toc-text">3.2.1基本的介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-2%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">3.2.2代码实现</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-3-%E6%95%B0%E7%BB%84%E6%A8%A1%E6%8B%9F%E7%8E%AF%E5%BD%A2%E9%98%9F%E5%88%97"><span class="toc-text">3.2.3 数组模拟环形队列</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B-%E9%93%BE%E8%A1%A8"><span class="toc-text">四.链表</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E9%93%BE%E8%A1%A8-Linked-List-%E4%BB%8B%E7%BB%8D"><span class="toc-text">4.1 链表(Linked List)介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link"><span class="toc-text"></span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2%E5%8D%95%E9%93%BE%E8%A1%A8%E7%9A%84%E5%BA%94%E7%94%A8%E5%AE%9E%E4%BE%8B-CRUD"><span class="toc-text">4.2单链表的应用实例(CRUD)</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-1%E5%90%91%E5%8D%95%E9%93%BE%E8%A1%A8%E4%B8%AD%E6%B7%BB%E5%8A%A0%E6%95%B0%E6%8D%AE"><span class="toc-text">4.2.1向单链表中添加数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-2%E4%BF%AE%E6%94%B9%E5%8D%95%E9%93%BE%E8%A1%A8%E4%B8%AD%E8%8A%82%E7%82%B9%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">4.2.2修改单链表中节点的方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-3%E5%88%A0%E9%99%A4%E5%8D%95%E9%93%BE%E8%A1%A8%E4%B8%AD%E8%8A%82%E7%82%B9%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">4.2.3删除单链表中节点的方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-4%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81"><span class="toc-text">4.2.4完整代码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-5-%E5%8D%95%E9%93%BE%E8%A1%A8%E7%9A%84%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%88%E6%96%B0%E6%B5%AA%EF%BC%8C%E7%99%BE%E5%BA%A6%EF%BC%8C%E8%85%BE%E8%AE%AF%EF%BC%89"><span class="toc-text">4.2.5 单链表的面试题(新浪,百度,腾讯)</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E6%B1%82%E5%8D%95%E9%93%BE%E8%A1%A8%E4%B8%AD%E6%9C%89%E6%95%88%E8%8A%82%E7%82%B9%E7%9A%84%E4%B8%AA%E6%95%B0"><span class="toc-text">1.求单链表中有效节点的个数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-%E6%9F%A5%E6%89%BE%E5%8D%95%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACK%E4%B8%AA%E8%8A%82%E7%82%B9-%E6%96%B0%E6%B5%AA%E9%9D%A2%E8%AF%95%E9%A2%98"><span class="toc-text">2.查找单链表中的倒数第K个节点(新浪面试题)</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-%E5%8D%95%E9%93%BE%E8%A1%A8%E7%9A%84%E5%8F%8D%E8%BD%AC-%E8%85%BE%E8%AE%AF%E9%9D%A2%E8%AF%95%E9%A2%98"><span class="toc-text">3.单链表的反转(腾讯面试题)</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-%E4%BB%8E%E5%B0%BE%E5%88%B0%E5%A4%B4%E6%89%93%E5%8D%B0%E5%8D%95%E9%93%BE%E8%A1%A8%EF%BC%88%E7%99%BE%E5%BA%A6%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%89"><span class="toc-text">4.从尾到头打印单链表(百度面试题)</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-%E5%90%88%E5%B9%B6%E4%B8%A4%E4%B8%AA%E6%9C%89%E5%BA%8F%E7%9A%84%E5%8D%95%E9%93%BE%E8%A1%A8%EF%BC%8C%E5%90%88%E5%B9%B6%E4%B9%8B%E5%90%8E%E7%9A%84%E9%93%BE%E8%A1%A8%E4%BE%9D%E7%84%B6%E6%9C%89%E5%BA%8F"><span class="toc-text">5.合并两个有序的单链表,合并之后的链表依然有序</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%A8%E9%83%A8%E4%BB%A3%E7%A0%81"><span class="toc-text">全部代码</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-%E5%8F%8C%E5%90%91%E9%93%BE%E8%A1%A8"><span class="toc-text">4.3 双向链表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-%E5%8D%95%E5%90%91%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8%E7%9A%84%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF-Josephu%E9%97%AE%E9%A2%98"><span class="toc-text">4.4 单向环形链表的应用场景(Josephu问题)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94-%E6%A0%88"><span class="toc-text">五.栈</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E4%BB%8B%E7%BB%8D"><span class="toc-text">1.介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-text">2.应用场景</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%9B%BE%E8%A7%A3"><span class="toc-text">3.图解</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E4%BD%BF%E7%94%A8%E6%95%B0%E7%BB%84%E6%A8%A1%E6%8B%9F%E6%A0%88"><span class="toc-text">4.使用数组模拟栈</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E6%A0%88%E5%AE%9E%E7%8E%B0%E7%BB%BC%E5%90%88%E8%AE%A1%E7%AE%97%E5%99%A8"><span class="toc-text">5.栈实现综合计算器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E9%80%86%E6%B3%A2%E5%85%B0%E8%AE%A1%E7%AE%97%E5%99%A8%E7%9A%84%E8%AE%BE%E8%AE%A1%E4%B8%8E%E5%AE%9E%E7%8E%B0"><span class="toc-text">6.逆波兰计算器的设计与实现</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD-%E9%80%92%E5%BD%92"><span class="toc-text">六.递归</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E7%AE%80%E5%8D%95%E4%BB%8B%E7%BB%8D"><span class="toc-text">1.简单介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B"><span class="toc-text">2.入门案例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E9%80%92%E5%BD%92%E8%A7%A3%E5%86%B3%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-text">3.递归解决的问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E9%80%92%E5%BD%92%E9%81%B5%E5%BE%AA%E7%9A%84%E8%A7%84%E5%88%99"><span class="toc-text">4.递归遵循的规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E9%80%92%E5%BD%92%E7%9A%84%E5%AE%9E%E9%99%85%E5%BA%94%E7%94%A8"><span class="toc-text">5.递归的实际应用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1%E9%80%92%E5%BD%92%E8%A7%A3%E5%86%B3%E8%BF%B7%E5%AE%AB%E9%97%AE%E9%A2%98"><span class="toc-text">5.1递归解决迷宫问题</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-%E9%80%92%E5%BD%92%E8%A7%A3%E5%86%B3%E5%85%AB%E7%9A%87%E5%90%8E%E9%97%AE%E9%A2%98"><span class="toc-text">5.2 递归解决八皇后问题</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%83-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95"><span class="toc-text">七.排序算法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E4%BB%8B%E7%BB%8D-1"><span class="toc-text">1.介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E6%97%B6%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A6"><span class="toc-text">2.时间复杂度</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E7%A9%BA%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A6"><span class="toc-text">3.空间复杂度</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F"><span class="toc-text">4.冒泡排序</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-text">4.1 基本介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-%E5%9B%BE%E8%A7%A3"><span class="toc-text">4.2 图解</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">4.3 代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F"><span class="toc-text">5.选择排序</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-text">5.1 基本介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2%E5%9B%BE%E8%A7%A3"><span class="toc-text">5.2图解</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-3%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">5.3代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F"><span class="toc-text">6.插入排序</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#6-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-text">6.1 基本介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-2-%E5%9B%BE%E8%A7%A3"><span class="toc-text">6.2 图解</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-3-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">6.3 代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F"><span class="toc-text">7.希尔排序</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#7-1%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-text">7.1基本介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-2-%E5%9B%BE%E8%A7%A3"><span class="toc-text">7.2.图解</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-3-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">7.3 代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F"><span class="toc-text">8.快速排序</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#8-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-text">8.1 基本介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-2-%E5%9B%BE%E8%A7%A3"><span class="toc-text">8.2 图解</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-3-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">8.3 代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#9-%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F"><span class="toc-text">9. 归并排序</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#9-1%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-text">9.1基本介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#9-2-%E5%9B%BE%E8%A7%A3"><span class="toc-text">9.2 图解</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#9-3%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">9.3代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-%E5%9F%BA%E6%95%B0%E6%8E%92%E5%BA%8F"><span class="toc-text">10.基数排序</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#10-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-text">10.1 基本介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-2-%E5%9B%BE%E8%A7%A3"><span class="toc-text">10.2 图解</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-3-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">10.3 代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#11-%E5%B8%B8%E7%94%A8%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95%E6%80%BB%E7%BB%93%E5%92%8C%E5%AF%B9%E6%AF%94"><span class="toc-text">11.常用排序算法总结和对比</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AB-%E6%9F%A5%E6%89%BE%E7%AE%97%E6%B3%95"><span class="toc-text">八.查找算法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E7%AE%80%E5%8D%95%E4%BB%8B%E7%BB%8D-1"><span class="toc-text">1.简单介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E7%BA%BF%E6%80%A7%E6%9F%A5%E6%89%BE%E7%AE%97%E6%B3%95"><span class="toc-text">2.线性查找算法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">2.1代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%E7%AE%97%E6%B3%95"><span class="toc-text">3.二分查找算法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-%E5%9B%BE%E8%A7%A3"><span class="toc-text">3.1 图解</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">3.2 代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E6%8F%92%E5%80%BC%E6%9F%A5%E6%89%BE%E7%AE%97%E6%B3%95"><span class="toc-text">4.插值查找算法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-%E5%9B%BE%E8%A7%A3"><span class="toc-text">4.1 图解</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">4.2 代码实现</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-text">4.3 注意事项</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%EF%BC%88%E9%BB%84%E9%87%91%E5%88%86%E5%89%B2%E6%B3%95%EF%BC%89%E6%9F%A5%E6%89%BE%E7%AE%97%E6%B3%95"><span class="toc-text">5.斐波那契(黄金分割法)查找算法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D-1"><span class="toc-text">5.1 基本介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-%E5%8E%9F%E7%90%86%E4%BB%8B%E7%BB%8D"><span class="toc-text">5.2 原理介绍</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#PDF%E7%AC%94%E8%AE%B0"><span class="toc-text">PDF笔记</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最近更新</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/8957.html" title="Linux从入门到进阶"><img src="/img/8.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Linux从入门到进阶"/></a><div class="content"><a class="title" href="/posts/8957.html" title="Linux从入门到进阶">Linux从入门到进阶</a><time datetime="2024-05-10T05:31:11.691Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/35630.html" title="接口测试工具"><img src="/img/2.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="接口测试工具"/></a><div class="content"><a class="title" href="/posts/35630.html" title="接口测试工具">接口测试工具</a><time datetime="2024-05-10T05:31:11.686Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/32246.html" title="SpringBoot中整合Swagger2"><img src="/img/3.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="SpringBoot中整合Swagger2"/></a><div class="content"><a class="title" href="/posts/32246.html" title="SpringBoot中整合Swagger2">SpringBoot中整合Swagger2</a><time datetime="2024-05-10T05:31:11.681Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/19306.html" title="Docker容器化技术"><img src="/img/1.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Docker容器化技术"/></a><div class="content"><a class="title" href="/posts/19306.html" title="Docker容器化技术">Docker容器化技术</a><time datetime="2024-05-10T05:31:11.675Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/20683.html" title="Linux中开发环境的搭建"><img src="/img/8.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Linux中开发环境的搭建"/></a><div class="content"><a class="title" href="/posts/20683.html" title="Linux中开发环境的搭建">Linux中开发环境的搭建</a><time datetime="2024-05-10T05:31:11.669Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comment-alt"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/cdn/js/medium-zoom.min.js"></script><script src="/cdn/js/instantpage.min.js" type="module"></script><script src="/cdn/js/snackbar.min.js"></script><div class="js-pjax"><script>function loadGitalk () {
|
||
function initGitalk () {
|
||
var gitalk = new Gitalk(Object.assign({
|
||
clientID: '00fb27b1e484536359c2',
|
||
clientSecret: 'be41a12281c68b6e228d1a27e8d08aeb91541145',
|
||
repo: 'BlogComment',
|
||
owner: 'JasonsGong',
|
||
admin: ['JasonsGong'],
|
||
id: '6ebda012d4e8be7e5b2b8b4687b857f7',
|
||
updateCountCallback: commentCount
|
||
},null))
|
||
|
||
gitalk.render('gitalk-container')
|
||
}
|
||
|
||
if (typeof Gitalk === 'function') initGitalk()
|
||
else {
|
||
getCSS('/cdn/css/gitalk.min.css')
|
||
getScript('/cdn/js/gitalk.min.js').then(initGitalk)
|
||
}
|
||
}
|
||
|
||
function commentCount(n){
|
||
let isCommentCount = document.querySelector('#post-meta .gitalk-comment-count')
|
||
if (isCommentCount) {
|
||
isCommentCount.textContent= n
|
||
}
|
||
}
|
||
|
||
if ('Gitalk' === 'Gitalk' || !true) {
|
||
if (true) btf.loadComment(document.getElementById('gitalk-container'), loadGitalk)
|
||
else loadGitalk()
|
||
} else {
|
||
function loadOtherComment () {
|
||
loadGitalk()
|
||
}
|
||
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span> 数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><br/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div><!-- hexo injector body_end start --><script data-pjax>
|
||
function butterfly_swiper_injector_config(){
|
||
var parent_div_git = document.getElementById('recent-posts');
|
||
var item_html = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/19306.html" alt=""><img width="48" height="48" src="/img/1.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-04-21</span><a class="blog-slider__title" href="posts/19306.html" alt="">Docker容器化技术</a><div class="blog-slider__text">Docker</div><a class="blog-slider__button" href="posts/19306.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/47003.html" alt=""><img width="48" height="48" src="/img/5.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-03-10</span><a class="blog-slider__title" href="posts/47003.html" alt="">常用正则表达式大全</a><div class="blog-slider__text">正则表达式</div><a class="blog-slider__button" href="posts/47003.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/20683.html" alt=""><img width="48" height="48" src="/img/8.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-06-05</span><a class="blog-slider__title" href="posts/20683.html" alt="">Linux中开发环境的搭建</a><div class="blog-slider__text">环境搭建</div><a class="blog-slider__button" href="posts/20683.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/63333.html" alt=""><img width="48" height="48" src="/img/10.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-06-03</span><a class="blog-slider__title" href="posts/63333.html" alt="">开发环境的搭建</a><div class="blog-slider__text">环境搭建</div><a class="blog-slider__button" href="posts/63333.html" alt="">详情 </a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
|
||
if (parent_div_git !== null && typeof parent_div_git !== 'undefined') {
|
||
parent_div_git.insertAdjacentHTML("afterbegin",item_html)
|
||
}
|
||
}
|
||
var elist = 'undefined'.split(',');
|
||
var cpage = location.pathname;
|
||
var epage = 'all';
|
||
var flag = 0;
|
||
|
||
for (var i=0;i<elist.length;i++){
|
||
if (cpage.includes(elist[i])){
|
||
flag++;
|
||
}
|
||
}
|
||
|
||
if ((epage ==='all')&&(flag == 0)){
|
||
butterfly_swiper_injector_config();
|
||
}
|
||
else if (epage === cpage){
|
||
butterfly_swiper_injector_config();
|
||
}
|
||
</script><script defer src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.js"></script><script defer data-pjax src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper_init.js"></script><!-- hexo injector body_end end --></body></html> |