Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1.Singly Linked List
- #include <stdio.h>
- #include <stdlib.h>
- // Define the structure for a node
- struct Node {
- int data;
- struct Node* next;
- };
- // Function to create a new node
- struct Node* createNode(int data) {
- struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
- newNode->data = data;
- newNode->next = NULL;
- return newNode;
- }
- // Function to insert an element into the list
- void insertElement(struct Node** head, int data) {
- struct Node* newNode = createNode(data);
- newNode->next = *head;
- *head = newNode;
- }
- // Function to delete the maximum element from the list
- void deleteMaxElement(struct Node** head) {
- if (*head == NULL) {
- printf("List is empty.\n");
- return;
- }
- struct Node *current = *head, *prev = NULL, *maxNode = *head, *prevMax = NULL;
- while (current != NULL) {
- if (current->data > maxNode->data) {
- maxNode = current;
- prevMax = prev;
- }
- prev = current;
- current = current->next;
- }
- if (prevMax == NULL) { // Max element is the head
- *head = maxNode->next;
- } else {
- prevMax->next = maxNode->next;
- }
- free(maxNode);
- printf("Maximum element deleted.\n");
- }
- // Function to sort the list in ascending order
- void sortList(struct Node** head) {
- if (*head == NULL || (*head)->next == NULL) {
- return;
- }
- struct Node *i, *j;
- int temp;
- for (i = *head; i != NULL; i = i->next) {
- for (j = i->next; j != NULL; j = j->next) {
- if (i->data > j->data) {
- temp = i->data;
- i->data = j->data;
- j->data = temp;
- }
- }
- }
- printf("List sorted.\n");
- }
- // Function to display the elements of the list
- void displayList(struct Node* head) {
- if (head == NULL) {
- printf("List is empty.\n");
- return;
- }
- printf("List elements: ");
- while (head != NULL) {
- printf("%d ", head->data);
- head = head->next;
- }
- printf("\n");
- }
- int main() {
- struct Node* head = NULL;
- int choice, value;
- while (1) {
- printf("\nMenu:\n");
- printf("1. Insert an element\n");
- printf("2. Delete the maximum element\n");
- printf("3. Sort the list\n");
- printf("4. Display the list\n");
- printf("5. Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- printf("Enter the value to insert: ");
- scanf("%d", &value);
- insertElement(&head, value);
- break;
- case 2:
- deleteMaxElement(&head);
- break;
- case 3:
- sortList(&head);
- break;
- case 4:
- displayList(head);
- break;
- case 5:
- printf("Exiting program.\n");
- exit(0);
- default:
- printf("Invalid choice. Please try again.\n");
- }
- }
- return 0;
- }
- 2.Implement stack and queue using linked list
- #include <stdio.h>
- #include <stdlib.h>
- // Define the structure for a node
- struct Node {
- int data;
- struct Node* next;
- };
- // Stack Implementation using Linked List
- struct Stack {
- struct Node* top;
- };
- void initStack(struct Stack* stack) {
- stack->top = NULL;
- }
- void push(struct Stack* stack, int data) {
- struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
- newNode->data = data;
- newNode->next = stack->top;
- stack->top = newNode;
- printf("%d pushed to stack.\n", data);
- }
- int pop(struct Stack* stack) {
- if (stack->top == NULL) {
- printf("Stack underflow.\n");
- return -1;
- }
- struct Node* temp = stack->top;
- int popped = temp->data;
- stack->top = stack->top->next;
- free(temp);
- return popped;
- }
- void displayStack(struct Stack* stack) {
- struct Node* temp = stack->top;
- printf("Stack elements: ");
- while (temp != NULL) {
- printf("%d ", temp->data);
- temp = temp->next;
- }
- printf("\n");
- }
- // Queue Implementation using Linked List
- struct Queue {
- struct Node* front;
- struct Node* rear;
- };
- void initQueue(struct Queue* queue) {
- queue->front = queue->rear = NULL;
- }
- void enqueue(struct Queue* queue, int data) {
- struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
- newNode->data = data;
- newNode->next = NULL;
- if (queue->rear == NULL) {
- queue->front = queue->rear = newNode;
- printf("%d enqueued to queue.\n", data);
- return;
- }
- queue->rear->next = newNode;
- queue->rear = newNode;
- printf("%d enqueued to queue.\n", data);
- }
- int dequeue(struct Queue* queue) {
- if (queue->front == NULL) {
- printf("Queue underflow.\n");
- return -1;
- }
- struct Node* temp = queue->front;
- int dequeued = temp->data;
- queue->front = queue->front->next;
- if (queue->front == NULL) {
- queue->rear = NULL;
- }
- free(temp);
- return dequeued;
- }
- void displayQueue(struct Queue* queue) {
- struct Node* temp = queue->front;
- printf("Queue elements: ");
- while (temp != NULL) {
- printf("%d ", temp->data);
- temp = temp->next;
- }
- printf("\n");
- }
- int main() {
- struct Stack stack;
- struct Queue queue;
- initStack(&stack);
- initQueue(&queue);
- int choice, value;
- while (1) {
- printf("\nMenu:\n");
- printf("1. Push to stack\n");
- printf("2. Pop from stack\n");
- printf("3. Display stack\n");
- printf("4. Enqueue to queue\n");
- printf("5. Dequeue from queue\n");
- printf("6. Display queue\n");
- printf("7. Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- printf("Enter the value to push: ");
- scanf("%d", &value);
- push(&stack, value);
- break;
- case 2:
- value = pop(&stack);
- if (value != -1) {
- printf("Popped value: %d\n", value);
- }
- break;
- case 3:
- displayStack(&stack);
- break;
- case 4:
- printf("Enter the value to enqueue: ");
- scanf("%d", &value);
- enqueue(&queue, value);
- break;
- case 5:
- value = dequeue(&queue);
- if (value != -1) {
- printf("Dequeued value: %d\n", value);
- }
- break;
- case 6:
- displayQueue(&queue);
- break;
- case 7:
- printf("Exiting program.\n");
- exit(0);
- default:
- printf("Invalid choice. Please try again.\n");
- }
- }
- return 0;
- }
- 3.Implementation of quick, heap and shell sort
- #include <stdio.h>
- void quickSort(int arr[], int low, int high) {
- if (low < high) {
- int pivot = arr[high];
- int i = low - 1;
- for (int j = low; j < high; j++) {
- if (arr[j] < pivot) {
- i++;
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- int temp = arr[i + 1];
- arr[i + 1] = arr[high];
- arr[high] = temp;
- int pi = i + 1;
- quickSort(arr, low, pi - 1);
- quickSort(arr, pi + 1, high);
- }
- }
- int main() {
- int arr[] = {10, 7, 8, 9, 1, 5};
- int n = sizeof(arr) / sizeof(arr[0]);
- quickSort(arr, 0, n - 1);
- printf("Quick Sorted array: ");
- for (int i = 0; i < n; i++) printf("%d ", arr[i]);
- return 0;
- }
- #include <stdio.h>
- void heapify(int arr[], int n, int i) {
- int largest = i;
- int left = 2 * i + 1;
- int right = 2 * i + 2;
- if (left < n && arr[left] > arr[largest]) largest = left;
- if (right < n && arr[right] > arr[largest]) largest = right;
- if (largest != i) {
- int temp = arr[i];
- arr[i] = arr[largest];
- arr[largest] = temp;
- heapify(arr, n, largest);
- }
- }
- void heapSort(int arr[], int n) {
- for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i);
- for (int i = n - 1; i > 0; i--) {
- int temp = arr[0];
- arr[0] = arr[i];
- arr[i] = temp;
- heapify(arr, i, 0);
- }
- }
- int main() {
- int arr[] = {12, 11, 13, 5, 6, 7};
- int n = sizeof(arr) / sizeof(arr[0]);
- heapSort(arr, n);
- printf("Heap Sorted array: ");
- for (int i = 0; i < n; i++) printf("%d ", arr[i]);
- return 0;
- }
- #include <stdio.h>
- void shellSort(int arr[], int n) {
- for (int gap = n / 2; gap > 0; gap /= 2) {
- for (int i = gap; i < n; i++) {
- int temp = arr[i];
- int j;
- for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
- arr[j] = arr[j - gap];
- }
- arr[j] = temp;
- }
- }
- }
- int main() {
- int arr[] = {12, 34, 54, 2, 3};
- int n = sizeof(arr) / sizeof(arr[0]);
- shellSort(arr, n);
- printf("Shell Sorted array: ");
- for (int i = 0; i < n; i++) printf("%d ", arr[i]);
- return 0;
- }
- 4.Selection and Bubble sort
- #include <stdio.h>
- void selectionSort(int arr[], int n) {
- for (int i = 0; i < n - 1; i++) {
- int minIndex = i;
- for (int j = i + 1; j < n; j++) {
- if (arr[j] < arr[minIndex]) {
- minIndex = j;
- }
- }
- int temp = arr[minIndex];
- arr[minIndex] = arr[i];
- arr[i] = temp;
- }
- }
- int main() {
- int arr[] = {64, 25, 12, 22, 11};
- int n = sizeof(arr) / sizeof(arr[0]);
- selectionSort(arr, n);
- printf("Selection Sorted array: ");
- for (int i = 0; i < n; i++) {
- printf("%d ", arr[i]);
- }
- return 0;
- }
- #include <stdio.h>
- void bubbleSort(int arr[], int n) {
- for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < n - i - 1; j++) {
- if (arr[j] > arr[j + 1]) {
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- }
- int main() {
- int arr[] = {5, 1, 4, 2, 8};
- int n = sizeof(arr) / sizeof(arr[0]);
- bubbleSort(arr, n);
- printf("Bubble Sorted array: ");
- for (int i = 0; i < n; i++) {
- printf("%d ", arr[i]);
- }
- return 0;
- }
- 5.Implementation of hashing technique
- #include <stdio.h>
- #include <stdlib.h>
- #define TABLE_SIZE 10
- // Define the structure for a node in the linked list
- struct Node {
- int data;
- struct Node* next;
- };
- // Define the hash table as an array of pointers to linked lists
- struct Node* hashTable[TABLE_SIZE];
- // Function to initialize the hash table
- void initializeHashTable() {
- for (int i = 0; i < TABLE_SIZE; i++) {
- hashTable[i] = NULL;
- }
- }
- // Hash function to compute the index
- int hashFunction(int key) {
- return key % TABLE_SIZE;
- }
- // Function to insert an element into the hash table
- void insert(int key) {
- int index = hashFunction(key);
- struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
- newNode->data = key;
- newNode->next = hashTable[index];
- hashTable[index] = newNode;
- }
- // Function to search for an element in the hash table
- int search(int key) {
- int index = hashFunction(key);
- struct Node* temp = hashTable[index];
- while (temp != NULL) {
- if (temp->data == key) {
- return 1; // Element found
- }
- temp = temp->next;
- }
- return 0; // Element not found
- }
- // Function to delete an element from the hash table
- void deleteElement(int key) {
- int index = hashFunction(key);
- struct Node* temp = hashTable[index];
- struct Node* prev = NULL;
- while (temp != NULL) {
- if (temp->data == key) {
- if (prev == NULL) {
- hashTable[index] = temp->next; // Delete the head node
- } else {
- prev->next = temp->next;
- }
- free(temp);
- printf("Element %d deleted.\n", key);
- return;
- }
- prev = temp;
- temp = temp->next;
- }
- printf("Element %d not found.\n", key);
- }
- // Function to display the hash table
- void displayHashTable() {
- for (int i = 0; i < TABLE_SIZE; i++) {
- printf("Bucket %d: ", i);
- struct Node* temp = hashTable[i];
- while (temp != NULL) {
- printf("%d -> ", temp->data);
- temp = temp->next;
- }
- printf("NULL\n");
- }
- }
- // Main function to demonstrate the hashing technique
- int main() {
- initializeHashTable();
- int choice, value;
- while (1) {
- printf("\nMenu:\n");
- printf("1. Insert an element\n");
- printf("2. Search for an element\n");
- printf("3. Delete an element\n");
- printf("4. Display the hash table\n");
- printf("5. Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- printf("Enter the value to insert: ");
- scanf("%d", &value);
- insert(value);
- break;
- case 2:
- printf("Enter the value to search: ");
- scanf("%d", &value);
- if (search(value)) {
- printf("Element %d found in the hash table.\n", value);
- } else {
- printf("Element %d not found in the hash table.\n", value);
- }
- break;
- case 3:
- printf("Enter the value to delete: ");
- scanf("%d", &value);
- deleteElement(value);
- break;
- case 4:
- displayHashTable();
- break;
- case 5:
- printf("Exiting program.\n");
- exit(0);
- default:
- printf("Invalid choice. Please try again.\n");
- }
- }
- return 0;
- }
- 6.Program for linear and binary search
- #include <stdio.h>
- int linear_search(int arr[], int size, int target) {
- for (int i = 0; i < size; i++) {
- if (arr[i] == target) {
- return i; // Return the index where the target is found
- }
- }
- return -1; // Return -1 if target is not found
- }
- int main() {
- int arr[] = {10, 20, 30, 40, 50};
- int size = sizeof(arr) / sizeof(arr[0]);
- int target = 30;
- int result = linear_search(arr, size, target);
- if (result != -1) {
- printf("Target found at index: %d\n", result);
- } else {
- printf("Target not found\n");
- }
- return 0;
- }
- #include <stdio.h>
- int binary_search(int arr[], int size, int target) {
- int low = 0;
- int high = size - 1;
- while (low <= high) {
- int mid = low + (high - low) / 2; // Find the middle index
- if (arr[mid] == target) {
- return mid; // Return the index where the target is found
- }
- if (arr[mid] < target) {
- low = mid + 1; // Eliminate the left half
- } else {
- high = mid - 1; // Eliminate the right half
- }
- }
- return -1; // Return -1 if target is not found
- }
- int main() {
- int arr[] = {10, 20, 30, 40, 50};
- int size = sizeof(arr) / sizeof(arr[0]);
- int target = 30;
- int result = binary_search(arr, size, target);
- if (result != -1) {
- printf("Target found at index: %d\n", result);
- } else {
- printf("Target not found\n");
- }
- return 0;
- }
- 7.Tree traversal methods
- #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* newNode(int data) {
- struct Node* node = (struct Node*)malloc(sizeof(struct Node));
- node->data = data;
- node->left = node->right = NULL;
- return node;
- }
- void inorder(struct Node* root) {
- if (root == NULL) {
- return;
- }
- inorder(root->left); // Visit left subtree
- printf("%d ", root->data); // Visit root node
- inorder(root->right); // Visit right subtree
- }
- void preorder(struct Node* root) {
- if (root == NULL) {
- return;
- }
- printf("%d ", root->data); // Visit root node
- preorder(root->left); // Visit left subtree
- preorder(root->right); // Visit right subtree
- }
- void postorder(struct Node* root) {
- if (root == NULL) {
- return;
- }
- postorder(root->left); // Visit left subtree
- postorder(root->right); // Visit right subtree
- printf("%d ", root->data); // Visit root node
- }
- int main() {
- // Create the binary tree
- struct Node* root = newNode(1);
- root->left = newNode(2);
- root->right = newNode(3);
- root->left->left = newNode(4);
- root->left->right = newNode(5);
- root->right->left = newNode(6);
- root->right->right = newNode(7);
- printf("In-order Traversal: ");
- inorder(root);
- printf("\n");
- printf("Pre-order Traversal: ");
- preorder(root);
- printf("\n");
- printf("Post-order Traversal: ");
- postorder(root);
- printf("\n");
- return 0;
- }
- 8.Implementation of prims algorithm
- #include <stdio.h>
- #include <limits.h>
- #define V 5 // Number of vertices
- // Function to find the vertex with the minimum key value
- int minKey(int key[], int mstSet[]) {
- int min = INT_MAX, minIndex;
- for (int v = 0; v < V; v++) {
- if (mstSet[v] == 0 && key[v] < min) {
- min = key[v];
- minIndex = v;
- }
- }
- return minIndex;
- }
- // Function to print the MST constructed
- void printMST(int parent[], int graph[V][V]) {
- printf("Edge \tWeight\n");
- for (int i = 1; i < V; i++) {
- printf("%d - %d \t%d\n", parent[i], i, graph[i][parent[i]]);
- }
- }
- // Function to implement Prim's algorithm to find the MST
- void primMST(int graph[V][V]) {
- int parent[V]; // Array to store the constructed MST
- int key[V]; // Key values used to pick minimum weight edge
- int mstSet[V]; // To represent vertices not yet included in MST
- // Initialize all keys as INFINITE and mstSet[] as false
- for (int i = 0; i < V; i++) {
- key[i] = INT_MAX;
- mstSet[i] = 0;
- }
- // Always include the first vertex in the MST
- key[0] = 0; // Make the key value of the first vertex 0
- parent[0] = -1; // The first node is always the root
- // The MST will have V vertices
- for (int count = 0; count < V - 1; count++) {
- // Pick the minimum key vertex from the set of vertices not yet included in MST
- int u = minKey(key, mstSet);
- // Add the picked vertex to the MST set
- mstSet[u] = 1;
- // Update the key values and parent index of the adjacent vertices of the picked vertex
- for (int v = 0; v < V; v++) {
- // graph[u][v] is non-zero only for adjacent vertices of u
- // mstSet[v] is false for vertices not yet included in MST
- // Update the key value of v
- if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v]) {
- key[v] = graph[u][v];
- parent[v] = u;
- }
- }
- }
- // Print the constructed MST
- printMST(parent, graph);
- }
- int main() {
- // Graph represented as an adjacency matrix
- int graph[V][V] = {
- {0, 2, 0, 6, 0},
- {2, 0, 3, 8, 5},
- {0, 3, 0, 0, 7},
- {6, 8, 0, 0, 9},
- {0, 5, 7, 9, 0}
- };
- // Function call to find and print the MST
- primMST(graph);
- return 0;
- }
- 9.Banking token system
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- // Sample token vault (this could be a secure database in a real system)
- #define VAULT_SIZE 10
- char* token_vault[VAULT_SIZE];
- // Function to generate a random token
- char* generate_token() {
- static const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- size_t length = 16;
- char* token = (char*)malloc(length + 1);
- for (size_t i = 0; i < length; i++) {
- token[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
- }
- token[length] = '\0';
- return token;
- }
- // Function to simulate tokenization
- void tokenize_data(char* sensitive_data) {
- // Generate a token for the sensitive data
- char* token = generate_token();
- // Store the token in the vault
- for (int i = 0; i < VAULT_SIZE; i++) {
- if (token_vault[i] == NULL) {
- token_vault[i] = token;
- break;
- }
- }
- printf("Sensitive Data: %s\n", sensitive_data);
- printf("Token Generated: %s\n", token);
- }
- // Main function to test tokenization
- int main() {
- srand(time(NULL)); // Seed the random number generator
- // Simulating sensitive data (e.g., bank account number)
- char sensitive_data[] = "1234567890";
- // Tokenize sensitive data
- tokenize_data(sensitive_data);
- return 0;
- }
- 10.Food delivery system
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_RESTAURANTS 3
- #define MAX_MENU_ITEMS 5
- // Structure for menu items
- typedef struct {
- char name[50];
- float price;
- } MenuItem;
- // Structure for restaurant
- typedef struct {
- char name[50];
- MenuItem menu[MAX_MENU_ITEMS];
- } Restaurant;
- // Structure for customer
- typedef struct {
- char name[50];
- char address[100];
- } Customer;
- // Structure for delivery
- typedef struct {
- char driverName[50];
- char status[50]; // E.g., "Picked up", "Delivered"
- } Delivery;
- // Function to print restaurant menu
- void printMenu(Restaurant restaurant) {
- printf("\nMenu for %s:\n", restaurant.name);
- for (int i = 0; i < MAX_MENU_ITEMS; i++) {
- printf("%d. %s - $%.2f\n", i + 1, restaurant.menu[i].name, restaurant.menu[i].price);
- }
- }
- // Function to create a restaurant with a menu
- void createRestaurant(Restaurant* restaurant, const char* name, MenuItem menu[]) {
- strcpy(restaurant->name, name);
- for (int i = 0; i < MAX_MENU_ITEMS; i++) {
- restaurant->menu[i] = menu[i];
- }
- }
- // Function to place an order
- void placeOrder(Customer* customer, Restaurant* restaurant, int itemIndex) {
- printf("\n%s placed an order for %s.\n", customer->name, restaurant->menu[itemIndex - 1].name);
- printf("Delivery will be made to %s\n", customer->address);
- }
- // Function to simulate delivery
- void processDelivery(Delivery* delivery) {
- printf("\nDelivery in progress...\n");
- printf("Driver: %s\n", delivery->driverName);
- printf("Status: %s\n", delivery->status);
- printf("Food Delivered!\n");
- }
- int main() {
- // Initialize menu items
- MenuItem pizzaMenu[] = {
- {"Margherita Pizza", 8.99},
- {"Pepperoni Pizza", 9.99},
- {"Veggie Pizza", 7.99},
- {"BBQ Chicken Pizza", 10.99},
- {"Cheese Pizza", 6.99}
- };
- MenuItem burgerMenu[] = {
- {"Cheeseburger", 5.99},
- {"Veggie Burger", 6.49},
- {"BBQ Burger", 7.99},
- {"Chicken Burger", 6.99},
- {"Double Cheeseburger", 8.49}
- };
- MenuItem sushiMenu[] = {
- {"California Roll", 12.99},
- {"Tuna Roll", 13.99},
- {"Salmon Roll", 14.99},
- {"Tempura Roll", 15.49},
- {"Veggie Roll", 10.99}
- };
- // Create restaurants
- Restaurant restaurants[MAX_RESTAURANTS];
- createRestaurant(&restaurants[0], "Pizza Palace", pizzaMenu);
- createRestaurant(&restaurants[1], "Burger Bistro", burgerMenu);
- createRestaurant(&restaurants[2], "Sushi Spot", sushiMenu);
- // Create a customer
- Customer customer = {"John Doe", "123 Elm St, Springfield"};
- // Print restaurant menus
- printf("Welcome to the Food Delivery System!\n");
- for (int i = 0; i < MAX_RESTAURANTS; i++) {
- printf("\n%d. %s\n", i + 1, restaurants[i].name);
- printMenu(restaurants[i]);
- }
- // Customer selects a restaurant and order item
- int restaurantChoice, itemChoice;
- printf("\nSelect a restaurant by number: ");
- scanf("%d", &restaurantChoice);
- printf("Select an item to order (1-%d): ", MAX_MENU_ITEMS);
- scanf("%d", &itemChoice);
- // Place the order
- placeOrder(&customer, &restaurants[restaurantChoice - 1], itemChoice);
- // Simulate delivery process
- Delivery delivery = {"Alice", "Picked up"};
- processDelivery(&delivery);
- strcpy(delivery.status, "Delivered");
- processDelivery(&delivery);
- return 0;
- }
- 11.Book rack allocation system
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_BOOKS 100
- #define MAX_RACKS 10
- #define MAX_BOOKS_PER_RACK 5
- // Structure to represent a book
- typedef struct {
- int bookID;
- char title[100];
- char author[100];
- char genre[50];
- int rackID; // ID of the rack where the book is allocated
- } Book;
- // Structure to represent a rack
- typedef struct {
- int rackID;
- int currentCapacity;
- Book books[MAX_BOOKS_PER_RACK];
- } Rack;
- // Function to initialize a book
- void initBook(Book *book, int bookID, const char *title, const char *author, const char *genre) {
- book->bookID = bookID;
- strcpy(book->title, title);
- strcpy(book->author, author);
- strcpy(book->genre, genre);
- book->rackID = -1; // Book is not yet allocated to any rack
- }
- // Function to initialize a rack
- void initRack(Rack *rack, int rackID) {
- rack->rackID = rackID;
- rack->currentCapacity = 0; // No books initially in the rack
- }
- // Function to allocate a book to a rack
- int allocateBookToRack(Book *book, Rack *racks, int numRacks) {
- for (int i = 0; i < numRacks; i++) {
- if (racks[i].currentCapacity <
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement