<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 == NULL ||stu->next == 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=stu->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=p1->next;<br> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> p1->next=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=p2->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->next = 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=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=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("p1 = %d,next = %d\n ",p1->number,p1->next->number);<br> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <wbr></wbr><wbr></wbr> <span style="color:#0b7ef">stu->next=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=705eb43a0100ogtt&url=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=705eb43a0100ogtt&url=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->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 |
|