0% found this document useful (0 votes)
9 views3 pages

19

The document contains two C programs: the first implements an undirected graph using an adjacency list, allowing users to input vertices and edges, and then prints the adjacency list. The second program implements a binary search tree with functionalities to insert nodes and perform a preorder traversal, providing a menu for user interaction. Both programs utilize dynamic memory allocation and linked data structures.

Uploaded by

kgxsumitsalunke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

19

The document contains two C programs: the first implements an undirected graph using an adjacency list, allowing users to input vertices and edges, and then prints the adjacency list. The second program implements a binary search tree with functionalities to insert nodes and perform a preorder traversal, providing a menu for user interaction. Both programs utilize dynamic memory allocation and linked data structures.

Uploaded by

kgxsumitsalunke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Q1

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int vertex;
struct Node* next;
} Node;
Node* createNode(int vertex) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->vertex = vertex;
newNode->next = NULL;
return newNode;
}
void addEdge(Node* adjList[], int src, int dest) {
// Add edge from src to dest
Node* newNode = createNode(dest);
newNode->next = adjList[src];
adjList[src] = newNode;
newNode = createNode(src);
newNode->next = adjList[dest];
adjList[dest] = newNode;
}
void printAdjList(Node* adjList[], int vertices) {
for (int i = 0; i < vertices; i++) {
Node* temp = adjList[i];
printf("Vertex %d: ", i);
while (temp) {
printf("-> %d ", temp->vertex);
temp = temp->next;
}
printf("\n");
}
}

int main() {
int vertices, edges;

printf("Enter number of vertices: ");


scanf("%d", &vertices);
Node* adjList[vertices];
for (int i = 0; i < vertices; i++) {
adjList[i] = NULL;
}

printf("Enter number of edges: ");


scanf("%d", &edges);

printf("Enter edges (src dest) one per line:\n");


for (int i = 0; i < edges; i++) {
int src, dest;
scanf("%d %d", &src, &dest);
addEdge(adjList, src, dest);
}

printf("\nAdjacency List:\n");
printAdjList(adjList, vertices);
for (int i = 0; i < vertices; i++) {
Node* temp = adjList[i];
while (temp) {
Node* toFree = temp;
temp = temp->next;
free(toFree);
}
}

return 0;
}

Q2

// btree.h

#ifndef BTREE_H
#define BTREE_H

typedef struct Node {


int data;
struct Node* left;
struct Node* right;
} Node;
Node* createNode(int data);
Node* insertNode(Node* root, int data);
void preorderTraversal(Node* root);

#endif

// btree.c

#include <stdio.h>
#include <stdlib.h>
#include "btree.h"

Node* createNode(int data) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
Node* insertNode(Node* root, int data) {
if (root == NULL)
return createNode(data);

if (data < root->data)


root->left = insertNode(root->left, data);
else if (data > root->data)
root->right = insertNode(root->right, data);

return root;
}
void preorderTraversal(Node* root) {
if (root == NULL)
return;

printf("%d ", root->data);


preorderTraversal(root->left);
preorderTraversal(root->right);
}

// main.c

#include <stdio.h>
#include <stdlib.h>
#include "btree.h"

int main() {
Node* root = NULL;
int choice, data;

do {
printf("\n--- Binary Search Tree Menu ---\n");
printf("1. Insert Node\n");
printf("2. Preorder Traversal\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &data);
root = insertNode(root, data);
break;
case 2:
printf("Preorder Traversal: ");
preorderTraversal(root);
printf("\n");
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while (choice != 3);

return 0;
}

You might also like