链表操作

来源:互联网 发布:知微数据华为 编辑:程序博客网 时间:2024/06/03 21:19

1、单链表的建立、查找、插入、删除、排序、逆置


#include <iostream>#include <algorithm>using namespace std;struct node{int data;node *next;};//建立node* Create(){int i=0;node *head=0;node *p1=0;node *p2=0;cin>>i;while(-1!=i){//cout<<"输入链表的值,以-1结束"<<endl;if(head==0){head=new node();head->data=i;p1=head;}else{p2=new node();p2->data=i;p1->next=p2;p1=p2;}cin>>i;}//链表结束if(head!=0)p1->next=0;//链尾指向0return head;}//遍历void Print(node* head){node *p;if(!head){cout<<"NULL"<<endl;}else{p=head;while(p){cout<<p->data<<' ';p=p->next;}}cout<<endl;}int Length(node* const head){int i=0;if(head==0)return 0;node *p=head;while(p!=0){i++;p=p->next;}return i;}//删除node*  Delete(node * head,int val){node *p1,*p2;if(!head){cout<<"空链表"<<endl;return NULL;}p1=head;p2=head;if(head->data==val)//删除头结点{head=head->next;delete p1;return head;}while(p2->data!=val && p2->next!=NULL){p1=p2;p2=p2->next;}if(p2->next==0 && p2->data!=val){cout<<"没有找到:"<<endl;return head;}if(p2->next==0 && p2->data==val){cout<<"找到,在最后一个结点"<<endl;p1->next=0;delete p2;return head;}if(p2->next!=0){p1->next=p2->next;delete p2;cout<<"找到了"<<endl;return head;}}node* Insert(node* head,int val){if(!head)//1空链表{head=new node();head->data=val;head->next=0;return head;}node *p1=head,*p2=head,*p3=new node();p3->data=val;if(head->data>=val)//2、在首节点插入{p3->next=head;head=p3;return head;}while( p2!=0 && p2->data<val)//顺序不能反,否则会错{p1=p2;p2=p2->next;}if(p2==0){p1->next=p3;p3->next=0;return head;}else{p1->next=p3;p3->next=p2;return head;}}//排序node* Sort(node *head){if(!head)return NULL;if(head->next==0)return head;node *p1=head,*p2=head->next,*p3=0;while(p1->next!=0){while(p2!=0){if(p1->data>p2->data){int n;n=p1->data;p1->data=p2->data;p2->data=n;}else{p2=p2->next;}}p1=p1->next;p2=p1;}return head;}//反转node* Reverse(node* head){if(head==0 || head->next==0)return head;node *p1=head,*p2=head->next,*p3;head->next=0;while(p2!=0){p3=p2;p2=p2->next;p3->next=p1;p1=p3;}head=p1;return head;}void main(){node *list1=0;list1=Create();node *list2=0;list2=Insert(list1,9);list2=Sort(list2);list2=Reverse(list2);Print(list2);system("pause");}


2、双指针在单链表中的应用

以下内容转自 http://blog.csdn.net/sicofield/article/details/9005133,感谢原作者sicofield的总结。

      经常能够碰到链表的题,当用一个指针遍历来解决问题的时候,不是无法解决就是效率不佳,典型的就是需要多次遍历且需要额外的存储空间。在这种情况下,可以尝试用两个指针来遍历链表,而两指针遍历链表又可以分为两种情况:1、让其中一个指针遍历快一点,比如一次在链表中走上两步;2、让其中一个指针现在链表中走上若干步。

       这里举三个链表相关的题目。

1、 判定链表中是否环

       第一种方法:可以对链表的元素进行标记,如果在预见NULL节点之前再次碰见已标记节点就存在环。缺点:需要改变节点内容,而节点一般是只读的。

       第二种方法:访问每一个元素将其存储在数组中,这个时候存储的元素是什么,如果链表中的元素有重复的值,难道存储节点地址?并且开辟了O(n)的额外空间,如果内存不够呢?

       第三种方法:如果假定链表存在环,那么环在前N个元素之中,此时可以设置一个指针指向链表的头部,然后遍历后N-1个元素,看是否是指针所指元素,如果都不同,指针指向后一个元素,然后遍历后N-2个元素。缺点:这个算法复杂度为O(n^2),而且建立在一个前提条件之下。

       最优的答案:设置两个指针,开始都指向链表头,然后其中一个指针每次向前走一步,另一个指针每次向前走两步,如果快的遇到NULL了,证明该链表中没有环,如果有环,快的指针每次都要比慢的多走一步,最终两个指针会相遇,(注意:这里快指针不会跳过慢指针而不相遇,因为它每次都只比慢指针多走一个单位)

