Advertisement
class_connect

Lab DS

Jan 1st, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 26.20 KB | None | 0 0
  1. 1.Singly Linked List
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. // Define the structure for a node
  7. struct Node {
  8.     int data;
  9.     struct Node* next;
  10. };
  11.  
  12. // Function to create a new node
  13. struct Node* createNode(int data) {
  14.     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  15.     newNode->data = data;
  16.     newNode->next = NULL;
  17.     return newNode;
  18. }
  19.  
  20. // Function to insert an element into the list
  21. void insertElement(struct Node** head, int data) {
  22.     struct Node* newNode = createNode(data);
  23.     newNode->next = *head;
  24.     *head = newNode;
  25. }
  26.  
  27. // Function to delete the maximum element from the list
  28. void deleteMaxElement(struct Node** head) {
  29.     if (*head == NULL) {
  30.         printf("List is empty.\n");
  31.         return;
  32.     }
  33.  
  34.     struct Node *current = *head, *prev = NULL, *maxNode = *head, *prevMax = NULL;
  35.  
  36.     while (current != NULL) {
  37.         if (current->data > maxNode->data) {
  38.             maxNode = current;
  39.             prevMax = prev;
  40.         }
  41.         prev = current;
  42.         current = current->next;
  43.     }
  44.  
  45.     if (prevMax == NULL) { // Max element is the head
  46.         *head = maxNode->next;
  47.     } else {
  48.         prevMax->next = maxNode->next;
  49.     }
  50.  
  51.     free(maxNode);
  52.     printf("Maximum element deleted.\n");
  53. }
  54.  
  55. // Function to sort the list in ascending order
  56. void sortList(struct Node** head) {
  57.     if (*head == NULL || (*head)->next == NULL) {
  58.         return;
  59.     }
  60.  
  61.     struct Node *i, *j;
  62.     int temp;
  63.  
  64.     for (i = *head; i != NULL; i = i->next) {
  65.         for (j = i->next; j != NULL; j = j->next) {
  66.             if (i->data > j->data) {
  67.                 temp = i->data;
  68.                 i->data = j->data;
  69.                 j->data = temp;
  70.             }
  71.         }
  72.     }
  73.  
  74.     printf("List sorted.\n");
  75. }
  76.  
  77. // Function to display the elements of the list
  78. void displayList(struct Node* head) {
  79.     if (head == NULL) {
  80.         printf("List is empty.\n");
  81.         return;
  82.     }
  83.  
  84.     printf("List elements: ");
  85.     while (head != NULL) {
  86.         printf("%d ", head->data);
  87.         head = head->next;
  88.     }
  89.     printf("\n");
  90. }
  91.  
  92. int main() {
  93.     struct Node* head = NULL;
  94.  
  95.     int choice, value;
  96.     while (1) {
  97.         printf("\nMenu:\n");
  98.         printf("1. Insert an element\n");
  99.         printf("2. Delete the maximum element\n");
  100.         printf("3. Sort the list\n");
  101.         printf("4. Display the list\n");
  102.         printf("5. Exit\n");
  103.         printf("Enter your choice: ");
  104.         scanf("%d", &choice);
  105.  
  106.         switch (choice) {
  107.             case 1:
  108.                 printf("Enter the value to insert: ");
  109.                 scanf("%d", &value);
  110.                 insertElement(&head, value);
  111.                 break;
  112.             case 2:
  113.                 deleteMaxElement(&head);
  114.                 break;
  115.             case 3:
  116.                 sortList(&head);
  117.                 break;
  118.             case 4:
  119.                 displayList(head);
  120.                 break;
  121.             case 5:
  122.                 printf("Exiting program.\n");
  123.                 exit(0);
  124.             default:
  125.                 printf("Invalid choice. Please try again.\n");
  126.         }
  127.     }
  128.  
  129.     return 0;
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. 2.Implement stack and queue using linked list
  140. #include <stdio.h>
  141. #include <stdlib.h>
  142.  
  143. // Define the structure for a node
  144. struct Node {
  145.     int data;
  146.     struct Node* next;
  147. };
  148.  
  149. // Stack Implementation using Linked List
  150. struct Stack {
  151.     struct Node* top;
  152. };
  153.  
  154. void initStack(struct Stack* stack) {
  155.     stack->top = NULL;
  156. }
  157.  
  158. void push(struct Stack* stack, int data) {
  159.     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  160.     newNode->data = data;
  161.     newNode->next = stack->top;
  162.     stack->top = newNode;
  163.     printf("%d pushed to stack.\n", data);
  164. }
  165.  
  166. int pop(struct Stack* stack) {
  167.     if (stack->top == NULL) {
  168.         printf("Stack underflow.\n");
  169.         return -1;
  170.     }
  171.     struct Node* temp = stack->top;
  172.     int popped = temp->data;
  173.     stack->top = stack->top->next;
  174.     free(temp);
  175.     return popped;
  176. }
  177.  
  178. void displayStack(struct Stack* stack) {
  179.     struct Node* temp = stack->top;
  180.     printf("Stack elements: ");
  181.     while (temp != NULL) {
  182.         printf("%d ", temp->data);
  183.         temp = temp->next;
  184.     }
  185.     printf("\n");
  186. }
  187.  
  188. // Queue Implementation using Linked List
  189. struct Queue {
  190.     struct Node* front;
  191.     struct Node* rear;
  192. };
  193.  
  194. void initQueue(struct Queue* queue) {
  195.     queue->front = queue->rear = NULL;
  196. }
  197.  
  198. void enqueue(struct Queue* queue, int data) {
  199.     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  200.     newNode->data = data;
  201.     newNode->next = NULL;
  202.     if (queue->rear == NULL) {
  203.         queue->front = queue->rear = newNode;
  204.         printf("%d enqueued to queue.\n", data);
  205.         return;
  206.     }
  207.     queue->rear->next = newNode;
  208.     queue->rear = newNode;
  209.     printf("%d enqueued to queue.\n", data);
  210. }
  211.  
  212. int dequeue(struct Queue* queue) {
  213.     if (queue->front == NULL) {
  214.         printf("Queue underflow.\n");
  215.         return -1;
  216.     }
  217.     struct Node* temp = queue->front;
  218.     int dequeued = temp->data;
  219.     queue->front = queue->front->next;
  220.     if (queue->front == NULL) {
  221.         queue->rear = NULL;
  222.     }
  223.     free(temp);
  224.     return dequeued;
  225. }
  226.  
  227. void displayQueue(struct Queue* queue) {
  228.     struct Node* temp = queue->front;
  229.     printf("Queue elements: ");
  230.     while (temp != NULL) {
  231.         printf("%d ", temp->data);
  232.         temp = temp->next;
  233.     }
  234.     printf("\n");
  235. }
  236.  
  237. int main() {
  238.     struct Stack stack;
  239.     struct Queue queue;
  240.     initStack(&stack);
  241.     initQueue(&queue);
  242.  
  243.     int choice, value;
  244.     while (1) {
  245.         printf("\nMenu:\n");
  246.         printf("1. Push to stack\n");
  247.         printf("2. Pop from stack\n");
  248.         printf("3. Display stack\n");
  249.         printf("4. Enqueue to queue\n");
  250.         printf("5. Dequeue from queue\n");
  251.         printf("6. Display queue\n");
  252.         printf("7. Exit\n");
  253.         printf("Enter your choice: ");
  254.         scanf("%d", &choice);
  255.  
  256.         switch (choice) {
  257.             case 1:
  258.                 printf("Enter the value to push: ");
  259.                 scanf("%d", &value);
  260.                 push(&stack, value);
  261.                 break;
  262.             case 2:
  263.                 value = pop(&stack);
  264.                 if (value != -1) {
  265.                     printf("Popped value: %d\n", value);
  266.                 }
  267.                 break;
  268.             case 3:
  269.                 displayStack(&stack);
  270.                 break;
  271.             case 4:
  272.                 printf("Enter the value to enqueue: ");
  273.                 scanf("%d", &value);
  274.                 enqueue(&queue, value);
  275.                 break;
  276.             case 5:
  277.                 value = dequeue(&queue);
  278.                 if (value != -1) {
  279.                     printf("Dequeued value: %d\n", value);
  280.                 }
  281.                 break;
  282.             case 6:
  283.                 displayQueue(&queue);
  284.                 break;
  285.             case 7:
  286.                 printf("Exiting program.\n");
  287.                 exit(0);
  288.             default:
  289.                 printf("Invalid choice. Please try again.\n");
  290.         }
  291.     }
  292.  
  293.     return 0;
  294. }
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303. 3.Implementation of quick, heap and shell sort
  304. #include <stdio.h>
  305.  
  306. void quickSort(int arr[], int low, int high) {
  307.     if (low < high) {
  308.         int pivot = arr[high];
  309.         int i = low - 1;
  310.         for (int j = low; j < high; j++) {
  311.             if (arr[j] < pivot) {
  312.                 i++;
  313.                 int temp = arr[i];
  314.                 arr[i] = arr[j];
  315.                 arr[j] = temp;
  316.             }
  317.         }
  318.         int temp = arr[i + 1];
  319.         arr[i + 1] = arr[high];
  320.         arr[high] = temp;
  321.         int pi = i + 1;
  322.  
  323.         quickSort(arr, low, pi - 1);
  324.         quickSort(arr, pi + 1, high);
  325.     }
  326. }
  327.  
  328. int main() {
  329.     int arr[] = {10, 7, 8, 9, 1, 5};
  330.     int n = sizeof(arr) / sizeof(arr[0]);
  331.     quickSort(arr, 0, n - 1);
  332.     printf("Quick Sorted array: ");
  333.     for (int i = 0; i < n; i++) printf("%d ", arr[i]);
  334.     return 0;
  335. }
  336.  
  337.  
  338. #include <stdio.h>
  339.  
  340. void heapify(int arr[], int n, int i) {
  341.     int largest = i;
  342.     int left = 2 * i + 1;
  343.     int right = 2 * i + 2;
  344.  
  345.     if (left < n && arr[left] > arr[largest]) largest = left;
  346.     if (right < n && arr[right] > arr[largest]) largest = right;
  347.  
  348.     if (largest != i) {
  349.         int temp = arr[i];
  350.         arr[i] = arr[largest];
  351.         arr[largest] = temp;
  352.  
  353.         heapify(arr, n, largest);
  354.     }
  355. }
  356.  
  357. void heapSort(int arr[], int n) {
  358.     for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i);
  359.     for (int i = n - 1; i > 0; i--) {
  360.         int temp = arr[0];
  361.         arr[0] = arr[i];
  362.         arr[i] = temp;
  363.  
  364.         heapify(arr, i, 0);
  365.     }
  366. }
  367.  
  368. int main() {
  369.     int arr[] = {12, 11, 13, 5, 6, 7};
  370.     int n = sizeof(arr) / sizeof(arr[0]);
  371.     heapSort(arr, n);
  372.     printf("Heap Sorted array: ");
  373.     for (int i = 0; i < n; i++) printf("%d ", arr[i]);
  374.     return 0;
  375. }
  376.  
  377.  
  378. #include <stdio.h>
  379.  
  380. void shellSort(int arr[], int n) {
  381.     for (int gap = n / 2; gap > 0; gap /= 2) {
  382.         for (int i = gap; i < n; i++) {
  383.             int temp = arr[i];
  384.             int j;
  385.             for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
  386.                 arr[j] = arr[j - gap];
  387.             }
  388.             arr[j] = temp;
  389.         }
  390.     }
  391. }
  392.  
  393. int main() {
  394.     int arr[] = {12, 34, 54, 2, 3};
  395.     int n = sizeof(arr) / sizeof(arr[0]);
  396.     shellSort(arr, n);
  397.     printf("Shell Sorted array: ");
  398.     for (int i = 0; i < n; i++) printf("%d ", arr[i]);
  399.     return 0;
  400. }
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410. 4.Selection and Bubble sort
  411. #include <stdio.h>
  412.  
  413. void selectionSort(int arr[], int n) {
  414.     for (int i = 0; i < n - 1; i++) {
  415.         int minIndex = i;
  416.         for (int j = i + 1; j < n; j++) {
  417.             if (arr[j] < arr[minIndex]) {
  418.                 minIndex = j;
  419.             }
  420.         }
  421.         int temp = arr[minIndex];
  422.         arr[minIndex] = arr[i];
  423.         arr[i] = temp;
  424.     }
  425. }
  426.  
  427. int main() {
  428.     int arr[] = {64, 25, 12, 22, 11};
  429.     int n = sizeof(arr) / sizeof(arr[0]);
  430.  
  431.     selectionSort(arr, n);
  432.  
  433.     printf("Selection Sorted array: ");
  434.     for (int i = 0; i < n; i++) {
  435.         printf("%d ", arr[i]);
  436.     }
  437.     return 0;
  438. }
  439.  
  440.  
  441. #include <stdio.h>
  442.  
  443. void bubbleSort(int arr[], int n) {
  444.     for (int i = 0; i < n - 1; i++) {
  445.         for (int j = 0; j < n - i - 1; j++) {
  446.             if (arr[j] > arr[j + 1]) {
  447.                 int temp = arr[j];
  448.                 arr[j] = arr[j + 1];
  449.                 arr[j + 1] = temp;
  450.             }
  451.         }
  452.     }
  453. }
  454.  
  455. int main() {
  456.     int arr[] = {5, 1, 4, 2, 8};
  457.     int n = sizeof(arr) / sizeof(arr[0]);
  458.  
  459.     bubbleSort(arr, n);
  460.  
  461.     printf("Bubble Sorted array: ");
  462.     for (int i = 0; i < n; i++) {
  463.         printf("%d ", arr[i]);
  464.     }
  465.     return 0;
  466. }
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476. 5.Implementation of hashing technique
  477. #include <stdio.h>
  478. #include <stdlib.h>
  479. #define TABLE_SIZE 10
  480.  
  481. // Define the structure for a node in the linked list
  482. struct Node {
  483.     int data;
  484.     struct Node* next;
  485. };
  486.  
  487. // Define the hash table as an array of pointers to linked lists
  488. struct Node* hashTable[TABLE_SIZE];
  489.  
  490. // Function to initialize the hash table
  491. void initializeHashTable() {
  492.     for (int i = 0; i < TABLE_SIZE; i++) {
  493.         hashTable[i] = NULL;
  494.     }
  495. }
  496.  
  497. // Hash function to compute the index
  498. int hashFunction(int key) {
  499.     return key % TABLE_SIZE;
  500. }
  501.  
  502. // Function to insert an element into the hash table
  503. void insert(int key) {
  504.     int index = hashFunction(key);
  505.     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  506.     newNode->data = key;
  507.     newNode->next = hashTable[index];
  508.     hashTable[index] = newNode;
  509. }
  510.  
  511. // Function to search for an element in the hash table
  512. int search(int key) {
  513.     int index = hashFunction(key);
  514.     struct Node* temp = hashTable[index];
  515.     while (temp != NULL) {
  516.         if (temp->data == key) {
  517.             return 1; // Element found
  518.         }
  519.         temp = temp->next;
  520.     }
  521.     return 0; // Element not found
  522. }
  523.  
  524. // Function to delete an element from the hash table
  525. void deleteElement(int key) {
  526.     int index = hashFunction(key);
  527.     struct Node* temp = hashTable[index];
  528.     struct Node* prev = NULL;
  529.  
  530.     while (temp != NULL) {
  531.         if (temp->data == key) {
  532.             if (prev == NULL) {
  533.                 hashTable[index] = temp->next; // Delete the head node
  534.             } else {
  535.                 prev->next = temp->next;
  536.             }
  537.             free(temp);
  538.             printf("Element %d deleted.\n", key);
  539.             return;
  540.         }
  541.         prev = temp;
  542.         temp = temp->next;
  543.     }
  544.     printf("Element %d not found.\n", key);
  545. }
  546.  
  547. // Function to display the hash table
  548. void displayHashTable() {
  549.     for (int i = 0; i < TABLE_SIZE; i++) {
  550.         printf("Bucket %d: ", i);
  551.         struct Node* temp = hashTable[i];
  552.         while (temp != NULL) {
  553.             printf("%d -> ", temp->data);
  554.             temp = temp->next;
  555.         }
  556.         printf("NULL\n");
  557.     }
  558. }
  559.  
  560. // Main function to demonstrate the hashing technique
  561. int main() {
  562.     initializeHashTable();
  563.  
  564.     int choice, value;
  565.     while (1) {
  566.         printf("\nMenu:\n");
  567.         printf("1. Insert an element\n");
  568.         printf("2. Search for an element\n");
  569.         printf("3. Delete an element\n");
  570.         printf("4. Display the hash table\n");
  571.         printf("5. Exit\n");
  572.         printf("Enter your choice: ");
  573.         scanf("%d", &choice);
  574.  
  575.         switch (choice) {
  576.             case 1:
  577.                 printf("Enter the value to insert: ");
  578.                 scanf("%d", &value);
  579.                 insert(value);
  580.                 break;
  581.             case 2:
  582.                 printf("Enter the value to search: ");
  583.                 scanf("%d", &value);
  584.                 if (search(value)) {
  585.                     printf("Element %d found in the hash table.\n", value);
  586.                 } else {
  587.                     printf("Element %d not found in the hash table.\n", value);
  588.                 }
  589.                 break;
  590.             case 3:
  591.                 printf("Enter the value to delete: ");
  592.                 scanf("%d", &value);
  593.                 deleteElement(value);
  594.                 break;
  595.             case 4:
  596.                 displayHashTable();
  597.                 break;
  598.             case 5:
  599.                 printf("Exiting program.\n");
  600.                 exit(0);
  601.             default:
  602.                 printf("Invalid choice. Please try again.\n");
  603.         }
  604.     }
  605.  
  606.     return 0;
  607. }
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616. 6.Program for linear and binary search
  617. #include <stdio.h>
  618.  
  619. int linear_search(int arr[], int size, int target) {
  620.     for (int i = 0; i < size; i++) {
  621.         if (arr[i] == target) {
  622.             return i;  // Return the index where the target is found
  623.         }
  624.     }
  625.     return -1;  // Return -1 if target is not found
  626. }
  627.  
  628. int main() {
  629.     int arr[] = {10, 20, 30, 40, 50};
  630.     int size = sizeof(arr) / sizeof(arr[0]);
  631.     int target = 30;
  632.  
  633.     int result = linear_search(arr, size, target);
  634.     if (result != -1) {
  635.         printf("Target found at index: %d\n", result);
  636.     } else {
  637.         printf("Target not found\n");
  638.     }
  639.     return 0;
  640. }
  641.  
  642.  
  643. #include <stdio.h>
  644.  
  645. int binary_search(int arr[], int size, int target) {
  646.     int low = 0;
  647.     int high = size - 1;
  648.    
  649.     while (low <= high) {
  650.         int mid = low + (high - low) / 2;  // Find the middle index
  651.        
  652.         if (arr[mid] == target) {
  653.             return mid;  // Return the index where the target is found
  654.         }
  655.         if (arr[mid] < target) {
  656.             low = mid + 1;  // Eliminate the left half
  657.         } else {
  658.             high = mid - 1;  // Eliminate the right half
  659.         }
  660.     }
  661.    
  662.     return -1;  // Return -1 if target is not found
  663. }
  664.  
  665. int main() {
  666.     int arr[] = {10, 20, 30, 40, 50};
  667.     int size = sizeof(arr) / sizeof(arr[0]);
  668.     int target = 30;
  669.  
  670.     int result = binary_search(arr, size, target);
  671.     if (result != -1) {
  672.         printf("Target found at index: %d\n", result);
  673.     } else {
  674.         printf("Target not found\n");
  675.     }
  676.     return 0;
  677. }
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686. 7.Tree traversal methods
  687. #include <stdio.h>
  688. #include <stdlib.h>
  689.  
  690. // Definition of a binary tree node
  691. struct Node {
  692.     int data;
  693.     struct Node* left;
  694.     struct Node* right;
  695. };
  696.  
  697. // Function to create a new node
  698. struct Node* newNode(int data) {
  699.     struct Node* node = (struct Node*)malloc(sizeof(struct Node));
  700.     node->data = data;
  701.     node->left = node->right = NULL;
  702.     return node;
  703. }
  704. void inorder(struct Node* root) {
  705.     if (root == NULL) {
  706.         return;
  707.     }
  708.     inorder(root->left);  // Visit left subtree
  709.     printf("%d ", root->data);  // Visit root node
  710.     inorder(root->right);  // Visit right subtree
  711. }
  712. void preorder(struct Node* root) {
  713.     if (root == NULL) {
  714.         return;
  715.     }
  716.     printf("%d ", root->data);  // Visit root node
  717.     preorder(root->left);  // Visit left subtree
  718.     preorder(root->right);  // Visit right subtree
  719. }
  720. void postorder(struct Node* root) {
  721.     if (root == NULL) {
  722.         return;
  723.     }
  724.     postorder(root->left);  // Visit left subtree
  725.     postorder(root->right);  // Visit right subtree
  726.     printf("%d ", root->data);  // Visit root node
  727. }
  728. int main() {
  729.     // Create the binary tree
  730.     struct Node* root = newNode(1);
  731.     root->left = newNode(2);
  732.     root->right = newNode(3);
  733.     root->left->left = newNode(4);
  734.     root->left->right = newNode(5);
  735.     root->right->left = newNode(6);
  736.     root->right->right = newNode(7);
  737.  
  738.     printf("In-order Traversal: ");
  739.     inorder(root);
  740.     printf("\n");
  741.  
  742.     printf("Pre-order Traversal: ");
  743.     preorder(root);
  744.     printf("\n");
  745.  
  746.     printf("Post-order Traversal: ");
  747.     postorder(root);
  748.     printf("\n");
  749.  
  750.     return 0;
  751. }
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762. 8.Implementation of prims algorithm
  763. #include <stdio.h>
  764. #include <limits.h>
  765.  
  766. #define V 5  // Number of vertices
  767.  
  768. // Function to find the vertex with the minimum key value
  769. int minKey(int key[], int mstSet[]) {
  770.     int min = INT_MAX, minIndex;
  771.  
  772.     for (int v = 0; v < V; v++) {
  773.         if (mstSet[v] == 0 && key[v] < min) {
  774.             min = key[v];
  775.             minIndex = v;
  776.         }
  777.     }
  778.     return minIndex;
  779. }
  780.  
  781. // Function to print the MST constructed
  782. void printMST(int parent[], int graph[V][V]) {
  783.     printf("Edge \tWeight\n");
  784.     for (int i = 1; i < V; i++) {
  785.         printf("%d - %d \t%d\n", parent[i], i, graph[i][parent[i]]);
  786.     }
  787. }
  788.  
  789. // Function to implement Prim's algorithm to find the MST
  790. void primMST(int graph[V][V]) {
  791.     int parent[V];  // Array to store the constructed MST
  792.     int key[V];     // Key values used to pick minimum weight edge
  793.     int mstSet[V];  // To represent vertices not yet included in MST
  794.  
  795.     // Initialize all keys as INFINITE and mstSet[] as false
  796.     for (int i = 0; i < V; i++) {
  797.         key[i] = INT_MAX;
  798.         mstSet[i] = 0;
  799.     }
  800.  
  801.     // Always include the first vertex in the MST
  802.     key[0] = 0;  // Make the key value of the first vertex 0
  803.     parent[0] = -1;  // The first node is always the root
  804.  
  805.     // The MST will have V vertices
  806.     for (int count = 0; count < V - 1; count++) {
  807.         // Pick the minimum key vertex from the set of vertices not yet included in MST
  808.         int u = minKey(key, mstSet);
  809.  
  810.         // Add the picked vertex to the MST set
  811.         mstSet[u] = 1;
  812.  
  813.         // Update the key values and parent index of the adjacent vertices of the picked vertex
  814.         for (int v = 0; v < V; v++) {
  815.             // graph[u][v] is non-zero only for adjacent vertices of u
  816.             // mstSet[v] is false for vertices not yet included in MST
  817.             // Update the key value of v
  818.             if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v]) {
  819.                 key[v] = graph[u][v];
  820.                 parent[v] = u;
  821.             }
  822.         }
  823.     }
  824.  
  825.     // Print the constructed MST
  826.     printMST(parent, graph);
  827. }
  828.  
  829. int main() {
  830.     // Graph represented as an adjacency matrix
  831.     int graph[V][V] = {
  832.         {0, 2, 0, 6, 0},
  833.         {2, 0, 3, 8, 5},
  834.         {0, 3, 0, 0, 7},
  835.         {6, 8, 0, 0, 9},
  836.         {0, 5, 7, 9, 0}
  837.     };
  838.  
  839.     // Function call to find and print the MST
  840.     primMST(graph);
  841.  
  842.     return 0;
  843. }
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852. 9.Banking token system
  853. #include <stdio.h>
  854. #include <stdlib.h>
  855. #include <string.h>
  856. #include <time.h>
  857.  
  858. // Sample token vault (this could be a secure database in a real system)
  859. #define VAULT_SIZE 10
  860. char* token_vault[VAULT_SIZE];
  861.  
  862. // Function to generate a random token
  863. char* generate_token() {
  864.     static const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  865.     size_t length = 16;
  866.     char* token = (char*)malloc(length + 1);
  867.    
  868.     for (size_t i = 0; i < length; i++) {
  869.         token[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
  870.     }
  871.     token[length] = '\0';
  872.    
  873.     return token;
  874. }
  875.  
  876. // Function to simulate tokenization
  877. void tokenize_data(char* sensitive_data) {
  878.     // Generate a token for the sensitive data
  879.     char* token = generate_token();
  880.    
  881.     // Store the token in the vault
  882.     for (int i = 0; i < VAULT_SIZE; i++) {
  883.         if (token_vault[i] == NULL) {
  884.             token_vault[i] = token;
  885.             break;
  886.         }
  887.     }
  888.  
  889.     printf("Sensitive Data: %s\n", sensitive_data);
  890.     printf("Token Generated: %s\n", token);
  891. }
  892.  
  893. // Main function to test tokenization
  894. int main() {
  895.     srand(time(NULL));  // Seed the random number generator
  896.  
  897.     // Simulating sensitive data (e.g., bank account number)
  898.     char sensitive_data[] = "1234567890";
  899.    
  900.     // Tokenize sensitive data
  901.     tokenize_data(sensitive_data);
  902.    
  903.     return 0;
  904. }
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913. 10.Food delivery system
  914. #include <stdio.h>
  915. #include <stdlib.h>
  916. #include <string.h>
  917.  
  918. #define MAX_RESTAURANTS 3
  919. #define MAX_MENU_ITEMS 5
  920.  
  921. // Structure for menu items
  922. typedef struct {
  923.     char name[50];
  924.     float price;
  925. } MenuItem;
  926.  
  927. // Structure for restaurant
  928. typedef struct {
  929.     char name[50];
  930.     MenuItem menu[MAX_MENU_ITEMS];
  931. } Restaurant;
  932.  
  933. // Structure for customer
  934. typedef struct {
  935.     char name[50];
  936.     char address[100];
  937. } Customer;
  938.  
  939. // Structure for delivery
  940. typedef struct {
  941.     char driverName[50];
  942.     char status[50]; // E.g., "Picked up", "Delivered"
  943. } Delivery;
  944.  
  945. // Function to print restaurant menu
  946. void printMenu(Restaurant restaurant) {
  947.     printf("\nMenu for %s:\n", restaurant.name);
  948.     for (int i = 0; i < MAX_MENU_ITEMS; i++) {
  949.         printf("%d. %s - $%.2f\n", i + 1, restaurant.menu[i].name, restaurant.menu[i].price);
  950.     }
  951. }
  952.  
  953. // Function to create a restaurant with a menu
  954. void createRestaurant(Restaurant* restaurant, const char* name, MenuItem menu[]) {
  955.     strcpy(restaurant->name, name);
  956.     for (int i = 0; i < MAX_MENU_ITEMS; i++) {
  957.         restaurant->menu[i] = menu[i];
  958.     }
  959. }
  960.  
  961. // Function to place an order
  962. void placeOrder(Customer* customer, Restaurant* restaurant, int itemIndex) {
  963.     printf("\n%s placed an order for %s.\n", customer->name, restaurant->menu[itemIndex - 1].name);
  964.     printf("Delivery will be made to %s\n", customer->address);
  965. }
  966.  
  967. // Function to simulate delivery
  968. void processDelivery(Delivery* delivery) {
  969.     printf("\nDelivery in progress...\n");
  970.     printf("Driver: %s\n", delivery->driverName);
  971.     printf("Status: %s\n", delivery->status);
  972.     printf("Food Delivered!\n");
  973. }
  974.  
  975. int main() {
  976.     // Initialize menu items
  977.     MenuItem pizzaMenu[] = {
  978.         {"Margherita Pizza", 8.99},
  979.         {"Pepperoni Pizza", 9.99},
  980.         {"Veggie Pizza", 7.99},
  981.         {"BBQ Chicken Pizza", 10.99},
  982.         {"Cheese Pizza", 6.99}
  983.     };
  984.  
  985.     MenuItem burgerMenu[] = {
  986.         {"Cheeseburger", 5.99},
  987.         {"Veggie Burger", 6.49},
  988.         {"BBQ Burger", 7.99},
  989.         {"Chicken Burger", 6.99},
  990.         {"Double Cheeseburger", 8.49}
  991.     };
  992.  
  993.     MenuItem sushiMenu[] = {
  994.         {"California Roll", 12.99},
  995.         {"Tuna Roll", 13.99},
  996.         {"Salmon Roll", 14.99},
  997.         {"Tempura Roll", 15.49},
  998.         {"Veggie Roll", 10.99}
  999.     };
  1000.  
  1001.     // Create restaurants
  1002.     Restaurant restaurants[MAX_RESTAURANTS];
  1003.     createRestaurant(&restaurants[0], "Pizza Palace", pizzaMenu);
  1004.     createRestaurant(&restaurants[1], "Burger Bistro", burgerMenu);
  1005.     createRestaurant(&restaurants[2], "Sushi Spot", sushiMenu);
  1006.  
  1007.     // Create a customer
  1008.     Customer customer = {"John Doe", "123 Elm St, Springfield"};
  1009.  
  1010.     // Print restaurant menus
  1011.     printf("Welcome to the Food Delivery System!\n");
  1012.     for (int i = 0; i < MAX_RESTAURANTS; i++) {
  1013.         printf("\n%d. %s\n", i + 1, restaurants[i].name);
  1014.         printMenu(restaurants[i]);
  1015.     }
  1016.  
  1017.     // Customer selects a restaurant and order item
  1018.     int restaurantChoice, itemChoice;
  1019.     printf("\nSelect a restaurant by number: ");
  1020.     scanf("%d", &restaurantChoice);
  1021.     printf("Select an item to order (1-%d): ", MAX_MENU_ITEMS);
  1022.     scanf("%d", &itemChoice);
  1023.  
  1024.     // Place the order
  1025.     placeOrder(&customer, &restaurants[restaurantChoice - 1], itemChoice);
  1026.  
  1027.     // Simulate delivery process
  1028.     Delivery delivery = {"Alice", "Picked up"};
  1029.     processDelivery(&delivery);
  1030.     strcpy(delivery.status, "Delivered");
  1031.     processDelivery(&delivery);
  1032.  
  1033.     return 0;
  1034. }
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043. 11.Book rack allocation system
  1044. #include <stdio.h>
  1045. #include <stdlib.h>
  1046. #include <string.h>
  1047.  
  1048. #define MAX_BOOKS 100
  1049. #define MAX_RACKS 10
  1050. #define MAX_BOOKS_PER_RACK 5
  1051.  
  1052. // Structure to represent a book
  1053. typedef struct {
  1054.     int bookID;
  1055.     char title[100];
  1056.     char author[100];
  1057.     char genre[50];
  1058.     int rackID;  // ID of the rack where the book is allocated
  1059. } Book;
  1060.  
  1061. // Structure to represent a rack
  1062. typedef struct {
  1063.     int rackID;
  1064.     int currentCapacity;
  1065.     Book books[MAX_BOOKS_PER_RACK];
  1066. } Rack;
  1067.  
  1068. // Function to initialize a book
  1069. void initBook(Book *book, int bookID, const char *title, const char *author, const char *genre) {
  1070.     book->bookID = bookID;
  1071.     strcpy(book->title, title);
  1072.     strcpy(book->author, author);
  1073.     strcpy(book->genre, genre);
  1074.     book->rackID = -1;  // Book is not yet allocated to any rack
  1075. }
  1076.  
  1077. // Function to initialize a rack
  1078. void initRack(Rack *rack, int rackID) {
  1079.     rack->rackID = rackID;
  1080.     rack->currentCapacity = 0;  // No books initially in the rack
  1081. }
  1082.  
  1083. // Function to allocate a book to a rack
  1084. int allocateBookToRack(Book *book, Rack *racks, int numRacks) {
  1085.     for (int i = 0; i < numRacks; i++) {
  1086.         if (racks[i].currentCapacity <
  1087.  
Tags: Lab DS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement