├── AES.c ├── BST.c ├── Binary Search Tree.c ├── Binary Search.c ├── Bubble sort example.c ├── E-Commerce.docx ├── Enqueue and Dequeue of Stack.c ├── Linear Search.c ├── Queue Example.c ├── insertion sort.c ├── insertion.c ├── merge sort.c ├── quicksort.c └── selection sort.c /AES.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | // AES S-box 5 | static const uint8_t sbox[256] = { 6 | // ... S-box values (omitted for brevity) ... (Advanced Encryption Standard) 7 | }; 8 | 9 | // AES round constants 10 | static const uint8_t rcon[10] = { 11 | // ... Round constants (omitted for brevity) ... 12 | }; 13 | 14 | // Key expansion function 15 | void keyExpansion(const uint8_t *key, uint8_t *roundKeys) { 16 | // Implement key expansion logic here 17 | (void)key; 18 | (void)roundKeys; 19 | } 20 | 21 | // SubBytes transformation 22 | void subBytes(uint8_t *state) { 23 | // Implement SubBytes logic here 24 | (void)state; 25 | } 26 | 27 | // ShiftRows transformation 28 | void shiftRows(uint8_t *state) { 29 | // Implement ShiftRows logic here 30 | (void)state; 31 | } 32 | 33 | // MixColumns transformation 34 | void mixColumns(uint8_t *state) { 35 | // Implement MixColumns logic here 36 | (void)state; 37 | } 38 | 39 | // AddRoundKey transformation 40 | void addRoundKey(uint8_t *state, const uint8_t *roundKey) { 41 | // Implement AddRoundKey logic here 42 | (void)state; 43 | (void)roundKey; 44 | } 45 | 46 | // AES encryption function 47 | void aesEncrypt(const uint8_t *input, const uint8_t *key, uint8_t *output) { 48 | // Implement AES encryption logic here 49 | (void)input; 50 | (void)key; 51 | (void)output; 52 | } 53 | 54 | // AES decryption function 55 | void aesDecrypt(const uint8_t *input, const uint8_t *key, uint8_t *output) { 56 | // Implement AES decryption logic here 57 | (void)input; 58 | (void)key; 59 | (void)output; 60 | } 61 | 62 | int main() { 63 | // Example key and plaintext 64 | uint8_t key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x97, 0x67, 0x45, 0xef, 0xd4, 0x13}; 65 | uint8_t plaintext[16] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'A', 'E', 'S', '!', '\0'}; 66 | 67 | // Buffer for ciphertext 68 | uint8_t ciphertext[16]; 69 | 70 | // Buffer for decrypted text 71 | uint8_t decryptedtext[16]; 72 | 73 | // Perform AES encryption 74 | aesEncrypt(plaintext, key, ciphertext); 75 | 76 | // Perform AES decryption 77 | aesDecrypt(ciphertext, key, decryptedtext); 78 | 79 | // Display results 80 | printf("Original Text: %s\n", plaintext); 81 | printf("Encrypted Text: "); 82 | 83 | int i; 84 | for (i = 0; i < 16; ++i) { 85 | printf("%02x", ciphertext[i]); 86 | } 87 | printf("\n"); 88 | 89 | printf("Decrypted Text: %s\n", decryptedtext); 90 | 91 | return 0;} 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /BST.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* left; 7 | struct Node* right; 8 | }; 9 | 10 | struct Node* createNode(int value) { 11 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 12 | newNode->data = value; 13 | newNode->left = NULL; 14 | newNode->right = NULL; 15 | return newNode; 16 | } 17 | 18 | struct Node* insert(struct Node* root, int value) { 19 | if (root == NULL) { 20 | return createNode(value); 21 | } 22 | if (value < root->data) { 23 | root->left = insert(root->left, value); 24 | } 25 | else if (value > root->data) { 26 | root->right = insert(root->right, value); 27 | } 28 | return root; 29 | } 30 | 31 | struct Node* search(struct Node* root, int value) { 32 | if (root == NULL || root->data == value) { 33 | return root; 34 | } 35 | if (value < root->data) { 36 | return search(root->left, value); 37 | } 38 | return search(root->right, value); 39 | } 40 | 41 | void inorderTraversal(struct Node* root) { 42 | if (root != NULL) { 43 | inorderTraversal(root->left); 44 | printf("%d ", root->data); 45 | inorderTraversal(root->right); 46 | } 47 | } 48 | 49 | 50 | int main() { 51 | struct Node* root = NULL; 52 | 53 | root = insert(root, 50); 54 | insert(root, 30); 55 | insert(root, 20); 56 | insert(root, 40); 57 | insert(root, 70); 58 | insert(root, 60); 59 | insert(root, 80); 60 | 61 | printf("Inorder traversal of the BST: "); 62 | inorderTraversal(root); 63 | printf("\n"); 64 | 65 | int searchValue = 40; 66 | struct Node* searchResult = search(root, searchValue); 67 | if (searchResult != NULL) { 68 | printf("%d is found in the BST.\n", searchValue); 69 | } else { 70 | printf("%d is not found in the BST.\n", searchValue); 71 | } 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Binary Search Tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node { 5 | int data; 6 | struct Node* left; 7 | struct Node* right; 8 | } Node; 9 | 10 | Node* createNode(int value) { 11 | Node* newNode = (Node*)malloc(sizeof(Node)); 12 | if (newNode == NULL) { 13 | printf("Memory allocation failed\n"); 14 | exit(1); 15 | } 16 | newNode->data = value; 17 | newNode->left = newNode->right = NULL; 18 | return newNode; 19 | } 20 | 21 | Node* insert(Node* root, int data) { 22 | if (root == NULL) { 23 | root = createNode(data); 24 | } else if (data <= root->data) { 25 | root->left = insert(root->left, data); 26 | } else { 27 | root->right = insert(root->right, data); 28 | } 29 | return root; 30 | } 31 | 32 | int search(Node* root, int data) { 33 | if (root == NULL) return 0; 34 | else if (root->data == data) return 1; 35 | else if (data <= root->data) return search(root->left, data); 36 | else return search(root->right, data); 37 | } 38 | 39 | void inorderTraversal(Node* root) { 40 | if (root != NULL) { 41 | inorderTraversal(root->left); 42 | printf("%d ", root->data); 43 | inorderTraversal(root->right); 44 | } 45 | } 46 | 47 | int main() { 48 | Node* root = NULL; 49 | root = insert(root, 10); 50 | root = insert(root, 5); 51 | root = insert(root, 15); 52 | root = insert(root, 3); 53 | root = insert(root, 7); 54 | 55 | printf("Inorder traversal: "); 56 | inorderTraversal(root); 57 | printf("\n"); 58 | 59 | int searchKey = 7; 60 | if (search(root, searchKey)) { 61 | printf("%d is found in the tree.\n", searchKey); 62 | } else { 63 | printf("%d is not found in the tree.\n", searchKey); 64 | } 65 | 66 | searchKey = 12; 67 | if (search(root, searchKey)) { 68 | printf("%d is found in the tree.\n", searchKey); 69 | } else { 70 | printf("%d is not found in the tree.\n", searchKey); 71 | } 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Binary Search.c: -------------------------------------------------------------------------------- 1 | #include 2 | int binarySearch(int arr[], int low, int high, int target) { 3 | while (low <= high) { 4 | int mid = low + (high - low) / 2; 5 | 6 | if (arr[mid] == target) 7 | return mid; 8 | if (arr[mid] < target) 9 | low = mid + 1; 10 | else 11 | high = mid - 1; 12 | } 13 | return -1;} 14 | 15 | int main() { 16 | int n, target; 17 | printf("Enter the size of the array: "); 18 | scanf("%d", &n); 19 | 20 | int arr[n]; 21 | printf("Enter %d sorted elements:\n", n); 22 | for (int i = 0; i < n; i++) { 23 | scanf("%d", &arr[i]); 24 | } 25 | 26 | printf("Enter the element to search: "); 27 | scanf("%d", &target); 28 | int index = binarySearch(arr, 0, n - 1, target); 29 | 30 | if (index != -1) { 31 | printf("Element %d found at index %d\n", target, index); 32 | } else { 33 | printf("Element %d not found in the array\n", target); 34 | } 35 | return 0;} 36 | -------------------------------------------------------------------------------- /Bubble sort example.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int *a, int *b); 3 | void bubbleSort(int arr[], int n); 4 | int main() { 5 | int n; 6 | printf("Enter the number of elements: "); 7 | scanf("%d", &n); 8 | int arr[n]; 9 | printf("Enter %d elements:\n", n); 10 | int i; 11 | for (i = 0; i < n; i++) { 12 | scanf("%d", &arr[i]); 13 | } 14 | 15 | bubbleSort(arr, n); 16 | printf("Sorted array:\n"); 17 | for (i = 0; i < n; i++) { 18 | printf("%d ", arr[i]); 19 | } 20 | return 0; 21 | } 22 | void swap(int *a, int *b) { 23 | int temp = *a; 24 | *a = *b; 25 | *b = temp; 26 | } 27 | 28 | void bubbleSort(int arr[], int n) { 29 | int i = 0, j = 0; 30 | while (i < n - 1) { 31 | j = 0; 32 | while (j < n - i - 1) { 33 | if (arr[j] > arr[j + 1]) { 34 | swap(&arr[j], &arr[j + 1]); 35 | } 36 | j++; 37 | } 38 | i++; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /E-Commerce.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anshumansinha3301/Data-Structures-Using-C/fdc56723fa28d90ab4d0d7a6bcb3a8152b28a3fa/E-Commerce.docx -------------------------------------------------------------------------------- /Enqueue and Dequeue of Stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX_SIZE 100 4 | struct Stack { 5 | int arr[MAX_SIZE]; 6 | int top; 7 | }; 8 | 9 | void initialize(struct Stack *stack) { 10 | stack->top = -1; 11 | } 12 | 13 | int isEmpty(struct Stack *stack) { 14 | return stack->top == -1; 15 | } 16 | 17 | int isFull(struct Stack *stack) { 18 | return stack->top == MAX_SIZE - 1; 19 | } 20 | void push(struct Stack *stack, int value) { 21 | if (isFull(stack)) { 22 | printf("Stack overflow\n"); 23 | return; 24 | } 25 | stack->arr[++stack->top] = value; 26 | } 27 | 28 | int pop(struct Stack *stack) { 29 | if (isEmpty(stack)) { 30 | printf("Stack underflow\n"); 31 | exit(1); 32 | } 33 | return stack->arr[stack->top--]; 34 | } 35 | 36 | void enqueue(struct Stack *stack1, struct Stack *stack2, int value) { 37 | while (!isEmpty(stack1)) { 38 | push(stack2, pop(stack1)); 39 | } 40 | 41 | push(stack1, value); 42 | 43 | while (!isEmpty(stack2)) { 44 | push(stack1, pop(stack2)); 45 | } 46 | } 47 | int dequeue(struct Stack *stack1) { 48 | if (isEmpty(stack1)) { 49 | printf("Queue underflow\n"); 50 | exit(1); 51 | } 52 | return pop(stack1); 53 | } 54 | int main() { 55 | struct Stack stack1, stack2; 56 | initialize(&stack1); 57 | initialize(&stack2); 58 | 59 | enqueue(&stack1, &stack2, 5); 60 | enqueue(&stack1, &stack2, 2); 61 | enqueue(&stack1, &stack2, 3); 62 | 63 | printf("Dequeued element: %d\n", dequeue(&stack1)); 64 | printf("Dequeued element: %d\n", dequeue(&stack1)); 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Linear Search.c: -------------------------------------------------------------------------------- 1 | #include 2 | int linearSearch(int arr[], int n, int target) { 3 | for (int i = 0; i < n; i++) { 4 | if (arr[i] == target) { 5 | return i; 6 | } 7 | } 8 | return -1; 9 | } 10 | int main() { 11 | int n, target; 12 | printf("Enter the size of the array: "); 13 | scanf("%d", &n); 14 | int arr[n]; 15 | printf("Enter %d elements:\n", n); 16 | for (int i = 0; i < n; i++) { 17 | scanf("%d", &arr[i]); 18 | } 19 | printf("Enter the element to search: "); 20 | scanf("%d", &target); 21 | int index = linearSearch(arr, n, target); 22 | if (index != -1) { 23 | printf("Element %d found at index %d\n", target, index); 24 | } else { 25 | printf("Element %d not found in the array\n", target); 26 | } 27 | return 0;} 28 | -------------------------------------------------------------------------------- /Queue Example.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_SIZE 100 3 | int queue[MAX_SIZE]; 4 | int front = -1; 5 | int rear = -1; 6 | 7 | int isEmpty() { 8 | return front == -1 && rear == -1; 9 | } 10 | 11 | int isFull() { 12 | return rear == MAX_SIZE - 1; 13 | } 14 | void enqueue(int value) { 15 | 16 | if (isEmpty()) { 17 | front = 0; 18 | } 19 | 20 | 21 | if (isFull()) { 22 | printf("Queue is full. Cannot insert %d.\n", value); 23 | return; 24 | } 25 | 26 | rear++; 27 | queue[rear] = value; 28 | 29 | printf("Inserted %d into the queue.\n", value); 30 | } 31 | 32 | 33 | void displayQueue() { 34 | if (isEmpty()) { 35 | printf("Queue is empty.\n"); 36 | return; 37 | } 38 | 39 | printf("Queue elements: "); 40 | for (int i = front; i <= rear; i++) { 41 | printf("%d ", queue[i]); 42 | } 43 | printf("\n"); 44 | } 45 | 46 | int main() { 47 | int numInsertions, value; 48 | 49 | printf("Enter how many times you want to insert data into the queue: "); 50 | scanf("%d", &numInsertions); 51 | 52 | for (int i = 0; i < numInsertions; i++) { 53 | printf("Enter a value to insert into the queue: "); 54 | scanf("%d", &value); 55 | enqueue(value); 56 | } 57 | 58 | displayQueue(); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /insertion sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void insertionSort(int arr[], int n) { 3 | int i, key, j; 4 | for (i = 1; i < n; i++) { 5 | key = arr[i]; 6 | j = i - 1; 7 | while (j >= 0 && arr[j] > key) { 8 | arr[j + 1] = arr[j]; 9 | j = j - 1; 10 | } 11 | arr[j + 1] = key; 12 | } 13 | } 14 | //code by Anshumansinha 15 | void printArray(int arr[], int size) { 16 | int i; 17 | for (i = 0; i < size; i++) 18 | printf("%d ", arr[i]); 19 | printf("\n"); 20 | } 21 | 22 | int main() { 23 | int arr[] = {12, 11, 13, 5, 6}; 24 | int n = sizeof(arr) / sizeof(arr[0]); 25 | 26 | printf("Original array: \n"); 27 | printArray(arr, n); 28 | 29 | insertionSort(arr, n); 30 | 31 | printf("Sorted array: \n"); 32 | printArray(arr, n); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /insertion.c: -------------------------------------------------------------------------------- 1 | #include 2 | void insertionSort(int arr[], int n) 3 | { 4 | for (int i = 1; i < n; i++) 5 | { 6 | int temp = arr[i]; 7 | int j = i - 1; 8 | while (j >= 0 && arr[j] > temp) 9 | { 10 | arr[j + 1] = arr[j]; 11 | j--; 12 | } 13 | arr[j + 1] = temp; 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | int n; 20 | printf("Enter the number of elements: "); 21 | scanf("%d", &n); 22 | 23 | int arr[n]; 24 | 25 | printf("Enter the elements:\n"); 26 | for (int i = 0; i < n; i++) 27 | { 28 | scanf("%d", &arr[i]); 29 | } 30 | 31 | insertionSort(arr, n); 32 | 33 | printf("Sorted array:\n"); 34 | for (int i = 0; i < n; i++) 35 | { 36 | printf("%d ", arr[i]); 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /merge sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void merge(int arr[], int left, int mid, int right) { 4 | int i, j, k; 5 | int n1 = mid - left + 1; 6 | int n2 = right - mid; 7 | 8 | int *leftArr = (int *)malloc(sizeof(int) * n1); 9 | int *rightArr = (int *)malloc(sizeof(int) * n2); 10 | 11 | for (i = 0; i < n1; i++) 12 | leftArr[i] = arr[left + i]; 13 | for (j = 0; j < n2; j++) 14 | rightArr[j] = arr[mid + 1 + j]; 15 | i = 0; 16 | j = 0; 17 | k = left; 18 | while (i < n1 && j < n2) { 19 | if (leftArr[i] <= rightArr[j]) { 20 | arr[k] = leftArr[i]; 21 | i++; 22 | } else { 23 | arr[k] = rightArr[j]; 24 | j++; 25 | } 26 | k++; 27 | } 28 | while (i < n1) { 29 | arr[k] = leftArr[i]; 30 | i++; 31 | k++; 32 | } 33 | while (j < n2) { 34 | arr[k] = rightArr[j]; 35 | j++; 36 | k++; 37 | } 38 | 39 | free(leftArr); 40 | free(rightArr); 41 | } 42 | 43 | void mergeSort(int arr[], int left, int right) { 44 | if (left < right) { 45 | int mid = left + (right - left) / 2; 46 | 47 | mergeSort(arr, left, mid); 48 | mergeSort(arr, mid + 1, right); 49 | 50 | merge(arr, left, mid, right); 51 | } 52 | } 53 | void printArray(int arr[], int size) { 54 | int i; 55 | for (i = 0; i < size; i++) 56 | printf("%d ", arr[i]); 57 | printf("\n"); 58 | } 59 | 60 | int main() { 61 | int arr[] = {12, 11, 13, 5, 6, 7}; 62 | int n = sizeof(arr) / sizeof(arr[0]); 63 | 64 | printf("Original array: \n"); 65 | printArray(arr, n); 66 | 67 | mergeSort(arr, 0, n - 1); 68 | 69 | printf("Sorted array: \n"); 70 | printArray(arr, n); 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /quicksort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int* a, int* b) { 3 | int t = *a; 4 | *a = *b; 5 | *b = t; 6 | } 7 | int partition(int arr[], int low, int high) { 8 | int pivot = arr[high]; 9 | int i = (low - 1); 10 | 11 | int j; 12 | for (j = low; j <= high - 1; j++) { 13 | if (arr[j] <= pivot) { 14 | i++; 15 | swap(&arr[i], &arr[j]); 16 | } 17 | } 18 | swap(&arr[i + 1], &arr[high]); 19 | return (i + 1); 20 | } 21 | void quicksort(int arr[], int low, int high) { 22 | if (low < high) { 23 | int pivot = partition(arr, low, high); 24 | 25 | quicksort(arr, low, pivot - 1); 26 | quicksort(arr, pivot + 1, high); 27 | } 28 | } 29 | 30 | void printArray(int arr[], int size) { 31 | int i; 32 | for (i = 0; i < size; i++) 33 | printf("%d ", arr[i]); 34 | printf("\n"); 35 | } 36 | 37 | int main() { 38 | int arr[] = {12, 11, 13, 5, 6, 7}; 39 | int n = sizeof(arr) / sizeof(arr[0]); 40 | 41 | printf("Original array: \n"); 42 | printArray(arr, n); 43 | 44 | quicksort(arr, 0, n - 1); 45 | 46 | printf("Sorted array: \n"); 47 | printArray(arr, n); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /selection sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int *a, int *b); 3 | void selectionSort(int arr[], int n); 4 | int main() { 5 | int n; 6 | printf("Enter the number of elements: "); 7 | scanf("%d", &n); 8 | int arr[n]; 9 | printf("Enter %d elements:\n", n); 10 | int i; 11 | for (i = 0; i < n; i++) { 12 | scanf("%d", &arr[i]); 13 | } 14 | selectionSort(arr, n); 15 | 16 | printf("Sorted array:\n"); 17 | for (i = 0; i < n; i++) { 18 | printf("%d ", arr[i]); 19 | } 20 | return 0; 21 | } 22 | void swap(int *a, int *b) { 23 | int temp = *a; 24 | *a = *b; 25 | *b = temp; 26 | } 27 | void selectionSort(int arr[], int n) { 28 | int i, j, minIndex; 29 | 30 | for (i = 0; i < n - 1; i++) { 31 | minIndex = i; 32 | for (j = i + 1; j < n; j++) { 33 | if (arr[j] < arr[minIndex]) { 34 | minIndex = j; 35 | } 36 | } 37 | swap(&arr[minIndex], &arr[i]); 38 | } 39 | } 40 | --------------------------------------------------------------------------------