[cpp] view plaincopy
  1. bool judge(list *head)  
  2. {  
  3.     if(head == NULL)  
  4.     {  
  5.         return false;//没有环  
  6.     }  
  7.   
  8.     list *pFast = head;  
  9.     list *pSlow = head;  
  10.   
  11.     while(pFast->next != NULL && pFast->next->next != NULL)  
  12.     {  
  13.         pFast = pFast->next->next;  
  14.         pSlow = pSlow->next;  
  15.   
  16.         if(pFast == pSlow)  
  17.         {  
  18.             return true;  
  19.         }  
  20.     }  
  21.   
  22.     return false;  
  23. }  

2、找到链表的中间节点

       受上一题的启发可以运用两个速度不同的指针来解决,快指针每次走两步,慢指针每次走一步,这样当快指针到达链表尾部的时候,慢指针就指向了链表的中间节点。

3、 输出链表中倒数第K个数

第一种方法:单个指针遍历两次,首先遍历一次链表统计总元素个数N,那么所要找的倒数第K个元素即为第N-K+1个元素。

第二种方法:双指针遍历一次,首先前指针先向前走K-1步,即初始的时候前指针指向第K个元素,然后后指针指向第一个元素,然后同步向后单步走,当后指针指向NULL的时候,前指针指向倒数第K个元素。

[cpp] view plaincopy
  1. //注意程序鲁棒性,输入参数检查,元素个数不足检查。  
  2. ListNode* FindKthToTail(ListNode* pListHead, unsigned int k)  
  3. {  
  4.     if(pListHead == NULL || k == 0)  
  5.         return NULL;                        //考虑参数异常  
  6.   
  7.     ListNode *pAhead = pListHead;  
  8.     ListNode *pBehind = NULL;  
  9.   
  10.     for(unsigned int i = 0; i < k - 1; ++ i)  
  11.     {  
  12.         if(pAhead->m_pNext != NULL)  
  13.             pAhead = pAhead->m_pNext;  
  14.         else                               //要考虑到链表的元素不足K个的情况  
  15.         {  
  16.             return NULL;  
  17.         }  
  18.     }  
  19.   
  20.     pBehind = pListHead;  
  21.     while(pAhead->m_pNext != NULL)  
  22.     {  
  23.         pAhead = pAhead->m_pNext;  
  24.         pBehind = pBehind->m_pNext;  
  25.     }  
  26.   
  27.     return pBehind;  
  28. }  

4、 两链表的第一个公共结点——输入两个链表,找出它们的第一个公共结点。

[cpp] view plaincopy
  1. unsigned int GetListLength(ListNode* pHead)  
  2. {  
  3.     unsigned int nLength = 0;  
  4.     ListNode* pNode = pHead;  
  5.     while(pNode != NULL)  
  6.     {  
  7.         ++ nLength;  
  8.         pNode = pNode->m_pNext;  
  9.     }  
  10.       
  11.     return nLength;  
  12. }  
  13. ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2)  
  14. {  
  15.     // 得到两个链表的长度  
  16.     unsigned int nLength1 = GetListLength(pHead1);  
  17.     unsigned int nLength2 = GetListLength(pHead2);  
  18.     int nLengthDif = nLength1 - nLength2;  
  19.    
  20.     ListNode* pListHeadLong = pHead1;  
  21.     ListNode* pListHeadShort = pHead2;  
  22.     if(nLength2 > nLength1)  
  23.     {  
  24.         pListHeadLong = pHead2;  
  25.         pListHeadShort = pHead1;  
  26.         nLengthDif = nLength2 - nLength1;  
  27.     }  
  28.    
  29.     // 先在长链表上走几步,再同时在两个链表上遍历  
  30.     for(int i = 0; i < nLengthDif; ++ i)  
  31.         pListHeadLong = pListHeadLong->m_pNext;  
  32.    
  33.     while((pListHeadLong != NULL) &&   
  34.         (pListHeadShort != NULL) &&  
  35.         (pListHeadLong != pListHeadShort))  
  36.     {  
  37.         pListHeadLong = pListHeadLong->m_pNext;  
  38.         pListHeadShort = pListHeadShort->m_pNext;  
  39.     }  
  40.    
  41.     // 得到第一个公共结点  
  42.     ListNode* pFisrtCommonNode = pListHeadLong;  
  43.    
  44.     return pFisrtCommonNode;  
  45. }  

        顺便说一句,单链表翻转的算法运用了3指针,这是为了记录前后节点。
0 0