(14)拓扑排序.cpp
上传用户:wxj1219
上传日期:2013-01-31
资源大小:6k
文件大小:2k
源码类别:

数据结构

开发平台:

C/C++

  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. int NodeCount;
  5. char Node[20];
  6. char Graph[20][20];
  7. char RuDu[20];
  8. char Checked[20];
  9. char Locate(char Char)
  10. {
  11. char Temp;
  12. for(Temp=0;Temp<NodeCount;Temp++)
  13. if(Node[Temp]==Char)
  14. break;
  15. return Temp;
  16. }
  17. void main()
  18. {
  19. char Temp1,Temp2;
  20. int Temp3;
  21. char Condition=1;
  22. char min,mintemp,mintemp1;
  23. clrscr();
  24. for(Temp2=0;Temp2<20;Temp2++)
  25. for(Temp1=0;Temp1<20;Temp1++)
  26. Graph[Temp2][Temp1]=0;
  27. for(Temp1=0;Temp1<20;Temp1++)
  28. Checked[Temp1]=0;
  29. cout<<"Input the node count:";
  30. cin>>NodeCount;
  31. cout<<"Input the node:";
  32. for(Temp1=0;Temp1<NodeCount;Temp1++)
  33.   cin>>Node[Temp1];
  34. cout<<"Input the link"<<endl;
  35. Temp1=0;
  36. while(1)
  37. {
  38. cout<<"Input the first node:";
  39. cin>>Temp1;
  40. cout<<"Input the second node:";
  41. cin>>Temp2;
  42. if(Temp1=='$')
  43. break;
  44. Temp1=Locate(Temp1);
  45. Temp2=Locate(Temp2);
  46. Graph[Temp1][Temp2]=1;
  47. }
  48. for(Temp2=0;Temp2<NodeCount;Temp2++)
  49. {
  50. for(Temp1=0;Temp1<NodeCount;Temp1++)
  51. cout<<(int)Graph[Temp2][Temp1]<<" ";
  52. cout<<endl;
  53. }
  54. while(Condition)
  55. {
  56. for(Temp1=0;Temp1<NodeCount;Temp1++)
  57. RuDu[Temp1]=0;
  58. for(Temp2=0;Temp2<NodeCount;Temp2++)
  59. {
  60. Temp3=0;
  61. for(Temp1=0;Temp1<NodeCount;Temp1++)
  62. if(Graph[Temp1][Temp2])
  63. Temp3++;
  64. RuDu[Temp2]=Temp3;
  65. }
  66. Condition=0;
  67. for(Temp1=0;Temp1<NodeCount;Temp1++)
  68. if((!RuDu[Temp1])&&(!Checked[Temp1]))
  69. {
  70. for(Temp2=0;Temp2<NodeCount;Temp2++)
  71. Graph[Temp1][Temp2]=0;
  72. Checked[Temp1]=1;
  73. cout<<Node[Temp1];
  74. Condition=1;
  75. }
  76. }
  77. }