Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node
- {
- int vertex;
- struct node *next;
- }*g[50];
- int visited[50]={0};
- void insert(int x, int y){
- struct node *p,*q = (struct node *) malloc(sizeof(struct node));
- q->vertex = y; q->next = NULL;
- if(g[x] == NULL) g[x] = q;
- else{
- p = g[x];
- while(p->next) p = p->next;
- p->next = q;
- }
- }
- void DFS(int i)
- {
- struct node *p;
- printf("%d ", i);
- p = g[i];
- visited[i] = 1;
- while(p != NULL){
- i=p->vertex;
- if(!visited[i]) DFS(i);
- p = p->next;
- }
- }
- int main()
- {
- int i, x, y, edge, n;
- printf("\nEnter number of vertices: ");
- scanf("%d", &n);
- for(i=0; i<n; i++) g[i]=NULL;
- printf("\nEnter number of edges: ");
- scanf("%d", &edge);
- for(i=0;i<edge;i++){
- printf("\nEnter an edge(u,v): ");
- scanf("%d %d", &x,&y);
- insert(x, y);
- insert(y, x);
- }
- printf("\nEnter a value: ");
- scanf("%d", &n);
- DFS(n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement