c语言基础-结构构体

<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>

点击打开链接

基于STM32F407,使用DFS算法实现最短迷宫路径检索,分为三种模式:1.DEBUG模式,2. 训练模式,3. 主程序模式 ,DEBUG模式主要分析bug,测量必要数据,训练模式用于DFS算法训练最短路径,并将最短路径以链表形式存储Flash, 主程序模式从Flash中….zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值