/*
题目:输入一个单向链表,输出该链表中倒数第 k 个结点。链表的倒数第 0 个结点为链表
的尾指针。
链表结点定义如下:
struct node
{
int data;
struct node *next;
};
typedef struct node* PtrToNode;
typedef PtrToNode Position;
typedef Position List;
*/
#include <iostream>
#include <assert.h>
#include "./List.h"
using namespace std;
void print_list(List list)
{
assert(list != NULL);
list = First(list);
while(list != NULL)
{
cout<<list->data<<" ";
list = list->next;
}
cout<<endl;
}
Position get_last_kth(List list, int k)
{
assert(list != NULL);
Position tmp = First(list);
while(tmp != NULL && --k >= )
tmp = tmp->next;
if(NULL == tmp)//the length of list <= k
return NULL;
Position ret = First(list);
while(tmp != NULL)
{
ret = ret->next;
tmp = tmp->next;
}
return ret;
}
int main(int argc, char const *argv[])
{
List list = CreateEmptyList();
int k;
for(int i = ; i != ; ++i)
PushBack(i, list);
print_list(list);
while(cin>>k)
{
Position tmp = get_last_kth(list, k);
cout<<"k:"<<k<<" = ";
if(tmp == NULL)
cout<<"NULL";
else
cout<<tmp->data;
cout<<endl;
}
return ;
}

最新文章

  1. Xamarin 跨移动端开发系列(01) -- 搭建环境、编译、调试、部署、运行
  2. Scrum Meeting 10-20151216
  3. Android 之 2048 的游戏逻辑分析
  4. linux下MYSQL备份与恢复
  5. 对js原型的理解
  6. 智能指针(二):shared_ptr实现原理
  7. Toad for Oracle 快捷键
  8. nginx 调优
  9. 《javascript高级程序设计》对象图
  10. 清空DNS缓存
  11. Solr学习(2) Solr4.2.0+IK Analyzer 2012
  12. Weex-语法笔记 一
  13. 【转】SWT/JFace的对话框
  14. ASP.NET Web API 2 消息处理管道
  15. Visual Studio Many Projects in One Solution VS中多工程开发
  16. Python云图——WordCloud了解一下
  17. IntelliJ IDEA 中SpringBoot对Run/Debug Configurations配置 SpringBoot热部署
  18. css 利用border 绘制三角形. triangle
  19. 动态添加布局、动态添加View、LinearLayout动态添加View;
  20. Python 通过gevent实现协程

热门文章

  1. Leetcode 7 Reverse Integer 数论
  2. 转:Media Player Classic - HC 源代码分析
  3. Socket编程基本流程实践
  4. Dalvik VM (DVM) 与Java VM (JVM)之间有哪些区别?
  5. 转:电子取证中AVI文件的文件雕复
  6. 奇怪吸引子---Russler
  7. 使用 Express 和 waterline 创建简单 Restful API
  8. Putty &amp; Ctrl+s 的魔咒
  9. Entity Framework Core 实现读写分离
  10. PHP中ob系列函数整理