0% found this document useful (0 votes)
19 views

Lab programs - Copy

Uploaded by

blesswinsj12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lab programs - Copy

Uploaded by

blesswinsj12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

implementing Prim's Algorithm for solving the "Min Cost to Connect All Points"
code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define MAX_POINTS 1000

// Function to calculate Manhattan distance


int manhattanDistance(int x1, int y1, int x2, int y2) {
return abs(x1 - x2) + abs(y1 - y2);
}

// Function to find the minimum cost to connect all points using Prim's algorithm
int minCostConnectPoints(int points[][2], int pointsSize) {
int minCost = 0;
int visited[MAX_POINTS] = {0};
int minDist[MAX_POINTS];

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


minDist[i] = INT_MAX;
}

minDist[0] = 0; // Start with the first point

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


int u = -1;

// Find the point with the smallest distance that is not visited
for (int j = 0; j < pointsSize; j++) {
if (!visited[j] && (u == -1 || minDist[j] < minDist[u])) {
u = j;
}
}

visited[u] = 1;
minCost += minDist[u];

// Update the distances of the remaining points


for (int v = 0; v < pointsSize; v++) {
if (!visited[v]) {
int dist = manhattanDistance(
points[u][0], points[u][1],
points[v][0], points[v][1]
);
if (dist < minDist[v]) {
minDist[v] = dist;
}
}
}
}

return minCost;
}

int main() {
int points[][2] = {
{0, 0},
{2, 2},
{3, 10},
{5, 2},
{7, 0}
};

int pointsSize = sizeof(points) / sizeof(points[0]);

int result = minCostConnectPoints(points, pointsSize);


printf("Minimum cost to connect all points: %d\n", result);

return 0;
}

Ouput:
Minimum cost to connect all points: 20
2. C program that demonstrates binary tree traversal using three common methods:
Inorder, Preorder, and Postorder.
Code :

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

// Definition of a binary tree node


struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Function to create a new node


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

// Inorder traversal (Left, Root, Right)


void inorderTraversal(struct Node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}

// Preorder traversal (Root, Left, Right)


void preorderTraversal(struct Node* root) {
if (root == NULL) return;
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}

// Postorder traversal (Left, Right, Root)


void postorderTraversal(struct Node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
// Main function to test the tree traversal
int main() {
// Creating a binary tree
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);

printf("Inorder Traversal: ");


inorderTraversal(root);
printf("\n");

printf("Preorder Traversal: ");


preorderTraversal(root);
printf("\n");

printf("Postorder Traversal: ");


postorderTraversal(root);
printf("\n");

return 0;
}
3. Case Study: Ticket Counter System

You are tasked with designing a ticket counter system for a train station. The system has the following
requirements:

1. Queue for Customers:

o Customers arrive at the ticket counter and are placed in a queue.

o The queue follows a FIFO (First-In-First-Out) principle.

2. Stack for Ticket History:

o Every time a ticket is sold, the transaction details (ticket ID) are stored in a stack.

o This helps in reversing transactions (if needed) by popping the most recent ticket
from the stack.

Code:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX 100

// Queue structure

typedef struct {

char queue[MAX][50];

int front;

int rear;

} Queue;

// Stack structure

typedef struct {

int stack[MAX];

int top;

} Stack;

// Initialize queue
void initQueue(Queue* q) {

q->front = -1;

q->rear = -1;

// Check if queue is empty

int isQueueEmpty(Queue* q) {

return q->front == -1;

// Check if queue is full

int isQueueFull(Queue* q) {

return q->rear == MAX - 1;

// Enqueue operation

void enqueue(Queue* q, char* name) {

if (isQueueFull(q)) {

printf("Queue is full! Cannot add more customers.\n");

return;

if (isQueueEmpty(q)) {

q->front = 0;

strcpy(q->queue[++q->rear], name);

printf("Enqueue: %s added to the queue.\n", name);

// Dequeue operation

void dequeue(Queue* q, Stack* s) {

if (isQueueEmpty(q)) {
printf("Queue is empty! No customers to serve.\n");

return;

char* customer = q->queue[q->front];

printf("Dequeue: %s served. ", customer);

// Simulate ticket generation and push to stack

int ticketID = 100 + q->front;

s->stack[++s->top] = ticketID;

printf("Ticket ID: %d generated.\n", ticketID);

if (q->front == q->rear) {

q->front = q->rear = -1; // Reset queue when empty

} else {

q->front++;

// Display queue

void viewQueue(Queue* q) {

if (isQueueEmpty(q)) {

printf("Queue is empty.\n");

return;

printf("Queue: ");

for (int i = q->front; i <= q->rear; i++) {

printf("%s ", q->queue[i]);

printf("\n");

}
// Initialize stack

void initStack(Stack* s) {

s->top = -1;

// Check if stack is empty

int isStackEmpty(Stack* s) {

return s->top == -1;

// Undo last transaction

void undoTransaction(Stack* s) {

if (isStackEmpty(s)) {

printf("No transactions to undo.\n");

return;

int ticketID = s->stack[s->top--];

printf("Undo Last Transaction: Ticket ID %d removed.\n", ticketID);

// Main function

int main() {

Queue q;

Stack s;

initQueue(&q);

initStack(&s);

int choice;

char name[50];

do {
printf("\nTicket Counter System:\n");

printf("1. Add customer to queue\n");

printf("2. Serve customer (Dequeue)\n");

printf("3. View queue\n");

printf("4. Undo last transaction\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter customer name: ");

scanf("%s", name);

enqueue(&q, name);

break;

case 2:

dequeue(&q, &s);

break;

case 3:

viewQueue(&q);

break;

case 4:

undoTransaction(&s);

break;

case 5:

printf("Exiting system. Goodbye!\n");

break;

default:

printf("Invalid choice. Please try again.\n");

} while (choice != 5);


return 0;

Ouput :

Ticket Counter System:

1. Add customer to queue

2. Serve customer (Dequeue)

3. View queue

4. Undo last transaction

5. Exit

Enter your choice: 1

Enter customer name: Alice

Enqueue: Alice added to the queue.

Ticket Counter System:

1. Add customer to queue

2. Serve customer (Dequeue)

3. View queue

4. Undo last transaction

5. Exit

Enter your choice: 1

Enter customer name: Bob

Enqueue: Bob added to the queue.

Ticket Counter System:

1. Add customer to queue

2. Serve customer (Dequeue)

3. View queue

4. Undo last transaction

5. Exit
Enter your choice: 3

Queue: Alice Bob

Ticket Counter System:

1. Add customer to queue

2. Serve customer (Dequeue)

3. View queue

4. Undo last transaction

5. Exit

Enter your choice: 2

Dequeue: Alice served. Ticket ID: 100 generated.

Ticket Counter System:

1. Add customer to queue

2. Serve customer (Dequeue)

3. View queue

4. Undo last transaction

5. Exit

Enter your choice: 4

Undo Last Transaction: Ticket ID 100 removed.

Ticket Counter System:

1. Add customer to queue

2. Serve customer (Dequeue)

3. View queue

4. Undo last transaction

5. Exit

Enter your choice: 5

Exiting system. Goodbye!

You might also like