前言:近期非常的无力,我有些羡慕17级的学生,作为一个以后要在这个行业谋生的大学生来说,确实有扎实的编程的基本功。
最后送给自己一句鸡汤,一切都来得及。
思考一个问题:用递归方法求单链表中倒数第K个结点。
递归
递归就是自己调用自己的概念。分为两个部分:
1.结束条件(递归不能无限进行下去)
2.递归方程(这一部分很难,需要发现规律,发现规律后,还要用编程思维把递归的方法表示出来)
老师说的一句话我认为找的很重要:要动态的去学习递归,不要只是停留在静态的方程上(所以要手动模拟一下会理解更深)
在网上找的大神的代码
#include<iostream>
using namespace std;
struct ListNode{
int value;
ListNode* next;
ListNode(int data = int()) :value(data), next(NULL)//给构造函数初始化
{}
};
class ListIsTooShort{};
ListNode* _FindKthToTail(ListNode* head,int& k)
{
if(head==NULL)
{
return NULL;
}
else
{
ListNode* tmp=_FindKthToTail(head->next,k);
if(k==0)
return tmp;
}
if(k==1)
{
k=0;
return head;
}
else if(k>1)
k--;
}
ListNode* FindKthToTail(ListNode* head, int k)throw(ListIsTooShort)
{
ListNode* tmp = _FindKthToTail(head, k);
if (k == 0)
return tmp;
else
throw ListIsTooShort();
}
//---------测试代码---------
void printList(ListNode* list)
{
if (list == NULL)
{
cout << "Nothing!" << endl;
return;
}
while (list != NULL)
{
cout << list->value << " ";
list = list->next;
}
cout << endl;
}
void test()
{
int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int i;
ListNode* list = NULL;
ListNode* tail = NULL;
for (i = 0; i<sizeof(arr) / sizeof(arr[0]); ++i)
{
if (list == NULL)
{
list = new ListNode(arr[i]);
tail = list;
}
else
{
tail->next = new ListNode(arr[i]);
tail = tail->next;
}
}
printList(list);
try{
cout << "the 1st Node to End is:" << FindKthToTail(list, 1)->value << endl;
}
catch (const ListIsTooShort&)
{
cout << "this list is too short!" << endl;
}
try{
cout << "the 2st Node to End is:" << FindKthToTail(list, 2)->value << endl;
}
catch (const ListIsTooShort&)
{
cout << "this list is too short!" << endl;
}
try{
cout << "the 11st Node to End is:" << FindKthToTail(list, 11)->value << endl;
}
catch (const ListIsTooShort&)
{
cout << "this list is too short!" << endl;
}
try{
cout << "the 15st Node to End is:" << FindKthToTail(list, 15)->value << endl;
}
catch (const ListIsTooShort&)
{
cout << "this list is too short!" << endl;
}
}
int main()
{
test();
system("pause");
return 0;
}
cin>>
cout<<
<string> : 包含string类的定义
cmath中常用库函数:
int abs(int i);//返回整型参数i的绝对值
double fabs(double x);//返回双精度参数x的绝对值
long labs(long n);//返回长整型参数n的绝对值
double exp(double x);//返回指数函数e^x的值
double log(double x);//返回logex的值
double log10(double x) 返回log10x的值
double pow(double x,double y) 返回x^y的值
double pow10(int p) 返回10^p的值
double sqrt(double x) 返回+√x的值
double acos(double x) 返回x的反余弦arccos(x)值,x为弧度
double asin(double x) 返回x的反正弦arcsin(x)值,x为弧度
double atan(double x) 返回x的反正切arctan(x)值,x为弧度
double cos(double x) 返回x的余弦cos(x)值,x为弧度
double sin(double x) 返回x的正弦sin(x)值,x为弧度
double tan(double x) 返回x的正切tan(x)值,x为弧度
double hypot(double x,double y) 返回直角三角形斜边的长度(z),
x和y为直角边的长度,z^2=x^2+y^2
double ceil(double x) 返回不小于x的最小整数
double floor(double x) 返回不大于x的最大整数
int rand() 产生一个随机数并返回这个数
double atof(char *nptr) 将字符串nptr转换成浮点数并返回这个浮点数
double atol(char *nptr) 将字符串nptr转换成长整数并返回这个整数
double atof(char *nptr) 将字符串nptr转换成双精度数,并返回这个数,错误返回0
int atoi(char *nptr) 将字符串nptr转换成整型数, 并返回这个数,错误返回0
long atol(char *nptr) 将字符串nptr转换成长整型数,并返回这个数,错误返回0