#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;
}
数据结构---递归用法
最新推荐文章于 2022-05-13 23:55:14 发布