当前位置:网站首页 > 区块链基础 > 正文

单向链表逆置(单向链表的逆转)



#include "stdafx.h" #include  
 
   
    
  
    
     #include  
  
    
     
     using 
     namespace 
      std;  
     struct 
      ListNode {  
     int 
      m_nKey; ListNode* 
      m_pNext; };  
     // 
     构造链表 
     void CreateList(ListNode *& 
     pHead) { fstream fin( 
     " 
     list.txt 
     " 
     ); ListNode *pNode = 
      NULL; ListNode *pTmp = 
      NULL;  
     int 
      data; fin>> 
     data;  
     while 
      (data) { pNode = 
     new 
      ListNode; pNode->m_nKey = 
      data; pNode->m_pNext = 
      NULL;  
     if (NULL == 
      pHead) { pHead = 
      pNode; pTmp = 
      pNode; }  
     else 
      { pTmp->m_pNext = 
      pNode; pTmp = 
      pNode; } fin>> 
     data; } }  
     // 
     翻转单链表 
     void ReverseLink(ListNode *& 
     pHead) {  
     if (NULL == 
      pHead) {  
     return 
     ; } ListNode *pNode = 
      pHead; ListNode *Prev = 
      NULL; ListNode *pNext = 
      NULL;  
     while (NULL != 
      pNode) { pNext = pNode-> 
     m_pNext;  
     if (NULL == 
      pNext) { pHead = 
      pNode; } pNode->m_pNext = 
      Prev; Prev = 
      pNode; pNode = 
      pNext; } }  
     void PrintList(ListNode * 
     pHead) {  
     if (NULL == 
      pHead) {  
     return 
     ; } ListNode *pNode = 
      pHead;  
     while (NULL != 
      pNode) { cout< 
     
       m_nKey<< 
      " 
      " 
      ; pNode = pNode-> 
      m_pNext; } cout<< 
      endl; }  
      int _tmain( 
      int argc, _TCHAR* 
       argv[]) { ListNode *pHead = 
       NULL; cout<< 
      " 
      原来的链表: 
      " 
      ; CreateList(pHead); PrintList(pHead); ReverseLink(pHead); cout<< 
      " 
      翻转的链表: 
      " 
      ; PrintList(pHead);  
      return 
      0 
      ; } 
      
     
 
   
   
到此这篇单向链表逆置(单向链表的逆转)的文章就 介绍到这了,更多相关内容请继续浏览下面的相关 推荐文章,希望大家都能在 编程的领域有一番成就!

版权声明


相关文章:

  • 单向链表归并排序(单向链表归并排序怎么排)2026-04-07 13:27:09
  • cp1200链接电脑(cp-2140m怎么连接电脑)2026-04-07 13:27:09
  • 单链表和双向链表的区别(单链表和双向链表的区别和联系)2026-04-07 13:27:09
  • 链接跳转工具(网站跳转工具)2026-04-07 13:27:09
  • 腾讯文档里的链接怎么打开(什么是腾讯文档链接)2026-04-07 13:27:09
  • 跳转链接怎么制作(跳转链接怎么制作视频)2026-04-07 13:27:09
  • 短链接防红跳转(短链接跳转浏览器)2026-04-07 13:27:09
  • 对于一个设有头指针和尾指针的单链表(设有一头指针为l的带有表头结点的非循环双向链表)2026-04-07 13:27:09
  • 跳转链接制作工具(跳转链接制作工具有哪些)2026-04-07 13:27:09
  • 单向链表存储密度高吗(单向链表在内存中是连续存储的)2026-04-07 13:27:09
  • 全屏图片