数据结构---递归用法

#include<iostream>
using namespace std;

struct Node
{
    Node(int data)
        :_data(data)
        ,_next(NULL)
    {}
    int _data;
    Node* _next;
};

//void PrintfListFromTail2Head(Node* pHead)//1.逆序打印单链表
//{
//  if(pHead)
//  {
//      PrintfListFromTail2Head(pHead->_next);
//      cout<<pHead->_data<<"";
//  }
//}
//
//void FunTest1()//1.逆序打印单链表
//{
//  Node node1(1),node2(2),node3(3),node4(4);
//  node1._next=&node2;
//  node2._next=&node3;
//  node3._next=&node4;
//  PrintfListFromTail2Head(&node1);
//}

//void DestoryList(Node*& pHead)//2.销毁链表中的结点
//{
//  if(pHead)
//  {
//      DestoryList(pHead->_next);
//      delete pHead;
//      pHead=NULL;
//  }
//}
//void FunTest2()//2.销毁链表中的结点
//{
//  Node* p1=new Node(1);
//  Node* p2=new Node(2);
//  Node* p3=new Node(3);
//  Node* p4=new Node(4);
//  p1->_next=p2;
//  p2->_next=p3;
//  p3->_next=p4;
//  DestoryList(p1);
//}

//Node* Find(Node* pHead,int data)//3.查找值为data的结点
//{
//  if(pHead)
//  {
//      Node* res=Find(pHead->_next,data);
//      if(res)
//      {
//          return res;
//      }
//      else
//      {
//          if(pHead->_data==data)
//               return pHead;
//      }
//  }
//  return NULL;
//}
//
//Node* FunTest3()//3.查找值为data的结点
//{
//  Node node1(1),node2(2),node3(3),node4(4);
//  node1._next=&node2;
//    node2._next=&node3;
//  node3._next=&node4;
//  return Find(&node1,2);
//}

//int BinarySearch(int* array,int left,int right,int data)//4.二分查找
//{
//  if(left<right)
//  {
//      int mid=left+((right-left)>>1);
//      if(array[mid]==data)
//          return mid;
//      else if(array[mid]>data)
//          return BinarySearch(array,left,mid-1,data);
//      else
//          return BinarySearch(array,mid+1,right,data);
//  }
//  return -1;
//}
//
//void FunTest4()//4.二分查找
//{
//  int array[]={1,2,3,4,5,6,7,8,9,10};
//  int result=BinarySearch(array,0,9,2);
//    cout<<result<<endl;
//}

//void PrintArray(int* array,int size)//5.逆序打印数组
//{
//  if(size)
//  {
//      cout<<array[size-1]<<"";
//      PrintArray(array,size-1);
//  }
//}
//
//void FunTest5()//5.逆序打印数组
//{
//  int array[]={1,2,3,4,5,6,7,8,9};
//  PrintArray(array,9);
//}

//bool IsPalindereme(char* str,size_t size)//6.判断是否为回文字符串
//{
//  if(size<=1)
//      return true;
//  if(str[0]!=str[size-1])
//      return false;
//  return IsPalindereme(str+1,size-2);
//}
//
//void FunTest6()//6.判断是否为回文字符串
//{
//  char* str="abbaba";
//  cout<<IsPalindereme(str,6)<<endl;
//}

void Perm(int* array,int size,int N)//7.全排列
{
    size_t idx;
    if(size==N)
    {
        for(idx=0;idx<N;++idx)
           cout<<array[idx]<<"";
        cout<<endl;
    }
    else
    {
        for(idx=N;idx<size;++idx)
        {
           std::swap(array[idx],array[N]);
           Perm(array,size,N+1);
           std::swap(array[idx],array[N]);
        }
    }
}

void FunTest7()//7.全排列
{
    int array[]={1,2,3,4};
    Perm(array,4,0);//全排列
    cout<<endl;
    Perm(array,4,2);//前两位不排
    cout<<endl;
    Perm(array,4,4);//4位都不动(不排)
}

int main()
{
    //FunTest1();//1.逆序打印单链表
    //FunTest2();//2.销毁链表中的结点
    //cout<<FunTest3()->_data<<endl;//3.查找值为data的结点
    //FunTest4();4.二分查找
    //FunTest5();//5.逆序打印数组
    //FunTest6();//6.判断是否为回文字符串
    FunTest7();//7.全排列
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值