递归实现的AVLtree的基本操作(嘿嘿,英文注释蛮爽的)

#include<iostream> #include<cstdlib> using namespace std; typedef struct TreeNode { int Element; int Height; TreeNode *Left; TreeNode *Right; }*MyAVLTree; MyAVLTree MakeEmpty(MyAVLTree &amp;T); void InorderTraverse(const MyAVLTree &amp;T); static int Max(int a,int b); static int ...
c++基础 | 2017-04-30 20:23 | 阅读 1438 次 | 评论 0 条

递归法实现二叉查找树的基本操作

#include<iostream> #include<cstdlib> using namespace std; /*查找二叉树,这里用的都是无敌的递归方法,因为它极容易理解, 也很容易编程,但值得注意的是返回值很重要*/ typedef struct TreeNode { int Element; TreeNode *Left; TreeNode *Right; }TreeNode,*MyTree; MyTree MakeEmpty(MyTree &amp;T); //二叉树置空 void InorderTraverse(const MyTree ...
c++基础 | 2017-04-29 22:36 | 阅读 1648 次 | 评论 0 条

全局有效的数据结构

一开始我用的变量是局部的它的毛病就是所有的操作只能做一次,然后啥也没改变。 那么解决的一个方法就是 引用地址 引用地址使得全局有效 不说了 直接上上点代码 typedef struct TreeNode { char Element; TreeNode *Left; TreeNode *Right; }*MyTree; void Exchange(MyTree T) { if(T) { MyTree Temp=NULL; if(T->Left||T->Right) { ...
c++基础 | 2017-04-28 16:22 | 阅读 1254 次 | 评论 0 条

数组实现的Circle队列

#include<iostream> #include<cstdlib> #define MAX 100 using namespace std; typedef struct Queue { int Array[MAX]; int Front; int Rear; } *CircleQueue; CircleQueue Create(void); //创建一个队列 int IsEmpty(CircleQueue Q); //是否为空 int IsFull(CircleQueue Q); //是否已满 void EnQueue(Circle...
c++基础 | 2017-04-27 19:19 | 阅读 1015 次 | 评论 0 条

数组实现的MyQueue顺序队列

#include<iostream> #include<cstdlib> using namespace std; typedef struct Queue { int *Array; int Front,Rear; }*MyQueue; MyQueue Create(void); //创建一个队列 int IsEmpty(MyQueue Q); //检验是否为空栈 void DeQueue(MyQueue Q); //出队 void EnQueue(int X,MyQueue Q); //入队 void GetQueueFront(MyQ...
c++基础 | 2017-04-24 18:49 | 阅读 1132 次 | 评论 0 条

链表实现的MyQueue顺序队列

#include<iostream> #include<cstdlib> #define MAXSIZE 100 using namespace std; typedef struct Node { struct Node *Next; //队列元素的节点连接 int Element; }MyNode; typedef struct Queue { Node *Front; //Front和Rear连接着各自应有的节点 Node *Rear; int Items; //队列元素的个数,用于判断是否空队列,...
c++基础 | 2017-04-23 09:44 | 阅读 1101 次 | 评论 0 条

C/C++栈的链表实现

#include<iostream> #include<cstdlib> #define LEN sizeof(struct Node) using namespace std; struct Node; typedef struct Node *PtrToNode; typedef PtrToNode Position; struct Node //节点 { int Element ; struct Node *Next; }; int IsEmpty(Position Top); //检验空栈 void GetStackTo...
c++基础 | 2017-04-22 11:03 | 阅读 1324 次 | 评论 1 条

C/C++栈的数组实现

#include<iostream> #include<cstdlib> using namespace std; int *Array=NULL; int Count=0; void Initial(int Size); //初始化栈 void Push(int Element); //入栈 int GetStackLenth(void);//获取栈的个数 void GetTopOfStack(void);//栈顶元素 void Pop(void); //出栈 void Traverse(void); //遍历 voi...
c++基础 | 2017-04-22 10:54 | 阅读 1042 次 | 评论 0 条

DoubleWayList 双向循环链表

//我发现,插入操作时,总是先把New与已有节点连接 #include<iostream> #include<cstdlib> #define LEN sizeof(struct Node) using namespace std; typedef struct Node *PtrToNode; typedef PtrToNode Position; struct Node { int Element; struct Node *Last; struct Node *Next; }; Position Create(void); ...
c++基础 | 2017-04-20 20:26 | 阅读 1238 次 | 评论 0 条

SingleCycleList单向循环链表

#include<iostream> #include<cstdlib> using namespace std; #define LEN sizeof(struct Node) #define N 6 typedef struct Node *PtrToNode; typedef PtrToNode Position; struct Node { int Element; struct Node *Next; }; Position Create(void); //创建一个循环链表 void Taverse(Position Hea...
c++基础 | 2017-04-17 12:55 | 阅读 1330 次 | 评论 0 条

SingleList单链表

#include<stdio.h> #include<stdlib.h> #include<malloc.h> struct Node; typedef struct Node *PtrToNode; //typedef PtrToNode List; typedef PtrToNode Position; struct Node { int Element; //元素 //int Subscrit; //下标 struct Node *Next; }; Position Create(void); //创建链表 void ...
C语言 | 2017-04-11 17:29 | 阅读 2643 次 | 评论 0 条

模拟drunkard的随机漫步

#include<iostream> #include<cmath> #include<cstdlib> using namespace std; const int SHIP=1; const int BAR=2; const int WATER=3; int drunkard(void) { int x=-10; //酒者一开始在酒馆 int y=0; //酒者一开始在跳板中央 int step=0; //初始化酒者的步数 while(abs(x)<=10&amp;&amp;abs(y)<=2) { switch(rand()%...
c++基础 | 2017-04-08 14:27 | 阅读 1007 次 | 评论 0 条

函数重载

#include<iostream> using namespace std; //名字相同,但是参数列表不能相同 //跟缺省值一起结合用,可能会出现报错/冲突 //返回值不能作为函数重载的条件 void fun(int a,double b) { cout <<"1: "<<a <<' ' <<b <<endl; } void fun(int a,float b) { cout <<"2: "<<a <<' ' <<b <<endl; } void fun(int a,char c=...
c++基础 | 2017-04-04 15:46 | 阅读 847 次 | 评论 0 条

函数缺省值与相对C语言增强版的for循环语句

#include<iostream> using namespace std; void fun(int a,char c,float d=1.233) //带缺省值的函数,从右到左,必须要连续赋予缺省值 { cout <<a <<' ' <<c <<' ' <<d <<endl; for(int i=0;i<10;i++) //可在循环内声明初始变量 { cout <<i <<' ' ; } //cout <<i; //这样是不对的,因为上面for循环里面的i只作用于循环内而非整个函数 cout <<endl...
c++基础 | 2017-04-04 14:26 | 阅读 1096 次 | 评论 0 条

引用的一个小应用

#include<iostream> using namespace std; void Swap(int &amp;Num1,int &amp;Num2) //引用做参数 { int Temp=Num1; Num1=Num2; Num2=Temp; cout <<Num1 <<' ' <<Num2 <<endl; } void Swap1(int a,int b) //普通参数, { int Temp=a; a=b; b=Temp; cout <<a <<' ' <<b <<endl; } void ...
c++基础 | 2017-04-04 13:29 | 阅读 830 次 | 评论 0 条

野指针的避免

初始化时置 NULL 指针变量一定要初始化为NULL,因为任何指针变量(除了static修饰的指针变量)刚被创建时不会自动成为NULL指针,它的缺省值是随机的。 释放时置 NULL 当指针p指向的内存空间释放时,没有设置指针p的值为NULL。delete和free只是把内存空间释放了,但是并没有将指针p的值赋为NULL。通常判断一个指针是否合法,都是使用if语句测试该指针是否为NULL。
C语言 | 2017-04-02 22:46 | 阅读 879 次 | 评论 0 条

C语言允许函数的返回值是一个指针(地址),这样的函数称为指针函数

#include <stdio.h> #include <string.h> char *strlong(char *str1, char *str2){ if(strlen(str1) >= strlen(str2)){ return str1; }else{ return str2; } } int main(){ char str1[30], str2[30], *s...
C语言 | 2017-04-02 11:34 | 阅读 1196 次 | 评论 0 条