- 博客(103)
- 收藏
- 关注
原创 数据结构题典009:递归实现单链表逆序数出(ANSI C)
设所考虑单链表含头结点,写出逆序输出表中元素的递归算法。void inv_trav_recur( link_list p ){ if( p != NULL ) { inv_trav_recur( p->next ); printf( "%d ", p->data ); }}void inverse_traverse_llist( link_list lst ){ i
2011-12-28 08:56:57 1009
原创 数据结构题典008:顺序表的合并(ANSI C)
题意:设有顺序表La和Lb,二者中元素均为非递减有序,空间足够大。设计算法将Lb中的元素合并到La中,使新的La元素仍非递减有序。分析:此题与将两个有序顺序表合并到第三个顺序表中的思路类似,只是为了减少移动次数,比较的次序从两线性表的尾部开始,这样每个元素最多只移动一次。/* * merging of two ordered sequences * * fduan, Dec. 27,
2011-12-28 01:25:43 875
原创 数据结构题典007:顺序表中元素块的位置交换(ANSI C)
假设有顺序表 L.elem[] = { a_1, a_2, ..., a_m, | b_1, b_2, ..., b_n },设计算法将L的两部分元素互换,使得 L.elem[] = { b_1, b_2, ..., b_n, |a_1, a_2, ..., a_m }思路一:循环移位,数组整体循环右移n个元素void circ_right_shift( int c[], int len,
2011-12-26 00:20:36 2237
原创 数据结构题典006:有序表中冗余元素的删除(ANSI C)
1、顺序表,假设元素已按升序排序/* * remove redundant elements from ordered sequence */int remove_redundant_elem( int c[], int n ){ int i = 0, j = 1; while( j < n ) { if( c[i] != c[j] ) c[++i]
2011-12-25 19:24:57 778
原创 数据结构题典005:单链表的复制(ANSI C)
数据结构题典005:单链表的复制(ANSI C)void clone_node( node_ptr src, node_ptr * dst ){ if( src != NULL ) { *dst = ( node_ptr )malloc( sizeof( node ) ); (*dst)->data = src->data; } else *dst = NULL;}
2011-12-25 10:48:23 766
原创 数据结构题典004:对单链表元素插入排序(ANSI C)
带头结点的单链表void insert_sort_llist( link_list * lst ){ node_ptr h = *lst, p = h->next, r = NULL, q = NULL; h->next = NULL; while( p != NULL ) { q = p->next; r = h; while( r->next != NULL && r
2011-12-25 10:38:00 755
原创 数据结构题典003:线性表的就地逆置/翻转(ANSI C)
1、顺序表a) 通过数组下标访问元素/* * 通过数组下标访问 */void reverse_sqlist( int c[], int n ){ int i = 0, j = n - 1; int t; for( ; i < j; ++i, --j ) { t = c[i]; c[i] = c[j]; c[j] = t; }}b) 通过指针访问元素/
2011-12-25 02:29:54 1076
原创 数据结构题典002:删除单链表中最大元素所在结点(ANSI C)
分析:此题关键在于找到最大元素所在的前驱结点。status_code remove_max_elem_llist( link_list * lst, elem_type * e ){ status_code res = Success; node_ptr h = *lst, p = h, pre_max = p; int max_v = -1000; while( p->next
2011-12-25 01:24:13 3266
原创 数据结构题典001:有序线性表的归并(ANSI C)
1、有序顺序表的归并排序,假设a[0] void merge_list( int a[], int m, int b[], int n, int ** c ){ int i = 0, j = 0; int *pa = a, *pb = b, *pc = NULL, *qa = a + m - 1, *qb = b + n - 1; if( *c != NULL )
2011-12-24 22:14:21 752
原创 用单链表实现多项式运算(ANSI C)
1、多项式结构定义polynomial.h#ifndef _polynomial_h#define _polynomial_hstruct poly_term{ int expn; int coef;};typedef struct poly_term poly_term;typedef poly_term * poly_term_ptr;struct poly_n
2011-12-23 02:48:01 747
原创 双循环链表的实现(ANSI C)
1、double_link_list.h#ifndef _double_link_list_h#define _double_link_list_h#include "common.h"typedef int elem_type;struct node;typedef struct node node;typedef node * node_ptr;typedef no
2011-12-21 00:58:58 451
原创 单循环链表结构的实现(ANSI C)
1、首先定义公共头文件common.h,其中包含了枚举类型status_code的定义,目的是将其作为循环链表结构操作的返回类型。#ifndef _common_h#define _common_henum status_code { Success, Fail, MemoryOut, NotPresent, RangeError };typedef enum status_co
2011-12-20 04:03:35 712
原创 K&R C Exercise 3-1 Solution
/* * Exercise 3-1 Write the binary search algorithm with only * one test inside the loop. * * Written by fduan on Dec. 15, 2011. */#include /* binary search: v[0] <= v[1] <= ... <= v[n-1] */
2011-12-15 03:00:01 996
原创 K&R C Exercise 2-9 Solution
/* * Exercise 2-9 In a two's complement number system, * x & ( x - 1 ) deletes the rightmost 1-bit in x. * Use this to write a fast version of bitcount. * * Written by fduan on Dec. 14, 2011.
2011-12-15 02:43:28 646
原创 K&R C Exercise 2-8 Solution
/* * Exercise 2-8 Write a function rightrot(x, n) that * returns the value of the integer x rotated to the * right by n bit positions. * * Written by fduan on Dec, 14, 2011. */#include /* th
2011-12-15 02:33:15 1091
原创 K&R C Exercise 2-7 Solution
/* * Exercise 2-7 Write a function invert(x, p, n) that returns * x with the n bits that begins at position p inverted, leaving * the other bits unchanged. * * fduan, Dec. 14, 2011. */#include
2011-12-15 02:03:39 764
原创 K&R C Exercise 2-6 Solution
/* * Exercise 2-6 Write a function setbits( x, p, n, y ) that * returns x with the n bits that begin at position p set to * the rightmost n bits of y, leaving the other bits unchanged. * * fduan
2011-12-14 02:26:26 827
原创 判断单链表中环存在与否的判别(C++)
这是一道面试题,要求用最简洁的代码写出判别单链表中是否存在环。我是这样做的,可将单链表视作一种简化的图,在依照链表指针域遍历链表时,保留每个结点的入度,若在到达尾结点之前出现入度为2的结点,说明链表中存在环,同时终止遍历。/* * 判别单链表中是否存在环 */#include using std::map;typedef int elem_type;struct Node
2011-12-13 17:38:38 673
原创 K&R C Exercise 2-5 Solution
/* * Exercise 2-5 Write the function any( s1, s2 ), which returns * the first location in the string s1 where any character from * the string s2 occurs, or -1 if s1 contains no character from * s
2011-12-13 01:23:52 593
原创 K&R C Exercise 2-4 Solution
/* * Exercise 2-4 Write an alternate version of squeeze( s1, s2 ) * that deletes each character in s1 that matches any character * in the string s2. * * fduan, Dec. 12, 2011. */#include void
2011-12-13 00:53:30 760
原创 K&R C Exercise 2-3 Solution
/* * Exercise 2-3 Write the function htoi(s), which converts a string * of hexadecimal digits into its equivalent integer value. */#include #include #define MAX_LEN 10int is_valid_hex_alpha(
2011-12-12 11:59:46 851
原创 K&R C Exercise 1-22 Solution
/* * Exercise 1-22 Write a program to "fold" long input lines into * two or more shorter lines after the last non-blank character * that occurs before the n-th column of input. Make sure your *
2011-12-12 00:15:07 649
原创 K&R C Exercise 1-21 Solution
/* * Exercise 1-21 Write a program entab that replaces strings of blanks * by the minimum number of tabs and blanks to achieve the same spacing. * * fduan, Dec. 12, 2011 */#include #define TA
2011-12-11 21:38:42 648
原创 K&R C Exercise 1-20 Solution
/* * Exercise 1-20 Write a program detab that replaces tabs in the input * with the proper number of blanks to space to the next tab stop. Assume * a fixed set of tab stops, say every n columns. *
2011-12-11 16:42:14 1268
原创 K&R C Exercise 1-19 Solution
/* * Exercise 1-19 Write a function reverse(s) that reverses * that character string s. Use it to write a program that * reverses its input a line at a time. * * fduan, Dec. 11, 2011 */#includ
2011-12-11 01:36:01 554
原创 K&R C Exercise 1-18 Solution
/* * Exercise 1-18 Write a program to remove trailing blanks and tabs from * each line of input, and to delete the entirely blank lines. * * fduan, Dec. 11, 2011 */#include #define MAX_LEN 10
2011-12-11 01:35:21 719
原创 K&R C Exercise 1-17 Solution
/* * Exercise 1-17 Write a program to print all input lines that are * longer than 80 characters. * * fduan, Dec. 11, 2011 */#include #define MAX_LEN 1000#define LONG_LINE 80int getline(
2011-12-10 22:01:02 1081
原创 K&R C Exercise 1-16 Solution
/* * Exercise 1-16 Revise the main routine of the longest line * program so it will correctly print the length of arbitrarily * long inputl lines, and as much as possible of the text. * * fduan
2011-12-10 21:27:14 716
原创 K&R C Exercise 1-13 Solution
/* * Exercise 1-13 * print a histogram of the lengths of words in its input. It is easy to draw the * histogram with the bars horizontal; a vertical orientation is more challenging. * * fduan,
2011-12-07 21:00:35 615
原创 K&R C Exercise 1-12 Solution
/* * Exercise 1-12 Write a program that prints its input one word per line. * * fduan, Dec. 07, 2011 */#include #define IN 1#define OUT 0int main(){ int c, state, nw; state = OUT; nw
2011-12-07 17:13:57 477
原创 K&R C Exercise 1-9 Solution
Exercise 1-9 Write a program to copy its input to output, replacing each string of one or more blanks by a single blank. 下面给出两种求解思路,第一种方法考虑输出字符的条件,不难看出,当当前字符为非空格时以及当前字符为空格,而上一个字符为非空格时,应当输出当前字符。为
2011-12-07 01:56:56 554
原创 单循环链表的实现(C语言版)
注:代码注释及说明待补充…… 结构声明typedef int elem_type;struct LNode{ elem_type elem; LNode * next;};typedef LNode * circ_list; 基本操作status_code init_list( circ_list & lst ){ lst = (circ_list)mal
2011-06-26 23:42:00 736
原创 Implementation of Static Linked List ( in C/C++ )
Using static linked list is an alternative solution when the programming language you are using does not supportpointer operations. This short article gives some key operations of the static linked
2011-06-25 21:36:00 969
原创 算法百题009:可数集
问题描述:有理数全体为一可数集合。 把表示所有有理数的分数按下列方法进行列举。1/1 1/2 1/3 1/4.....2/1 2/2 2/3....3/1 3/2 ....4/1..............我们以z字型方法给上表的每项编号。特定方法:第一项是1/1,然后是1/2、2/1、3/1、2/2、1/3、1/4、2/3……。编程输入项号N(1 分析
2011-06-13 21:09:00 784
原创 算法百题008:Bash Game(又名取石子游戏)
Bash Game (巴什博奕) 取自百度百科 http://baike.baidu.com/view/1952623.htm 设只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个。最后取光者得胜。显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者拿走多少个,后取者都能够一次拿走剩余的物品,后者取胜。因此我们发现了如何取胜的法则:如果n=(m+
2011-06-13 15:28:00 755
原创 Solution of ZOJ 1078 Palindrome Number
We say that a number is a palindrom if it is the sane when read from left to right or from right to left. The objective of this problem is to verify if a set of given numbers are palindroms in any basis from 2 to 16.
2011-05-29 19:38:00 595
原创 Solution of ZOJ 1067 Color Me Less
A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just such a mapping in a standard twenty-four bit RGB color space. The input consists of a target set of sixteen RGB color
2011-05-29 19:21:00 691
原创 Non-recursive Version of DFS Algorithm
DFS is the most important graph traversal algorithm and has been adopted as the default traversal module for many software system. In this short article, the non-recursive implementation of DFS in C++ is given.
2011-05-21 12:30:00 876
原创 Solution of ZOJ 2679 Old Bill
<br />Among grandfather��s papers a bill was found:<br />72 turkeys $_679_<br />The first and the last digits of the number that obviously represented the total price of those turkeys are replaced here by blanks (denoted _), for they are faded and are now
2011-05-17 16:29:00 755
原创 Solution of ZOJ 2840 File Search
Have you ever used file searching tools provided by an operating system? For example, in DOS, if you type "dir *.exe", the OS will list all executable files with extension "exe" in the current directory.
2011-05-17 15:36:00 1011
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人