逆转单向链表

 // Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std ;

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


void reverse(Node **head)
{
    if((!*head)|| !((*head)->next))
 {
  return;
 }

    struct Node *pre,*cur,*nex;
    pre = *head;
    cur = (*head)->next;
    while(cur)
    {
         nex = cur->next;
         cur->next = pre;
         pre = cur;
         cur = nex;
    }
    (*head)->next = NULL;//原来头指针(现在的尾指针)的next为NULL
 (*head)= pre;//改变头指针
   
}

Node * reverse1(Node *p,Node *head)
{
     if((!p)|| !(p->next))
     {
          head->next = p;
          return p;
     }
     else
     {
          Node *temp = reverse1(p->next,head);
          temp->next = p;
          p->next = NULL;
          return p;
     }
 }

void RecursiveReverse(Node **head)
{
     struct Node *first;
     struct Node *rest;
     if(!(*head))
  {
  return;
  }
     first = *head;
     rest = first->next;
     if(!rest)
  {
  return;
  }
     RecursiveReverse(&rest);
     first->next->next = first;
     first->next = NULL;
     *head = rest;
}


Node * G_p= NULL;
void RecursionReverse (Node * p1, Node *  p2 )
{
 if ( p2 == NULL)
 {
  G_p= p1;
  return ;
 }
 
 RecursionReverse( p2, p2->next);
 p2->next= p1;

}


void RecursionReverse1 (Node  * &p1, Node *  p2 )
{
 if ( p2 == NULL)
 {
  return ;
 }
 if ( p1 == p2)
 {
  p1= NULL;
 }
 Node *  p3= p1;
 p1= p2;
 RecursionReverse1( p1, p1->next);
 p2->next= p3;

}
void RecursionReverse (Node * p1, Node *  p2 , Node *&  p)
{
 if ( p2 == NULL)
 {
  p= p1;
  return ;
 }
 
 RecursionReverse( p2, p2->next, p);
 p2->next= p1;

}
void Display( Node *head)
{
 while ( head)
 {
  cout << head->data<<" ";
  head= head->next;
 }
}

void reverse2(Node * &top)
{
 if (top)
 {
  Node* r = top , *p = r->next ,*s ;
  r->next = NULL ;
  if (!p)
  {
   cout <<"only 1 element!/n";
   return   ;
  }
  else
  {
   if ( !p->next )
   {
    cout <<"2 elements!/n";
    p->next = r;
    return  ;
   }
   while (p)
   {
    s= p->next ;
    if (!s)
    {
     p->next= r;
     top= p;
     return  ;
    }
    p->next= r ;
    r= p;
    p= s;
   }
   return  ;

  }

 }
 else
 {
  cout <<"no element!/n";
  return   ;
 }
}

int _tmain(int argc, _TCHAR* argv[])
{
 node* head= new node  ;
 head->data= 0;
 node* p= head;
 for ( int i= 1; i< 5; ++i)
 {
  node* pn= new node ;
  pn->data= i;
  p->next= pn;
  p= pn;
 }
 p->next= NULL;

 cout <<"逆转前:   ";
 Display(head );
 
 cout <<"/n逆转后:   ";

 //方法一:void reverse(Node **head)
 //reverse( & head);//头指针发生改变 

 //方法二:void RecursionReverse (Node * p1, Node *  p2 ) //每二个进行换位:(^,0), 1, 2 ,3...
 /*RecursionReverse( NULL, head );
 head= G_p   ;*/

 //方法三:void RecursionReverse1 (Node  * &p1, Node *  p2 )//第一个参数直接改变头指针head
 //RecursionReverse1( head, head );

 //方法四:void RecursionReverse (Node * p1, Node *  p2 , Node *&  p)//第三个参数p只是最后一次调用,为了改变头指针head
 //RecursionReverse( NULL, head, head );

 //方法五:void RecursiveReverse(Node **head)
 //RecursiveReverse(&head);

 //方法六:Node * reverse1(Node *p,Node *head)
 /*reverse1( head    , head  );
 head= p;*/

 //方法七:Node * reverse1(Node *p,Node *head)
 reverse2( head);


 Display( head );//显示链表

 


 system("pause");
 return 0;
}

 

没有更多推荐了,返回首页