Lab programs - Copy
Lab programs - Copy
implementing Prim's Algorithm for solving the "Min Cost to Connect All Points"
code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// 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];
// 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];
return minCost;
}
int main() {
int points[][2] = {
{0, 0},
{2, 2},
{3, 10},
{5, 2},
{7, 0}
};
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>
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:
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>
// 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;
int isQueueEmpty(Queue* q) {
int isQueueFull(Queue* q) {
// Enqueue operation
if (isQueueFull(q)) {
return;
if (isQueueEmpty(q)) {
q->front = 0;
strcpy(q->queue[++q->rear], name);
// Dequeue operation
if (isQueueEmpty(q)) {
printf("Queue is empty! No customers to serve.\n");
return;
s->stack[++s->top] = ticketID;
if (q->front == q->rear) {
} else {
q->front++;
// Display queue
void viewQueue(Queue* q) {
if (isQueueEmpty(q)) {
printf("Queue is empty.\n");
return;
printf("Queue: ");
printf("\n");
}
// Initialize stack
void initStack(Stack* s) {
s->top = -1;
int isStackEmpty(Stack* s) {
void undoTransaction(Stack* s) {
if (isStackEmpty(s)) {
return;
// Main function
int main() {
Queue q;
Stack s;
initQueue(&q);
initStack(&s);
int choice;
char name[50];
do {
printf("\nTicket Counter System:\n");
printf("5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
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:
break;
default:
Ouput :
3. View queue
5. Exit
3. View queue
5. Exit
3. View queue
5. Exit
Enter your choice: 3
3. View queue
5. Exit
3. View queue
5. Exit
3. View queue
5. Exit