朱胜本
CreazyApple · 共 48 篇文章
字符型数组表示大整数 并排序、求和
/* 建立一种数据结构,可以存储任意个、任意长度的整数, * 利用这个数据结构,输入一串数,排序,求累加和 * 思路:用以链表表示,用字符型数组表示大整数 链头存储和 */ #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct _Node{ char *data; int length; struct _Node *next; }Node; char *GetSum(char *,char *); int CompareData(char *a,char *b); Node* InitList(vo…
2012-09-06 15:43 · 评论(0)
关于字符指针和字符数组的一些错误
老犯此类错误!!! char *p = "abc";则abc存储在常量存储区,而p直接指向了这个常量存储区,所以不能改变abc的内容 *p = 'd'; 是错误的,因为是常量存储区,不能改变。 char *p = NULL; *p = 'd'; 也是错误的。 char *p; *p='d'; 也是错误的,提示指针没有赋初值。 以上几种更不可以strcpy(p,"abc"); 总之,上面的指针都相当于const char *p,不能修改*p的内容!但可以修改其指向,如: char *a = "abc"; p = a; 是可以的。但这样的操作不安全!!因为如果一旦a被回收了,p就跟着完蛋了。尤其…
2012-09-06 11:28 · 评论(0)
采用命令行方式复制任意多个文件内容到一个文件中(华科保研机试)
采用命令行方式复制任意多个文件内容到一个文件中,如下所示: 命令行形式:mycopy1.txt 2.txt 3.txt 4.txt ... 功能:复制2.txt3.txt 4.txt …的内容到1.txt中 #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char buffer[80] = {0}; int i = 0; FILE *fout = NULL; FILE *fin = NULL; if(argc < 3) { printf("please input right argument…
2012-09-06 10:08 · 评论(0)
采用命令行方式复制任意多个文件内容到一个文件中(华科保研机试)
采用命令行方式复制任意多个文件内容到一个文件中,如下所示: 命令行形式:mycopy1.txt 2.txt 3.txt 4.txt ... 功能:复制2.txt3.txt 4.txt …的内容到1.txt中 #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char buffer[80] = {0}; int i = 0; FILE *fout = NULL; FILE *fin = NULL; if(argc < 3) { printf("please input right argument…
2012-09-06 10:08 · 评论(0)
有向图、有向网、无向图、无向网
#include<stdio.h> #include<stdlib.h> #include<limits.h> #include<string.h> #define MAX_NAME 5//顶点字符串的最大长度+1 #define MAX_INFO 20//相关信息字符串的最大长度+1 typedef int VRType; typedef char InfoType; typedef char VertexType[MAX_NAME]; #define INFINITY INT_MAX // 用整型最大值代替∞ #define MAX_VEX_NUM 20 typedef enum{DG,…
2012-09-06 09:02 · 评论(0)
有向图、有向网、无向图、无向网
#include<stdio.h> #include<stdlib.h> #include<limits.h> #include<string.h> #define MAX_NAME 5//顶点字符串的最大长度+1 #define MAX_INFO 20//相关信息字符串的最大长度+1 typedef int VRType; typedef char InfoType; typedef char VertexType[MAX_NAME]; #define INFINITY INT_MAX // 用整型最大值代替∞ #define MAX_VEX_NUM 20 typedef enum{DG,…
2012-09-06 09:02 · 评论(0)
冒泡排序和选择排序
/* 冒泡排序 */ #include<stdio.h> void BubbleSort (int s[],int n) { int i,j,temp=0; int exchange; exchange=0; for(i=0;i<n;i++) { for(j=n-1; j > i; j--) { if(s[j] < s[j-1]){ temp=s[j];//注意冒泡排序交换的是相邻两个元素 s[j]=s[j-1]; s[j-1]=temp; exchange=1; } } if(!exchange) return; } } /* 选择排序 */ void SelectSort(int arr…
2012-09-05 23:26 · 评论(0)
冒泡排序和选择排序
/* 冒泡排序 */ #include<stdio.h> void BubbleSort (int s[],int n) { int i,j,temp=0; int exchange; exchange=0; for(i=0;i<n;i++) { for(j=n-1; j > i; j--) { if(s[j] < s[j-1]){ temp=s[j];//注意冒泡排序交换的是相邻两个元素 s[j]=s[j-1]; s[j-1]=temp; exchange=1; } } if(!exchange) return; } } /* 选择排序 */ void SelectSort(int arr…
2012-09-05 23:26 · 评论(0)
构建哈夫曼树并打印哈夫曼编码
#include <stdio.h> #include <stdlib.h> typedef struct _Node{ float data; struct _Node *lchild; struct _Node *rchild; int huffmanCode[10],pos;//保存编码,在数组中从后往前存储,最多10位 } Node,*Tree; /* 排序算法,从大到小 */ void Sort(float a[],int low,int high) { int i; float temp; if(low == high)return; for(i=low; i<=high; i+…
2012-09-05 21:08 · 评论(0)
构建哈夫曼树并打印哈夫曼编码
#include <stdio.h> #include <stdlib.h> typedef struct _Node{ float data; struct _Node *lchild; struct _Node *rchild; int huffmanCode[10],pos;//保存编码,在数组中从后往前存储,最多10位 } Node,*Tree; /* 排序算法,从大到小 */ void Sort(float a[],int low,int high) { int i; float temp; if(low == high)return; for(i=low; i<=high; i+…
2012-09-05 21:08 · 评论(0)
二叉树的各种操作复习
/*二叉树的各种操作复习*/ #include <stdio.h> #define BACK_ODER -1 #define IN_ODER 0 #define PRE_ODER 1 #define LEVEL_ODER 2//层次化遍历 typedef struct _Node{ char data; struct _Node *lchild; struct _Node *rchild; } Node,*Tree; /* 生成二叉树的普通方法 * 按先序次序输入二叉树中结点的值 * 构造二叉链表表示的二叉树T。输入空格表示空子树。 */ Node * CreateTree() { char…
2012-09-04 14:17 · 评论(0)
二叉树的各种操作复习
/*二叉树的各种操作复习*/ #include <stdio.h> #define BACK_ODER -1 #define IN_ODER 0 #define PRE_ODER 1 #define LEVEL_ODER 2//层次化遍历 typedef struct _Node{ char data; struct _Node *lchild; struct _Node *rchild; } Node,*Tree; /* 生成二叉树的普通方法 * 按先序次序输入二叉树中结点的值 * 构造二叉链表表示的二叉树T。输入空格表示空子树。 */ Node * CreateTree() { char…
2012-09-04 14:17 · 评论(0)
已知二叉树先序和中序遍历,生成二叉树
参考: 如先序为:abdc,中序为:bdac . 则程序可以求出后序为:dbca 。此种题型也为数据结构常考题型。 算法思想:先序遍历树的规则为中左右,则说明第一个元素必为树的根节点,比如上例 中的a就为根节点,由于中序遍历为:左中右,再根据根节点a,我们就可以知道,左子树包含 元素为:db,右子树包含元素:c,再把后序进行分解为db和c(根被消去了),然后递归的 进行左子树的求解(左子树的中序为:db,后序为:db),递归的进行右子树的求解(即右 子树的中序为:c,后序为:c)。如此递归到没有左右子树为止。 关于“已知先序和后序求中序”的思考:该问题不可解,因为对于先序和后序不能唯一的确定…
2012-09-02 23:39 · 评论(0)
已知二叉树先序和中序遍历,生成二叉树
参考: 如先序为:abdc,中序为:bdac . 则程序可以求出后序为:dbca 。此种题型也为数据结构常考题型。 算法思想:先序遍历树的规则为中左右,则说明第一个元素必为树的根节点,比如上例 中的a就为根节点,由于中序遍历为:左中右,再根据根节点a,我们就可以知道,左子树包含 元素为:db,右子树包含元素:c,再把后序进行分解为db和c(根被消去了),然后递归的 进行左子树的求解(左子树的中序为:db,后序为:db),递归的进行右子树的求解(即右 子树的中序为:c,后序为:c)。如此递归到没有左右子树为止。 关于“已知先序和后序求中序”的思考:该问题不可解,因为对于先序和后序不能唯一的确定…
2012-09-02 23:39 · 评论(0)