您的位置:首页 > 其它

常用算法总结

2012-05-18 10:30 417 查看
1.收集

1).问题描述:将整数789转换为987

算法源代码:



2.线性表

3.链表

4.队列和栈

5.树与.二叉树

6.查找

7.排序

图的拓扑排序

在程序当前目录下建立graph.txt文件,写入下面内容,VC++ 6.0下程序运行通过。

v1 v2

v1 v3

v1 v4

v2 v4

v2 v5

v3 v6

v4 v3

v4 v6

v4 v7

v5 v4

v5 v7

v7 v6

[cpp] view
plaincopy

/*

# Graph Topsort Algorithm

# Copyright (C) 2010-**** Long Cheng <fantasywindy@gmail.com>

# @version 1.1.1-20100720

*/

#include <iostream>

#include <string>

#include <queue>

#include <fstream>

#include <sstream>

using std::cout;

using std::cin;

using std::endl;

using std::string;

using std::queue;

using std::istringstream;

using std::getline;

using std::ifstream;

#define MAX 100 //定义图的最大顶点数量为了100

typedef struct Edge //定义图的边

{

int weight; //边的权值,此程序中没有用上

int next; //边的结束顶点,如边v1->v2的话,那么next就是顶点v2的编号

struct Edge *nextEdge; //下一个邻边

} Edge;

typedef struct Node

{

Edge *firstEdge; //从该顶点出发的第一条边

int no; //顶点的编号(程序中的内部编号)

int degree; //顶点的入度

string val; //顶点的标签,如v1顶点显示为字符串"v1"

}Node;

typedef Node NodeList[MAX];

typedef struct Graph //定义图的数据结构

{

int n; //图的顶点数

int e; //图的边数(此程序中没有用上)

NodeList nodes; //图的顶点数组

}Graph;

int SearchVertex(Graph *g, string str) //在图中寻找标记为"XXX"的顶点是否已经出现过

{

for(int i=0; i<g->n; ++i)

{

if( str == g->nodes[i].val )

{

return i;

}

}

return -1;

}

void Topsort(Graph *g)

{

queue<int> q; //初始化队列

for(int i=0; i<g->n; ++i) //开始时将入度为0的顶点先入栈

{

if( g->nodes[i].degree == 0 )

{

q.push(i);

}

}

int counter=0; //出队列的顶点个数

while( q.empty() == false )

{

int k=q.front(); //取队列首元素的编号后将其出队列

q.pop();

counter++; //出队列的顶点个数加1

cout<<g->nodes[k].val<<endl;

Edge *p=g->nodes[k].firstEdge; //遍历出队列的顶点的边,将所有与其相连的顶点的入度减1,如果某顶点的入度恰好变为0,则将其入队列

while( p != NULL )

{

if( --(g->nodes[p->next].degree) == 0 )

{

q.push(p->next);

}

p=p->nextEdge;

}

}

if( counter != g->n ) //如果最终出队列的顶点个数不等于图的顶点数,那么存在环。

{

cout<<"There is a circle!"<<endl;

}

}

int main()

{

Graph *g=new Graph;

string v1;

string v2;

int nodecount=0;

int edgecount=0;

cout<<"please input the edge of the graph. ( eg: a b )"<<endl;

ifstream file("graph.txt");

string line;

while(getline(file,line)) //输入所有的边

{

istringstream istr(line);

istr>>v1>>v2;

int v1_no;

int v2_no;

if( ( v1_no=SearchVertex(g, v1) ) == -1 ) //判断边的两个顶点是否之前已经出现过,若未出现过,则初始化此顶点

{

g->nodes[nodecount].val=v1;

g->nodes[nodecount].no=nodecount;

g->nodes[nodecount].firstEdge=NULL;

g->nodes[nodecount].degree=0;

v1_no=nodecount;

++nodecount;

g->n=nodecount;

}

if( ( v2_no=SearchVertex(g, v2) ) == -1 )

{

g->nodes[nodecount].val=v2;

g->nodes[nodecount].no=nodecount;

g->nodes[nodecount].firstEdge=NULL;

g->nodes[nodecount].degree=0;

v2_no=nodecount;

++nodecount;

g->n=nodecount;

}

g->nodes[v2_no].degree++;

Edge *e=new Edge; //将边插入对应的顶点的边链表

e->next=v2_no;

e->nextEdge=NULL;

Edge *p=g->nodes[v1_no].firstEdge;

Edge *q=NULL;

while( p != NULL )

{

q=p;

p=p->nextEdge;

}

if( q == NULL )

{

g->nodes[v1_no].firstEdge=e;

}

else

{

q->nextEdge=e;

}

}

/*

for( int i=0; i<g->n; ++i)

{

cout<<g->nodes[i].no<<'/t'<<g->nodes[i].val<<endl;

}

*/

Topsort(g);

for(int i=0; i<g->n; ++i) //释放边new出来的堆空间

{

Edge *pre, *cur;

cur=g->nodes[i].firstEdge;

pre=NULL;

while( cur != NULL )

{

pre=cur;

cur=cur->nextEdge;

delete pre;

}

}

delete g; //释放图new 出来的空间

return 0;

}

8.文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: