<div id="article_content" class="article_content csdn-tracking-statistics tracking-click" data-mod="popu_519" data-dsm="post" style="overflow: hidden;"> <div class="markdown_views"> <h2 id="概念"><a name="t0"></a>概念</h2> <p>我们先说说线性表,线性表的基本特点:</p> <ul> <li>同一性:线性表存储的所有数据都是同类型数据</li> <li>有穷性:线性表的数据个数是有限的,表长为数据中元素的个数(连续)</li> <li>有序性:相邻元素存在序偶关系</li> </ul> <p>顺序存储:内存中用一块地址连续的存储空间存储数据 <br> 采用顺序存储的线性表教顺序表</p> <h2 id="实现过程"><a name="t1"></a>实现过程</h2> <p><font color="#E84348">顺序表结构体</font></p> <pre class="prettyprint"><code class="hljs cs has-numbering"><span class="hljs-preprocessor">#<span class="hljs-keyword">define</span> MAXSIZE 100</span> <span class="hljs-preprocessor">#<span class="hljs-keyword">define</span> FALSE 0</span> <span class="hljs-preprocessor">#<span class="hljs-keyword">define</span> TRUE 1</span> typedef <span class="hljs-keyword">struct</span> { <span class="hljs-keyword">int</span> elem[MAXSIZE]; <span class="hljs-comment">//每个数组元素存储一个int型的数据,最多存MAXSIZE个</span> <span class="hljs-keyword">int</span> length; <span class="hljs-comment">//当前线性表存储的元素数据个数</span> }seqList;</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre> <p><font color="#E84348">顺序表初始化</font></p> <pre class="prettyprint"><code class="hljs glsl has-numbering"><span class="hljs-keyword">void</span> initSeqList(seqList *tempList) { tempList-><span class="hljs-built_in">length</span> = <span class="hljs-number">0</span>;<span class="hljs-comment">//视具体学习应用情况对elem数组初始化</span> }</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li></ul></pre> <p><font color="#E84348">顺序表创建(伪代码)</font></p> <pre class="prettyprint"><code class="hljs cpp has-numbering">seqList tempList; <span class="hljs-comment">//实例一个线性表</span> <span class="hljs-keyword">int</span> temp; <span class="hljs-comment">//循环存储,大小100</span> <span class="hljs-keyword">for</span>(temp = <span class="hljs-number">0</span>; temp < <span class="hljs-number">100</span>; temp ++) { <span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &tempList->elem[temp]); }</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li></ul></pre> <p><font color="#E84348">顺序表插入数据</font></p> <pre class="prettyprint"><code class="hljs perl has-numbering">//线性表的插入,向线性表表tempList的dest位置插入num, dest位置从<span class="hljs-number">1</span>开始计算 <span class="hljs-keyword">int</span> insertSeqList(seqList <span class="hljs-variable">*tempList</span>, <span class="hljs-keyword">int</span> dest, <span class="hljs-keyword">int</span> num) { <span class="hljs-keyword">int</span> temp; <span class="hljs-regexp">//</span>判满 <span class="hljs-keyword">if</span>(tempList-><span class="hljs-keyword">length</span> == MAXSIZE-<span class="hljs-number">1</span>) { <span class="hljs-keyword">printf</span>(<span class="hljs-string">"线性表已表满!"</span>); <span class="hljs-keyword">return</span> FALSE; } //判断dest的正确性,tempList-><span class="hljs-keyword">length</span> + <span class="hljs-number">1</span>,添加在最后面 <span class="hljs-keyword">if</span>(dest < <span class="hljs-number">1</span> || dest > tempList-><span class="hljs-keyword">length</span> + <span class="hljs-number">1</span>) { <span class="hljs-keyword">printf</span>(<span class="hljs-string">"插入的位置错误!"</span>); <span class="hljs-keyword">return</span> FALSE; } //正确执行操作 <span class="hljs-keyword">for</span>(temp = tempList-><span class="hljs-keyword">length</span>; temp >= dest; temp--) { tempList->elem[temp] = tempList->elem[temp-<span class="hljs-number">1</span>]; } tempList->elem[temp] = num; tempList-><span class="hljs-keyword">length</span> ++; <span class="hljs-keyword">return</span> TRUE; }</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li></ul></pre> <p><font color="#E84348">顺序表删除数据</font></p> <pre class="prettyprint"><code class="hljs perl has-numbering">//线性表删除 <span class="hljs-keyword">int</span> deleteSeqList(seqList <span class="hljs-variable">*tempList</span>, <span class="hljs-keyword">int</span> dest) { <span class="hljs-keyword">int</span> temp; <span class="hljs-regexp">//</span>判空 <span class="hljs-keyword">if</span>(tempList-><span class="hljs-keyword">length</span> == <span class="hljs-number">0</span>) { <span class="hljs-keyword">printf</span>(<span class="hljs-string">"线性表为空!"</span>); <span class="hljs-keyword">return</span> FALSE; } //不能删除不存在的位置 <span class="hljs-keyword">if</span>(dest < <span class="hljs-number">1</span> || dest > tempList-><span class="hljs-keyword">length</span>) { <span class="hljs-keyword">printf</span>(<span class="hljs-string">"删除的位置不存在!"</span>); <span class="hljs-keyword">return</span> FALSE; } <span class="hljs-keyword">for</span>(temp = dest; temp < tempList-><span class="hljs-keyword">length</span>; temp ++) { tempList->elem[temp-<span class="hljs-number">1</span>] = tempList->elem[temp]; } tempList-><span class="hljs-keyword">length</span> --; <span class="hljs-keyword">return</span> TRUE; }</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li></ul></pre> <p><font color="#E84348">顺序表修改数据</font></p> <pre class="prettyprint"><code class="hljs objectivec has-numbering"><span class="hljs-comment">//把线性表表tempList的dest位置改为num, dest位置从1开始计算</span> <span class="hljs-keyword">int</span> changeSeqList(seqList *tempList, <span class="hljs-keyword">int</span> dest, <span class="hljs-keyword">int</span> num) { <span class="hljs-comment">//判空</span> <span class="hljs-keyword">if</span>(tempList->length == <span class="hljs-number">0</span>) { printf(<span class="hljs-string">"线性表为空!"</span>); <span class="hljs-keyword">return</span> <span class="hljs-literal">FALSE</span>; } <span class="hljs-comment">//不能替换不存在的位置</span> <span class="hljs-keyword">if</span>(dest < <span class="hljs-number">1</span> || dest > tempList->length) { printf(<span class="hljs-string">"替换的位置不存在!"</span>); <span class="hljs-keyword">return</span> <span class="hljs-literal">FALSE</span>; } tempList->elem[dest-<span class="hljs-number">1</span>] = num; <span class="hljs-keyword">return</span> <span class="hljs-literal">TRUE</span>; }</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li></ul></pre> <p><font color="#E84348">顺序表查找数据</font></p> <pre class="prettyprint"><code class="hljs lasso has-numbering"><span class="hljs-comment">//线性表查找</span> int searchSeqList(seqList <span class="hljs-subst">*</span>tempList, int dest) { <span class="hljs-comment">//判空</span> <span class="hljs-keyword">if</span>(tempList<span class="hljs-subst">-></span>length <span class="hljs-subst">==</span> <span class="hljs-number">0</span>) { printf(<span class="hljs-string">"线性表为空!"</span>); <span class="hljs-keyword">return</span> <span class="hljs-literal">FALSE</span>; } <span class="hljs-comment">//不能查找不存在的位置</span> <span class="hljs-keyword">if</span>(dest <span class="hljs-subst"><</span> <span class="hljs-number">1</span> <span class="hljs-subst">||</span> dest <span class="hljs-subst">></span> tempList<span class="hljs-subst">-></span>length) { printf(<span class="hljs-string">"查找的位置不存在!"</span>); <span class="hljs-keyword">return</span> <span class="hljs-literal">FALSE</span>; } <span class="hljs-keyword">return</span> tempList<span class="hljs-subst">-></span>elem<span class="hljs-preprocessor">[</span>dest<span class="hljs-subst">-</span><span class="hljs-number">1</span><span class="hljs-preprocessor">]</span><span class="hljs-markup">; }</span></code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li></ul></pre> <p>在理解顺序表的时候一定要理解几点:</p> <ul> <li>顺序表数据一定连续,中间不存在空数据</li> <li>每个数据的位置是一定的,实现随机存取</li> <li>顺序表的存储空间是静态分配的</li> <li>顺序表是基于数组的,所以语言环境很大</li> <li>顺序表侧重实现查找</li> </ul> <h2 id="实例实现"><a name="t2"></a>实例实现</h2> <p><img src="https://img-blog.csdn.net/20160126000048655" alt="这里写图片描述" title=""></p> <p>实例代码可以点击<a href="https://github.com/52UUD/Algorithm-Design/blob/master/%E9%A1%BA%E5%BA%8F%E8%A1%A8/%E9%A1%BA%E5%BA%8F%E8%A1%A8%E7%9A%84%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5%E5%AE%9E%E7%8E%B0.c" target="_blank">Demo</a>查看</p> <p><font color="#e84384">更多数据结构与算法实例代码查看(下载)地址:</font><a href="https://github.com/52UUD/Algorithm-Design" target="_blank">https://github.com/52UUD/Algorithm-Design</a></p> </div> <link rel="stylesheet" href="http://csdnimg.cn/release/phoenix/production/markdown_views-993eb3f29c.css"> </div>
点击打开链接
c语言基础-结构构体
最新推荐文章于 2024-03-12 21:29:21 发布