Link List
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的更多相关文章
- oracle db link的查看创建与删除
1.查看dblink select owner,object_name from dba_objects where object_type='DATABASE LINK'; 或者 select * ...
- 解决Java程序连接mysql数据库出现CommunicationsException: Communications link failure错误的问题
一.背景 最近在家里捣鼓一个公司自己搭建的demo的时候,发现程序一启动就会出现CommunicationsException: Communications link failure错误,经过一番排 ...
- 解决绝对定位div position: absolute 后面的<a> Link不能点击
今天布局的时候,遇到一个bug,当DIV设置为绝对定位时,这个div后面的相对定位的层里面的<a>Link标签无法点击. 网上的解决方案是在绝对定位层里面添加:pointer-events ...
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
同时安装了VS2012和VS2010,用VS2010 时 >LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 问题说明:当安装VS2012之后 ...
- VS2013的 Browser Link 引起的问题
环境:vs2013 问题:在调用一个WebApi的时候出现了错误: 于是我用Fiddler 4直接调用这个WebApi,状态码是200(正常的),JSon里却提示在位置9409处文本非法, 以Text ...
- angular中的compile和link函数
angular中的compile和link函数 前言 这篇文章,我们将通过一个实例来了解 Angular 的 directives (指令)是如何处理的.Angular 是如何在 HTML 中找到这些 ...
- AngularJS之指令中controller与link(十二)
前言 在指令中存在controller和link属性,对这二者心生有点疑问,于是找了资料学习下. 话题 首先我们来看看代码再来分析分析. 第一次尝试 页面: <custom-directive& ...
- Visual Studio 2013中因SignalR的Browser Link引起的Javascript错误一则
众所周知Visual Studio 2013中有一个由SignalR机制实现的Browser Link功能,意思是开发人员可以同时使用多个浏览器进行调试,当按下IDE中的Browser Link按钮后 ...
- link与@import的区别
我们都知道link与@import都可以引入css样式表,那么这两种的区别是什么呢?先说说它们各自的链接方式,然后说说它们的区别~~~ link链入的方式: <link rel="st ...
- 错误提示:LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt 的解决方法
最近在win7 系统下,打算利用 cmake 生成项目文件,然后用vs2010进行编译.但是在cmake的时候出现错误弹窗:
随机推荐
- 为duilib的MenuDemo增加消息响应,优化代码和显示效果
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/38253297 第一部分 我在前一段时间研究了怎么制作duilib的菜单, ...
- [原创]cocos2d-x + Lua接入iOS原生SDK的实现方案
相信很多朋友在使用cocos2d-x+lua开发游戏时都遇到过接入iOS原生SDK的问题,比如常见的接应用内支付SDK,广告SDK或是一些社交平台SDK等等,我也没少接过这类SDK.这篇文章主要是对我 ...
- Nodejs与Net 和SQL 交互利器Edge.js
https://github.com/tjanczuk http://www.cnblogs.com/joylee/archive/2013/02/05/msnodesql.html edge.js这 ...
- Java处理InterruptedException异常小结
对于InterruptedException,一种常见的处理方式是捕捉它,然后什么也不做(或者记录下它,不过这也好不到哪去).不幸的是,这种方法忽略了这样一个事实:这期间可能发生中断,而中断可能导致应 ...
- GUI、模块化与结对编程(homework-03)
摘要: 在本次作业博客里,我将主要阐述作业3的收获.作业3表面是将之前的程序转换为图形界面(之前程序见http://www.cnblogs.com/shone/p/3348372.html),然而本质 ...
- 现代程序设计 homework-04
题目要求: 第四次作业,构造一个方阵将指定单词填入 stage 1:每个单词只出现1次,且八个方向各至少有两个单词 stage 2:矩阵长宽相等 stage 3:方阵的四个角都要参与单词的构建 算法思 ...
- Android实例-Delphi开发蓝牙官方实例解析(XE10+小米2+小米5)
相关资料:1.http://blog.csdn.net/laorenshen/article/details/411498032.http://www.cnblogs.com/findumars/p/ ...
- HDU 5821 Ball (贪心)
Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
- Spring SimpleJdbcTemplate batchUpdate() example
In this tutorial, we show you how to use batchUpdate() in SimpleJdbcTemplate class. See batchUpdate( ...
- Git使用过程中出现项目文件无法签入Source Control的情况
在VS中使用Git进行项目source control的过程中,有些文件不在source control之下,右键点击时,也找不到Undo, Commit命令 无法把他们签入进Source Contr ...