数据结构与算法
Hustudent20080101
MFC Qt 瑞萨 ARM Linux
展开
-
链表打印
#include "stdafx.h"#include typedef struct _Node{ _Node(char *varname,_Node& prev,int isNexttoSb=1) { memset(name,0,256); strcpy(name,varname); if(isNexttoSb) prev.pNext=this; pNext=NU原创 2012-05-02 18:14:51 · 664 阅读 · 0 评论 -
链表,按序号检索
// ListTable.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include typedef struct _Node{ _Node(char *varname,_Node& prev,int isNexttoSb=1) { memset(name,0,原创 2012-05-02 18:36:12 · 679 阅读 · 0 评论 -
链表:插入,删除
// ListTable.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include typedef struct _Node{ _Node(char *varname,_Node& prev,int isNexttoSb=1) { memset(name,0,原创 2012-05-02 18:58:11 · 453 阅读 · 0 评论 -
链表:按序号插入、删除
// ListTable.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include typedef struct _Node{ _Node(char *varname,_Node& prev,int isNexttoSb=1) { memset(name,0,原创 2012-05-02 19:15:25 · 636 阅读 · 0 评论 -
静态链表:显示,插入
// staticList.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include typedef struct _Node{ void setName(char* p) { memset(name,0,256); strcpy(name,p); }原创 2012-05-02 21:35:16 · 542 阅读 · 0 评论 -
静态链表:InsertAt(Node *nodeFirst,Node& node,int destIndex)
// staticList.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include typedef struct _Node{ void setName(char* p) { memset(name,0,256); strcpy(name,p); }原创 2012-05-02 21:54:38 · 799 阅读 · 0 评论 -
链表:多项式加和
// PolyList.cpp : Defines the entry point for the console application.//#include "stdafx.h"struct Item{ Item(int num,int power) { this->num=num; this->power=power; pNext=NULL; } int nu原创 2012-05-03 09:13:46 · 732 阅读 · 0 评论 -
链表:多项式相乘
// PolyList.cpp : Defines the entry point for the console application.//#include "stdio.h"struct Item{ Item(int num,int power) { this->num=num; this->power=power; pNext=NULL; } Item(It原创 2012-05-03 10:25:34 · 777 阅读 · 0 评论 -
链表:复杂链表的拷贝
// CopyComplexList.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include struct NameItem{ NameItem(char * name) { strcpy(this->name,name); pOther=NULL;原创 2012-05-03 22:18:29 · 755 阅读 · 0 评论 -
一元多项式的计算
#include#includetypedefstruct Polynomial{ float coef; int expn; struct Polynomial *next;}*Polyn,Polynomial; //Polyn为结点指针类型voidInsert(Polyn p,Polyn h){ if(p->coef==0) free(p);转载 2012-05-05 15:22:06 · 827 阅读 · 0 评论 -
反转一个链表。循环算法
//1、反转一个链表。循环算法。 #include #include struct List{ List(char * name) { strcpy(this->name,name); pnext=NULL; } List* InsertSbAfterMe(List* Sb) { Sb->pnext=this->pnext; this->pnext=Sb;原创 2012-05-05 16:17:01 · 674 阅读 · 0 评论 -
反转一个链表。递归算法
//2、反转一个链表。递归算法。 #include #include struct List{ List(char * name) { strcpy(this->name,name); pnext=NULL; } List* InsertSbAfterMe(List* Sb) { Sb->pnext=this->pnext; this->pnext=S原创 2012-05-05 17:18:03 · 676 阅读 · 0 评论 -
创建二叉树,并对其进行 先序、中序、后序遍历
//创建二叉树,并对其进行 先序、中序、后序遍历#include //#include typedef struct BiTNode { BiTNode(char* name) { strcpy(this->name,name); lchild=NULL; rchild=NULL; } BiTNode* InsertSbOnMyLeft(BiTNode* Sb) {原创 2012-05-05 19:36:14 · 2670 阅读 · 0 评论 -
二叉树的层次遍历和图的广度优先搜索的相同点和不同点
相同点:两者都是从一个结点b出发一次访问其相邻结点,对于树来说,就是它的左右孩子结点,而图则是连通的结点。不同点:对图来说,一个顶点的相邻结点有多个,而二叉树只有两个。另外,广度遍历图的时候,需要加上一个Visited[MAVX]数组,来记录已访问的结点,避免重复访问同个结点。比如:(a1,a2) (a1,a3)(a2,a3)访问a1后,广度遍历就会访问a2和a3,访问a2后,又会访问a3,这样转载 2012-05-05 19:45:05 · 1641 阅读 · 0 评论 -
二叉树与图的深度优先和广度优先遍历
[转】二叉树与图的深度优先和广度优先遍历. (2011-07-20 02:49:35)转载▼标签: 杂谈 图的深度优先搜查法是树的先根遍历的普及,它的大约信念是:从图G的某个顶峰v0动身,拜会v0,然后抉择一个与v0邻接且没被拜会过的顶峰vi访 问,再从vi动身抉择一个与vi邻接且未被拜会的顶峰vj举行拜会,顺次继转载 2012-05-05 19:48:29 · 990 阅读 · 0 评论 -
CopyComplexList2
// CopyComplexList.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include struct NameItem{ NameItem(char * name) { strcpy(this->name,name); pOther=NULL;原创 2012-05-06 20:10:11 · 462 阅读 · 0 评论 -
InsertSort
// InsertSort.cpp : Defines the entry point for the console application.//#include "stdafx.h"int * InsertSort(int Arry[],int n){ int pos2Insert; int tmpKard; for(int i=1;i<n;i++) { tmpKar原创 2012-05-06 21:26:51 · 444 阅读 · 0 评论 -
“快速排序法”
// QuickSort.cpp : Defines the entry point for the console application.//#include "stdafx.h"//“快速排序法”使用的是递归原理,//下面我结合一个例子来说明“快速排序法”的原理。//首先给出一个数组{53,12,98,63,18,72,80,46, 32,21},//先找到第一个数--53,转载 2012-05-07 09:07:10 · 562 阅读 · 0 评论 -
quicksortNew;partitionNew
void quicksortNew(int n[], int left,int right){int dp;int step=right-left;if(step<=0)return;else if(step==1){ if(n[left]>n[right]) swap(n[left],n[right]); return;}else{ /* 这就是下面原创 2012-05-07 10:25:23 · 438 阅读 · 0 评论 -
字符串的全排列
//问题:给定字符串S,生成该字符串的全排列。//方法1:依次从字符串中取出一个字符作为最终排列//的第一个字符,对剩余字符组成的字符串生成全排列,//最终结果为取出的字符和剩余子串全排列的组合。//?#include #include using namespace std; void permute1(string prefix, string str){ if(s转载 2012-05-07 20:06:59 · 485 阅读 · 0 评论 -
字符串全排列【方法2:利用交换的思想】
优点:该方法易于理解,但无法移除重复的排列,如:s="ABA",会生成两个“AAB”。方法2:利用交换的思想,具体见实例,但该方法不如方法1容易理解。//方法2:利用交换的思想,具体见实例,但该方法不如方法1容易理解。////?#include #include #include using namespace std; void swap(char* x, c转载 2012-05-07 20:31:59 · 516 阅读 · 0 评论 -
广度优先遍历二叉树
//3、广度优先遍历二叉树。 #include #include #include //int i =0;typedef struct treenode /*树节点结构体定义*/ { treenode(char* name) { strcpy(this->data,name);原创 2012-05-05 21:21:39 · 784 阅读 · 0 评论 -
深度优先遍历二叉树
//3、深度优先遍历二叉树。 #include #include #include typedef struct treenode /*树节点结构体定义*/ { treenode(char* name) { strcpy(this->data,name); lchild=NULL原创 2012-05-06 10:21:29 · 711 阅读 · 0 评论 -
输出一个字符串所有排列。注意有重复字符
////4、输出一个字符串所有排列。注意有重复字符。#include#includevoid sortstr(char v[]);char p[256];void perm(char s[], int i, int n){ int j; // char temp; for(j=0;j<n;++j) { if(j!=0 && s[j]==s[j-1]); else if转载 2012-05-06 11:00:56 · 1026 阅读 · 0 评论 -
快速排序,选择排序
快速排序是对冒泡排序的一种改进。它的基本思想是:通过一躺排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按次方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。最坏情况的时间复杂度为O(n2),最好情况时间复杂度为O(nlog2n)。 选择排序是一种简单直观的排序算法。它的工作原理如下。首先在未排序序列中找转载 2012-05-06 20:33:11 · 533 阅读 · 0 评论 -
判断一个链表是否有循环
// ListIsLoop.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include //6、判断一个链表是否有循环。struct List{ List(char* name) { strcpy(this->name,name); next=NULL;转载 2012-05-08 09:13:23 · 707 阅读 · 0 评论 -
SortList
// sortList.cpp : Defines the entry point for the console application.//#include "stdafx.h"typedef struct List{ int data; List* pNext; List(int data) { this->data=data; pNext=NULL;原创 2012-05-09 12:29:31 · 490 阅读 · 0 评论 -
StaticQuene//int getLength()
// StaticQuene.cpp : Defines the entry point for the console application.//#include "stdafx.h"struct StaticQuene{private: char buff[10]; int front; int rear;public: bool isFull; bool isEmp原创 2012-05-09 21:31:18 · 554 阅读 · 0 评论 -
void Traverse()//while( walker!=rear) { printf("%c",buff[walker]); walker=(walker+1)%10;
// StaticQuene.cpp : Defines the entry point for the console application.//#include "stdafx.h"struct StaticQuene{private: char buff[10]; int front; int rear;public: bool isFull() { retur原创 2012-05-10 09:39:41 · 684 阅读 · 0 评论 -
InsertNodeAtPos(pHead,new List(7),7);//pos代表pHead后面第pos个节点
// sortList.cpp : Defines the entry point for the console application.//#include "stdafx.h"typedef struct List{ int data; List* pNext; List(int data) { this->data=data; pNext=NULL;原创 2012-05-09 14:13:37 · 1060 阅读 · 0 评论 -
汉诺塔算法
#include int main(){ void hanoi(int n,char one,char two,char three); // 对hanoi函数的声明 int m; printf("input the number of diskes:"); scanf("%d",&m); printf("The step to move %d diske转载 2012-05-10 12:54:56 · 2546 阅读 · 0 评论 -
Sort(PList pHead)//选择排序法
// SelectSortList.cpp : Defines the entry point for the console application.//#include "stdafx.h"typedef struct List{ int data; List* pNext; List(int data) { this->data=data; pNext=NULL;原创 2012-05-09 13:10:31 · 501 阅读 · 0 评论 -
单链表逆序
2、 单链表逆序 第二个题目是很经典的“单链表逆序”问题。很多公司的面试题库中都有这道题,有的公司明确题目要求不能使用额外的节点存储空间,有的没有明确说明,但是如果面试者使用了额外的节点存储空间做中转,会得到一个比较低的分数。如何在不使用额外存储节点的情况下使一个单链表的所有节点逆序?我们先用迭代循环的思想来分析这个问题,链表的初始状态如图(1)所示:图(1转载 2012-05-27 19:20:16 · 77003 阅读 · 34 评论 -
StaticQuene//isFull(){return ((rear+1)%10)==front;
// StaticQuene.cpp : Defines the entry point for the console application.//#include "stdafx.h"struct StaticQuene{private: char buff[10]; int front; int rear;public: bool isFull() { retur原创 2012-05-10 09:03:54 · 765 阅读 · 0 评论 -
静态循环链表【数组】
// StaticQuene.cpp : Defines the entry point for the console application.//#include "stdafx.h"struct StaticQuene{private: char buff[10]; int front; int rear;public: bool isFull; bool isEmp原创 2012-05-09 20:54:28 · 651 阅读 · 1 评论 -
走迷宫//迷宫地图在数据文件maze.in中
走迷宫已知N*N迷宫地图在数据文件maze.in中,找出一条从源点(sx,sy)到目标点(tx,ty)的通路。#include "stdio.h"#define N 20main( ){ int a[N][N]; /* 可以定义为char类型,这样可存放大地图 */int sx, sy, tx, ty, t, i, j, k, n, x, y;int dx[4], dy[转载 2012-05-11 10:46:47 · 945 阅读 · 0 评论 -
partitionNew//QuickSort3
int partitionNew(int n[],int left,int right){int lo;lo=left;int val=n[left];int hi=right;while(lo!=hi){ while(lo!=hi) { if(n[hi]<val) { printf("%d<%d:n[%d]<n[%d]\n",n[hi],n[lo],hi,lo)原创 2012-05-12 16:36:36 · 448 阅读 · 0 评论 -
汉诺塔算法//printf("%d:%c->%c\n",n,one,three);
// Hanoi.cpp : Defines the entry point for the console application.//#include "stdafx.h"//#include int main() { void hanoi(int n,char one,char two,char three); // 对hanoi函数的声明转载 2012-05-10 17:28:09 · 3394 阅读 · 0 评论 -
走迷宫
#include#include#include "conio.h"#define M 15#define N 15struct mark //定义迷宫内点的坐标类型{ int x; int y;};struct Element //"恋"栈元素,嘿嘿。。{ int x,y; //x行,y列 int d; //d下一步的方向};type转载 2012-05-10 21:44:22 · 644 阅读 · 0 评论 -
栈:自下而上输出【导入另一个栈再输出】
if(a==end.x && b==end.y && maze[a][b]==0) //如果到了出口 { elem.x=i; elem.y=j; elem.d=d; Push(S1,elem); elem.x=a; elem.y=b;转载 2012-05-11 09:35:03 · 583 阅读 · 0 评论