顛倒一個鏈表的順序 C++

來源:互聯網
上載者:User

標籤:返回   span   style   space   include   clu   數組元素   設定   while   

首先我們定義一個頭結點:

    struct Node{          int data;          Node*next;      };  

 接下來我們寫一個函數來建立一個鏈表:

    //a是一個數組,n代表數組元素的個數      Node*createLinkList(int a[],int n)      {          if(a==NULL||n==0)              return NULL;          Node*L,*T=NULL;          for(int i=0;i<n;i++)          {              if(i==0)              {L=new Node;              L->data=a[i];              L->next=NULL;              T=L;              }else              {                  Node*s=new Node;                  s->data=a[i];                  s->next=NULL;                  T->next=s;                  T=s;//結點移動              }          }          return L;      }  

 下面就開始我們的翻轉方法部分

1)首先我們先用遞迴方法來進行處理

(1)如果一個鏈表為空白鏈表,那麼他的逆序還是為空白

(2)如果一個鏈表中只有一個節點,那麼他的逆序就是這個鏈表本身.

(3)如果一個鏈表的長度大於一,那麼我們做如下遞迴.

把當前鏈表的除了前端節點L之外的剩餘節點群組成的鏈表逆序,也就是遞迴調用,並得到剩餘鏈表逆序後的頭結點p,

此時將L的下一個節點的的next指向L, 並將L的next指標置空.然後返回p.

(簡單來說,先是一層層尋找,找到尾結點返回給p,然後返回上一層,這時最後一個節點為L->next,將L->next的next指向現在第二層的L,然後再把L的next置空,即完成了第一次翻轉,然後再返回給p;把這次的兩個結點看成最後一個,然後在倒數第三層重複反轉,最後完成整個反轉)

    Node*reverseLinkList(Node*L)      {              if(L==NULL) return NULL;          if(L->next==NULL) return L;                        Node*p=reverseLinkList(L->next);              L->next->next=L;//倒數第一個指向倒數第二個              L->next=NULL;//倒數第二個指標置空                    return p;      }  

 2)第二種方法就不進行遞迴進行翻轉,然而我們直接在最後翻轉會出現斷鏈,進而不能進行連續遍曆,所以我們可以定義三個結點,pnow指向當前結點,pre指向它的前置,nex指向它的後驅,首先令pnow的下一個指標指向nex,然後令pnow的next拆掉並串連pre,進行翻轉,這時pnow和nex之間是斷開的,然後將pnow設定為前驅pre,將nex設定為pnow,即完成了一段翻轉,重複到完畢即可。

    Node*noreverseLinkList(Node*L)      {          if(L==NULL||L->next==NULL) return L;          Node*pnow=L,*pre=NULL,*nex=NULL,*tail=NULL;          while(pnow!=NULL)          {              nex=pnow->next;              if(NULL==nex){                  tail=pnow;}              pnow->next=pre;              pre=pnow;              pnow=nex;                        }                    return tail;      }  

 為了驗證,我們還要寫一個顯示函數:

    void display(Node*L)      {  Node*p=L;          if(p==NULL) return ;          while(p!=NULL)          {              cout<<p->data<<" ";              p=p->next;          }          cout<<endl;            }  

 完整代碼:

include<iostream>  using namespace std;  struct Node{      int data;      Node*next;  };  //a是一個數組,n代表數組元素的個數  Node*createLinkList(int a[],int n)  {      if(a==NULL||n==0)          return NULL;      Node*L,*T=NULL;      for(int i=0;i<n;i++)      {          if(i==0)          {L=new Node;          L->data=a[i];          L->next=NULL;          T=L;          }else          {              Node*s=new Node;              s->data=a[i];              s->next=NULL;              T->next=s;              T=s;//結點移動          }      }      return L;  }  void display(Node*L)  {  Node*p=L;      if(p==NULL) return ;      while(p!=NULL)      {          cout<<p->data<<" ";          p=p->next;      }      cout<<endl;    }  Node*reverseLinkList(Node*L)  {          if(L==NULL) return NULL;      if(L->next==NULL) return L;                Node*p=reverseLinkList(L->next);          L->next->next=L;//倒數第一個指向倒數第二個          L->next=NULL;//倒數第二個指標置空            return p;  }  Node*noreverseLinkList(Node*L)  {      if(L==NULL||L->next==NULL) return L;      Node*pnow=L,*pre=NULL,*nex=NULL,*tail=NULL;      while(pnow!=NULL)      {          nex=pnow->next;          if(NULL==nex){              tail=pnow;}          pnow->next=pre;          pre=pnow;          pnow=nex;                }            return tail;  }          int _tmain(int argc, _TCHAR* argv[])  {         int a[] = {1,2,3,4,5,6};       int b[] = {1,2,3,4,5,6,7};      Node *L = createLinkList(a,6);        display(L);        display(noreverseLinkList(L));      Node *A = createLinkList(b,7);       display(reverseLinkList(A));      system("pause");      return 0;  }  

 謝謝閱讀。詳細解讀:(有圖理解)http://blog.csdn.net/zhaoruixiang1111/article/details/49932603

顛倒一個鏈表的順序 C++

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.