C程序:判断链表是否有环
/*
这是个常见的面试题哦,总之我面试的时候遇到过, 当时没有答上来
回去后想出来下面的方法一,该法还有个附加优点,可以判断出链表在哪个地方形成环的(即如果想拆开这个环,从哪个地方断开)。
后来知道还有个经典算法,即使用两个指针,一快一慢向前试探,如果最终重合则链表有环,果然优美的算法。
*/
#include <stdio.h>
typedef struct list {
int data;
struct list *next;
} LIST;
/* Method 1: check the occurrence of p->next from head to p */
int check_circle_1(LIST *head) {
LIST *p = head, *q= NULL;
if (p == NULL)
return 0;
while (p->next) {
/* check whether p points to itself */
if (p->next == p) {
return 1;
}
/* check the occurrence of p->next in head to p */
q = head;
while (q != p) {
if (q == p->next) {
return 1;
}
q = q->next;
相关文档:
#include <stdio.h>
#include <string.h> /* 程序多次调用biodkey(),应包含头文件bios.h */
#include <bios.h><br>/* 程序多次调用clrscr(),应包含头文件conio.h */
#include <conio.h>
#define MAX 100
#define PAGE 2
#define PRINT1 printf("------------------------------ ......
第二章 数据类型
一、数据类型分类
1 联合类型
2 函数类型
3 数量类型
3.1算术类型
① 基本数据类型
整型(包括char)
浮点型 ......
大家好,这里有IBM的三个长期需求:均是需要2年以上相关工作经验,其中Java以及Testing需要英语可以交流,C/Unix不需要语言。
Java 大连
描述:Java programming, knowledge in J2SE, SWT/JFace, XML. Eclipse programming, knowledge in eclipse architecture. Clear understanding of plugin development. Hands-on exp ......
用宏实现一个swap功能
#include <stdio.h>
#include <stdlib.h>
#define SWAP( TYPE,ARG1,ARG2 ) \
void TYPE##Swap( TYPE *p, TYPE *q ) \
{ \
TYPE tmp = *p; \
*p = *q; \
*q = tmp; \
} \
TYPE##Swap(&ARG1,&ARG2 ......
汉诺塔算法的递归与非递归的C以及C++源代码
By Minidxer | January 30, 2008
汉诺塔(又称河内塔)问题其实是印度的一个古老的传说。
开天辟地的神勃拉玛(和中国的盘古差不多的神吧)在一个庙里留下了三根金刚石的棒,第一根上面套着64个圆的金片,最大的一个在底下,其余一个比一个小,依次叠上 ......