From d9c9c636b3aec706aa2c8b9f3a0882923a81f5ab Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:08:19 +0530 Subject: [PATCH 01/13] Create rivest_cipher.c --- algorithms/ciphers/rivest_cipher.c | 79 ++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 algorithms/ciphers/rivest_cipher.c diff --git a/algorithms/ciphers/rivest_cipher.c b/algorithms/ciphers/rivest_cipher.c new file mode 100644 index 00000000..a0209b07 --- /dev/null +++ b/algorithms/ciphers/rivest_cipher.c @@ -0,0 +1,79 @@ +/* +C code provided is an implementation of the RC4 algorithm, which is a widely used symmetric stream cipher. Here's what the code does: + +Initialization: The code initializes the RC4 cipher with a secret key (key) and a plaintext message (plaintext) that you want to encrypt. It also creates arrays for storing the ciphertext and decrypted text. + +Key Scheduling: The code performs key scheduling to initialize two arrays, S and T, of 256 bytes each. The key is used to create T, which is repeated to match the length of the key. The S array is initially filled with values from 0 to 255. + +Key Mixing: The key and plaintext are mixed with the S array by performing a series of swaps. This process randomizes the S array based on the key and ensures that the key is well-distributed throughout the array. + +Encryption: The code enters a loop to generate the keystream and use it to encrypt the plaintext. For each character in the plaintext, it generates a pseudo-random value (rnd) from the S array and XORs it with the corresponding character in the plaintext to produce the ciphertext. + +Decryption: The code includes a rc4_decrypt function, which is essentially the same as the encryption process. This is because RC4 is a symmetric cipher, meaning that the same process is used for both encryption and decryption. To decrypt, you can simply call rc4_decrypt with the same key and the ciphertext. + +Printing: Finally, the code prints the original message, the encrypted message (in hexadecimal format), and the decrypted message to the console. +*/ + + +#include +#include + +void rc4_encrypt(unsigned char *key, unsigned char *plaintext, unsigned char *ciphertext) { + unsigned char S[256]; + unsigned char T[256]; + int keylen = strlen((char *)key); + int plaintextlen = strlen((char *)plaintext); + int i, j = 0, k = 0; + + for (i = 0; i < 256; i++) { + S[i] = i; + T[i] = key[i % keylen]; + } + + for (i = 0; i < 256; i++) { + j = (j + S[i] + T[i]) % 256; + // Swap S[i] and S[j] + unsigned char temp = S[i]; + S[i] = S[j]; + S[j] = temp; + } + + i = 0; + j = 0; + + for (int m = 0; m < plaintextlen; m++) { + i = (i + 1) % 256; + j = (j + S[i]) % 256; + // Swap S[i] and S[j] + unsigned char temp = S[i]; + S[i] = S[j]; + S[j] = temp; + + int rnd = S[(S[i] + S[j]) % 256]; + ciphertext[m] = plaintext[m] ^ rnd; + } +} + +void rc4_decrypt(unsigned char *key, unsigned char *ciphertext, unsigned char *plaintext) { + rc4_encrypt(key, ciphertext, plaintext); // RC4 decryption is the same as encryption +} + +int main() { + unsigned char key[] = "SecretKey"; + unsigned char plaintext[] = "Hello, RC4!"; + unsigned char ciphertext[12]; + unsigned char decryptedtext[12]; + + rc4_encrypt(key, plaintext, ciphertext); + rc4_decrypt(key, ciphertext, decryptedtext); + + printf("Original message: %s\n", plaintext); + printf("Encrypted message: "); + for (int i = 0; i < 12; i++) { + printf("%02X ", ciphertext[i]); + } + printf("\n"); + printf("Decrypted message: %s\n", decryptedtext); + + return 0; +} From c74a0761728e4ef0ab6859f6ca978ed0eb488477 Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:10:49 +0530 Subject: [PATCH 02/13] Create hill_cipher.c --- algorithms/ciphers/hill_cipher.c | 92 ++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 algorithms/ciphers/hill_cipher.c diff --git a/algorithms/ciphers/hill_cipher.c b/algorithms/ciphers/hill_cipher.c new file mode 100644 index 00000000..96717291 --- /dev/null +++ b/algorithms/ciphers/hill_cipher.c @@ -0,0 +1,92 @@ +/* +C code provided is an implementation of the RC4 algorithm, which is a widely used symmetric stream cipher. Here's what the code does: + +Initialization: The code initializes the RC4 cipher with a secret key (key) and a plaintext message (plaintext) that you want to encrypt. It also creates arrays for storing the ciphertext and decrypted text. + +Key Scheduling: The code performs key scheduling to initialize two arrays, S and T, of 256 bytes each. The key is used to create T, which is repeated to match the length of the key. The S array is initially filled with values from 0 to 255. + +Key Mixing: The key and plaintext are mixed with the S array by performing a series of swaps. This process randomizes the S array based on the key and ensures that the key is well-distributed throughout the array. + +Encryption: The code enters a loop to generate the keystream and use it to encrypt the plaintext. For each character in the plaintext, it generates a pseudo-random value (rnd) from the S array and XORs it with the corresponding character in the plaintext to produce the ciphertext. + +Decryption: The code includes a rc4_decrypt function, which is essentially the same as the encryption process. This is because RC4 is a symmetric cipher, meaning that the same process is used for both encryption and decryption. To decrypt, you can simply call rc4_decrypt with the same key and the ciphertext. + +Printing: Finally, the code prints the original message, the encrypted message (in hexadecimal format), and the decrypted message to the console. +*/ + + +#include +#include + +#define MOD 26 // Modulus for the English alphabet + +// Function to calculate the greatest common divisor (GCD) of two numbers +int gcd(int a, int b) { + if (b == 0) { + return a; + } + return gcd(b, a % b); +} + +// Function to find the modular multiplicative inverse of a number +int modInverse(int a, int m) { + for (int x = 1; x < m; x++) { + if ((a * x) % m == 1) { + return x; + } + } + return -1; // Inverse doesn't exist +} + +// Function to encrypt a message using a Hill cipher +void hillCipherEncrypt(char* message, int keyMatrix[3][3], int keySize) { + int i, j, k, msgLen; + int block[keySize]; + + // Calculate the message length + for (msgLen = 0; message[msgLen] != '\0'; msgLen++); + + // Pad the message with 'X' to make it evenly divisible by the key size + while (msgLen % keySize != 0) { + message[msgLen] = 'X'; + msgLen++; + } + + // Perform encryption for each block of characters + for (i = 0; i < msgLen; i += keySize) { + // Convert the block to numerical values (A=0, B=1, ..., Z=25) + for (j = 0; j < keySize; j++) { + block[j] = message[i + j] - 'A'; + } + + // Encrypt the block using the key matrix + for (j = 0; j < keySize; j++) { + message[i + j] = 'A' + (char)((block[0] * keyMatrix[j][0] + block[1] * keyMatrix[j][1] + block[2] * keyMatrix[j][2]) % MOD); + } + } +} + +int main() { + char message[] = "HELLOHILL"; // Message to be encrypted + int keyMatrix[3][3] = {{6, 24, 1}, {13, 16, 10}, {20, 17, 15}}; // Encryption key + int keySize = 3; // Size of the encryption key + + // Check if the key matrix is valid (determinant should be co-prime with 26) + int det = keyMatrix[0][0] * (keyMatrix[1][1] * keyMatrix[2][2] - keyMatrix[2][1] * keyMatrix[1][2]) - + keyMatrix[0][1] * (keyMatrix[1][0] * keyMatrix[2][2] - keyMatrix[2][0] * keyMatrix[1][2]) + + keyMatrix[0][2] * (keyMatrix[1][0] * keyMatrix[2][1] - keyMatrix[2][0] * keyMatrix[1][1]); + det = (det % MOD + MOD) % MOD; // Ensure the determinant is positive + + if (gcd(det, MOD) != 1) { + printf("The key matrix is not valid for Hill cipher encryption.\n"); + return 1; + } + + // Encrypt the message + hillCipherEncrypt(message, keyMatrix, keySize); + + // Print the encrypted message + printf("Encrypted message: %s\n", message); + + return 0; +} From 5572da577d78c6eeb72d766c96ea14b9c29bd9a2 Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:12:50 +0530 Subject: [PATCH 03/13] Create linked_list_operations.c --- .../linked-list/linked_list_operations.c | 452 ++++++++++++++++++ 1 file changed, 452 insertions(+) create mode 100644 algorithms/linked-list/linked_list_operations.c diff --git a/algorithms/linked-list/linked_list_operations.c b/algorithms/linked-list/linked_list_operations.c new file mode 100644 index 00000000..63bf8f42 --- /dev/null +++ b/algorithms/linked-list/linked_list_operations.c @@ -0,0 +1,452 @@ +#include +#include + +// Define a singly linked list structure +struct sll { + int data; + struct sll *next; +}; + +// Function prototypes +void createList(struct sll **); +void traversal(struct sll *); +void insert(struct sll *); +int search(struct sll *, int); +void sort(struct sll **); +void reverse(struct sll **); +int count(struct sll *); +void deleteAfter(struct sll *); +void deleteBefore(struct sll **, int); +void deleteAtPosition(struct sll **, int); +void deleteAfterNode(struct sll **, int); +void insertAfter(struct sll *, int, int); +void insertBefore(struct sll **, int, int); +void appendList(struct sll **, struct sll *); + +int main() { + struct sll *node = NULL; // Initialize the main linked list + + // Create and display List 1 + printf("Create List 1:\n"); + createList(&node); + printf("List 1:\n"); + traversal(node); + + int choice; + do { + // Display the main menu + printf("\nMenu:\n"); + printf("1. Insert\n"); + printf("2. Search\n"); + printf("3. Sort\n"); + printf("4. Reverse\n"); + printf("5. Delete After\n"); + printf("6. Delete Before\n"); + printf("7. Delete at Position\n"); + printf("8. Delete After Node\n"); + printf("9. Insert After\n"); + printf("10. Insert Before\n"); + printf("11. Append List\n"); + printf("12. Count\n"); + printf("13. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + insert(node); + break; + case 2: + // Search for an element + { + int target; + printf("Enter the element to search: "); + scanf("%d", &target); + int result = search(node, target); + if (result != -1) { + printf("%d found at position %d\n", target, result); + } else { + printf("%d not found in the list\n", target); + } + } + break; + case 3: + // Sort the list + sort(&node); + printf("List sorted in ascending order:\n"); + traversal(node); + break; + case 4: + // Reverse the list + reverse(&node); + printf("List reversed:\n"); + traversal(node); + break; + case 5: + // Delete after a node + deleteAfter(node); + break; + case 6: + // Delete before a node + { + int target; + printf("Enter the element before which to delete: "); + scanf("%d", &target); + deleteBefore(&node, target); + } + break; + case 7: + // Delete at a position + { + int position; + printf("Enter the position at which to delete: "); + scanf("%d", &position); + deleteAtPosition(&node, position); + } + break; + case 8: + // Delete after a specific node value + { + int data; + printf("Enter the data of the node after which to delete: "); + scanf("%d", &data); + deleteAfterNode(&node, data); + } + break; + case 9: + // Insert after a specific node value + { + int data, newData; + printf("Enter the data of the node after which to insert: "); + scanf("%d", &data); + printf("Enter the data to insert: "); + scanf("%d", &newData); + insertAfter(node, data, newData); + } + break; + case 10: + // Insert before a specific node value + { + int data, newData; + printf("Enter the data of the node before which to insert: "); + scanf("%d", &data); + printf("Enter the data to insert: "); + scanf("%d", &newData); + insertBefore(&node, data, newData); + } + break; + case 11: + // Create and append another list + { + printf("Create List to Append:\n"); + struct sll *node2 = NULL; + createList(&node2); + printf("List to Append:\n"); + traversal(node2); + + printf("Appending List to List 1...\n"); + appendList(&node, node2); + printf("Combined List:\n"); + traversal(node); + } + break; + case 12: + // Count the number of elements in the list + printf("Number of elements in the list: %d\n", count(node)); + break; + case 13: + printf("Exiting...\n"); + break; + default: + printf("Invalid choice, please try again.\n"); + } + } while (choice != 13); + + return 0; +} + +// Function to create a linked list +void createList(struct sll **node) { + char ch; + int i = 1; + + *node = (struct sll *)malloc(sizeof(struct sll)); + if (*node == NULL) { + printf("\nMemory is not allocated\n"); + exit(0); + } + + struct sll *current = *node; + // Read the data of the first node + printf("Enter the data of %d node: ", i); + scanf("%d", ¤t->data); + current->next = NULL; + i++; + while ((ch = getchar()) != '\n'); // Clear the newline from input buffer + + do { + current->next = (struct sll *)malloc(sizeof(struct sll)); + if (current->next == NULL) { + printf("\nMemory is not allocated "); + exit(0); + } + current = current->next; + printf("\nEnter the data of the %d node: ", i); // second node + scanf("%d", ¤t->data); + current->next = NULL; + i++; + printf("Would you like to continue? (y/n): "); + while ((ch = getchar()) != '\n'); // Clear the newline from input buffer + ch = getchar(); // Read the user's choice + } while (ch == 'y' || ch == 'Y'); // Continue while user enters 'y' or 'Y' +} + +// Function to traverse and print the linked list +void traversal(struct sll *node) { + printf("Linked List: "); + while (node != NULL) { + printf("\t%d\t", node->data); + node = node->next; + } + printf("\n"); +} + +// Function to insert a new node at the end of the linked list +void insert(struct sll *node) { + struct sll *newNode; + newNode = (struct sll *)malloc(sizeof(struct sll)); + if (newNode == NULL) { + printf("\nMemory is not allocated"); + exit(0); + } + printf("\nEnter the data of the new node: "); + scanf("%d", &newNode->data); + newNode->next = NULL; + while (node->next != NULL) { + node = node->next; + } + node->next = newNode; +} + +// Function to search for an element in the linked list +int search(struct sll *node, int target) { + int position = 1; + while (node != NULL) { + if (node->data == target) { + return position; + } + node = node->next; + position++; + } + return -1; +} + +// Function to sort the linked list in ascending order +void sort(struct sll **head) { + if (*head == NULL || (*head)->next == NULL) { + return; + } + + int swapped; + struct sll *ptr1; + struct sll *lptr = NULL; + + do { + swapped = 0; + ptr1 = *head; + + while (ptr1->next != lptr) { + if (ptr1->data > ptr1->next->data) { + // Swap data values + int temp = ptr1->data; + ptr1->data = ptr1->next->data; + ptr1->next->data = temp; + swapped = 1; + } + ptr1 = ptr1->next; + } + lptr = ptr1; + } while (swapped); +} + +// Function to reverse the linked list +void reverse(struct sll **head) { + struct sll *prev = NULL; + struct sll *current = *head; + struct sll *next; + + while (current != NULL) { + next = current->next; + current->next = prev; + prev = current; + current = next; + } + + *head = prev; +} + +// Function to count the number of elements in the linked list +int count(struct sll *node) { + int count = 0; + while (node != NULL) { + count++; + node = node->next; + } + return count; +} + +// Function to delete a node after the current node +void deleteAfter(struct sll *node) { + if (node == NULL || node->next == NULL) { + printf("Cannot delete. List is empty or the last node.\n"); + return; + } + + struct sll *temp = node->next; + node->next = temp->next; + free(temp); +} + +// Function to delete a node before a specific target node value +void deleteBefore(struct sll **head, int target) { + if (*head == NULL || (*head)->next == NULL) { + printf("Cannot delete. List is empty or has only one element.\n"); + return; + } + + if ((*head)->data == target) { + printf("Cannot delete before the first node.\n"); + return; + } + + struct sll *current = *head; + while (current->next != NULL && current->next->data != target) { + current = current->next; + } + + if (current->next == NULL) { + printf("Element %d not found in the list.\n", target); + return; + } + + struct sll *temp = current->next; + current->next = temp->next; + free(temp); +} + +// Function to delete a node at a specific position +void deleteAtPosition(struct sll **head, int position) { + if (*head == NULL) { + printf("Cannot delete. List is empty.\n"); + return; + } + + if (position <= 0) { + printf("Invalid position.\n"); + return; + } + + if (position == 1) { + struct sll *temp = *head; + *head = temp->next; + free(temp); + return; + } + + struct sll *current = *head; + for (int i = 1; i < position - 1 && current != NULL; i++) { + current = current->next; + } + + if (current == NULL || current->next == NULL) { + printf("Invalid position.\n"); + return; + } + + struct sll *temp = current->next; + current->next = temp->next; + free(temp); +} + +// Function to delete a node after a specific node value +void deleteAfterNode(struct sll **head, int data) { + struct sll *current = *head; + + while (current != NULL && current->data != data) { + current = current->next; + } + + if (current == NULL) { + printf("Element %d not found in the list.\n", data); + return; + } + + if (current->next == NULL) { + printf("Cannot delete. Element %d is the last node.\n", data); + return; + } + + struct sll *temp = current->next; + current->next = temp->next; + free(temp); +} + +// Function to insert a new node after a specific node value +void insertAfter(struct sll *node, int data, int newData) { + struct sll *current = node; + while (current != NULL) { + if (current->data == data) { + struct sll *newNode = (struct sll *)malloc(sizeof(struct sll)); + if (newNode == NULL) { + printf("\nMemory is not allocated"); + exit(0); + } + newNode->data = newData; + newNode->next = current->next; + current->next = newNode; + return; + } + current = current->next; + } + printf("Element %d not found in the list. Insertion failed.\n", data); +} + +// Function to insert a new node before a specific node value +void insertBefore(struct sll **head, int data, int newData) { + struct sll *current = *head; + struct sll *prev = NULL; + + while (current != NULL) { + if (current->data == data) { + struct sll *newNode = (struct sll *)malloc(sizeof(struct sll)); + if (newNode == NULL) { + printf("\nMemory is not allocated"); + exit(0); + } + newNode->data = newData; + newNode->next = current; + if (prev != NULL) { + prev->next = newNode; + } else { + *head = newNode; + } + return; + } + prev = current; + current = current->next; + } + printf("Element %d not found in the list. Insertion failed.\n", data); +} + +// Function to append List 2 to List 1 +void appendList(struct sll **list1, struct sll *list2) { + if (*list1 == NULL) { + *list1 = list2; + } else { + struct sll *current = *list1; + while (current->next != NULL) { + current = current->next; + } + current->next = list2; + } +} From 0b01b3825b6122c312e21058e5deba88cf9bd05b Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:14:15 +0530 Subject: [PATCH 04/13] Create circular_linked_list.c --- algorithms/linked-list/circular_linked_list.c | 453 ++++++++++++++++++ 1 file changed, 453 insertions(+) create mode 100644 algorithms/linked-list/circular_linked_list.c diff --git a/algorithms/linked-list/circular_linked_list.c b/algorithms/linked-list/circular_linked_list.c new file mode 100644 index 00000000..822e7ca7 --- /dev/null +++ b/algorithms/linked-list/circular_linked_list.c @@ -0,0 +1,453 @@ +#include +#include + +// Define a circular linked list structure +struct circularLinkedList { + int data; + struct circularLinkedList *next; +}; + +// Function prototypes +void createList(struct circularLinkedList **head, int n); +void traverseList(struct circularLinkedList *head); +void insertAtBeginning(struct circularLinkedList **head, int data); +void insertAtEnd(struct circularLinkedList **head, int data); +void insertAtN(struct circularLinkedList **head, int data, int position); +void deleteFromBeginning(struct circularLinkedList **head); +void deleteFromEnd(struct circularLinkedList **head); +void deleteFromN(struct circularLinkedList **head, int position); +void sortList(struct circularLinkedList **head); +int searchList(struct circularLinkedList *head, int key); +void reverseList(struct circularLinkedList **head); +int countList(struct circularLinkedList *head); +void appendList(struct circularLinkedList **headA, struct circularLinkedList **headB); + +int main(){ + struct circularLinkedList *head = NULL; // Initialize the main circular list + + // Create and display List 1 + printf("Create List 1:\n"); + createList(&head, 5); + printf("List 1:\n"); + traverseList(head); + + int choice, data, position, key; + do { + // Display the main menu + printf("\n\nMain Menu\n"); + printf("1. Insert at beginning\n"); + printf("2. Insert at end\n"); + printf("3. Insert at N\n"); + printf("4. Delete from beginning\n"); + printf("5. Delete from end\n"); + printf("6. Delete from N\n"); + printf("7. Sort the list\n"); + printf("8. Search an element\n"); + printf("9. Reverse the list\n"); + printf("10. Count the number of elements\n"); + printf("11. Append List 2 to List 1\n"); + printf("0. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + // Insert at beginning + printf("Enter the data to insert at beginning: "); + scanf("%d", &data); + insertAtBeginning(&head, data); + printf("List 1:\n"); + traverseList(head); + break; + case 2: + // Insert at end + printf("Enter the data to insert at end: "); + scanf("%d", &data); + insertAtEnd(&head, data); + printf("List 1:\n"); + traverseList(head); + break; + case 3: + // Insert at N + printf("Enter the data to insert at N: "); + scanf("%d", &data); + printf("Enter the position to insert at N: "); + scanf("%d", &position); + insertAtN(&head, data, position); + printf("List 1:\n"); + traverseList(head); + break; + case 4: + // Delete from beginning + deleteFromBeginning(&head); + printf("List 1:\n"); + traverseList(head); + break; + case 5: + // Delete from end + deleteFromEnd(&head); + printf("List 1:\n"); + traverseList(head); + break; + case 6: + // Delete from N + printf("Enter the position to delete from N: "); + scanf("%d", &position); + deleteFromN(&head, position); + printf("List 1:\n"); + traverseList(head); + break; + case 7: + // Sort the list + sortList(&head); + printf("List 1:\n"); + traverseList(head); + break; + case 8: + // Search an element + printf("Enter the element to search: "); + scanf("%d", &key); + position = searchList(head, key); + if (position == -1) { + printf("Element not found\n"); + } else { + printf("Element found at position %d\n", position); + } + break; + case 9: + // Reverse the list + reverseList(&head); + printf("List 1:\n"); + traverseList(head); + break; + case 10: + // Count the number of elements + printf("Number of elements in the list: %d\n", countList(head)); + break; + struct circularLinkedList *head2; + case 11: + // Append List 2 to List 1 + head2 = NULL; // Initialize the second circular list + printf("Create List 2:\n"); + createList(&head2, 5); + printf("List 2:\n"); + traverseList(head2); + appendList(&head, &head2); + printf("List 1:\n"); + traverseList(head); + break; + case 0: + // Exit + break; + default: + printf("Invalid choice, please try again.\n"); + } + } while (choice != 0); + + return 0; +} + +// Function to create a circular linked list of n nodes +void createList(struct circularLinkedList **head, int n) { + int i, data; + struct circularLinkedList *prevNode, *newNode; + + prevNode = NULL; + for (i = 1; i <= n; i++) { + // Create a new node + newNode = (struct circularLinkedList *)malloc(sizeof(struct circularLinkedList)); + + printf("Enter the data of node %d: ", i); + scanf("%d", &data); + + newNode->data = data; + newNode->next = NULL; + + // Link the new node with the previous node + if (prevNode != NULL) { + prevNode->next = newNode; + } + + // Move the previous node ahead + prevNode = newNode; + + // Link the last node with the first node + if (i == n) { + newNode->next = *head; + } + + // Update the head pointer if the list is empty + if (*head == NULL) { + *head = newNode; + } + } +} + +// Function to traverse the circular linked list +void traverseList(struct circularLinkedList *head) { + struct circularLinkedList *currentNode; + + // Return if the list is empty + if (head == NULL) { + printf("List is empty.\n"); + return; + } + + currentNode = head; + do { + // Print the data of current node + printf("%d -> ", currentNode->data); + + // Move to the next node + currentNode = currentNode->next; + } while (currentNode != head); + printf("%d -> ", currentNode->data); +} + +// Function to insert a node at the beginning of the circular linked list +void insertAtBeginning(struct circularLinkedList **head, int data) { + struct circularLinkedList *newNode, *currentNode; + + // Create a new node + newNode = (struct circularLinkedList *)malloc(sizeof(struct circularLinkedList)); + newNode->data = data; + newNode->next = NULL; + + // If the list is empty + if (*head == NULL) { + *head = newNode; + newNode->next = *head; + } else { + // Else, insert the new node at the beginning + currentNode = *head; + while (currentNode->next != *head) { + currentNode = currentNode->next; + } + currentNode->next = newNode; + newNode->next = *head; + *head = newNode; + } +} + +// Function to insert a node at the end of the circular linked list +void insertAtEnd(struct circularLinkedList **head, int data) { + struct circularLinkedList *newNode, *currentNode; + + // Create a new node + newNode = (struct circularLinkedList *)malloc(sizeof(struct circularLinkedList)); + newNode->data = data; + newNode->next = NULL; + + // If the list is empty + if (*head == NULL) { + *head = newNode; + newNode->next = *head; + } else { + // Else, insert the new node at the end + currentNode = *head; + while (currentNode->next != *head) { + currentNode = currentNode->next; + } + currentNode->next = newNode; + newNode->next = *head; + } +} + +// Function to insert a node at N of the circular linked list +void insertAtN(struct circularLinkedList **head, int data, int position) { + struct circularLinkedList *newNode, *currentNode; + int i; + + // Create a new node + newNode = (struct circularLinkedList *)malloc(sizeof(struct circularLinkedList)); + newNode->data = data; + newNode->next = NULL; + + // If the list is empty + if (*head == NULL) { + *head = newNode; + newNode->next = *head; + } else { + // Else, insert the new node at N + currentNode = *head; + for (i = 1; i < position - 1; i++) { + currentNode = currentNode->next; + } + newNode->next = currentNode->next; + currentNode->next = newNode; + } +} + +// Function to delete a node from the beginning of the circular linked list +void deleteFromBeginning(struct circularLinkedList **head) { + struct circularLinkedList *currentNode, *tempNode; + + // If the list is empty + if (*head == NULL) { + printf("List is empty.\n"); + } else { + // Else, delete the node from the beginning + currentNode = *head; + while (currentNode->next != *head) { + currentNode = currentNode->next; + } + tempNode = *head; + currentNode->next = (*head)->next; + *head = (*head)->next; + free(tempNode); + } +} + +// Function to delete a node from the end of the circular linked list +void deleteFromEnd(struct circularLinkedList **head) { + struct circularLinkedList *currentNode, *prevNode; + + // If the list is empty + if (*head == NULL) { + printf("List is empty.\n"); + } else { + // Else, delete the node from the end + currentNode = *head; + while (currentNode->next != *head) { + prevNode = currentNode; + currentNode = currentNode->next; + } + prevNode->next = currentNode->next; + free(currentNode); + } +} + +// Function to delete a node from N of the circular linked list +void deleteFromN(struct circularLinkedList **head, int position) { + struct circularLinkedList *currentNode, *prevNode; + int i; + + // If the list is empty + if (*head == NULL) { + printf("List is empty.\n"); + } else { + // Else, delete the node from N + currentNode = *head; + for (i = 1; i < position; i++) { + prevNode = currentNode; + currentNode = currentNode->next; + } + prevNode->next = currentNode->next; + free(currentNode); + } +} + +// Function to sort the circular linked list in ascending order +void sortList(struct circularLinkedList **head) { + struct circularLinkedList *currentNode, *index; + int temp; + + // If the list is empty + if (*head == NULL) { + printf("List is empty.\n"); + } else { + // Else, sort the list in ascending order + currentNode = *head; + do { + index = currentNode->next; + while (index != *head) { + if (currentNode->data > index->data) { + temp = currentNode->data; + currentNode->data = index->data; + index->data = temp; + } + index = index->next; + } + currentNode = currentNode->next; + } while (currentNode->next != *head); + } +} + +// Function to search an element in the circular linked list +int searchList(struct circularLinkedList *head, int key) { + struct circularLinkedList *currentNode; + int position = 1; + + // If the list is empty + if (head == NULL) { + printf("List is empty.\n"); + return -1; + } + + currentNode = head; + do { + // If the key is found + if (currentNode->data == key) { + return position; + } + position++; + currentNode = currentNode->next; + } while (currentNode != head); + return -1; +} + +// Function to reverse the circular linked list +void reverseList(struct circularLinkedList **head) { + struct circularLinkedList *prevNode, *currentNode, *nextNode; + + // If the list is empty + if (*head == NULL) { + printf("List is empty.\n"); + } else { + // Else, reverse the list + prevNode = *head; + currentNode = (*head)->next; + nextNode = (*head)->next->next; + while (currentNode != *head) { + currentNode->next = prevNode; + prevNode = currentNode; + currentNode = nextNode; + nextNode = nextNode->next; + } + currentNode->next = prevNode; + *head = prevNode; + } +} + +// Function to count the number of elements in the circular linked list +int countList(struct circularLinkedList *head) { + struct circularLinkedList *currentNode; + int count = 0; + + // If the list is empty + if (head == NULL) { + printf("List is empty.\n"); + return 0; + } + + currentNode = head; + do { + count++; + currentNode = currentNode->next; + } while (currentNode != head); + return count; +} + +// Function to append List 2 to List 1 +void appendList(struct circularLinkedList **headA, struct circularLinkedList **headB) { + struct circularLinkedList *currentNode; + + // If List 1 is empty + if (*headA == NULL) { + *headA = *headB; + } else { + // Else, append List 2 to List 1 + currentNode = *headA; + while (currentNode->next != *headA) { + currentNode = currentNode->next; + } + currentNode->next = *headB; + currentNode = *headB; + while (currentNode->next != *headB) { + currentNode = currentNode->next; + } + currentNode->next = *headA; + } +} + +// Path: circularLinkedList.c From 1fd43f9b4430272cb943da18cc0259e56ec25b21 Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:15:33 +0530 Subject: [PATCH 05/13] Create double_linked_list.c --- algorithms/linked-list/double_linked_list.c | 283 ++++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 algorithms/linked-list/double_linked_list.c diff --git a/algorithms/linked-list/double_linked_list.c b/algorithms/linked-list/double_linked_list.c new file mode 100644 index 00000000..ae41efd4 --- /dev/null +++ b/algorithms/linked-list/double_linked_list.c @@ -0,0 +1,283 @@ +#include +#include + +struct Node +{ + int info; + struct Node *next; + struct Node *prev; +}*head; + +void create(int info) +{ + struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); + temp->info = info; + temp->next = NULL; + temp->prev = NULL; + if (head == NULL) + { + head = temp; + } + else + { + struct Node *ptr = head; + while (ptr->next != NULL) + { + ptr = ptr->next; + } + ptr->next = temp; + temp->prev = ptr; + } +} +// traversing the linked list +void traverse() +{ + struct Node *temp = head; + while (temp != NULL) + { + printf("%d\t", temp->info); + temp = temp->next; + } +} +// searching for an element +void search(int info) +{ + struct Node *temp = head; + while (temp != NULL) + { + if (temp->info == info) + { + printf("found in the doubly linked list \n"); + return; + } + temp = temp->next; + } + printf("Key not found\n"); +} +// insertion at begin +void insert_begin(int info) +{ + struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); + temp->info = info; + temp->next = head; + temp->prev = NULL; + if (head != NULL) + head->prev = temp; + head = temp; +} +// insertion at the end +void insert_end(int info) +{ + struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); + temp->info = info; + temp->next = NULL; + if (head == NULL) + { + temp->prev = NULL; + head = temp; + } + else + { + struct Node *temp2 = head; + while (temp2->next != NULL) + { + temp2 = temp2->next; + } + temp2->next = temp; + temp->prev = temp2; + } +} +// insert at any position +void insert_pos(int pos, int info) +{ + struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); + struct Node *temp2 = head; + int i; + + for (i = 1; i < pos - 1; i++) + { + temp = temp->next; + if (temp == NULL) + { + printf("\nThere are less than %d elements", pos); + return; + } + } + temp->info = info; + if (pos == 1) + { + insert_begin(info); + return; + } + + else + { + struct Node *temp2 = temp->next; + temp->next = temp; + temp2->prev = temp; + temp->prev = temp; + temp->next = temp2; + } +} + +void delete_begin() +{ + if (head == NULL) + printf("\n no memory"); + else + { + struct Node *temp = head; + head = head->next; + head->prev = NULL; + free(temp); + } +} + +void delete_end() +{ + if (head == NULL) + printf("\n UNDERFLOW"); + else if (head->next == NULL) + { + head = NULL; + free(head); + } + else + { + struct Node *temp = head; + while (temp->next != NULL) + temp = temp->next; + temp->prev->next = NULL; + free(temp); + } +} + +void delete_pos(int pos) +{ + struct Node *temp = head; + + int i; + + for (i = 1; i < pos; i++) + { + temp = temp->next; + + if (temp == NULL) + { + printf("\nThere are less than %d elements", pos); + return; + } + } + + if (pos == 1) + { + delete_begin(); + return; + } + + else + { + struct Node *temp2 = temp->prev; + + temp2->next = temp->next; + + free(temp); + + return; + } +} + +int main() +{ + + int choice, data, pos, key; + + do + { + + printf("\n\n*MAIN MENU*\n"); + printf("1. Create\n"); + printf("2. Display\n"); + printf("3. Search\n"); + printf("4. Insert at beginning\n"); + printf("5. Insert at end\n"); + printf("6. Insert at any position\n"); + printf("7. Delete from beginning\n"); + printf("8. Delete from end\n"); + printf("9. Delete from any position\n"); + printf("10. EXIT\n"); + + printf("\nEnter your option : "); + scanf("%d", &choice); +int n; + switch (choice) + { + + case 1: + + printf("Enter the number of nodes to be insert in the list : "); + scanf("%d",&n); + for(int i=0;i Date: Fri, 20 Oct 2023 20:17:13 +0530 Subject: [PATCH 06/13] Create stack_using_linkedlist.c --- .../linked-list/stack_using_linkedlist.c | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 algorithms/linked-list/stack_using_linkedlist.c diff --git a/algorithms/linked-list/stack_using_linkedlist.c b/algorithms/linked-list/stack_using_linkedlist.c new file mode 100644 index 00000000..a50bc7b8 --- /dev/null +++ b/algorithms/linked-list/stack_using_linkedlist.c @@ -0,0 +1,99 @@ +#include +#include + +// Define a structure for a node in the linked list +struct Node { + int data; + struct Node* next; +}; + +// Define the stack structure +struct Stack { + struct Node* top; +}; + +// Function to create an empty stack +struct Stack* createStack() { + struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); + stack->top = NULL; + return stack; +} + +// Function to check if the stack is empty +int isEmpty(struct Stack* stack) { + return (stack->top == NULL); +} + +// Function to push an element onto the stack +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; +} + +// Function to pop an element from the stack +int pop(struct Stack* stack) { + if (isEmpty(stack)) { + printf("Stack is empty. Cannot pop.\n"); + return -1; // You can choose a different error value if needed. + } + struct Node* temp = stack->top; + int data = temp->data; + stack->top = temp->next; + free(temp); + return data; +} + +// Function to peek the top element of the stack without removing it +int peek(struct Stack* stack) { + if (isEmpty(stack)) { + printf("Stack is empty. Cannot peek.\n"); + return -1; // You can choose a different error value if needed. + } + return stack->top->data; +} + +// Function to display the elements of the stack +void display(struct Stack* stack) { + if (isEmpty(stack)) { + printf("Stack is empty.\n"); + return; + } + struct Node* current = stack->top; + while (current != NULL) { + printf("%d ", current->data); + current = current->next; + } + printf("\n"); +} + +// Function to free the memory used by the stack +void destroyStack(struct Stack* stack) { + while (!isEmpty(stack)) { + pop(stack); + } + free(stack); +} + +int main() { + struct Stack* stack = createStack(); + + push(stack, 10); + push(stack, 20); + push(stack, 30); + + printf("Stack elements: "); + display(stack); + + printf("Top element: %d\n", peek(stack)); + + printf("Popped element: %d\n", pop(stack)); + + printf("Stack elements after popping: "); + display(stack); + + destroyStack(stack); + + return 0; +} From 967f1b41a056c4e68f4978ef321afd50a8a549b6 Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:18:11 +0530 Subject: [PATCH 07/13] Create queue_using_linked_list.c --- .../linked-list/queue_using_linked_list.c | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 algorithms/linked-list/queue_using_linked_list.c diff --git a/algorithms/linked-list/queue_using_linked_list.c b/algorithms/linked-list/queue_using_linked_list.c new file mode 100644 index 00000000..21d8a414 --- /dev/null +++ b/algorithms/linked-list/queue_using_linked_list.c @@ -0,0 +1,100 @@ +#include +#include + +// Define a structure for a node in the linked list +struct Node { + int data; + struct Node* next; +}; + +// Define the queue structure +struct Queue { + struct Node* front; + struct Node* rear; +}; + +// Function to create an empty queue +struct Queue* createQueue() { + struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue)); + queue->front = queue->rear = NULL; + return queue; +} + +// Function to check if the queue is empty +int isEmpty(struct Queue* queue) { + return (queue->front == NULL); +} + +// Function to enqueue (add) an element to the queue +void enqueue(struct Queue* queue, int data) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = data; + newNode->next = NULL; + + if (isEmpty(queue)) { + queue->front = queue->rear = newNode; + } else { + queue->rear->next = newNode; + queue->rear = newNode; + } +} + +// Function to dequeue (remove) an element from the queue +int dequeue(struct Queue* queue) { + if (isEmpty(queue)) { + printf("Queue is empty. Cannot dequeue.\n"); + return -1; // You can choose a different error value if needed. + } + struct Node* temp = queue->front; + int data = temp->data; + queue->front = temp->next; + + if (queue->front == NULL) { + queue->rear = NULL; // If the last element is dequeued, update the rear pointer. + } + + free(temp); + return data; +} + +// Function to display the elements of the queue +void display(struct Queue* queue) { + if (isEmpty(queue)) { + printf("Queue is empty.\n"); + return; + } + struct Node* current = queue->front; + while (current != NULL) { + printf("%d ", current->data); + current = current->next; + } + printf("\n"); +} + +// Function to free the memory used by the queue +void destroyQueue(struct Queue* queue) { + while (!isEmpty(queue)) { + dequeue(queue); + } + free(queue); +} + +int main() { + struct Queue* queue = createQueue(); + + enqueue(queue, 10); + enqueue(queue, 20); + enqueue(queue, 30); + + printf("Queue elements: "); + display(queue); + + printf("Dequeued element: %d\n", dequeue(queue)); + + printf("Queue elements after dequeue: "); + display(queue); + + destroyQueue(queue); + + return 0; +} From 1ca0bf24a34e565828134a4698a4f7774792673f Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:18:58 +0530 Subject: [PATCH 08/13] Rename stack_using_linkedlist.c to stack_using_linked_list.c --- .../{stack_using_linkedlist.c => stack_using_linked_list.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename algorithms/linked-list/{stack_using_linkedlist.c => stack_using_linked_list.c} (100%) diff --git a/algorithms/linked-list/stack_using_linkedlist.c b/algorithms/linked-list/stack_using_linked_list.c similarity index 100% rename from algorithms/linked-list/stack_using_linkedlist.c rename to algorithms/linked-list/stack_using_linked_list.c From ef6e8064f8d6c1b945b8e1fe93ed61a338f98977 Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:40:51 +0530 Subject: [PATCH 09/13] Create infix_to_postfix.c --- algorithms/converting/infix_to_postfix.c | 108 +++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 algorithms/converting/infix_to_postfix.c diff --git a/algorithms/converting/infix_to_postfix.c b/algorithms/converting/infix_to_postfix.c new file mode 100644 index 00000000..d79b4b81 --- /dev/null +++ b/algorithms/converting/infix_to_postfix.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include + +#define MAX_SIZE 100 + +// Define a structure for a stack +struct Stack { + char data[MAX_SIZE]; + int top; +}; + +// Function to initialize a stack +void initialize(struct Stack* stack) { + stack->top = -1; +} + +// Function to check if the stack is empty +int isEmpty(struct Stack* stack) { + return (stack->top == -1); +} + +// Function to push an element onto the stack +void push(struct Stack* stack, char item) { + if (stack->top >= MAX_SIZE - 1) { + printf("Stack overflow\n"); + exit(1); + } + stack->data[++stack->top] = item; +} + +// Function to pop an element from the stack +char pop(struct Stack* stack) { + if (isEmpty(stack)) { + printf("Stack underflow\n"); + exit(1); + } + return stack->data[stack->top--]; +} + +// Function to get the precedence of an operator +int getPrecedence(char operator) { + if (operator == '+' || operator == '-') { + return 1; + } else if (operator == '*' || operator == '/') { + return 2; + } + return 0; +} + +// Function to check if a character is an operator +int isOperator(char character) { + return (character == '+' || character == '-' || character == '*' || character == '/'); +} + +// Function to convert infix to postfix expression +void infixToPostfix(char infix[], char postfix[]) { + struct Stack operatorStack; + initialize(&operatorStack); + + int length = strlen(infix); + int j = 0; + + for (int i = 0; i < length; i++) { + char symbol = infix[i]; + + if (isalnum(symbol)) { + postfix[j++] = symbol; + } else if (symbol == '(') { + push(&operatorStack, symbol); + } else if (symbol == ')') { + while (!isEmpty(&operatorStack) && operatorStack.data[operatorStack.top] != '(') { + postfix[j++] = pop(&operatorStack); + } + // Pop the corresponding '(' + if (!isEmpty(&operatorStack) && operatorStack.data[operatorStack.top] == '(') { + pop(&operatorStack); + } + } else if (isOperator(symbol)) { + while (!isEmpty(&operatorStack) && getPrecedence(operatorStack.data[operatorStack.top]) >= getPrecedence(symbol)) { + postfix[j++] = pop(&operatorStack); + } + push(&operatorStack, symbol); + } + } + + // Pop any remaining operators from the stack + while (!isEmpty(&operatorStack)) { + postfix[j++] = pop(&operatorStack); + } + + // Null-terminate the postfix expression + postfix[j] = '\0'; +} + +int main() { + char infix[MAX_SIZE], postfix[MAX_SIZE]; + + printf("Enter an infix expression: "); + scanf("%s", infix); + + infixToPostfix(infix, postfix); + + printf("The postfix expression is: %s\n", postfix); + + return 0; +} From 6c9d547c60b02c35c2c1d313f0679ebe99fcf47b Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:41:41 +0530 Subject: [PATCH 10/13] Create postfix_to_infix.c --- algorithms/converting/postfix_to_infix.c | 90 ++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 algorithms/converting/postfix_to_infix.c diff --git a/algorithms/converting/postfix_to_infix.c b/algorithms/converting/postfix_to_infix.c new file mode 100644 index 00000000..bb58cb74 --- /dev/null +++ b/algorithms/converting/postfix_to_infix.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +#define MAX_SIZE 100 + +// Define a structure for a stack +struct Stack { + char data[MAX_SIZE][MAX_SIZE]; // Use a 2D array to store operands and operators + int top; +}; + +// Function to initialize a stack +void initialize(struct Stack* stack) { + stack->top = -1; +} + +// Function to check if the stack is empty +int isEmpty(struct Stack* stack) { + return (stack->top == -1); +} + +// Function to push an element onto the stack +void push(struct Stack* stack, char item[], int isOperator) { + if (stack->top >= MAX_SIZE - 1) { + printf("Stack overflow\n"); + exit(1); + } + + if (isOperator) { + // Enclose operators in parentheses for proper infix expression + sprintf(stack->data[++stack->top], "(%s)", item); + } else { + strcpy(stack->data[++stack->top], item); + } +} + +// Function to pop an element from the stack +void pop(struct Stack* stack, char result[]) { + if (isEmpty(stack)) { + printf("Stack underflow\n"); + exit(1); + } + strcpy(result, stack->data[stack->top--]); +} + +// Function to convert postfix to infix expression +void postfixToInfix(char postfix[], char infix[]) { + struct Stack operandStack; + initialize(&operandStack); + + int length = strlen(postfix); + + for (int i = 0; i < length; i++) { + char symbol = postfix[i]; + + if (isalnum(symbol)) { + char operand[2]; + operand[0] = symbol; + operand[1] = '\0'; + push(&operandStack, operand, 0); // 0 indicates it's an operand + } else if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/') { + char operand1[MAX_SIZE], operand2[MAX_SIZE]; + pop(&operandStack, operand1); + pop(&operandStack, operand2); + + char expression[MAX_SIZE]; + sprintf(expression, "%s %c %s", operand2, symbol, operand1); + + push(&operandStack, expression, 1); // 1 indicates it's an operator + } + } + + // The final result is at the top of the stack + pop(&operandStack, infix); +} + +int main() { + char postfix[MAX_SIZE], infix[MAX_SIZE]; + + printf("Enter a postfix expression: "); + scanf("%s", postfix); + + postfixToInfix(postfix, infix); + + printf("The infix expression is: %s\n", infix); + + return 0; +} From 5d7a017cf7a83bb968f6f4a51b5bdcc6d59065f8 Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:42:25 +0530 Subject: [PATCH 11/13] Create prefix_to_infix.c --- algorithms/converting/prefix_to_infix.c | 90 +++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 algorithms/converting/prefix_to_infix.c diff --git a/algorithms/converting/prefix_to_infix.c b/algorithms/converting/prefix_to_infix.c new file mode 100644 index 00000000..a6d8efdc --- /dev/null +++ b/algorithms/converting/prefix_to_infix.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +#define MAX_SIZE 100 + +// Define a structure for a stack +struct Stack { + char data[MAX_SIZE][MAX_SIZE]; // Use a 2D array to store operands and operators + int top; +}; + +// Function to initialize a stack +void initialize(struct Stack* stack) { + stack->top = -1; +} + +// Function to check if the stack is empty +int isEmpty(struct Stack* stack) { + return (stack->top == -1); +} + +// Function to push an element onto the stack +void push(struct Stack* stack, char item[], int isOperator) { + if (stack->top >= MAX_SIZE - 1) { + printf("Stack overflow\n"); + exit(1); + } + + if (isOperator) { + // Enclose operators in parentheses for proper infix expression + sprintf(stack->data[++stack->top], "(%s)", item); + } else { + strcpy(stack->data[++stack->top], item); + } +} + +// Function to pop an element from the stack +void pop(struct Stack* stack, char result[]) { + if (isEmpty(stack)) { + printf("Stack underflow\n"); + exit(1); + } + strcpy(result, stack->data[stack->top--]); +} + +// Function to convert prefix to infix expression +void prefixToInfix(char prefix[], char infix[]) { + struct Stack operandStack; + initialize(&operandStack); + + int length = strlen(prefix); + + for (int i = length - 1; i >= 0; i--) { + char symbol = prefix[i]; + + if (isalnum(symbol)) { + char operand[2]; + operand[0] = symbol; + operand[1] = '\0'; + push(&operandStack, operand, 0); // 0 indicates it's an operand + } else if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/') { + char operand1[MAX_SIZE], operand2[MAX_SIZE]; + pop(&operandStack, operand1); + pop(&operandStack, operand2); + + char expression[MAX_SIZE]; + sprintf(expression, "%s %c %s", operand1, symbol, operand2); + + push(&operandStack, expression, 1); // 1 indicates it's an operator + } + } + + // The final result is at the top of the stack + pop(&operandStack, infix); +} + +int main() { + char prefix[MAX_SIZE], infix[MAX_SIZE]; + + printf("Enter a prefix expression: "); + scanf("%s", prefix); + + prefixToInfix(prefix, infix); + + printf("The infix expression is: %s\n", infix); + + return 0; +} From c8bde859ba38278d4359853f520f9fe6546f0c92 Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:47:17 +0530 Subject: [PATCH 12/13] Create sparse_matrix_multiplication_linked_list.c --- ...sparse_matrix_multiplication_linked_list.c | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 algorithms/linked-list/sparse_matrix_multiplication_linked_list.c diff --git a/algorithms/linked-list/sparse_matrix_multiplication_linked_list.c b/algorithms/linked-list/sparse_matrix_multiplication_linked_list.c new file mode 100644 index 00000000..713ffa8c --- /dev/null +++ b/algorithms/linked-list/sparse_matrix_multiplication_linked_list.c @@ -0,0 +1,109 @@ +// multiply using linked list +#include +#include + +struct node +{ + int row; + int col; + int val; + struct node *next; +}; + +struct sparse_matrix +{ + struct node *head; +}; + +void insert_node(struct sparse_matrix *mat, int row, int col, int val) +{ + struct node *new_node = malloc(sizeof(struct node)); + new_node->row = row; + new_node->col = col; + new_node->val = val; + new_node->next = NULL; + + if (mat->head == NULL) + { + mat->head = new_node; + } + else + { + struct node *te = mat->head; + while (te->next != NULL) + { + te = te->next; + } + te->next = new_node; + } +} + +void multiply(struct sparse_matrix *mat1, struct sparse_matrix *mat2, struct sparse_matrix *res) +{ + struct node *temp = mat1->head; + while (temp != NULL) + { + struct node *temp2 = mat2->head; + while (temp2 != NULL) + { + if (temp->col == temp2->row) + { + int row = temp->row; + int col = temp2->col; + int val = temp->val * temp2->val; + + struct node *te = res->head; + while (te != NULL && (te->row < row || te->col < col)) + { + te = te->next; + } + + if (te == NULL || te->row != row || te->col != col) + { + insert_node(res, row, col, val); + } + else + { + te->val += val; + } + } + temp2 = temp2->next; + } + temp = temp->next; + } +} + +void print_sparse_matrix(struct sparse_matrix *mat) +{ + struct node *te = mat->head; + while (te != NULL) + { + printf("(%d, %d, %d) ", te->row, te->col, te->val); + te = te->next; + } + printf("\n"); +} + +int main() +{ + struct sparse_matrix mat1; + struct sparse_matrix mat2; + struct sparse_matrix res; + + mat1.head = NULL; + mat2.head = NULL; + res.head = NULL; + + insert_node(&mat1, 1, 2, 3); + insert_node(&mat1, 2, 3, 4); + + insert_node(&mat2, 1, 3, 5); + insert_node(&mat2, 2, 4, 6); + + multiply(&mat1, &mat2, &res); + + printf("The product of the two sparse matrices is: "); + print_sparse_matrix(&res); + + return 0; +} From 3f703fe14b3fc5638b94906e05355cbc6e30a3e6 Mon Sep 17 00:00:00 2001 From: Aditya <82191607+adityaanandz@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:49:20 +0530 Subject: [PATCH 13/13] Create sparse_matrix_addition_linked_list.c --- .../sparse_matrix_addition_linked_list.c | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 algorithms/linked-list/sparse_matrix_addition_linked_list.c diff --git a/algorithms/linked-list/sparse_matrix_addition_linked_list.c b/algorithms/linked-list/sparse_matrix_addition_linked_list.c new file mode 100644 index 00000000..ca72cd1f --- /dev/null +++ b/algorithms/linked-list/sparse_matrix_addition_linked_list.c @@ -0,0 +1,125 @@ +// add using liked list +#include +#include + +struct node +{ + int row; + int col; + int val; + struct node *next; +}; + +struct sparse_matrix +{ + struct node *head; +}; + +void insert_node(struct sparse_matrix *mat, int row, int col, int val) +{ + struct node *new_node = malloc(sizeof(struct node)); + new_node->row = row; + new_node->col = col; + new_node->val = val; + new_node->next = NULL; + + if (mat->head == NULL) + { + mat->head = new_node; + } + else + { + struct node *temp = mat->head; + while (temp->next != NULL) + { + temp = temp->next; + } + temp->next = new_node; + } +} + +void add(struct sparse_matrix *mat1, struct sparse_matrix *mat2, struct sparse_matrix *res) +{ + struct node *temp = mat1->head; + struct node *temp2 = mat2->head; + + while (temp != NULL && temp2 != NULL) + { + if (temp->row < temp2->row) + { + insert_node(res, temp->row, temp->col, temp->val); + temp = temp->next; + } + else if (temp->row > temp2->row) + { + insert_node(res, temp2->row, temp2->col, temp2->val); + temp2 = temp2->next; + } + else + { + if (temp->col < temp2->col) + { + insert_node(res, temp->row, temp->col, temp->val); + temp = temp->next; + } + else if (temp->col > temp2->col) + { + insert_node(res, temp2->row, temp2->col, temp2->val); + temp2 = temp2->next; + } + else + { + insert_node(res, temp->row, temp->col, temp->val + temp2->val); + temp = temp->next; + temp2 = temp2->next; + } + } + } + + while (temp != NULL) + { + insert_node(res, temp->row, temp->col, temp->val); + temp = temp->next; + } + + while (temp2 != NULL) + { + insert_node(res, temp2->row, temp2->col, temp2->val); + temp2 = temp2->next; + } +} + +void print(struct sparse_matrix *mat) +{ + struct node *temp = mat->head; + while (temp != NULL) + { + printf("(%d, %d, %d) ", temp->row, temp->col, temp->val); + temp = temp->next; + } + printf("\n"); +} + +int main() +{ + struct sparse_matrix mat1; + struct sparse_matrix mat2; + struct sparse_matrix res; + + mat1.head = NULL; + mat2.head = NULL; + res.head = NULL; + + insert_node(&mat1, 1, 2, 3); + insert_node(&mat1, 2, 3, 4); + + insert_node(&mat2, 1, 3, 5); + insert_node(&mat2, 2, 4, 6); + + add(&mat1, &mat2, &res); + + printf("The sum of the two sparse matrices is: "); + print(&res); + + return 0; +}