链表(待续)
来源:互联网 发布:爱情动作片软件 编辑:程序博客网 时间:2024/06/13 22:28
#include<iostream>#include <stdio.h>#include <stdlib.h>#include "List.h"using namespace std;//--------------------------------------------------------------------------------------------------------------------------//创建新的ListNode节点ListNode* CreateListNode(int value){ ListNode* pNode = new ListNode(); pNode->m_nValue = value; pNode->m_pNext = NULL; return pNode;}//连接两个节点void ConnectListNodes(ListNode* pCurrent, ListNode* pNext){ if(pCurrent == NULL) { printf("Error to connect two nodes.\n"); exit(1); } pCurrent->m_pNext = pNext;}//返回链表长度int LengthOfList(ListNode* pHead){if(pHead == NULL)return 0;int dLength = 0;ListNode* pNode = pHead;while(pNode != NULL){dLength++;pNode = pNode->m_pNext;}return dLength;}//判断链表是否为空bool IsEmpty(ListNode* pHead){if(pHead == NULL){cout<<"the list is empty\n";return false;}else{cout<<"the list is not empty\n";return true;}}//返回第K个节点的值int valueOfTheKListNode(ListNode* pHead , int K){if(pHead == NULL || K <= 0){cout<<"K is Invalid"<<endl;return false;}int Length = LengthOfList(pHead);if(K > Length){ cout<<"K is out of the length;";return false;}ListNode* pNode = pHead;int count = 1;while(pNode != NULL && count < K){pNode = pNode->m_pNext;count++;}return pNode->m_nValue;}//返回链表中第一个等于值value的位置int LocateElem(ListNode* pHead , int value){if(pHead == NULL)return -1;ListNode* pNode = pHead;int location = 1;while(pNode->m_nValue != value){location++;pNode = pNode->m_pNext;}return location;}//输出指定节点值void PrintListNode(ListNode* pNode){ if(pNode == NULL) { printf("The node is NULL\n"); } else { printf("The key in node is %d.\n", pNode->m_nValue); }}//输出链表(不带头结点)void PrintList(ListNode* pHead){ printf("PrintList starts.\n"); ListNode* pNode = pHead; while(pNode != NULL) { printf("%d\t", pNode->m_nValue); pNode = pNode->m_pNext; } printf("\nPrintList ends.\n");}//销毁单链表void DestroyList(ListNode* pHead){ ListNode* pNode = pHead; while(pNode != NULL) { pHead = pHead->m_pNext; delete pNode; pNode = pHead; }}//链表尾部插入新的节点void AddToTail(ListNode** pHead, int value){ ListNode* pNew = new ListNode(); pNew->m_nValue = value; pNew->m_pNext = NULL; if(*pHead == NULL) { *pHead = pNew; } else { ListNode* pNode = *pHead; while(pNode->m_pNext != NULL) pNode = pNode->m_pNext; pNode->m_pNext = pNew; }}//删除链表节点void RemoveNode(ListNode** pHead, int value){ if(pHead == NULL || *pHead == NULL) return; ListNode* pToBeDeleted = NULL; if((*pHead)->m_nValue == value) { pToBeDeleted = *pHead; *pHead = (*pHead)->m_pNext; } else { ListNode* pNode = *pHead; while(pNode->m_pNext != NULL && pNode->m_pNext->m_nValue != value) pNode = pNode->m_pNext; if(pNode->m_pNext != NULL && pNode->m_pNext->m_nValue == value) { pToBeDeleted = pNode->m_pNext; pNode->m_pNext = pNode->m_pNext->m_pNext; } } if(pToBeDeleted != NULL) { delete pToBeDeleted; pToBeDeleted = NULL; }}void main(){ListNode* pNode1 = CreateListNode(20);ListNode* pNode2 = CreateListNode(30);ListNode* pNode3 = CreateListNode(40);ListNode* pNode4 = CreateListNode(56);ConnectListNodes(pNode1 , pNode2);ConnectListNodes(pNode2 , pNode3);ConnectListNodes(pNode3 , pNode4);PrintList(pNode1);AddToTail(&pNode1 , 25);PrintList(pNode1);cout<<LengthOfList(pNode1)<<endl;IsEmpty(pNode1);//cout<<valueOfTheKListNode(pNode1 , 5);cout<<LocateElem(pNode1 , 20);}
//题目一两个链表的第一个公共节点//题目二从尾到头打印链表//题目三链表中倒数第k个节点//题目四反转链表//题目五合并两个排序的链表#include<iostream>#include "List.h"using namespace std;unsigned int GetListLength(ListNode* pHead);//题目一两个链表的第一个公共节点ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2){ // 得到两个链表的长度 unsigned int nLength1 = GetListLength(pHead1); unsigned int nLength2 = GetListLength(pHead2); int nLengthDif = nLength1 - nLength2; ListNode* pListHeadLong = pHead1; ListNode* pListHeadShort = pHead2; if(nLength2 > nLength1) { pListHeadLong = pHead2; pListHeadShort = pHead1; nLengthDif = nLength2 - nLength1; } // 先在长链表上走几步,再同时在两个链表上遍历 for(int i = 0; i < nLengthDif; ++ i) pListHeadLong = pListHeadLong->m_pNext; while((pListHeadLong != NULL) && (pListHeadShort != NULL) && (pListHeadLong != pListHeadShort)) { pListHeadLong = pListHeadLong->m_pNext; pListHeadShort = pListHeadShort->m_pNext; } // 得到第一个公共结点 ListNode* pFisrtCommonNode = pListHeadLong; return pFisrtCommonNode;}unsigned int GetListLength(ListNode* pHead){ unsigned int nLength = 0; ListNode* pNode = pHead; while(pNode != NULL) { ++ nLength; pNode = pNode->m_pNext; } return nLength;}//题目二:从头到尾打印链表(递归)void PrintListReversingly_Recursively(ListNode* pHead){if(pHead != NULL){if(pHead->m_pNext != NULL){PrintListReversingly_Recursively(pHead->m_pNext);}printf("%d\t" , pHead->m_nValue);}}//题目三:链表中倒数第k个节点ListNode* FindKthToTail(ListNode* pHead , unsigned int k){if(pHead == NULL || k == 0)return NULL;ListNode* pBegin = pHead;ListNode* pBehind = NULL;for(int i = 0; i < k - 1; ++i){if(pBegin->m_pNext != NULL)pBegin = pBegin->m_pNext;elsereturn NULL;}pBehind = pHead;while(pBegin->m_pNext != NULL){pBegin = pBegin->m_pNext;pBehind = pBehind->m_pNext;}return pBehind;}//题目四反转链表ListNode* ReverseList(ListNode* pHead){ListNode* ReverseListHead = NULL;ListNode* pNode = pHead;ListNode* pPrev = NULL;while(pNode != NULL){ListNode* pNext = pNode->m_pNext;if(pNext == NULL)ReverseListHead = pNode;pNode->m_pNext = pPrev;pPrev = pNode;pNode = pNext;}return ReverseListHead;}//题目五:合并两个以经为排序的链表ListNode* MergeSortedList(ListNode* pHead1 , ListNode* pHead2){if(pHead1 == NULL)return pHead2;else if(pHead2 == NULL)return pHead1;ListNode* MergeListHead = NULL;if(pHead1->m_nValue < pHead2->m_nValue){MergeListHead = pHead1;MergeListHead->m_pNext = MergeSortedList(pHead1->m_pNext , pHead2);}else{MergeListHead = pHead2;MergeListHead->m_pNext = MergeSortedList(pHead1 , pHead2->m_pNext);}return MergeListHead;}int main(){ListNode* pNode1 = CreateListNode(1);ListNode* pNode2 = CreateListNode(2);ListNode* pNode3 = CreateListNode(3);ListNode* pNode4 = CreateListNode(4);ListNode* pNode5 = CreateListNode(5);ConnectListNodes(pNode1 , pNode2);ConnectListNodes(pNode2 , pNode3);ConnectListNodes(pNode3 , pNode4);ConnectListNodes(pNode4 , pNode5);ListNode* pNode6 = CreateListNode(6);ListNode* pNode7 = CreateListNode(7);ListNode* pNode8 = CreateListNode(8);ListNode* pNode9 = CreateListNode(9);ListNode* pNode10 = CreateListNode(10);ConnectListNodes(pNode6 , pNode7);ConnectListNodes(pNode7 , pNode8);ConnectListNodes(pNode8 , pNode9);ConnectListNodes(pNode9, pNode10);PrintList(pNode1);PrintListReversingly_Recursively(pNode1);printf("\n");ListNode* pNodek = FindKthToTail(pNode1 , 5);printf("倒数第k个链表节点是:%d\n" , pNodek->m_nValue);/*//反转链表printf("原始链表:\n");PrintList(pNode1);printf("反转链表:\n");ListNode* RpNode = ReverseList(pNode1);PrintList(RpNode);*///合并排序链表PrintList(pNode1);PrintList(pNode6);ListNode* pMergeNode = MergeSortedList(pNode1 , pNode6);PrintList(pMergeNode);return 0;}
0 0
- 链表(待续)
- 待续
- 待续
- 待续
- 待续
- 待续
- 待续。。。
- (待续......)
- 待续。。。。。
- 待续
- 未完待续
- 未完待续
- 未完待续
- QT待续
- CreateTimerQueueTimer-待续
- 备注,待续
- window(待续)
- 待续了解
- s3c2440的Nand flash裸机程序
- 导航控制器UINavigationController
- linux下vi编译器的使用
- Android:Activity,线程,和内存泄漏
- android中popupwindow布局的一些小问题
- 链表(待续)
- poj 1089 Intervals
- ADF MDS-00010:DulicateRefException
- QVM人工智能引擎
- DG不能自动mount导致数据库不能正常启动:ORA-01157、ORA-01110、ORA-17503、ORA-15001、ORA-15001
- android socket编程实例
- 盘点2013智能电网行业十大新闻事件
- 树状数组
- NYOJ 九的余数