0% found this document useful (0 votes)
4 views11 pages

ITE1004_da1

The document contains two C program codes demonstrating data structures: one for a priority print queue and another for a breadth-first search (BFS) algorithm on a graph. The print queue allows jobs to be enqueued based on priority and dequeued for processing, while the BFS implementation traverses a graph using an adjacency matrix. Both programs include functions for creating, enqueuing, and dequeuing elements, with example outputs provided.

Uploaded by

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

ITE1004_da1

The document contains two C program codes demonstrating data structures: one for a priority print queue and another for a breadth-first search (BFS) algorithm on a graph. The print queue allows jobs to be enqueued based on priority and dequeued for processing, while the BFS implementation traverses a graph using an adjacency matrix. Both programs include functions for creating, enqueuing, and dequeuing elements, with example outputs provided.

Uploaded by

Akshat Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

DIGITAL ASSINGEMENT

ITE1004 DATA STRUCTURES AND ALGORITHM


AKSHAT 20BIT0425
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Queue structure for print jobs


struct PrintQueue {
char jobDescription[100];
int priority;
struct PrintQueue* next;
};
// Function to add a print job to the queue
void enqueue(struct PrintQueue** queue, char description[], int priority) {
// Create a new print job
struct PrintQueue* newJob = (struct PrintQueue*)malloc(sizeof(struct PrintQueue));
strcpy(newJob->jobDescription, description);
newJob->priority = priority;
newJob->next = NULL;

// Add the job to the queue based on priority


if (*queue == NULL || priority < (*queue)->priority) {
newJob->next = *queue;
*queue = newJob;
} else {
struct PrintQueue* current = *queue;
while (current->next != NULL && current->next->priority <= priority) {
current = current->next;
}
newJob->next = current->next;
current->next = newJob;
}
}

// Function to remove and process the highest priority job from the queue
void dequeue(struct PrintQueue** queue) {
if (*queue == NULL) {
printf("Print queue is empty.\n");
return;
}
struct PrintQueue* temp = *queue;
*queue = (*queue)->next;
printf("Printing job: %s\n", temp->jobDescription);
free(temp);
}

int main() {
struct PrintQueue* printQueue = NULL;

// Add print jobs to the queue


enqueue(&printQueue, "Job A", 2);
enqueue(&printQueue, "Job B", 1);
enqueue(&printQueue, "Job C", 3);

// Process print jobs


dequeue(&printQueue);
dequeue(&printQueue);
dequeue(&printQueue);

return 0;
}

OUTPUT
Printing job: Job B
Printing job: Job A
Printing job: Job C
PROGRAM CODE:

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

// Queue structure for BFS traversal


struct Queue {
int data;
struct Queue* next;
};

// Function to create a new queue node


struct Queue* createQueueNode(int data) {
struct Queue* newNode = (struct Queue*)malloc(sizeof(struct Queue));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to create an empty queue


struct Queue* createQueue() {
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->next = NULL;
return queue;
}

// Function to enqueue a vertex to the queue


void enqueue(struct Queue* queue, int vertex) {
struct Queue* newNode = createQueueNode(vertex);
struct Queue* current = queue;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}

// Function to dequeue a vertex from the queue


int dequeue(struct Queue* queue) {
if (queue->next == NULL) {
return -1; // Queue is empty
}
int vertex = queue->next->data;
struct Queue* temp = queue->next;
queue->next = queue->next->next;
free(temp);
return vertex;
}

// Function to perform BFS traversal on a graph


void BFS(int** graph, int numVertices, int startVertex) {
int* visited = (int*)malloc(numVertices * sizeof(int));
for (int i = 0; i < numVertices; i++) {
visited[i] = 0; // Initialize all vertices as unvisited
}

struct Queue* queue = createQueue();


printf("Breadth-First Search (BFS) starting from vertex %d:\n", startVertex);
printf("%d ", startVertex);
visited[startVertex] = 1;

enqueue(queue, startVertex);

while (queue->next != NULL) {


int currentVertex = dequeue(queue);

for (int i = 0; i < numVertices; i++) {


if (graph[currentVertex][i] == 1 && visited[i] == 0) {
printf("%d ", i);
visited[i] = 1;
enqueue(queue, i);
}
}
}

printf("\n");
free(visited);
}

int main() {
int numVertices = 6; // Number of vertices in the graph
int startVertex = 0; // Starting vertex for BFS

// Adjacency matrix representation of the graph


int graph[][6] = {
{0, 1, 1, 0, 0, 0},
{1, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 1, 0},
{0, 1, 1, 0, 0, 1},
{0, 0, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 0}
};

BFS(graph, numVertices, startVertex);


return 0;
}
OUTPUT:
Breadth-First Search (BFS) starting from vertex 0:
012345

You might also like