#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;
else
return 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;
}