
数据结构课堂练习
TooSIMple_
十倍努力做突出一个!
展开
-
层次遍历求二叉树的高度(非递归)
#include #include #define MAXQSIZE 100typedef struct BTNode{ char data; struct BTNode *lchild,*rchild;}BTNode,*BiTree;typedef struct{ BiTree *base; int front; int rear;}SqQueue;void Init原创 2016-10-24 22:27:36 · 3956 阅读 · 0 评论 -
邻接表存储图
#include #include #define MAX_VERTEX_NUM 20typedef struct ArcNode{ int Adjvex;//存储所指顶点的位置 struct ArcNode *nextarc;//指向下一结点}ArcNode;typedef struct VNode{ char data;//存储结点 ArcNode *firstarc;//原创 2016-11-02 20:24:33 · 283 阅读 · 0 评论 -
关于邻接表的拓扑排序
算法思想如下:利用邻接表存储图,先设一个辅助数组indegree[]来计算所有节点的入度;首先把入度为零的节点入栈,当栈不为空时,把栈中的元素出栈,然后删除输出元素为弧尾的所有弧,并判断相应节点的入度是否为零,为零,则压入栈中。重复执行,直到栈空。 #include <stdio.h>#include <stdlib.h>#define MAX 30i...原创 2016-11-17 20:54:51 · 6104 阅读 · 5 评论 -
关于邻接矩阵的拓扑排序
#include <stdio.h>#include <stdlib.h>#define MAX 20int indegree[MAX];//用来计算所有节点的入度之和typedef int AdjMatrix[MAX][MAX];typedef struct{ int vexnum,arcnum; AdjMatrix arcs...原创 2016-11-17 20:59:40 · 4780 阅读 · 2 评论 -
克鲁斯卡尔算法
设N=(V,{E})是连通网 1)令最小生成树的初始状态为只有n个顶点而无边的非连通图T=(V, {}),图中每个顶点自成一个连通分量; 2)在E中选择代价最小的边,若该边依附的顶点落在T中不同的连通分量上,则将此边加入到T中,否则舍去此边而选择下一条代价最小的边; 3)反复执行第2)步,直至T中所有顶点都在同一连通分量原创 2016-11-15 18:18:21 · 648 阅读 · 0 评论 -
迪杰斯特拉算法
#include #include #define MAX 20#define INFINITY 65538typedef int AdjMatrix[MAX][MAX];//存储图 typedef int PathMatrix[MAX][MAX];//记录v0到各点的最短路径 typedef int ShortPath[MAX];//辅助数组,记录最短距离 typedef str原创 2016-12-04 15:42:33 · 379 阅读 · 0 评论 -
折半查找
#include void BinarySearch(int a[], int left, int right, int num){ if(left > right){ printf("查找失败!\n"); return ; } int mid = (left + right) / 2; if(a[mid] == num){ printf("要查找的数字的位置为原创 2016-12-04 15:43:22 · 312 阅读 · 0 评论 -
插入排序
1.直接插入排序每次选取待插入元素与之前排好序的元素进行比较,比其大,则插入,否则移动元素。#include #define MAX 200typedef struct{ int r[MAX]; int length;}SqList;void InitList(SqList &L){ int n; printf("please input the num of co原创 2016-12-06 10:54:45 · 307 阅读 · 0 评论 -
快速排序的非递归形式
快速排序的非递归形式,用栈来存储分治后的起止下标即可#include #include #include #include using namespace std;int a[100];int partition(int a[], int i, int j){ int temp = a[i]; while(i < j){ while(i = temp) j原创 2016-12-15 15:11:37 · 295 阅读 · 0 评论 -
希尔排序
该方法的基本思想是:先将整个待排元素序列分割成若干个子序列(由相隔某个“增量”的元素组成的)分别进行直接插入排序,然后依次缩减增量再进行排序,待整个序列中的元素基本有序(增量足够小)时,再对全体元素进行一次直接插入排序。因为直接插入排序在元素基本有序的情况下(接近最好情况),效率是很高的,因此希尔排序在时间效率上比前两种方法有较大提高。#include #define M原创 2016-12-30 17:07:55 · 248 阅读 · 0 评论 -
邻接矩阵存储图
#include #include #define MAX_VERTEX_NUM 20typedef int AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];typedef struct{ AdjMatrix arcs;//定义一个指向二维数组的变量 int vexnum,arcnum;//图当前的顶点数和边数 char vexs[MAX_VER原创 2016-11-02 19:27:23 · 529 阅读 · 0 评论 -
邻接表的深度优先遍历以及广度优先遍历
#include #include #define MAX 20int visited[20];typedef struct ArcNode{ int adjvex; struct ArcNode *nextarc;}ArcNode,*LinkNode;typedef struct{ char data; ArcNode *firstarc;}VNode,AdjList[原创 2016-11-13 17:25:31 · 4908 阅读 · 0 评论 -
邻接矩阵的深度优先遍历(递归以及非递归),广度优先遍历
#include #include #define MAX_VERTEX_NUM 20typedef int AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];typedef struct{ int vexnum,arcnum; char vexs[MAX_VERTEX_NUM]; AdjMatrix arc;}MGraph;typedef s原创 2016-11-13 17:24:34 · 5166 阅读 · 0 评论 -
判断一棵树是否为完全二叉树
#include #include #define MAXQSIZE 100typedef struct BTNode{ char data; struct BTNode *lchild,*rchild;}BTNode,*BiTree;typedef struct{ BiTree *base; int front; int rear;}SqQueue;void Init原创 2016-10-24 22:30:09 · 1152 阅读 · 0 评论 -
三元组存储求矩阵和
#include #include #define MAXSIZE 12500int a[100][100];typedef struct{ int i,j; int e;}Triple;typedef struct{ Triple data[MAXSIZE]; int mu,nu,tu;}TSMatrix;void InitArray(int m, int n){原创 2016-10-24 22:32:00 · 583 阅读 · 0 评论 -
矩阵的快速转置
#include #include #define MAXSIZE 12500int a[1000][1000],num[12500],cpot[12500];typedef struct{ int i,j,e;}Triple;typedef struct{ Triple data[MAXSIZE]; int mu,nu,tu;//row,col,value;}TSMatr原创 2016-10-24 22:33:28 · 1491 阅读 · 0 评论 -
模式匹配_KMP
#include #include int nextval[1000];typedef struct{ char *base; int length;}HString;void InitString(HString &H){ int n; printf("please input the n:"); scanf("%d",&n); H.base = (char原创 2016-10-24 22:35:30 · 257 阅读 · 0 评论 -
两栈共用同一存储空间
/*Á½Õ»¹²ÓÃͬһ´æ´¢¿Õ¼ä*/#include #include #define MAX 100typedef struct { int base[MAX];//²»ÓÃÔÙ¶¯Ì¬ÉêÇ룬ÒÑÓÐ×ã¹»´óÈ·¶¨¿Õ¼ä int top[2];//É趨Á½¸ötop±êÖ¾ }DuSqStack;void InitStack(DuSqStack原创 2016-10-25 18:04:32 · 859 阅读 · 0 评论 -
舞会配对
#include #include #define MAXQSIZE 100typedef struct { char *base; int front; int rear;}SqQueue;void InitQueue(SqQueue &Q){ char c; int n; Q.base = (char *)malloc(MAXQSIZE * sizeof(char原创 2016-10-25 18:07:58 · 1518 阅读 · 0 评论 -
括号匹配
#include #include #define MAX 100typedef struct{ char *base; int top;}SqStack; void InitStack (SqStack &s){ s.base = (char *)malloc(MAX * sizeof(char)); if(!s.base ) return ; s.top = 0原创 2016-10-05 20:37:14 · 263 阅读 · 0 评论 -
数制转换
#include #include #define MAX 100typedef struct{ int top; int *base; }SqStack;void InitStack(SqStack &s){ s.base = (int *)malloc(MAX * sizeof(int)); if(!s.base) return ; s.top = 0; }原创 2016-10-05 20:38:29 · 278 阅读 · 0 评论 -
迷宫求解
#include #include #define MAX 100typedef struct { int x; int y; int d;}ElemType;typedef struct { int x; int y;}Mark;typedef struct { ElemType *base; int top; int stacksize;}SqStack;原创 2016-10-05 20:39:31 · 372 阅读 · 0 评论 -
普里姆算法
普里姆算法的基本思想如下:假设N={V,{E}}是连通网,TE是N上最小生成树中边的集合。从U={u}开始,TE={}开始,重复执行下述操作:在所有的u属于U,v属于V-U的边(u,v)的边中找到权值最小的一条边,并且并入TE,同时u并入U,直到U=V;先设一个辅助数组closedge[MAX],初始时先把第一个结点存入closedge[i].adjvex,令closedge数组中adjv原创 2016-11-13 17:20:09 · 1108 阅读 · 0 评论 -
POJ 2299 Ultra-QuickSort (归并排序)
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted i原创 2017-01-20 11:19:13 · 252 阅读 · 0 评论