![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C/C++
文章平均质量分 51
fduan
这个作者很懒,什么都没留下…
展开
-
解析简单的算术表达式(第1版)
解析简单的算术表达式(不含括号),如:2*12/3+3*9/1-3=这一版本目前尚未添加对特殊情况的处理,如分母为0的情形、表达式中含有非法字符的情形等。#include int parse_expr( char * p, char * q ){ int a = 0, b = 0; char op; while( *p >= '0' && *p <=原创 2015-05-11 01:35:38 · 823 阅读 · 0 评论 -
数据结构题典020:栈的应用——数制转换(ANSI C)
题目:将十进制数 src 转换为 k 进制数。原理:N = ( N div k ) x k + N mod k,其中div为整除,mod为取余。void num_sys_convert( int src, int k ){ link_stack ls; init_stack_link( &ls ); while( src != 0 ) { push_link( &ls,原创 2011-12-30 15:02:58 · 668 阅读 · 0 评论 -
链式栈的实现(ANSI C)
1、链式栈ADT的定义#ifndef _link_stack_h#define _link_stack_htypedef int elem_type;struct stack_node;typedef struct stack_node stack_node;typedef stack_node * snode_ptr;typedef stack_node * link_sta原创 2011-12-30 14:33:50 · 463 阅读 · 0 评论 -
顺序栈的实现(ANSI C)
1、顺序栈ADT定义#ifndef _seq_stack_h#define _seq_stack_h#define STACK_INIT_SIZE 100#define STACK_INCR_SIZE 10typedef struct seq_stack{ int * base; int * top; int size;}seq_stack;void init_sta原创 2011-12-30 14:15:24 · 509 阅读 · 0 评论 -
数据结构题典017:从无序数据建立有序顺序表(ANSI C)
题目:从无序的输入数据中建立一个递增有序的顺序表。int order_insert_array( int x[], int e, int n, int m ){ int len = m, pos, i; if( m == 0 ) x[len++] = e; else { i = 0; while( i < m && x[i] < e ) ++i; pos = (原创 2011-12-29 22:59:00 · 1891 阅读 · 0 评论 -
数据结构题典018:三个有序单链表求交(ANSI C)
题目:设有三个带头结点的元素按非递减有序排列的单链表A, B, C,对A进行如下操作,使A中仅含有三个表中的交,且没有值相同的结点,并释放无用结点。限定时间复杂度为O( m + n + p ),其中m, n, p分别为三个表的长度。/* * Intersection of three ordered linked lists. * * fduan, Dec. 29, 2011.原创 2011-12-29 23:24:36 · 1010 阅读 · 0 评论 -
数据结构题典014:单链表的子序列检测(ANSI C)
题目:设有两个整数序列 A = a_1, a_2, ..., a_m和 B = b_1, b_2, ..., b_n已存入两个单链表,设计算法判断序列B是否为序列A的子序列。int is_subseq_llist( link_list lst_a, link_list lst_b ){ node_ptr pa = lst_a->next, pb = lst_b->next; while(原创 2011-12-29 00:13:23 · 1340 阅读 · 1 评论 -
数据结构题典013:链表合并之二(ANSI C)
题目:设有两个元素递增的单链表(带头结点),编写算法将二者合并为按元素递减排列的链表L,要求利用原表的结点空间存放L。/* * fduan, Dec. 28, 2011. */void calc_union_v2( link_list * lst_a, link_list * lst_b ){ node_ptr pa = *lst_a, pb = *lst_b, p = NULL;原创 2011-12-28 23:50:35 · 811 阅读 · 0 评论 -
数据结构题典016:按递增次序输出单链表所有元素(ANSI C)
题目:按递增次序输出单链表所有元素,并释放结点的存储空间。要求不使用数组做辅助空间。#include void min_elem( link_list * lst, node_ptr * min_prev, node_ptr * min_ptr ){ node_ptr p = (*lst)->next, prev = NULL; elem_type min_v = INT_MAX;原创 2011-12-29 02:40:09 · 3001 阅读 · 0 评论 -
数据结构题典012:链表求交集之二(ANSI C)
问题:已知两个按元素递增排列的链表,求二者交集,要求将结果放入第一个链表中。/* * Intersection of two ordered linked lists. * * fduan, Dec. 28, 2011. */void intersect_v2( link_list * lst_a, link_list lst_b ){ node_ptr pa = *lst_a原创 2011-12-28 23:24:09 · 989 阅读 · 0 评论 -
数据结构题典021:栈的应用——括号匹配的检验(C++)
题目:假设表达式中允许出现圆括号和方括号,其嵌套顺序随意,设计算法检验给定表达式中的括号是否匹配。/* * Brackets matching algorithm by utilizing stack. * * fduan, Dec. 31, 2011. */bool is_pair( char c1, char c2 ){ return ( c1 == '(' && c2原创 2011-12-31 03:16:18 · 2026 阅读 · 0 评论 -
Test
int ex01( LinkedList * lst, int x ){ assert( *lst != NULL ); int n = 0; NodePtr p = ( *lst )->next; NodePtr r = NULL, q = NULL; NodePtr pre = NULL; if( p == NULL ) return n;原创 2015-05-09 02:10:25 · 576 阅读 · 0 评论 -
简单选择排序(C & Java 实现)
C语言版 #include #define SWAP( a, i, j ) { t = a[i]; a[i] = a[j]; a[j] = t; }void disp_array( int a[], int n ){ int i; for( i = 0; i < n; ++i ) printf( "%d ", a[i] );原创 2012-08-30 06:14:26 · 675 阅读 · 0 评论 -
数据结构题典022:栈的应用——行编辑程序(C语言版)
编写程序实现从终端接收用户输入的数据,并存入用户数据区。输入#表示上一字符无效,输入@表示当前输入行整行无效。/* * line editor by using stack. * * fduan, Dec. 31, 2011. */void line_editor(){ using std::stack; stack ss; std::string li原创 2011-12-31 04:20:23 · 3636 阅读 · 1 评论 -
二叉树的先序、中序和后序的非递归遍历(C语言实现)
1、顺序栈的声明#ifndef _seq_stack_h#define _seq_stack_h#include "binary_tree.h"#define STACK_INIT_SIZE 5#define STACK_INCR_SIZE 2typedef bitree_node * stack_elem_type;//typedef int stack_原创 2012-08-06 05:34:29 · 3004 阅读 · 0 评论 -
给定正整数 N, 求使N < 2^m 成立的最小m(C语言版)
给定n,求使n unsigned int fixsize( unsigned int n ) { n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return n + 1;}原创 2012-02-09 23:32:51 · 1277 阅读 · 0 评论 -
堆排序算法(C & Java 实现)
C语言版#include #define SWAP( a, i, j ) { t = a[i]; a[i] = a[j]; a[j] = t; }#define LESS( a, b ) ( a < b ) void disp_array( int a[], int n ){ int i; for( i = 0; i < n; ++i ) printf( "%d ",原创 2012-08-30 11:05:30 · 617 阅读 · 0 评论 -
冒泡排序算法(C & Java 实现)
C语言版#include #define SWAP( a, b ) { t = a; a = b; b = t; }#define LESS( a, b ) a < bvoid disp_array( int a[], int n );void bubble_sort( int a[], int n ){ int i, j, t; int flag = 1; /原创 2012-08-28 00:58:18 · 822 阅读 · 0 评论 -
快速排序算法(C & Java 实现)
C语言版 #include void disp_array( int a[], int n ){ int i; for( i = 0; i < n; ++i ) printf( "%d ", a[i] ); printf( "\n" );}int partition( int a[], int low, int high ){ in原创 2012-08-30 07:28:27 · 786 阅读 · 0 评论 -
最小堆的实现(C 语言版)
最小堆本质上是一棵局部有序的完全二叉树,适于需要查找序列中前k小的元素的场合,如构造Huffman树。核心算法为 向上调整(fix up)和向下调整(fix down)算法。void fix_up_min_heap( int arr[], int n, int len, int i ){ int j = ( i - 1 ) / 2; // parent index int tmp =原创 2012-08-23 20:09:25 · 4937 阅读 · 0 评论 -
面试题解:二维数组中的查找(C 语言版)
题目描述:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。输入:输入可能包含多个测试样例,对于每个测试案例,输入的第一行为两个整数m和n(1输入的第二行包括一个整数t(1接下来的m行,每行有n个数,代表题目所给出的m行n列的矩阵(矩阵如题目描述所示,每一原创 2012-08-19 21:55:31 · 1770 阅读 · 0 评论 -
数据结构题典010:有序单链表的交集(ANSI C)
题目:设有两个非递减有序的单链表,编写算法求二者的交集(以链表形式存放),要求交集中元素保持递增有序。分析:此题关键是要跳过相邻的重复元素。/* * Intersection of two non-descending linked lists. * * fduan, Dec. 28, 2011. */void intersection( link_list lst_a, l原创 2011-12-28 22:08:45 · 1927 阅读 · 0 评论 -
数据结构题典011:有序单链表的并集(ANSI C)
题目:与009类似,但这次是求并集。分析:与有序顺序表归并思路类似,只是注意要处理两个顺序表当前指针指向的元素相同的情形(此时只为并集中增加一项)。/* * Union set of two linked lists which have non-descending order. * * fduan, Dec. 28, 2011. */void calc_union( lin原创 2011-12-28 22:24:52 · 1812 阅读 · 0 评论 -
数据结构题典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 · 1006 阅读 · 0 评论 -
单循环链表结构的实现(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 阅读 · 0 评论 -
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 · 592 阅读 · 0 评论 -
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 · 753 阅读 · 0 评论 -
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 · 612 阅读 · 0 评论 -
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 · 716 阅读 · 0 评论 -
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 · 641 阅读 · 0 评论 -
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 · 476 阅读 · 0 评论 -
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 · 551 阅读 · 0 评论 -
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 · 1265 阅读 · 0 评论 -
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 · 635 阅读 · 0 评论 -
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 · 849 阅读 · 0 评论 -
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 · 715 阅读 · 0 评论 -
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 · 1080 阅读 · 0 评论 -
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 · 992 阅读 · 0 评论 -
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 · 825 阅读 · 0 评论 -
数据结构题典008:顺序表的合并(ANSI C)
题意:设有顺序表La和Lb,二者中元素均为非递减有序,空间足够大。设计算法将Lb中的元素合并到La中,使新的La元素仍非递减有序。分析:此题与将两个有序顺序表合并到第三个顺序表中的思路类似,只是为了减少移动次数,比较的次序从两线性表的尾部开始,这样每个元素最多只移动一次。/* * merging of two ordered sequences * * fduan, Dec. 27,原创 2011-12-28 01:25:43 · 873 阅读 · 0 评论