Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 2

#include <stdio.

h>
#include <stdlib.h>
// 13 practical
typedef struct node {
int data;
struct node *next;
} Node;

typedef struct graph {


Node *head;
} Graph;

Graph *createGraph() {
Graph *graph = (Graph *)malloc(sizeof(Graph));
graph->head = NULL;
return graph;
}

void addEdge(Graph *graph, int src, int dest) {


Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = dest;
newNode->next = graph->head;
graph->head = newNode;
}

void printGraph(Graph *graph) {


Node *temp = graph->head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("\n");
}

void generateSDT(Graph *graph) {


Node *temp = graph->head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
Graph *graph = createGraph();
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 3);

printf("The graph is: \n");


printGraph(graph);
printf("The SDT of the graph is: \n");
generateSDT(graph);

return 0;
}

// The graph is:


// 0 -> 1
// 0 -> 2
// 1 -> 3
// 2 -> 3

// The SDT of the graph is:


// 0 1 2 3

You might also like