常用算法总结

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 plain copy
  1. /*  
  2. # Graph Topsort Algorithm   
  3. # Copyright (C) 2010-****  Long Cheng  <fantasywindy@gmail.com>  
  4. # @version 1.1.1-20100720 
  5. */    
  6. #include <iostream>  
  7. #include <string>  
  8. #include <queue>  
  9. #include <fstream>  
  10. #include <sstream>  
  11. using std::cout;  
  12. using std::cin;  
  13. using std::endl;  
  14. using std::string;  
  15. using std::queue;  
  16. using std::istringstream;  
  17. using std::getline;  
  18. using std::ifstream;  
  19. #define MAX 100                //定义图的最大顶点数量为了100  
  20. typedef struct Edge            //定义图的边  
  21. {  
  22.     int weight;                //边的权值,此程序中没有用上  
  23.     int next;                  //边的结束顶点,如边v1->v2的话,那么next就是顶点v2的编号  
  24.     struct Edge *nextEdge;     //下一个邻边  
  25. } Edge;  
  26. typedef struct Node  
  27. {  
  28.     Edge *firstEdge;           //从该顶点出发的第一条边  
  29.     int no;                    //顶点的编号(程序中的内部编号)  
  30.     int degree;                //顶点的入度  
  31.     string val;                //顶点的标签,如v1顶点显示为字符串"v1"  
  32. }Node;  
  33. typedef Node NodeList[MAX];      
  34. typedef struct Graph           //定义图的数据结构  
  35. {  
  36.     int n;                     //图的顶点数  
  37.     int e;                     //图的边数(此程序中没有用上)  
  38.     NodeList nodes;            //图的顶点数组  
  39. }Graph;  
  40. int SearchVertex(Graph *g, string str)       //在图中寻找标记为"XXX"的顶点是否已经出现过  
  41. {  
  42.     for(int i=0; i<g->n; ++i)  
  43.     {  
  44.         if( str == g->nodes[i].val )  
  45.         {  
  46.             return i;  
  47.         }  
  48.     }  
  49.     return -1;  
  50. }  
  51. void Topsort(Graph *g)                        
  52. {  
  53.     queue<int> q;                          //初始化队列  
  54.     for(int i=0; i<g->n; ++i)              //开始时将入度为0的顶点先入栈  
  55.     {  
  56.         if( g->nodes[i].degree == 0 )  
  57.         {  
  58.             q.push(i);  
  59.         }  
  60.     }  
  61.     int counter=0;                         //出队列的顶点个数  
  62.     while( q.empty() == false )              
  63.     {  
  64.         int k=q.front();                   //取队列首元素的编号后将其出队列  
  65.         q.pop();  
  66.         counter++;                         //出队列的顶点个数加1  
  67.         cout<<g->nodes[k].val<<endl;  
  68.         Edge *p=g->nodes[k].firstEdge;     //遍历出队列的顶点的边,将所有与其相连的顶点的入度减1,如果某顶点的入度恰好变为0,则将其入队列  
  69.         while( p != NULL )  
  70.         {     
  71.             if( --(g->nodes[p->next].degree) == 0 )  
  72.             {  
  73.                 q.push(p->next);  
  74.             }  
  75.             p=p->nextEdge;  
  76.         }  
  77.     }  
  78.     if( counter != g->n )                  //如果最终出队列的顶点个数不等于图的顶点数,那么存在环。  
  79.     {  
  80.         cout<<"There is a circle!"<<endl;  
  81.     }  
  82. }  
  83. int main()  
  84. {  
  85.     Graph *g=new Graph;  
  86.     string v1;  
  87.     string v2;  
  88.     int nodecount=0;  
  89.     int edgecount=0;  
  90.     cout<<"please input the edge of the graph. ( eg: a  b )"<<endl;  
  91.     ifstream file("graph.txt");  
  92.     string line;  
  93.     while(getline(file,line))     //输入所有的边      
  94.     {  
  95.         istringstream istr(line);  
  96.         istr>>v1>>v2;  
  97.         int v1_no;  
  98.         int v2_no;  
  99.         if( ( v1_no=SearchVertex(g, v1) ) == -1 )   //判断边的两个顶点是否之前已经出现过,若未出现过,则初始化此顶点  
  100.         {  
  101.             g->nodes[nodecount].val=v1;  
  102.             g->nodes[nodecount].no=nodecount;  
  103.             g->nodes[nodecount].firstEdge=NULL;  
  104.             g->nodes[nodecount].degree=0;  
  105.             v1_no=nodecount;  
  106.             ++nodecount;  
  107.             g->n=nodecount;  
  108.           
  109.         }  
  110.         if( ( v2_no=SearchVertex(g, v2) ) == -1 )  
  111.         {  
  112.             g->nodes[nodecount].val=v2;  
  113.             g->nodes[nodecount].no=nodecount;  
  114.             g->nodes[nodecount].firstEdge=NULL;  
  115.             g->nodes[nodecount].degree=0;  
  116.             v2_no=nodecount;  
  117.             ++nodecount;  
  118.             g->n=nodecount;  
  119.           
  120.         }  
  121.         g->nodes[v2_no].degree++;  
  122.           
  123.         Edge *e=new Edge;         //将边插入对应的顶点的边链表  
  124.         e->next=v2_no;  
  125.         e->nextEdge=NULL;  
  126.         Edge *p=g->nodes[v1_no].firstEdge;  
  127.         Edge *q=NULL;  
  128.         while( p != NULL )  
  129.         {  
  130.             q=p;  
  131.             p=p->nextEdge;  
  132.         }  
  133.         if( q == NULL )  
  134.         {  
  135.             g->nodes[v1_no].firstEdge=e;  
  136.         }  
  137.         else  
  138.         {  
  139.             q->nextEdge=e;  
  140.         }  
  141.     }  
  142.     /* 
  143.     for( int i=0; i<g->n; ++i) 
  144.     { 
  145.         cout<<g->nodes[i].no<<'/t'<<g->nodes[i].val<<endl; 
  146.     } 
  147.     */  
  148.     Topsort(g);  
  149.     for(int i=0; i<g->n; ++i)        //释放边new出来的堆空间  
  150.     {  
  151.         Edge *pre, *cur;  
  152.         cur=g->nodes[i].firstEdge;  
  153.         pre=NULL;  
  154.         while( cur != NULL )  
  155.         {  
  156.             pre=cur;  
  157.             cur=cur->nextEdge;  
  158.             delete pre;  
  159.         }  
  160.     }  
  161.     delete g;                       //释放图new 出来的空间  
  162.     return 0;  
  163. }  

 


8.文件






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值