c++单向链表排序(c++单链表类)

c++单向链表排序(c++单链表类)define CRT SECURE NO WARNINGS include lt stdio h gt include lt stdlib h gt struct Node span style color rgba 0 0 255 1 int lt span

大家好,我是讯享网,很高兴认识大家。



#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> struct Node { 
讯享网</span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id; Node</span>*<span style="color: rgba(0, 0, 0, 1)"> next; 
}; //初始化头节点 Node* init_m_head() {
Node</span>* temp = (Node*)malloc(<span style="color: rgba(0, 0, 255, 1)">sizeof</span><span style="color: rgba(0, 0, 0, 1)">(Node)); temp</span>-&gt;id = <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">; temp</span>-&gt;next =<span style="color: rgba(0, 0, 0, 1)"> NULL; </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> temp; 
} //初始化子节点 Node* init_Node(int num) {
讯享网Node</span>* temp= (Node*)malloc(<span style="color: rgba(0, 0, 255, 1)">sizeof</span><span style="color: rgba(0, 0, 0, 1)">(Node)); temp</span>-&gt;id =<span style="color: rgba(0, 0, 0, 1)"> num; temp</span>-&gt;next =<span style="color: rgba(0, 0, 0, 1)"> NULL; </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> temp; 
} //节点链接 void nodelink(Node* n1, Node* n2) {
n2</span>-&gt;next = n1-&gt;<span style="color: rgba(0, 0, 0, 1)">next; n1</span>-&gt;next =<span style="color: rgba(0, 0, 0, 1)"> n2; 
} //节点交换 void swap(Node* n1, Node* n2) {
讯享网
讯享网Node</span>* temp =<span style="color: rgba(0, 0, 0, 1)"> n1; n1 </span>=<span style="color: rgba(0, 0, 0, 1)"> n2; n2 </span>=<span style="color: rgba(0, 0, 0, 1)"> temp; 
} //void maopao(Node* list) // { // Node* temp = list-&gt;next; // Node* pre = list; // while (temp != NULL) // { // if (temp-&gt;id &gt; temp-&gt;next-&gt;id) // {
// pre-&gt;next = temp-&gt;next; //头节点挂到temp的节点后面 // temp-&gt;next = temp-&gt;next-&gt;next; // temp-&gt;next-&gt;next = temp; // // } // pre = temp; // // temp = temp-&gt;next; //
// } // }
void maopao( Node * head) {
 Node </span>* p, *q, *<span style="color: rgba(0, 0, 0, 1)">tail; tail </span>=<span style="color: rgba(0, 0, 0, 1)"> NULL; </span><span style="color: rgba(0, 0, 255, 1)">while</span> ((head-&gt;next-&gt;next) !=<span style="color: rgba(0, 0, 0, 1)"> tail) { p </span>=<span style="color: rgba(0, 0, 0, 1)"> head; q </span>= head-&gt;<span style="color: rgba(0, 0, 0, 1)">next; </span><span style="color: rgba(0, 0, 255, 1)">while</span> (q-&gt;next !=<span style="color: rgba(0, 0, 0, 1)"> tail) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> ((q-&gt;id) &gt; (q-&gt;next-&gt;<span style="color: rgba(0, 0, 0, 1)">id)) { p</span>-&gt;next = q-&gt;<span style="color: rgba(0, 0, 0, 1)">next; q</span>-&gt;next = q-&gt;next-&gt;<span style="color: rgba(0, 0, 0, 1)">next; p</span>-&gt;next-&gt;next =<span style="color: rgba(0, 0, 0, 1)"> q; q </span>= p-&gt;<span style="color: rgba(0, 0, 0, 1)">next; } q </span>= q-&gt;<span style="color: rgba(0, 0, 0, 1)">next; p </span>= p-&gt;<span style="color: rgba(0, 0, 0, 1)">next; } tail </span>=<span style="color: rgba(0, 0, 0, 1)"> q; } 
} void node_printf(Node*list) {
讯享网Node</span>* temp = list-&gt;<span style="color: rgba(0, 0, 0, 1)">next; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (temp ==<span style="color: rgba(0, 0, 0, 1)"> NULL) { printf(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">空表! 
);
 </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">; } printf(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">链表打印为: 
);
讯享网</span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (temp) { printf(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%d </span><span style="color: rgba(128, 0, 0, 1)">"</span>, temp-&gt;<span style="color: rgba(0, 0, 0, 1)">id); temp </span>= temp-&gt;<span style="color: rgba(0, 0, 0, 1)">next; } 
} int main() {
Node</span>* temp=init_m_head();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">初始化头节点</span> Node* n1 = init_Node(<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">); Node</span>* n2 = init_Node(<span style="color: rgba(128, 0, 128, 1)">5</span><span style="color: rgba(0, 0, 0, 1)">); Node</span>* n3 = init_Node(<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">); Node</span>* n4 = init_Node(<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">); nodelink(temp, n1); nodelink(temp, n2); nodelink(temp, n3); nodelink(temp, n4); maopao(temp); node_printf(temp); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">; 
}

讯享网

小讯
上一篇 2025-04-21 15:13
下一篇 2025-06-07 09:05

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/201267.html