At first, i prepared to go through 《the introduction to algorithm》 ,however , i found some part of the book is difficult to understand; what’s more , i can’t borrow the book in the library. Then i found another book, 《algorithm in C》,which reveal most basic idea of some classical algorithm, so i decide to read this book firstly.

example 1:josephus funciton

#include<stdlib.h>
#include<stdio.h>
typedef struct node* link;
struct node
{
int item;
link next;
}; int main(int argc,char *argv[])
{
int i, N, M;
scanf("%d%d",&N,&M);
node *t = new node,*x;
x = t;
t->item = 1;
t->next = t;
for (i = 2; i <= N; i++)
{
x->next = new node;
x = x->next;
x->item = i;
x->next = t;
}
while (x != x->next)
{
for (i = 1; i < M; i++)
{
x = x->next;
}
x->next = x->next->next;
N--;
}
printf("%d\n",x->item);
}

example2: reverse link list

#include<stdlib.h>
#include<stdio.h>
typedef struct node* link;
struct node
{
int item;
link next;
node()
{
next = NULL;
}
}; link reverse(link x)
{
/*
将y后节点的指针保存在t中,然后将y的链接指向r,再使r移到y,y移到t
*/
link t, y = x, r = NULL;
while (y != NULL)
{
t = y->next;
y->next = r;
r = y;
y = t;
}
return r;
} int main(int argc,char *argv[])
{
int i, N, M;
scanf("%d%d",&N,&M);
node *t = new node,*x,*head;
x = t;
head = t;
t->item = 1;
t->next = t;
for (i = 2; i <= N; i++)
{
x->next = new node;
x = x->next;
x->item = i;
}
node *newhead = reverse(head);
while (newhead != NULL)
{
printf("%d ",newhead->item);
newhead = newhead->next;
}
}

Link List的更多相关文章

  1. oracle db link的查看创建与删除

    1.查看dblink select owner,object_name from dba_objects where object_type='DATABASE LINK'; 或者 select * ...

  2. 解决Java程序连接mysql数据库出现CommunicationsException: Communications link failure错误的问题

    一.背景 最近在家里捣鼓一个公司自己搭建的demo的时候,发现程序一启动就会出现CommunicationsException: Communications link failure错误,经过一番排 ...

  3. 解决绝对定位div position: absolute 后面的<a> Link不能点击

    今天布局的时候,遇到一个bug,当DIV设置为绝对定位时,这个div后面的相对定位的层里面的<a>Link标签无法点击. 网上的解决方案是在绝对定位层里面添加:pointer-events ...

  4. LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    同时安装了VS2012和VS2010,用VS2010 时 >LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 问题说明:当安装VS2012之后 ...

  5. VS2013的 Browser Link 引起的问题

    环境:vs2013 问题:在调用一个WebApi的时候出现了错误: 于是我用Fiddler 4直接调用这个WebApi,状态码是200(正常的),JSon里却提示在位置9409处文本非法, 以Text ...

  6. angular中的compile和link函数

    angular中的compile和link函数 前言 这篇文章,我们将通过一个实例来了解 Angular 的 directives (指令)是如何处理的.Angular 是如何在 HTML 中找到这些 ...

  7. AngularJS之指令中controller与link(十二)

    前言 在指令中存在controller和link属性,对这二者心生有点疑问,于是找了资料学习下. 话题 首先我们来看看代码再来分析分析. 第一次尝试 页面: <custom-directive& ...

  8. Visual Studio 2013中因SignalR的Browser Link引起的Javascript错误一则

    众所周知Visual Studio 2013中有一个由SignalR机制实现的Browser Link功能,意思是开发人员可以同时使用多个浏览器进行调试,当按下IDE中的Browser Link按钮后 ...

  9. link与@import的区别

    我们都知道link与@import都可以引入css样式表,那么这两种的区别是什么呢?先说说它们各自的链接方式,然后说说它们的区别~~~ link链入的方式: <link rel="st ...

  10. 错误提示:LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt 的解决方法

    最近在win7 系统下,打算利用 cmake 生成项目文件,然后用vs2010进行编译.但是在cmake的时候出现错误弹窗:

随机推荐

  1. 【LR】录制测试脚本中的基本菜单

    学习来源: MBoo,小强老师性能测试及Loadrunner培训  ——录制测试脚本: 1.Vuser -> run-time settings ->General Run Logic : ...

  2. ARM体系的异常中断

    在ARM体系中,通常有3种方式控制处理器的流程  1:在正常执行过程中,每执行一条ARM指令,程序计数器寄存器PC的值加四个字节,在每执行一条Thumb指令,程序计数器寄存器PC的值加两个字节,整个过 ...

  3. QT-【转】基础(略)

    第0篇 开始学习Qt 与Qt Creator 第1篇 基础(一)Qt开发环境的搭建和hello world 第2篇 基础(二)编写Qt多窗口程序 第3篇 基础(三)Qt登录对话框 第4篇 基础(四)添 ...

  4. JavaScript中的事件冒泡机制

    事件冒泡机制 事件冒泡发生的条件:当为多个嵌套的元素设置了相同的事件处理程序,它们将触发事件冒泡机制.在事件冒泡中,最内部的元素将首先触发其事件,然后是栈内的下一个元素触发该事件,以此类推,直到到达最 ...

  5. Spring 中context.start作用

    我们经常会看到 如下代码 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configPath. ...

  6. DOM笔记(三):Element接口和HTMLElement接口

    一.Element接口 Element接口表示一个元素,该接口扩展自Node接口,自然继承了Node接口的属性和方法,也有一套针对元素的属性和方法. Element接口常见的属性比较少,常用的就是一个 ...

  7. homework-09

    这次作业主要考察C++11的简单用法,个人感觉这样的练习对我这种编程能力比较差的非常有用,加深了对C++11的理解. Lambda的用法 计算“Hello World!”中 a.字母‘e’的个数 b. ...

  8. 现代C++作业2 与 围棋homework-06

    本文第一部分是现代C++作业2,第二部分是对围棋程序的部分建议,还有一些修改和优化体现在Github里面的代码中. 首先是现代C++作业. 1. 了解Lambda的用法.计算“Hello World! ...

  9. 分享一道我认为非常有思考价值JavaScript题目

    这是一道综合性的题目,如果你能快速清晰的分析整理出来,那我相信你对JavaScript是有一定的理解的了.我会先将题目的图片截取出来,供大家思考,在结尾在给出我的分析过程和答案,作个总结. 好,废话不 ...

  10. Beginning OpenGL ES 2.0 with GLKit Part 1

    Update 10/24/12: If you’d like a new version of this tutorial fully updated for iOS 6 and Xcode 4.5, ...