Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct AdjListNode
  5. {
  6. int dest;
  7. int valoare;
  8. struct AdjListNode* next;
  9. }Node_T;
  10.  
  11. typedef struct AdjList
  12. {
  13. Node_T *head;
  14. }List_T;
  15.  
  16. typedef struct Graph
  17. {
  18. int V;
  19. List_T* array;
  20. }Graph_T;
  21.  
  22. Node_T* newAdjListNode(int dest,int valoare)
  23. {
  24. Node_T* newNode = (Node_T*) malloc(sizeof(Node_T));
  25. newNode->dest = dest;
  26. newNode->next = NULL;
  27. newNode->valoare = valoare;
  28. return newNode;
  29. }
  30.  
  31. Graph_T* createGraph(int V)
  32. {
  33. Graph_T* graph = (Graph_T*) malloc(sizeof(Graph_T));
  34. graph->V = V;
  35.  
  36. graph->array = (List_T*) malloc(V * sizeof(List_T));
  37.  
  38. int i;
  39. for (i = 0; i < V; ++i)
  40. graph->array[i].head = NULL;
  41. return graph;
  42. }
  43.  
  44. void addEdge(Graph_T* graph, int src, int dest,int valoare)
  45. {
  46. Node_T* newNode = newAdjListNode(dest,valoare);
  47. newNode->next = graph->array[src].head;
  48. graph->array[src].head = newNode;
  49.  
  50. newNode = newAdjListNode(src,valoare);
  51. newNode->next = graph->array[dest].head;
  52. graph->array[dest].head = newNode;
  53. }
  54. void printGraph(Graph_T* graph)
  55. {
  56. int v;
  57. for (v = 0; v < graph->V; ++v)
  58. {
  59. Node_T* pCrawl = graph->array[v].head;
  60. printf("\n Adjacency list of vertex %d\n head ", v);
  61. while (pCrawl)
  62. {
  63. printf("-> %d%s%d", pCrawl->dest," ",pCrawl->valoare);
  64. pCrawl = pCrawl->next;
  65. }
  66. printf("\n");
  67. }
  68. }
  69.  
  70. Graph_T* createGraphValue(){
  71. int length;
  72. printf("%s","nr de noduri");
  73. scanf("%d",&length);
  74. int V = length;
  75. Graph_T* graph = createGraph(V);
  76. int i,n;
  77. printf("%s","nr de muchii");
  78. scanf("%d",&n);
  79. int x,y,val;
  80. for(i=1;i<=n;i++){
  81. printf("%s","introduceti nodul de pornire si in care ajunge");
  82. scanf("%d%d%d",&x,&y,&val);
  83. addEdge(graph, x, y,val);
  84.  
  85. }
  86. return graph;
  87. }
  88.  
  89. int main()
  90. {
  91. Graph_T* graph = createGraphValue();
  92. printGraph(graph);
  93.  
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement