头文件:List.h
主程序文件:main.cpp
/***********
List.h
************/
//链表节点结构
struct ListNode
{
int m_ivalue;
ListNode* m_pNext;
};
//节点从尾部加入链表
void AddToTail(ListNode** pHead, int value);
//节点从头部加入链表
void AddToHead(ListNode** pHead,int value);
//打印链表
void PrintList(ListNode* pHead);
//摧毁链表
void DestroyList(ListNode** pHead);
//删除值为value的节点
void RemoveNode(ListNode** pHead, int value);
//逆序打印链表,利用栈实现
void PrintReverseList(ListNode* pHead);
//删除链表中toBeDeleted指向的节点
void DeleteNode(ListNode** pHead,ListNode* toBeDeleted);
//寻找倒数第K个节点
ListNode* KthNodeFromEnd(ListNode* pHead,int k);
//返回链表中的中间节点
ListNode* MidInList(ListNode* pHead);
//逆序链表
void ReverseList(ListNode** pHead);
//将两个有序的链表合并为一个有序链表
ListNode* Merge(ListNode* pHead1,ListNode* pHead2);
//用递归的方法将两个有序的链表合并为一个有序链表
ListNode* MergeRecurse(ListNode* pHead1,ListNode* pHead2);
源文件:List.cpp
/***********
List.cpp
************/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include "List.h"
using namespace std;
void AddToTail(ListNode** pHead, int value)
{
ListNode* pNode = new ListNode();
if(NULL == pNode)
{
cout<<"No more memory"<<endl;
return;
}
pNode->m_ivalue = value;
pNode->m_pNext = NULL;
if(NULL == (*pHead))
{
(*pHead) = pNode;
}
else
{
ListNode* ptmp = (*pHead);
while(ptmp->m_pNext != NULL)
ptmp = ptmp->m_pNext;
ptmp->m_pNext = pNode;
}
}
void AddToHead(ListNode** pHead,int value)
{
ListNode* pNode = new ListNode();
if(NULL == pNode)
{
cout<<"No more memory"<<endl;
return;
}
pNode->m_ivalue = value;
pNode->m_pNext = NULL;
if(NULL == (*pHead))
{
(*pHead) = pNode;
}
else
{
pNode->m_pNext = (*pHead);
(*pHead) = pNode;
}
}
void PrintList(ListNode* pHead)
{
if(NULL == pHead)
{
cout<<"No Data"<<endl;
}
else
{
while(pHead != NULL)
{
printf("%d ",pHead->m_ivalue);
pHead = pHead->m_pNext;
}
printf("\n");
}
}
void DestroyList(ListNode** pHead)
{
if(NULL == pHead)
return;
ListNode* pNode = (*pHead);
while(pNode != NULL)
{
(*pHead) = (*pHead)->m_pNext;
delete pNode;
pNode = (*pHead);
}
}
void RemoveNode(ListNode** pHead, int value)
{
if(NULL == (*pHead))
return;
ListNode* prior,*pDelete;
if((*pHead)->m_ivalue == value)
{
pDelete = (*pHead);
(*pHead) = (*pHead)->m_pNext;
}
else
{
prior = (*pHead);
pDelete = (*pHead)->m_pNext;
while(pDelete!=NULL && pDelete->m_ivalue != value)
{
pDelete = pDelete->m_pNext;
prior = prior->m_pNext;
}
if(pDelete!=NULL && pDelete->m_ivalue == value)
{
prior->m_pNext = pDelete->m_pNext;
}
}
if(pDelete != NULL)
{
delete pDelete;
pDelete = NULL;
}
}
//利用栈实现逆序打印链表
void PrintReverseList(ListNode* pHead)
{
if(NULL == pHead)
return;
stack<ListNode*> nodes;
ListNode* pNode;
while(pHead)
{
nodes.push(pHead);
pHead = pHead->m_pNext;
}
while(!nodes.empty())
{
pNode = nodes.top();
printf("%d ",pNode->m_ivalue);
nodes.pop();
}
printf("\n");
}
void DeleteNode(ListNode** pHead,ListNode* toBeDeleted)
{
if(NULL == (*pHead) || NULL == pHead || NULL == toBeDeleted)
{
cout<<"bad paramer"<<endl;
return;
}
ListNode* pNode = NULL;
if(NULL == toBeDeleted->m_pNext && (*pHead) == toBeDeleted)
{
*pHead = NULL;
delete toBeDeleted;
toBeDeleted = NULL;
}
else if(NULL == toBeDeleted->m_pNext)
{
pNode = *pHead;
while(pNode->m_pNext != toBeDeleted)
{
pNode = pNode->m_pNext;
}
pNode->m_pNext = NULL;
delete toBeDeleted;
toBeDeleted = NULL;
}
else
{
pNode = toBeDeleted->m_pNext;
toBeDeleted->m_ivalue = pNode->m_ivalue;
toBeDeleted->m_pNext = pNode->m_pNext;
delete pNode;
}
}
ListNode* KthNodeFromEnd(ListNode* pHead,int k)
{
if(NULL == pHead || k<=0)
{
printf("bad paramer!\n");
return NULL;
}
int i=1;
ListNode* pPrior = pHead;
ListNode* pBehind = pHead;
for(i; i<k; i++)
{
if(NULL != pPrior->m_pNext)
pPrior = pPrior->m_pNext;
else
{
printf("No Kth data\n");
return NULL;
}
}
while(NULL != pPrior->m_pNext)
{
pPrior = pPrior->m_pNext;
pBehind = pBehind->m_pNext;
}
return pBehind;
}
//返回链表的中间节点
ListNode* MidInList(ListNode* pHead)
{
if(NULL == pHead)
return NULL;
if(NULL == pHead->m_pNext || NULL == pHead->m_pNext->m_pNext)
{
return pHead;
}
else
{
ListNode* pNodeTwoStep = pHead;
ListNode* pNodeOneStep = pHead;
while(pNodeTwoStep->m_pNext != NULL)
{
pNodeTwoStep = pNodeTwoStep->m_pNext;
if(NULL != pNodeTwoStep->m_pNext)
pNodeTwoStep = pNodeTwoStep->m_pNext;
else
break;
pNodeOneStep = pNodeOneStep->m_pNext;
}
return pNodeOneStep;
}
}
//逆序链表
void ReverseList(ListNode** pHead)
{
if(NULL == pHead)
{
printf("bad paramer\n");
return;
}
ListNode* pBehind;
ListNode* pMid;
ListNode* pPrior;
if(NULL == (*pHead)->m_pNext)
return;
else if(NULL == (*pHead)->m_pNext->m_pNext)
{
pPrior = (*pHead)->m_pNext;
pPrior->m_pNext = (*pHead);
(*pHead)->m_pNext = NULL;
(*pHead) = pPrior;
}
else
{
pBehind = (*pHead);
pMid = pBehind->m_pNext;
pPrior = pMid->m_pNext;
while(pPrior != NULL)
{
pMid->m_pNext = pBehind;
pBehind = pMid;
pMid = pPrior;
pPrior = pPrior->m_pNext;
}
pMid->m_pNext = pBehind;
(*pHead)->m_pNext = NULL;
(*pHead) = pMid;
}
}
//将两个有序的链表合并为一个有序链表
ListNode* Merge(ListNode* pHead1,ListNode* pHead2)
{
if(NULL == pHead1 && NULL == pHead2)
return NULL;
else if(NULL == pHead1)
return pHead2;
else if(NULL == pHead2)
return pHead1;
ListNode* pMergeHead;
ListNode* pTmpNode;
if(pHead1->m_ivalue < pHead2->m_ivalue)
{
pMergeHead = pHead1;
pHead1 = pHead1->m_pNext;
}
else
{
pMergeHead = pHead2;
pHead2 = pHead2->m_pNext;
}
pTmpNode = pMergeHead;
while(pHead1!=NULL && pHead2!=NULL)
{
if(pHead1->m_ivalue < pHead2->m_ivalue)
{
pTmpNode->m_pNext = pHead1;
pHead1 = pHead1->m_pNext;
pTmpNode = pTmpNode->m_pNext;
}
else
{
pTmpNode->m_pNext = pHead2;
pHead2 = pHead2->m_pNext;
pTmpNode = pTmpNode->m_pNext;
}
}
if(NULL == pHead1)
pTmpNode->m_pNext = pHead2;
else if(NULL == pHead2)
pTmpNode->m_pNext = pHead1;
return pMergeHead;
}
//用递归的方法将两个有序的链表合并为一个有序链表
ListNode* MergeRecurse(ListNode* pHead1,ListNode* pHead2)
{
if(NULL == pHead1 && NULL == pHead2)
return NULL;
else if(NULL == pHead1)
return pHead2;
else if(NULL == pHead2)
return pHead1;
ListNode* pMergeHead;
if(pHead1->m_ivalue < pHead2->m_ivalue)
{
pMergeHead = pHead1;
pMergeHead->m_pNext = MergeRecurse(pHead1->m_pNext,pHead2);
}
else
{
pMergeHead = pHead2;
pMergeHead->m_pNext = MergeRecurse(pHead1,pHead2->m_pNext);
}
return pMergeHead;
}
主程序文件:main.cpp
/***********
main.cpp
************/
#include <iostream>
#include <stdio.h>
#include "List.h"
using namespace std;
ListNode* returnpNode(ListNode* pHead)
{
ListNode* pNode = pHead;
while(pNode->m_ivalue != 8)
pNode = pNode->m_pNext;
return pNode;
}
int main()
{
ListNode* head1 = NULL;
ListNode* head2 = NULL;
ListNode *pNode = NULL;
AddToTail(&head1,1);
AddToTail(&head1,3);
AddToTail(&head1,5);
AddToTail(&head2,2);
AddToTail(&head2,4);
AddToTail(&head2,6);
AddToTail(&head2,7);
PrintList(head1);
PrintList(head2);
pNode = MergeRecurse(NULL,NULL);
PrintList(pNode);
return 0;
}