#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 ; }
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/qkl-jc/43829.html