Assignment 9
Assignment 9
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
} Region;
}
return min_index;
}
dist[start_node] = 0;
newWarehouse->capacity = capacity;
newWarehouse->inventory = NULL;
newWarehouse->next = NULL;
return newWarehouse;
}
void addWarehouse(Region *region, char *name, char *loca on, int capacity) {
Warehouse *warehouse = createWarehouse(name, loca on, capacity);
warehouse->next = region->warehouses;
region->warehouses = warehouse;
}
if (current == NULL) {
prin ("Warehouse not found.\n");
return;
if (prev == NULL) {
region->warehouses = current->next;
} else {
prev->next = current->next;
}
free(current);
void addInventoryItem(Warehouse *warehouse, char *name, int quan ty, float unit_cost) {
InventoryItem *item = createInventoryItem(name, quan ty, unit_cost);
item->next = warehouse->inventory;
warehouse->inventory = item;
}
if (strcmp(current->name, name) == 0) {
return current;
}
current = current->next;
}
return NULL;
}
if (current == NULL) {
prin ("Inventory item not found.\n");
return;
}
if (prev == NULL) {
warehouse->inventory = current->next;
} else {
prev->next = current->next;
}
free(current);
}
void userInterface() {
Region *region = createRegion("Main Region");
int choice;
char warehouseName[50];
char loca on[100];
int capacity;
char itemName[50];
int quan ty;
float unit_cost;
int from, to, weight, start_node, end_node;
Warehouse *warehouse;
InventoryItem *item;
Graph g;
ini alize_graph(&g, MAX_WAREHOUSES);
do {
prin ("\nWarehouse Management System\n");
prin ("1. Add Warehouse\n");
prin ("2. Delete Warehouse\n");
prin ("3. Add Inventory Item\n");
prin ("4. Update Inventory Item\n");
switch (choice) {
case 1:
prin ("Enter warehouse name: ");
scanf("%s", warehouseName);
prin ("Enter loca on: ");
scanf("%s", loca on);
prin ("Enter capacity: ");
scanf("%d", &capacity);
addWarehouse(region, warehouseName, loca on, capacity);
prin ("Warehouse added.\n");
break;
case 2:
prin ("Enter warehouse name to delete: ");
scanf("%s", warehouseName);
deleteWarehouse(region, warehouseName);
prin ("Warehouse deleted.\n");
break;
case 3:
if (warehouse != NULL) {
printInventory(warehouse);
} else {
prin ("Warehouse not found.\n");
}
break;
case 8:
prin ("Enter warehouse name: ");
scanf("%s", warehouseName);
warehouse = findWarehouse(region, warehouseName);
if (warehouse != NULL) {
float totalCost = calculateTotalCost(warehouse);
prin ("Total Cost of Inventory: %.2f\n", totalCost);
} else {
prin ("Warehouse not found.\n");
}
break;
case 9:
prin ("Enter warehouse name: ");
scanf("%s", warehouseName);
warehouse = findWarehouse(region, warehouseName);
if (warehouse != NULL) {
reportAnaly cs(warehouse);
} else {
prin ("Warehouse not found.\n");
}
break;
case 10:
prin ("Enter from warehouse index: ");
scanf("%d", &from);
prin ("Enter to warehouse index: ");
scanf("%d", &to);
prin ("Enter route weight: ");
scanf("%d", &weight);
add_edge(&g, from, to, weight);
prin ("Route added.\n");
break;
case 11:
prin ("Enter start warehouse index: ");
scanf("%d", &start_node);
prin ("Enter end warehouse index: ");
scanf("%d", &end_node);
dijkstra(&g, start_node, end_node);
break;
case 12:
prin ("Exi ng the system.\n");
break;
default:
prin ("Invalid choice. Please try again.\n");
break;
}
} while (choice != 12);
}
int main() {
userInterface();
return 0;
}
Output:
Topic:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
typedef struct {
int id;
char name[MAX_NAME_LEN];
char loca on[MAX_NAME_LEN];
int pla ormCount;
} Sta on;
typedef struct {
int travelTime;
int transfers;
int cketPrice;
} CostFactors;
return graph;
}
return newNode;
}
graph->adjList[src] = newNode;
void addSta on(Graph* graph, int id, const char* name, const char* loca on, int
pla ormCount) {
if (graph->numSta ons >= MAX_STATIONS) {
prin ("Maximum number of sta ons reached.\n");
return;
}
if (index == -1) {
prin ("Sta on not found.\n");
return;
}
free(graph->sta ons[index]);
for (int i = index; i < graph->numSta ons - 1; i++) {
graph->sta ons[i] = graph->sta ons[i + 1];
}
graph->numSta ons--;
prin ("Sta on removed.\n");
}
void updateSta on(Graph* graph, int id, const char* name, const char* loca on, int
pla ormCount) {
for (int i = 0; i < graph->numSta ons; i++) {
if (graph->sta ons[i]->id == id) {
strncpy(graph->sta ons[i]->name, name, MAX_NAME_LEN);
strncpy(graph->sta ons[i]->loca on, loca on, MAX_NAME_LEN);
parent[i] = -1;
}
dist[src] = 0;
prin ("\n");
}
}
curr = &(graph->adjList[dest]);
while (*curr != NULL) {
if ((*curr)->sta on == src) {
Node* temp = *curr;
*curr = (*curr)->next;
free(temp);
break;
}
curr = &((*curr)->next);
}
}
void displayRoute(int parent[], int dest) {
// Traverse from des na on to source and store the path in the stack
int crawl = dest;
while (parent[crawl] != -1) {
stack[++top] = crawl;
crawl = parent[crawl];
}
stack[++top] = crawl;
dist[src] = 0;
if (dist[dest] == INF) {
prin ("No path found from %d to %d.\n", src, dest);
} else {
void handleDisrup on(Graph* graph, int disruptedSrc, int disruptedDest, int start, int end,
int travelTime) {
// Remove the edge represen ng the disrup on
removeEdge(graph, disruptedSrc, disruptedDest);
// Restore the disrupted edge if you want to reuse the original graph
addEdge(graph, disruptedSrc, disruptedDest, travelTime);
}
int calculateRouteCost(Graph* graph, int dist[], int parent[], int start, int end, CostFactors
factors) {
int travelTime = dist[end];
if (travelTime == INF) {
prin ("No valid path exists.\n");
return INF;
}
void dijkstraWithCostCalcula on(Graph* graph, int src, int dest, CostFactors factors) {
int dist[MAX_STATIONS];
int sptSet[MAX_STATIONS];
int parent[MAX_STATIONS];
void displayMenu() {
prin ("\nTrain Journey Alterna ve Route Applica on\n");
prin ("1. Add Sta on\n");
prin ("2. Add Route\n");
prin ("3. Remove Route (Simulate Disrup on)\n");
prin ("4. Find Alterna ve Route\n");
prin ("5. Exit\n");
prin ("Enter your choice: ");
}
int main() {
Graph* graph = createGraph(5); // Example with ini al capacity of 5 sta ons
int choice;
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
addSta onUI(graph);
break;
case 2:
addRouteUI(graph);
break;
case 3:
removeRouteUI(graph);
break;
case 4:
findAlterna veRouteUI(graph);
break;
case 5:
// Free allocated memory and exit
for (int i = 0; i < graph->numSta ons; i++) {
Node* pCrawl = graph->adjList[i];
while (pCrawl != NULL) {
Node* temp = pCrawl;
pCrawl = pCrawl->next;
free(temp);
}
}
for (int i = 0; i < graph->numSta ons; i++) {
free(graph->sta ons[i]);
}
free(graph);
return 0;
default:
prin ("Invalid choice. Please try again.\n");
}
}
return 0;
}
Output: