链表(未完待续)
#include <iostream> #include <stdlib.h> #include <vector> #include <set> #include <stack> #include <algorithm> #include <string> #include <sstream> using namespace std; struct ListNode { int m_nValue; ListNode*m_pNext; }; //创建一个链表结点 ListNode* CreateListNode(int value) { ListNode* pNode=new ListNode(); pNode->m_nValue=value; pNode->m_pNext=NULL; return pNode; } //在链表尾部添加结点 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; } } ListNode * CreateLink(int a[],int k) { ListNode * Head=NULL,*q=NULL; for(int i=0;i<k;i++) { ListNode * pNew=new ListNode(); pNew->m_nValue=a[i]; pNew->m_pNext=NULL; if(Head==NULL) { Head=pNew; q=pNew; } else { q->m_pNext=pNew; q=q->m_pNext; } } return Head; } //从头到尾输出链表 void printLink(ListNode* pHead) { ListNode* p=pHead; while (p) { cout<<p->m_nValue<<" "; p=p->m_pNext; } cout<<endl; } //从尾到头输出链表 void PrintListReverse(ListNode* pHead) { //利用栈的先进后出机制 stack<ListNode*>nodes; ListNode* pNode=pHead; while (pNode!=NULL) { nodes.push(pNode); pNode=pNode->m_pNext; } while(!nodes.empty()) { pNode=nodes.top(); cout<<pNode->m_nValue<<" "; nodes.pop(); } } //翻转链表 //还未搞透,以后重点复习 ListNode * ReverseList(ListNode *head) { ListNode *p1,*p2,*p3; if(head==NULL) return head; p1=head; p2=p1->m_pNext; while(p2) { p3=p2->m_pNext; //要改变p2->m_pNext的指针,所以必须先保留p2->m_pNext p2->m_pNext=p1; p1=p2; //循环往后 p2=p3; } head->m_pNext=NULL; //原先的head已经变成tail,别忘了置空,只有到这步才能置空 head=p1; return head; } //删除链表中第一个含有value的值 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 { //中间变量pNode ListNode* pNode=*pHead; //如果pNode不为删除数则遍历 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 DeleteNode(ListNode**pListHead, ListNode* pToBeDeleted) { if(!pToBeDeleted||!pListHead) return; //删除的结点不是尾结点 if (pToBeDeleted->m_pNext!=NULL) { ListNode* pNext= pToBeDeleted->m_pNext; pToBeDeleted->m_nValue=pNext->m_nValue; pToBeDeleted->m_pNext=pNext->m_pNext; delete pNext; pNext=NULL; } //链表中只有一个结点 else if(*pListHead==pToBeDeleted) { delete pToBeDeleted; pToBeDeleted=NULL; *pListHead=NULL; } //删除的结点为尾节点,则从前往后遍历 else { ListNode* pNode=*pListHead; while (pNode->m_pNext!=pToBeDeleted) { pNode=pNode->m_pNext; } pNode->m_pNext=NULL; delete pToBeDeleted; pToBeDeleted=NULL; } } //合并两个排序的链表 ListNode* Merge(ListNode* pHead1,ListNode* pHead2) { if(pHead1==NULL) return pHead2; if(pHead2==NULL) return pHead1; ListNode* pMergedHead=NULL; if (pHead1->m_nValue<pHead2->m_nValue) { pMergedHead=pHead1; pMergedHead->m_pNext=Merge(pHead1->m_pNext,pHead2); } else { pMergedHead=pHead2; pMergedHead->m_pNext=Merge(pHead1,pHead2->m_pNext); } return pMergedHead; } //找到倒数第K个结点 ListNode* FindKthToTail(ListNode* pListHead,unsigned int k) { if(pListHead==NULL||k<=0) return NULL; ListNode *pAhead=pListHead; ListNode *pBehead=pListHead; for (int i=0;i<k-1;++i) { if(pAhead->m_pNext!=NULL) pAhead=pAhead->m_pNext; else return NULL; } //pBehead=pListHead; while (pAhead->m_pNext!=NULL) { pAhead=pAhead->m_pNext; pBehead=pBehead->m_pNext; } return pBehead; } int main() { int a[]={1,3,5,7},b[]={2,4,6,8}; //创建链表 ListNode* Head=CreateLink(a,4); ListNode* Head1=CreateLink(b,4); //打印链表 printLink(Head); //尾部加值 //AddToTail(&Head,6); //在链表中加入值 //RemoveNode(&Head,3); // printLink(Head); //删除结点 /* ListNode* pNode=Head->m_pNext; DeleteNode(&Head, pNode); printLink(Head);*/ //从尾到头输出链表 //PrintListReverse(Head); //找到链表的倒数i个结点 //printLink( FindKthToTail(Head,2)); //反转链表 // printLink(ReverseList(Head)); //合并有序链表 printLink(Merge(Head,Head1)); system("pause"); return 0; }