当前位置:网站首页 > C++编程 > 正文

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



#define _CRT_SECURE_NO_WARNINGS #include  
 
   
    
  
    
     #include  
  
    
     
     struct 
      Node {  
     int 
      id; Node* 
      next; };  
     // 
     初始化头节点 Node* 
      init_m_head() { Node* temp = (Node*)malloc( 
     sizeof 
     (Node)); temp->id = 
     0 
     ; temp->next = 
      NULL;  
     return 
      temp; }  
     // 
     初始化子节点 
      Node* init_Node( 
     int 
      num) { Node* temp= (Node*)malloc( 
     sizeof 
     (Node)); temp->id = 
      num; temp->next = 
      NULL;  
     return 
      temp; }  
     // 
     节点链接 
     void nodelink(Node* n1, Node* 
      n2) { n2->next = n1-> 
     next; n1->next = 
      n2; }  
     // 
     节点交换 
     void swap(Node* n1, Node* 
      n2) { Node* temp = 
      n1; n1 = 
      n2; n2 = 
      temp; }  
     // 
     void maopao(Node* list)  
     // 
      {  
     // 
      Node* temp = list->next;  
     // 
      Node* pre = list;  
     // 
      while (temp != NULL)  
     // 
      {  
     // 
      if (temp->id > temp->next->id)  
     // 
      {  
     // 
      pre->next = temp->next;  
     // 
     头节点挂到temp的节点后面  
     // 
      temp->next = temp->next->next;  
     // 
      temp->next->next = temp;  
     // 
     // 
      }  
     // 
      pre = temp;  
     // 
     // 
      temp = temp->next;  
     // 
     // 
      }  
     // 
      } 
     void maopao( Node * 
      head) { Node * p, *q, * 
     tail; tail = 
      NULL;  
     while ((head->next->next) != 
      tail) { p = 
      head; q = head-> 
     next;  
     while (q->next != 
      tail) {  
     if ((q->id) > (q->next-> 
     id)) { p->next = q-> 
     next; q->next = q->next-> 
     next; p->next->next = 
      q; q = p-> 
     next; } q = q-> 
     next; p = p-> 
     next; } tail = 
      q; } }  
     void node_printf(Node* 
     list) { Node* temp = list-> 
     next;  
     if (temp == 
      NULL) { printf( 
     " 
     空表!  
     " 
     );  
     return 
     ; } printf( 
     " 
     链表打印为:  
     " 
     );  
     while 
      (temp) { printf( 
     " 
     %d  
     ", temp-> 
     id); temp = temp-> 
     next; } }  
     int 
      main() { Node* temp=init_m_head(); 
     // 
     初始化头节点 Node* n1 = init_Node( 
     1 
     ); Node* n2 = init_Node( 
     5 
     ); Node* n3 = init_Node( 
     2 
     ); Node* n4 = init_Node( 
     3 
     ); nodelink(temp, n1); nodelink(temp, n2); nodelink(temp, n3); nodelink(temp, n4); maopao(temp); node_printf(temp);  
     return 
     0 
     ; } 
     
 
   
   
到此这篇c++单向链表排序(c++单链表类)的文章就 介绍到这了,更多相关内容请继续浏览下面的相关 推荐文章,希望大家都能在 编程的领域有一番成就!

版权声明


相关文章:

  • cpu参数对比工具(cpu参数对比工具2024最新版本)2026-05-25 23:18:08
  • apc和对乙酰氨基酚片区别(对乙酰氨基酚和阿咖酚散)2026-05-25 23:18:08
  • c++中数组合并(c++数组拼接函数)2026-05-25 23:18:08
  • autoit无法获取的控件(无法获取application)2026-05-25 23:18:08
  • consultation翻译(consultancy翻译)2026-05-25 23:18:08
  • xdc是什么意思?(xdc是什么意思呢)2026-05-25 23:18:08
  • vscode gcc编译器(vs code 编译器)2026-05-25 23:18:08
  • cnn算法优缺点(cnn的优缺点)2026-05-25 23:18:08
  • xpac官网(xpj官网)2026-05-25 23:18:08
  • tcpip工具(tcping工具)2026-05-25 23:18:08
  • 全屏图片