单链表逆序详解

论坛 期权论坛     
选择匿名的用户   2021-5-30 02:26   216   0
<p> <a href="http://blog.sina.com.cn/s/blog_71319eae0100tlz7.html">http://blog.sina.com.cn/s/blog_71319eae0100tlz7.html</a></p>
<p>1、具有链表头的单链表<br> 一段单链表逆序的程序 <wbr></wbr><wbr></wbr><br> typedef struct student<br> {<!-- --><br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr>  <wbr></wbr><wbr></wbr>int number;<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr>  <wbr></wbr><wbr></wbr>char name[20];<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr>  <wbr></wbr><wbr></wbr>int score;<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr>  <wbr></wbr><wbr></wbr>struct student *next;<br> }student;<br> <br> <br> student *reverse(student *stu)<br> {<!-- --><br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> student *p1,*p2,*p3;<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> if(stu &#61;&#61; NULL ||stu-&gt;next &#61;&#61; NULL)<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> return stu;<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <span style="color:#0b7ef"><span style="color:#0b7ef"></span>p1&#61;stu-&gt;next<span style="color:#0b7ef">;</span> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> </span><wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> //p1指向链表头节点的下一个节点<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> p2&#61;p1-&gt;next;<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> p1-&gt;next&#61;NULL;<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> while(p2)<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> {<!-- --><br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> p3&#61;p2-&gt;next;<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> p2-&gt;next &#61; p1;<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> p1&#61;p2;<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> p2&#61;p3;<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> }<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> printf(&#34;p1 &#61; %d,next &#61; %d\n &#34;,p1-&gt;number,p1-&gt;next-&gt;number);<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <span style="color:#0b7ef">stu-&gt;next&#61;p1;</span> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> //将链表头节点指向p1<br>  <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> return stu;<br> }<br> <br> 分析:<br> 假设需要逆序的单链表为:<br> <a href="http://photo.blog.sina.com.cn/showpic.html#blogid&#61;705eb43a0100ogtt&amp;url&#61;http://s3.sinaimg.cn/orignal/705eb43at99cab5e9a672" rel="noopener noreferrer" target="_blank"><img alt="单链表逆序详解" name="image_operate_96961294996903377" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-6806796e9755feb65b91169dc6b4d514" title="单链表逆序详解"></a><br> <br> 则逆序以后的链表为:<br> <a href="http://photo.blog.sina.com.cn/showpic.html#blogid&#61;705eb43a0100ogtt&amp;url&#61;http://s9.sinaimg.cn/orignal/705eb43at99cabd5c4ab8" rel="noopener noreferrer" target="_blank"><img alt="单链表逆序详解" name="image_operate_54821294996903533" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-9c2e66ae692f66ad60703d997e91e67b" title="单链表逆序详解"> </a><br> <br> 过程:<br> (1)取p1指向header-&gt;next <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><w
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP