Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct AdjListNode
- {
- int dest;
- int valoare;
- struct AdjListNode* next;
- }Node_T;
- typedef struct AdjList
- {
- Node_T *head;
- }List_T;
- typedef struct Graph
- {
- int V;
- List_T* array;
- }Graph_T;
- Node_T* newAdjListNode(int dest,int valoare)
- {
- Node_T* newNode = (Node_T*) malloc(sizeof(Node_T));
- newNode->dest = dest;
- newNode->next = NULL;
- newNode->valoare = valoare;
- return newNode;
- }
- Graph_T* createGraph(int V)
- {
- Graph_T* graph = (Graph_T*) malloc(sizeof(Graph_T));
- graph->V = V;
- graph->array = (List_T*) malloc(V * sizeof(List_T));
- int i;
- for (i = 0; i < V; ++i)
- graph->array[i].head = NULL;
- return graph;
- }
- void addEdge(Graph_T* graph, int src, int dest,int valoare)
- {
- Node_T* newNode = newAdjListNode(dest,valoare);
- newNode->next = graph->array[src].head;
- graph->array[src].head = newNode;
- newNode = newAdjListNode(src,valoare);
- newNode->next = graph->array[dest].head;
- graph->array[dest].head = newNode;
- }
- void printGraph(Graph_T* graph)
- {
- int v;
- for (v = 0; v < graph->V; ++v)
- {
- Node_T* pCrawl = graph->array[v].head;
- printf("\n Adjacency list of vertex %d\n head ", v);
- while (pCrawl)
- {
- printf("-> %d%s%d", pCrawl->dest," ",pCrawl->valoare);
- pCrawl = pCrawl->next;
- }
- printf("\n");
- }
- }
- Graph_T* createGraphValue(){
- int length;
- printf("%s","nr de noduri");
- scanf("%d",&length);
- int V = length;
- Graph_T* graph = createGraph(V);
- int i,n;
- printf("%s","nr de muchii");
- scanf("%d",&n);
- int x,y,val;
- for(i=1;i<=n;i++){
- printf("%s","introduceti nodul de pornire si in care ajunge");
- scanf("%d%d%d",&x,&y,&val);
- addEdge(graph, x, y,val);
- }
- return graph;
- }
- int main()
- {
- Graph_T* graph = createGraphValue();
- printGraph(graph);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement