├── .vscode └── launch.json ├── Addition of 2D Array └── addition_of_matrix.c ├── Binary Search Tree └── binary_search_tree.c ├── Binary Search ├── README.md └── binary_search.c ├── Bubble Sort ├── Bubble_Sort.c └── README.md ├── Circular Queue Using Linked List └── circular_queue_using_linked_list.c ├── Circular Queue using Array ├── README.md └── circular_queue_using_array.c ├── Heap Sort └── heap_sorting.c ├── Insertion Sort ├── README.md └── insertion_sort.c ├── Lab_work ├── bfs_using_linked_list.c ├── binary_search.c ├── dfs_using_linked_list.c ├── insertion_sort.c ├── readme.md └── tree_traversal_using_linked_list.c ├── Linear Search ├── Linear_Search.c └── readme.md ├── Multiplication of 2D Array └── multiplication_of_matrix.c ├── Practise └── Arrays │ └── Operations_on_array.c ├── Queue Using Array ├── Queue_Using_Array.c └── README.md ├── Queue using Linked List ├── Queue_Using_Linked_List.c └── README.md ├── README.md ├── Selection Sort ├── README.md └── selection_sort.c ├── Stack using Array ├── README.md └── stack_using_array.c ├── Stack using Linked List ├── README.md └── stack_using_linked_list.c ├── Substraction of 2D Array └── subsraction_of_matric.c └── Transpose of 2D Array ├── transpose_of_2d_array └── transpose_of_2d_array.c /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "(gdb) Launch", 9 | "type": "cppdbg", 10 | "request": "launch", 11 | "program": "enter program name, for example ${workspaceFolder}/a.out", 12 | "args": [], 13 | "stopAtEntry": false, 14 | "cwd": "${fileDirname}", 15 | "environment": [], 16 | "externalConsole": false, 17 | "MIMode": "gdb", 18 | "setupCommands": [ 19 | { 20 | "description": "Enable pretty-printing for gdb", 21 | "text": "-enable-pretty-printing", 22 | "ignoreFailures": true 23 | } 24 | ] 25 | } 26 | ] 27 | } -------------------------------------------------------------------------------- /Addition of 2D Array/addition_of_matrix.c: -------------------------------------------------------------------------------- 1 | // C program to add 2d array or matrices by taking input of elements form the user 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int i, j, m, n, sum = 0; //declaring variables 9 | printf("Enter the number of rows and columns of matrix 1: "); //taking input of rows and columns of matrix 1 10 | scanf("%d %d", &m, &n); 11 | int a[m][n]; 12 | printf("Enter the elements of matrix 1: \n"); //taking input of elements of matrix 1 13 | for (i = 0; i < m; i++) 14 | { //loop for rows 15 | for (j = 0; j < n; j++) 16 | { //loop for columns 17 | scanf("%d", &a[i][j]); 18 | } 19 | } 20 | printf("Enter the number of rows and columns of matrix 2: "); //taking input of rows and columns of matrix 2 21 | scanf("%d %d", &m, &n); 22 | int b[m][n]; 23 | printf("Enter the elements of matrix 2: \n"); 24 | for (i = 0; i < m; i++) 25 | { 26 | for (j = 0; j < n; j++) 27 | { 28 | scanf("%d", &b[i][j]); 29 | } 30 | } 31 | for (i = 0; i < m; i++) 32 | { // Loop for sum of rows 33 | for (j = 0; j < n; j++) 34 | { // Loop for sum of columns 35 | sum = sum + a[i][j] + b[i][j]; //sum of elements of matrix 1 and matrix 2 36 | } 37 | } 38 | // printing sum of 2 matrices in matrix form 39 | printf("Sum of 2 matrices is: \n"); 40 | for (i = 0; i < m; i++) 41 | { 42 | for (j = 0; j < n; j++) 43 | { 44 | printf("%d ", sum); 45 | } 46 | printf("\n"); 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Binary Search Tree/binary_search_tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //Represent a node of binary tree 6 | struct node 7 | { 8 | int data; 9 | struct node *left; 10 | struct node *right; 11 | }; 12 | 13 | //Represent the root of binary tree 14 | struct node *root = NULL; 15 | 16 | //createNode() will create a new node 17 | struct node *createNode(int data) 18 | { 19 | //Create a new node 20 | struct node *newNode = (struct node *)malloc(sizeof(struct node)); 21 | //Assign data to newNode, set left and right child to NULL 22 | newNode->data = data; 23 | newNode->left = NULL; 24 | newNode->right = NULL; 25 | 26 | return newNode; 27 | } //Represent a queue 28 | struct queue 29 | { 30 | int front, rear, size; 31 | struct node **arr; 32 | }; 33 | 34 | //createQueue() will create a queue 35 | struct queue *createQueue() 36 | { 37 | struct queue *newQueue = (struct queue *)malloc(sizeof(struct queue)); 38 | 39 | newQueue->front = -1; 40 | newQueue->rear = 0; 41 | newQueue->size = 0; 42 | 43 | newQueue->arr = (struct node **)malloc(100 * sizeof(struct node *)); 44 | 45 | return newQueue; 46 | } 47 | 48 | //Adds a node to queue 49 | void enqueue(struct queue *queue, struct node *temp) 50 | { 51 | queue->arr[queue->rear++] = temp; 52 | queue->size++; 53 | } 54 | 55 | //Deletes a node from queue 56 | struct node *dequeue(struct queue *queue) 57 | { 58 | queue->size--; 59 | return queue->arr[++queue->front]; 60 | } 61 | 62 | //insertNode() will add new node to the binary tree 63 | void insertNode(int data) 64 | { 65 | //Create a new node 66 | struct node *newNode = createNode(data); 67 | //Check whether tree is empty 68 | if (root == NULL) 69 | { 70 | root = newNode; 71 | return; 72 | } 73 | else 74 | { 75 | //Queue will be used to keep track of nodes of tree level-wise 76 | struct queue *queue = createQueue(); 77 | //Add root to the queue 78 | enqueue(queue, root); 79 | while (true) 80 | { 81 | struct node *node = dequeue(queue); 82 | //If node has both left and right child, add both the child to queue 83 | if (node->left != NULL && node->right != NULL) 84 | { 85 | enqueue(queue, node->left); 86 | enqueue(queue, node->right); 87 | } 88 | else 89 | { 90 | //If node has no left child, make newNode as left child 91 | if (node->left == NULL) 92 | { 93 | node->left = newNode; 94 | enqueue(queue, node->left); 95 | } 96 | //If node has left child but no right child, make newNode as right child 97 | else 98 | { 99 | node->right = newNode; 100 | enqueue(queue, node->right); 101 | } 102 | break; 103 | } 104 | } 105 | } 106 | } 107 | 108 | //inorder() will perform inorder traversal on binary search tree 109 | void inorderTraversal(struct node *node) 110 | { 111 | //Check whether tree is empty 112 | if (root == NULL) 113 | { 114 | printf("Tree is empty\n"); 115 | return; 116 | } 117 | else 118 | { 119 | 120 | if (node->left != NULL) 121 | inorderTraversal(node->left); 122 | printf("%d ", node->data); 123 | if (node->right != NULL) 124 | inorderTraversal(node->right); 125 | } 126 | } 127 | 128 | int main() 129 | { 130 | 131 | //Add nodes to the binary tree 132 | insertNode(1); 133 | //1 will become root node of the tree 134 | printf("Binary tree after insertion: \n"); 135 | //Binary after inserting nodes 136 | inorderTraversal(root); 137 | 138 | insertNode(2); 139 | insertNode(3); 140 | //2 will become left child and 3 will become right child of root node 1 141 | printf("\nBinary tree after insertion: \n"); 142 | //Binary after inserting nodes 143 | inorderTraversal(root); 144 | 145 | insertNode(4); 146 | insertNode(5); 147 | //4 will become left child and 5 will become right child of node 2 148 | printf("\nBinary tree after insertion: \n"); 149 | //Binary after inserting nodes 150 | inorderTraversal(root); 151 | 152 | insertNode(6); 153 | insertNode(7); 154 | //6 will become left child and 7 will become right child of node 3 155 | printf("\nBinary tree after insertion: \n"); 156 | //Binary after inserting nodes 157 | inorderTraversal(root); 158 | 159 | return 0; 160 | } 161 | -------------------------------------------------------------------------------- /Binary Search/README.md: -------------------------------------------------------------------------------- 1 | # Binary Search 2 | 3 | ## Binary Search Algorithm 4 | 5 | Binary search algorithm finds a given element in a list of elements with O(log n) time complexity where n is total number of elements in the list. The binary search algorithm can be used with only a sorted list of elements. That means the binary search is used only with a list of elements that are already arranged in an order. The binary search can not be used for a list of elements arranged in random order. This search process starts comparing the search element with the middle element in the list. If both are matched, then the result is "element found". Otherwise, we check whether the search element is smaller or larger than the middle element in the list. If the search element is smaller, then we repeat the same process for the left sublist of the middle element. If the search element is larger, then we repeat the same process for the right sublist of the middle element. We repeat this process until we find the search element in the list or until we left with a sublist of only one element. And if that element also doesn't match with the search element, then the result is "Element not found in the list". 6 | 7 | Binary search is implemented using following steps... 8 | 9 | - Step 1 - Read the search element from the user. 10 | - Step 2 - Find the middle element in the sorted list. 11 | - Step 3 - Compare the search element with the middle element in the sorted list. 12 | - Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function. 13 | - Step 5 - If both are not matched, then check whether the search element is smaller or larger than the middle element. 14 | - Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left sublist of the middle element. 15 | - Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right sublist of the middle element. 16 | - Step 8 - Repeat the same process until we find the search element in the list or until sublist contains only one element. 17 | - Step 9 - If that element also doesn't match with the search element, then display "Element is not found in the list!!!" and terminate the function. 18 | 19 | -------------------------------------------------------------------------------- /Binary Search/binary_search.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | int first, last, middle, size, i, sElement, list[100]; // Declare variables 7 | 8 | printf("Enter the size of the list: "); 9 | scanf("%d", &size); 10 | 11 | printf("Enter %d integer values in Assending order\n", size); 12 | 13 | for (i = 0; i < size; i++) // Input the values 14 | scanf("%d", &list[i]); 15 | 16 | printf("Enter value to be search: "); // Input the value to be search 17 | scanf("%d", &sElement); 18 | 19 | first = 0; // Initialize first and last 20 | last = size - 1; 21 | middle = (first + last) / 2; // Initialize middle 22 | 23 | while (first <= last) 24 | { 25 | if (list[middle] < sElement) // If middle element is less than search element 26 | first = middle + 1; 27 | else if (list[middle] == sElement) 28 | { 29 | printf("Element found at index %d.\n", middle); 30 | break; 31 | } 32 | else 33 | last = middle - 1; 34 | 35 | middle = (first + last) / 2; 36 | } 37 | if (first > last) 38 | printf("Element Not found in the list."); 39 | } -------------------------------------------------------------------------------- /Bubble Sort/Bubble_Sort.c: -------------------------------------------------------------------------------- 1 | // C program to sort an array using bubble sort 2 | #include 3 | 4 | int main() 5 | { 6 | int array[100], n, c, d, swap; // n = number of elements, c = counter, d = temporary variable 7 | 8 | printf("Enter number of elements\n"); // ask user for number of elements 9 | scanf("%d", &n); 10 | 11 | printf("Enter %d integers\n", n); 12 | 13 | for (c = 0; c < n; c++) // loop to input elements 14 | scanf("%d", &array[c]); 15 | 16 | for (c = 0; c < n - 1; c++) // loop to sort elements 17 | { 18 | for (d = 0; d < n - c - 1; d++) // loop to sort elements 19 | { 20 | if (array[d] > array[d + 1]) // if statement to swap elements 21 | /* For decreasing order use '<' instead of '>' */ 22 | { 23 | swap = array[d]; 24 | array[d] = array[d + 1]; 25 | array[d + 1] = swap; 26 | } 27 | } 28 | } 29 | 30 | printf("Sorted list in ascending order:\n"); // print sorted list 31 | 32 | for (c = 0; c < n; c++) // loop to print elements 33 | printf("%d\n", array[c]); 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Bubble Sort/README.md: -------------------------------------------------------------------------------- 1 | # Implementation of Bubble Sort Data Structure using C Language 2 | By : [Mohd. Huzaifa](https://www.instagram.com/huzaifa_raxtar/) 3 | 4 | ## Sorting 5 | 6 | - Sorting is the process of arranging data into meaningful order so that you can analyze it more effectively. For example, you might want to order sales data by calendar month so that you can produce a graph of sales performance.  7 | 8 | ## What is Bubble Sort? 9 | 10 | - Bubble sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. 11 | 12 | ## How Bubble Sort Works 13 | 14 | - We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and precise. 15 | - Bubble sort starts with very first two elements, comparing them to check which one is greater. 16 | 17 | 18 | 19 | 20 | ## Algorithm of Bubble Sort 21 | 22 | Bubble Sort in C is a sorting algorithm where we repeatedly iterate through the array and swap adjacent elements that are unordered. We repeat this until the array is sorted. 23 | 24 | We assume list is an array of n elements. We further assume that swap function swaps the values of the given array elements. 25 | 26 | ```C 27 | begin BubbleSort(list) 28 | 29 | for all elements of list 30 | if list[i] > list[i+1] 31 | swap(list[i], list[i+1]) 32 | end if 33 | end for 34 | 35 | return list 36 | 37 | end BubbleSort 38 | ``` 39 | 40 | ## Time Complexity of Bubble Sort 41 | 42 | 1. Worst case time complexity: O(n^2)
43 | Worst case occurs when array is reverse sorted. 44 | 45 | 2. Best case time complexity: O(n)
46 | Best case occurs when array is already sorted. 47 | 48 | 3. Average case time complexity: O(n^2) 49 | 50 | 51 | -------------------------------------------------------------------------------- /Circular Queue Using Linked List/circular_queue_using_linked_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct node 4 | { 5 | int data; 6 | struct node *next; 7 | } node; 8 | node *createQueue() 9 | { 10 | node *rear = NULL, *front = NULL; 11 | printf("How many items you want to create in queue?"); 12 | int n, data; 13 | scanf("%d", &n); 14 | int i = 0; 15 | while (i < n) 16 | { 17 | if (i == 0) 18 | { 19 | printf("Enter the data: "); 20 | scanf("%d", &data); 21 | node *newNode = (node *)malloc(sizeof(node)); 22 | newNode->data = data; 23 | newNode->next = NULL; 24 | rear = newNode; 25 | front = rear; 26 | } 27 | else 28 | { 29 | printf("Enter the data: "); 30 | scanf("%d", &data); 31 | node *newNode = (node *)malloc(sizeof(node)); 32 | newNode->data = data; 33 | newNode->next = front; 34 | rear->next = newNode; 35 | rear = newNode; 36 | } 37 | ++i; 38 | } 39 | return rear; 40 | } 41 | void display(node *rear, node *front) 42 | { 43 | if (!rear) 44 | { 45 | printf("\nNOTHING TO DISPLAY\n"); 46 | 47 | return; 48 | } 49 | node *temp = front; 50 | if (!rear->next) 51 | { 52 | printf("%d\n", rear->data); 53 | return; 54 | } 55 | if (temp != rear) 56 | { 57 | printf("%d ", temp->data); 58 | display(rear, temp->next); 59 | } 60 | else 61 | { 62 | printf("%d\n", temp->data); 63 | } 64 | } 65 | void enqueue(node **rear, int data) 66 | { 67 | node *newNode = (node *)malloc(sizeof(node)); 68 | newNode->data = data; 69 | if (!(*rear)) 70 | { 71 | newNode->next = NULL; 72 | *rear = newNode; 73 | return; 74 | } 75 | node *temp = *rear; 76 | if (!(*rear)->next) 77 | { 78 | newNode->next = temp; 79 | } 80 | else 81 | { 82 | newNode->next = temp->next; 83 | } 84 | temp->next = newNode; 85 | *rear = newNode; 86 | } 87 | int dequeue(node *rear) 88 | { 89 | int data; 90 | if (!rear) 91 | { 92 | printf("\nNO ITEMS IN THE QUEUE\n"); 93 | return 0; 94 | } 95 | if (!rear->next) 96 | { 97 | data = rear->data; 98 | free(rear); 99 | return data; 100 | } 101 | node *temp = rear->next; 102 | data = temp->data; 103 | rear->next = temp->next; 104 | free(temp); 105 | return data; 106 | } 107 | void main() 108 | { 109 | node *rear = createQueue(); 110 | display(rear, rear->next); 111 | printf("i am enqueing 99 \next"); 112 | enqueue(&rear, 99); 113 | display(rear, rear->next); 114 | int data = dequeue(rear); 115 | printf("Delete item is: %d\n", data); 116 | display(rear, rear->next); 117 | } 118 | -------------------------------------------------------------------------------- /Circular Queue using Array/README.md: -------------------------------------------------------------------------------- 1 | # Circular Queue using Array 2 | 3 | ## What is Circular Queue? 4 | A circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. 5 | 6 | ## Implementation of Circular Queue 7 | To implement a circular queue data structure using an array, we first perform the following steps before we implement actual operations. 8 | 9 | - Step 1 - Include all the header files which are used in the program and define a constant 'SIZE' with specific value. 10 | - Step 2 - Declare all user defined functions used in circular queue implementation. 11 | - Step 3 - Create a one dimensional array with above defined SIZE (int cQueue[SIZE]) 12 | - Step 4 - Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int front = -1, rear = -1) 13 | - Step 5 - Implement main method by displaying menu of operations list and make suitable function calls to perform operation selected by the user on circular queue. 14 | 15 | ## enQueue(value) - Inserting value into the Circular Queue 16 | 17 | In a circular queue, enQueue() is a function which is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at rear position. The enQueue() function takes one integer value as parameter and inserts that value into the circular queue. We can use the following steps to insert an element into the circular queue... 18 | 19 | - Step 1 - Check whether queue is FULL. ((rear == SIZE-1 && front == 0) || (front == rear+1)) 20 | - Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and terminate the function. 21 | - Step 3 - If it is NOT FULL, then check rear == SIZE - 1 && front != 0 if it is TRUE, then set rear = -1. 22 | - Step 4 - Increment rear value by one (rear++), set queue[rear] = value and check 'front == -1' if it is TRUE, then set front = 0. 23 | 24 | ## deQueue() - Deleting a value from the Circular Queue 25 | 26 | In a circular queue, deQueue() is a function used to delete an element from the circular queue. In a circular queue, the element is always deleted from front position. The deQueue() function doesn't take any value as a parameter. We can use the following steps to delete an element from the circular queue... 27 | 28 | - Step 1 - Check whether queue is EMPTY. (front == -1 && rear == -1) 29 | - Step 2 - If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and terminate the function. 30 | - Step 3 - If it is NOT EMPTY, then display queue[front] as deleted element and increment the front value by one (front ++). Then check whether front == SIZE, if it is TRUE, then set front = 0. Then check whether both front - 1 and rear are equal (front -1 == rear), if it TRUE, then set both front and rear to '-1' (front = rear = -1). 31 | 32 | ## display() - Displays the elements of a Circular Queue 33 | 34 | We can use the following steps to display the elements of a circular queue... 35 | 36 | - Step 1 - Check whether queue is EMPTY. (front == -1) 37 | - Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function. 38 | - Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front'. 39 | - Step 4 - Check whether 'front <= rear', if it is TRUE, then display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i <= rear' becomes FALSE. 40 | - Step 5 - If 'front <= rear' is FALSE, then display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until'i <= SIZE - 1' becomes FALSE. 41 | - Step 6 - Set i to 0. 42 | - Step 7 - Again display 'cQueue[i]' value and increment i value by one (i++). Repeat the same until 'i <= rear' becomes FALSE. -------------------------------------------------------------------------------- /Circular Queue using Array/circular_queue_using_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 5 // defining the size of the queue 4 | 5 | void enQueue(int); // function to enqueue 6 | void deQueue(); // function to dequeue 7 | void display(); // function to display the queue 8 | 9 | int cQueue[SIZE], front = -1, rear = -1; // declaring the queue array and front and rear variables 10 | 11 | void main() 12 | { 13 | int choice, value; 14 | while (1) 15 | { 16 | printf("\n****** MENU ******\n"); 17 | printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n"); 18 | printf("Enter your choice: "); 19 | scanf("%d", &choice); 20 | switch (choice) 21 | { 22 | case 1: // inserting the value in the queue 23 | printf("\nEnter the value to be insert: "); 24 | scanf("%d", &value); 25 | enQueue(value); 26 | break; 27 | case 2: // deleting the value from the queue 28 | deQueue(); 29 | break; 30 | case 3: // displaying the queue 31 | display(); 32 | break; 33 | case 4: 34 | exit(0); 35 | default: 36 | printf("\nPlease select the correct choice!!!\n"); 37 | } 38 | } 39 | } 40 | void enQueue(int value) 41 | { 42 | if ((front == 0 && rear == SIZE - 1) || (front == rear + 1)) 43 | printf("\nCircular Queue is Full! Insertion not possible!!!\n"); 44 | else 45 | { 46 | if (rear == SIZE - 1 && front != 0) 47 | rear = -1; 48 | cQueue[++rear] = value; 49 | printf("\nInsertion Success!!!\n"); 50 | if (front == -1) 51 | front = 0; 52 | } 53 | } 54 | void deQueue() 55 | { 56 | if (front == -1 && rear == -1) 57 | printf("\nCircular Queue is Empty! Deletion is not possible!!!\n"); 58 | else 59 | { 60 | printf("\nDeleted element : %d\n", cQueue[front++]); 61 | if (front == SIZE) 62 | front = 0; 63 | if (front - 1 == rear) 64 | front = rear = -1; 65 | } 66 | } 67 | void display() 68 | { 69 | if (front == -1) 70 | printf("\nCircular Queue is Empty!!!\n"); 71 | else 72 | { 73 | int i = front; 74 | printf("\nCircular Queue Elements are : \n"); 75 | if (front <= rear) 76 | { 77 | while (i <= rear) 78 | printf("%d\t", cQueue[i++]); 79 | } 80 | else 81 | { 82 | while (i <= SIZE - 1) 83 | printf("%d\t", cQueue[i++]); 84 | i = 0; 85 | while (i <= rear) 86 | printf("%d\t", cQueue[i++]); 87 | } 88 | } 89 | } -------------------------------------------------------------------------------- /Heap Sort/heap_sorting.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int *a, int *b) 4 | { 5 | int tmp = *a; 6 | *a = *b; 7 | *b = tmp; 8 | } 9 | 10 | void heapify(int arr[], int n, int i) 11 | { 12 | int max = i; //Initialize max as root 13 | int leftChild = 2 * i + 1; 14 | int rightChild = 2 * i + 2; 15 | 16 | //If left child is greater than root 17 | if (leftChild < n && arr[leftChild] > arr[max]) 18 | max = leftChild; 19 | 20 | //If right child is greater than max 21 | if (rightChild < n && arr[rightChild] > arr[max]) 22 | max = rightChild; 23 | //If max is not root 24 | if (max != i) 25 | { 26 | swap(&arr[i], &arr[max]); 27 | //heapify the affected sub-tree recursively 28 | heapify(arr, n, max); 29 | } 30 | } 31 | 32 | //Main function to perform heap sort 33 | void heapSort(int arr[], int n) 34 | { 35 | //Rearrange array (building heap) 36 | for (int i = n / 2 - 1; i >= 0; i--) 37 | heapify(arr, n, i); 38 | 39 | //Extract elements from heap one by one 40 | for (int i = n - 1; i >= 0; i--) 41 | { 42 | swap(&arr[0], &arr[i]); //Current root moved to the end 43 | 44 | heapify(arr, i, 0); //calling max heapify on the heap reduced 45 | } 46 | } 47 | //print size of array n using utility function 48 | void display(int arr[], int n) 49 | { 50 | for (int i = 0; i < n; ++i) 51 | printf("%d ", arr[i]); 52 | printf("\n"); 53 | } 54 | 55 | //Driver code 56 | int main() 57 | { 58 | int arr[] = {11, 34, 9, 5, 16, 10}; 59 | int n = sizeof(arr) / sizeof(arr[0]); 60 | 61 | printf("Original array:\n"); 62 | display(arr, n); 63 | heapSort(arr, n); 64 | 65 | printf("Sorted array:\n"); 66 | display(arr, n); 67 | } 68 | -------------------------------------------------------------------------------- /Insertion Sort/README.md: -------------------------------------------------------------------------------- 1 | # Insertion Sort 2 | 3 | Insertion sort algorithm arranges a list of elements in a particular order. In insertion sort algorithm, every iteration moves an element from unsorted portion to sorted portion until all the elements are sorted in the list. 4 | 5 | ## Process for insertion sort 6 | 7 | The insertion sort algorithm is performed using the following steps... 8 | 9 | - Step 1 - Assume that first element in the list is in sorted portion and all the remaining elements are in unsorted portion. 10 | - Step 2: Take first element from the unsorted portion and insert that element into the sorted portion in the order specified. 11 | - Step 3: Repeat the above process until all the elements from the unsorted portion are moved into the sorted portion. 12 | 13 | ## Complexity of the Insertion Sort Algorithm 14 | 15 | To sort an unsorted list with 'n' number of elements, we need to make (1+2+3+......+n-1) = (n (n-1))/2 number of comparisions in the worst case. If the list is already sorted then it requires 'n' number of comparisions. 16 | 17 | - **Worst Case** : O(n^2) 18 | - **Best Case** : Ω(n) 19 | - **Average Case** : Θ(n^2) -------------------------------------------------------------------------------- /Insertion Sort/insertion_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | 7 | int size, i, j, temp, list[100]; 8 | 9 | printf("Enter the size of the list: "); 10 | scanf("%d", &size); 11 | 12 | printf("Enter %d integer values: ", size); 13 | for (i = 0; i < size; i++) 14 | scanf("%d", &list[i]); 15 | 16 | //Insertion sort logic 17 | for (i = 1; i < size; i++) 18 | { 19 | temp = list[i]; 20 | j = i - 1; 21 | while ((temp < list[j]) && (j >= 0)) 22 | { 23 | list[j + 1] = list[j]; 24 | j = j - 1; 25 | } 26 | list[j + 1] = temp; 27 | } 28 | 29 | printf("List after Sorting is: "); 30 | for (i = 0; i < size; i++) 31 | printf(" %d", list[i]); 32 | } -------------------------------------------------------------------------------- /Lab_work/bfs_using_linked_list.c: -------------------------------------------------------------------------------- 1 | // Implementation of Breadth First Search using Linked List 2 | 3 | 4 | #include 5 | #include 6 | 7 | #define MAX 100 8 | 9 | #define initial 1 10 | #define waiting 2 11 | #define visited 3 12 | 13 | int n; 14 | int adj[MAX][MAX]; 15 | int state[MAX]; 16 | void create_graph(); 17 | void BF_Traversal(); 18 | void BFS(int v); 19 | 20 | int queue[MAX], front = -1, rear = -1; 21 | void insert_queue(int vertex); 22 | int delete_queue(); 23 | int isEmpty_queue(); 24 | 25 | int main() 26 | { 27 | create_graph(); 28 | BF_Traversal(); 29 | return 0; 30 | } 31 | 32 | void BF_Traversal() 33 | { 34 | int v; 35 | for (v = 0; v < n; v++) 36 | state[v] = initial; 37 | printf("Enter Start Vertex for BFS: \n"); 38 | scanf("%d", &v); 39 | BFS(v); 40 | } 41 | 42 | void BFS(int v) 43 | { 44 | int i; 45 | insert_queue(v); 46 | state[v] = waiting; 47 | while (!isEmpty_queue()) 48 | { 49 | v = delete_queue(); 50 | printf("%d ", v); 51 | state[v] = visited; 52 | for (i = 0; i < n; i++) 53 | { 54 | if (adj[v][i] == 1 && state[i] == initial) 55 | { 56 | insert_queue(i); 57 | state[i] = waiting; 58 | } 59 | } 60 | } 61 | printf("\n"); 62 | } 63 | 64 | void insert_queue(int vertex) 65 | { 66 | if (rear == MAX - 1) 67 | printf("Queue Overflow\n"); 68 | else 69 | { 70 | if (front == -1) 71 | front = 0; 72 | rear = rear + 1; 73 | queue[rear] = vertex; 74 | } 75 | } 76 | 77 | int isEmpty_queue() 78 | { 79 | if (front == -1 || front > rear) 80 | return 1; 81 | else 82 | return 0; 83 | } 84 | 85 | int delete_queue() 86 | { 87 | int delete_item; 88 | if (front == -1 || front > rear) 89 | { 90 | printf("Queue Underflow\n"); 91 | exit(1); 92 | } 93 | delete_item = queue[front]; 94 | front = front + 1; 95 | return delete_item; 96 | } 97 | 98 | void create_graph() 99 | { 100 | int count, max_edge, origin, destin; 101 | 102 | printf("Enter number of vertices : "); 103 | scanf("%d", &n); 104 | max_edge = n * (n - 1); 105 | 106 | for (count = 1; count <= max_edge; count++) 107 | { 108 | printf("Enter edge %d( -1 -1 to quit ) : ", count); 109 | scanf("%d %d", &origin, &destin); 110 | 111 | if ((origin == -1) && (destin == -1)) 112 | break; 113 | 114 | if (origin >= n || destin >= n || origin < 0 || destin < 0) 115 | { 116 | printf("Invalid edge!\n"); 117 | count--; 118 | } 119 | else 120 | { 121 | adj[origin][destin] = 1; 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /Lab_work/binary_search.c: -------------------------------------------------------------------------------- 1 | // Write a C program to implement Binary Search on an array of integres which takes input from the user. 2 | 3 | #include 4 | #include 5 | 6 | /* In binary search, we divide the array into two parts. 7 | The first part contains all the elements which are smaller than the element to be searched. 8 | The second part contains all the elements which are greater than the element to be searched. 9 | We keep dividing the array into two parts until we find the element to be searched. 10 | If the element to be searched is not found, then we return -1. */ 11 | 12 | // Function to perform binary search 13 | 14 | int binarySearch(int arr[], int l, int r, int x) 15 | { 16 | // Base 17 | if (r >= l) 18 | { 19 | int mid = l + (r - l) / 2; 20 | // If the element is present at the middle itself 21 | if (arr[mid] == x) 22 | return mid; 23 | // If 24 | if (arr[mid] > x) 25 | return binarySearch(arr, l, mid - 1, x); 26 | return binarySearch(arr, mid + 1, r, x); 27 | } 28 | return -1; 29 | } 30 | 31 | // Driver program to test above function 32 | int main() 33 | { 34 | int arr[100], n, x, result; 35 | printf("Enter the number of elements: "); 36 | scanf("%d", &n); 37 | printf("Enter the elements: "); 38 | for (int i = 0; i < n; i++) 39 | { 40 | scanf("%d", &arr[i]); 41 | } 42 | x = sizeof(arr) / sizeof(arr[0]); 43 | printf("\nEnter the element to be searched: "); 44 | scanf("%d", &x); 45 | result = binarySearch(arr, 0, n - 1, x); 46 | if (result == -1) 47 | printf("Element not found"); 48 | else 49 | printf("Element found at index %d\n", result); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Lab_work/dfs_using_linked_list.c: -------------------------------------------------------------------------------- 1 | // Implementation of Depth First Search using Linked List 2 | 3 | // DFS algorithm in C 4 | 5 | #include 6 | #include 7 | 8 | struct node 9 | { 10 | int vertex; 11 | struct node *next; 12 | }; 13 | 14 | struct node *createNode(int v); 15 | 16 | struct Graph 17 | { 18 | int numVertices; 19 | int *visited; 20 | 21 | // We need int** to store a two dimensional array. 22 | // Similary, we need struct node** to store an array of Linked lists 23 | struct node **adjLists; 24 | }; 25 | 26 | // DFS algo 27 | void DFS(struct Graph *graph, int vertex) 28 | { 29 | struct node *adjList = graph->adjLists[vertex]; 30 | struct node *temp = adjList; 31 | 32 | graph->visited[vertex] = 1; 33 | printf("Visited %d \n", vertex); 34 | 35 | while (temp != NULL) 36 | { 37 | int connectedVertex = temp->vertex; 38 | 39 | if (graph->visited[connectedVertex] == 0) 40 | { 41 | DFS(graph, connectedVertex); 42 | } 43 | temp = temp->next; 44 | } 45 | } 46 | 47 | // Create a node 48 | struct node *createNode(int v) 49 | { 50 | struct node *newNode = malloc(sizeof(struct node)); 51 | newNode->vertex = v; 52 | newNode->next = NULL; 53 | return newNode; 54 | } 55 | 56 | // Create graph 57 | struct Graph *createGraph(int vertices) 58 | { 59 | struct Graph *graph = malloc(sizeof(struct Graph)); 60 | graph->numVertices = vertices; 61 | 62 | graph->adjLists = malloc(vertices * sizeof(struct node *)); 63 | 64 | graph->visited = malloc(vertices * sizeof(int)); 65 | 66 | int i; 67 | for (i = 0; i < vertices; i++) 68 | { 69 | graph->adjLists[i] = NULL; 70 | graph->visited[i] = 0; 71 | } 72 | return graph; 73 | } 74 | 75 | // Add edge 76 | void addEdge(struct Graph *graph, int src, int dest) 77 | { 78 | // Add edge from src to dest 79 | struct node *newNode = createNode(dest); 80 | newNode->next = graph->adjLists[src]; 81 | graph->adjLists[src] = newNode; 82 | 83 | // Add edge from dest to src 84 | newNode = createNode(src); 85 | newNode->next = graph->adjLists[dest]; 86 | graph->adjLists[dest] = newNode; 87 | } 88 | 89 | // Print the graph 90 | void printGraph(struct Graph *graph) 91 | { 92 | int v; 93 | for (v = 0; v < graph->numVertices; v++) 94 | { 95 | struct node *temp = graph->adjLists[v]; 96 | printf("\n Adjacency list of vertex %d\n ", v); 97 | while (temp) 98 | { 99 | printf("%d -> ", temp->vertex); 100 | temp = temp->next; 101 | } 102 | printf("\n"); 103 | } 104 | } 105 | 106 | int main() 107 | { 108 | struct Graph *graph = createGraph(4); 109 | addEdge(graph, 0, 1); 110 | addEdge(graph, 0, 2); 111 | addEdge(graph, 1, 2); 112 | addEdge(graph, 2, 3); 113 | 114 | printGraph(graph); 115 | 116 | DFS(graph, 2); 117 | 118 | return 0; 119 | } -------------------------------------------------------------------------------- /Lab_work/insertion_sort.c: -------------------------------------------------------------------------------- 1 | // Write a C program to implement Insertion Sort on an array of integres which takes input from the user. 2 | 3 | /* 4 | In insertion sort, we insert an element into a sorted array. 5 | The element is inserted into the correct position in the array. 6 | We start from the second element and compare it with the first element. 7 | If the first element is greater than the second element, we swap them. 8 | We continue the process and compare the second element with the third element and so on. 9 | keep repeating the process until we reach the last element. 10 | */ 11 | /* C Program to sort an array in ascending order using Insertion Sort */ 12 | #include 13 | int main() 14 | { 15 | int n, i, j, temp; 16 | int arr[64]; 17 | 18 | printf("Enter number of elements\n"); 19 | scanf("%d", &n); 20 | 21 | printf("Enter %d integers\n", n); 22 | for (i = 0; i < n; i++) 23 | { 24 | scanf("%d", &arr[i]); 25 | } 26 | for (i = 1; i <= n - 1; i++) 27 | { 28 | j = i; 29 | while (j > 0 && arr[j - 1] > arr[j]) 30 | { 31 | temp = arr[j]; 32 | arr[j] = arr[j - 1]; 33 | arr[j - 1] = temp; 34 | j--; 35 | } 36 | } 37 | printf("Sorted list in ascending order:\n"); 38 | for (i = 0; i <= n - 1; i++) 39 | { 40 | printf("%d\n", arr[i]); 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Lab_work/readme.md: -------------------------------------------------------------------------------- 1 | # Data Structures Lab Work 2 | 3 | 4 | 1. Implement Tree Traversal using Linked Lists 5 | 2. Implement Breadth First Search using Linked List 6 | 3. Implement Depth First Search using Linked List 7 | 4. Implement Binary Search 8 | 5. Implement Insertion Sort 9 | -------------------------------------------------------------------------------- /Lab_work/tree_traversal_using_linked_list.c: -------------------------------------------------------------------------------- 1 | // Implementation of a binary tree traversal using a linked list. 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // Represent a node of binary tree 9 | struct node 10 | { 11 | int data; 12 | struct node *left; 13 | struct node *right; 14 | }; 15 | 16 | // Represent the root of binary tree 17 | struct node *root = NULL; 18 | 19 | // createNode() will create a new node 20 | struct node *createNode(int data) 21 | { 22 | // Create a new node 23 | struct node *newNode = (struct node *)malloc(sizeof(struct node)); 24 | // Assign data to newNode, set left and right child to NULL 25 | newNode->data = data; 26 | newNode->left = NULL; 27 | newNode->right = NULL; 28 | 29 | return newNode; 30 | } // Represent a queue 31 | struct queue 32 | { 33 | int front, rear, size; 34 | struct node **arr; 35 | }; 36 | 37 | // createQueue() will create a queue 38 | struct queue *createQueue() 39 | { 40 | struct queue *newQueue = (struct queue *)malloc(sizeof(struct queue)); 41 | 42 | newQueue->front = -1; 43 | newQueue->rear = 0; 44 | newQueue->size = 0; 45 | 46 | newQueue->arr = (struct node **)malloc(100 * sizeof(struct node *)); 47 | 48 | return newQueue; 49 | } 50 | 51 | // Adds a node to queue 52 | void enqueue(struct queue *queue, struct node *temp) 53 | { 54 | queue->arr[queue->rear++] = temp; 55 | queue->size++; 56 | } 57 | 58 | // Deletes a node from queue 59 | struct node *dequeue(struct queue *queue) 60 | { 61 | queue->size--; 62 | return queue->arr[++queue->front]; 63 | } 64 | 65 | // insertNode() will add new node to the binary tree 66 | void insertNode(int data) 67 | { 68 | // Create a new node 69 | struct node *newNode = createNode(data); 70 | // Check whether tree is empty 71 | if (root == NULL) 72 | { 73 | root = newNode; 74 | return; 75 | } 76 | else 77 | { 78 | // Queue will be used to keep track of nodes of tree level-wise 79 | struct queue *queue = createQueue(); 80 | // Add root to the queue 81 | enqueue(queue, root); 82 | while (true) 83 | { 84 | struct node *node = dequeue(queue); 85 | // If node has both left and right child, add both the child to queue 86 | if (node->left != NULL && node->right != NULL) 87 | { 88 | enqueue(queue, node->left); 89 | enqueue(queue, node->right); 90 | } 91 | else 92 | { 93 | // If node has no left child, make newNode as left child 94 | if (node->left == NULL) 95 | { 96 | node->left = newNode; 97 | enqueue(queue, node->left); 98 | } 99 | // If node has left child but no right child, make newNode as right child 100 | else 101 | { 102 | node->right = newNode; 103 | enqueue(queue, node->right); 104 | } 105 | break; 106 | } 107 | } 108 | } 109 | } 110 | 111 | // inorder() will perform inorder traversal on binary search tree 112 | void inorderTraversal(struct node *node) 113 | { 114 | // Check whether tree is empty 115 | if (root == NULL) 116 | { 117 | printf("Tree is empty\n"); 118 | return; 119 | } 120 | else 121 | { 122 | 123 | if (node->left != NULL) 124 | inorderTraversal(node->left); 125 | printf("%d ", node->data); 126 | if (node->right != NULL) 127 | inorderTraversal(node->right); 128 | } 129 | } 130 | 131 | int main() 132 | { 133 | 134 | // Add nodes to the binary tree 135 | insertNode(1); 136 | // 1 will become root node of the tree 137 | printf("Binary tree after insertion: \n"); 138 | // Binary after inserting nodes 139 | inorderTraversal(root); 140 | 141 | insertNode(2); 142 | insertNode(3); 143 | // 2 will become left child and 3 will become right child of root node 1 144 | printf("\nBinary tree after insertion: \n"); 145 | // Binary after inserting nodes 146 | inorderTraversal(root); 147 | 148 | insertNode(4); 149 | insertNode(5); 150 | // 4 will become left child and 5 will become right child of node 2 151 | printf("\nBinary tree after insertion: \n"); 152 | // Binary after inserting nodes 153 | inorderTraversal(root); 154 | 155 | insertNode(6); 156 | insertNode(7); 157 | // 6 will become left child and 7 will become right child of node 3 158 | printf("\nBinary tree after insertion: \n"); 159 | // Binary after inserting nodes 160 | inorderTraversal(root); 161 | 162 | return 0; 163 | } 164 | -------------------------------------------------------------------------------- /Linear Search/Linear_Search.c: -------------------------------------------------------------------------------- 1 | /* 2 | C program to input N numbers and store them in an array. 3 | Do a linear search for a given key and report success or failure. 4 | */ 5 | // By: Priyanshu, Sahil Ali & Subhan Raj 6 | #include 7 | 8 | int main() 9 | { 10 | int num; 11 | 12 | int i, keynum, found = 0; 13 | 14 | printf("\nEnter the number of elements "); 15 | scanf("%d", &num); 16 | int array[num]; 17 | printf("Enter the elements one by one \n"); 18 | for (i = 0; i < num; i++) 19 | { 20 | scanf("%d", &array[i]); 21 | } 22 | 23 | printf("\nEnter the element to be searched "); 24 | scanf("%d", &keynum); 25 | /* Linear search begins */ 26 | for (i = 0; i < num; i++) 27 | { 28 | if (keynum == array[i]) 29 | { 30 | found = 1; 31 | break; 32 | } 33 | } 34 | if (found == 1) 35 | { 36 | 37 | printf("\nElement is present in the array at index %d and\n", i); 38 | printf("\nThe position of element in Array is %d\n", i + 1); 39 | } 40 | else 41 | printf("\nElement is not present in the array\n"); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Linear Search/readme.md: -------------------------------------------------------------------------------- 1 | # C Program to Implement Linear Search 2 | 3 | A C Program which finds the position of an element in an array using Linear Search Algorithm. We have to take an array and a value to be searched in the array as input from the user, and then find the position of that element in array by using linear search algorithm. 4 | 5 | ## Expected Input and Output 6 | 7 | 1. Average Case: If the searched element is other than the first and the last element of the array. For example: 8 | 9 | ```C 10 | If the input array is: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 11 | And the element to be searched is: 5 12 | Then the output should be: 4 13 | ``` 14 | Average case time complexity: O(n) 15 | 16 | 2. Best Case: If the searched element is the first element of the array. For example: 17 | 18 | ```C 19 | If the input array is {4, 6, 1, 2, 5, 3} 20 | and if the element searched is 4, 21 | then the expected output will be Position 0. 22 | ``` 23 | Best case time complexity: O(1) 24 | 25 | 3. Worst Case: If the searched element is the last element of the array. For example: 26 | 27 | ```C 28 | If the input array is {4, 6, 1, 2, 5, 3} 29 | and if the element searched is 3, 30 | then the expected output will be Position 5. 31 | ``` 32 | Worst case time complexity: O(n) 33 | 34 | ## Solution of the problem 35 | 36 | 1. We will take the input array and the element to be searched as input from the user. 37 | 2. For finding the position of the element in the array, we will start from the first element of the array and compare it with the element to be searched. 38 | 3. We will keep on comparing the element with the element to be searched until we find the element to be searched. 39 | 4. We will return the position of the element to be searched. 40 | 5. we will return ```false``` if the element to be searched is not found in the array. 41 |
42 | 43 | Cases in which program will return false: 44 | 45 | 1. we will return false if the array is empty. 46 | 2. we will return false if the element to be searched is not an integer. 47 | 3. we will return false if the array is not an array. 48 | 4. we will return false if the array is not an array of integers. 49 | 50 |
51 | 52 | ## Program Explanation 53 | 54 | 1. In Linear search, we search an element or value in a given array by traversing the array from the starting, till the desired element or value is found. 55 | 2. The array is searched sequentially and the position is returned if the key element to be searched is available in the array, otherwise -1 is returned. 56 | 3. Here in this C Program we have not made any function specifically for linear search, rather we can look for presence of element in an array in the main function itself. 57 | 4. We traverse the array from the 0th index in increasing order of index, if we find the element we break the loop there itself and print the position of the element in the array, but if the element requested is not there in array, we simply print that “Element is not present in the array”. 58 | 5. If we’d have created a separate function for linear search and the element could not be found in the array, we would have returned -1 in that case denoting absence of the element. 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /Multiplication of 2D Array/multiplication_of_matrix.c: -------------------------------------------------------------------------------- 1 | // C program to multiply 2d array or matrices by taking input of elements form the user 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | printf("\n\n *** C program for Multiplication of 2D arrays or matrices*** \n\n"); 9 | int i, j, k, m, n, p, q; // i,j,k,m,n,p,q are used for looping 10 | printf("Enter the number of rows and columns of first matrix: "); // taking input of rows and columns of first matrix 11 | scanf("%d %d", &m, &n); 12 | printf("Enter the number of rows and columns of second matrix: "); // taking input of rows and columns of second matrix 13 | scanf("%d %d", &p, &q); 14 | if (n != p) // checking if the number of columns of first matrix is equal to the number of rows of second matrix 15 | { 16 | printf("\nThe multiplication of matrices is not possible.\n"); // printing error message 17 | printf("\nThe number of columns of first matrix must be equal to the number of rows of second matrix.\n"); 18 | return 0; 19 | } 20 | else // if the number of columns of first matrix is equal to the number of rows of second matrix 21 | { 22 | int a[m][n], b[p][q], c[m][q]; // declaring 2d arrays a,b,c 23 | printf("Enter the elements of first matrix: \n"); // taking input of elements of first matrix 24 | for (i = 0; i < m; i++) 25 | { 26 | for (j = 0; j < n; j++) 27 | { 28 | scanf("%d", &a[i][j]); 29 | } 30 | } 31 | printf("Enter the elements of second matrix: \n"); // taking input of elements of second matrix 32 | for (i = 0; i < p; i++) 33 | { 34 | for (j = 0; j < q; j++) 35 | { 36 | scanf("%d", &b[i][j]); 37 | } 38 | } 39 | for (i = 0; i < m; i++) 40 | { 41 | for (j = 0; j < q; j++) 42 | { 43 | c[i][j] = 0; // initializing elements of c to 0 44 | for (k = 0; k < n; k++) 45 | { 46 | c[i][j] += a[i][k] * b[k][j]; // calculating elements of c 47 | } 48 | } 49 | } 50 | printf("The product of the two matrices is: \n"); 51 | for (i = 0; i < m; i++) 52 | { 53 | for (j = 0; j < q; j++) 54 | { 55 | printf("%d ", c[i][j]); // printing elements of c 56 | } 57 | printf("\n"); 58 | } 59 | } 60 | 61 | return 0; 62 | } -------------------------------------------------------------------------------- /Practise/Arrays/Operations_on_array.c: -------------------------------------------------------------------------------- 1 | // Traversal in Array 2 | #include 3 | 4 | int main() 5 | { 6 | /* Creating an Array and taking input */ 7 | //Taking input of number of elements in array 8 | int size; 9 | printf("\nEnter number of Elements in Array: "); 10 | scanf("%d", &size); 11 | int array[size]; 12 | printf("\nEnter %d Elements in Array: ", size); 13 | for (int i = 0; i < size; i++) 14 | { 15 | scanf("\n%d", &array[i]); 16 | } 17 | // printing the array 18 | for (int i = 0; i < size; i++) 19 | { 20 | printf("\n%d", array[i]); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Queue Using Array/Queue_Using_Array.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | // C Program To Implementation Of Queue Using Array 4 | 5 | #include 6 | #include 7 | #define MAX 10 //Defines the maximum size of the queue 8 | void insert(); //Function to insert an element in the queue 9 | void delete (); //Function to delete an element from the queue 10 | void display(); //Function to display the queue 11 | int queue[MAX]; //Array to store the queue 12 | int rear = -1; //Index of the rear of the queue 13 | int front = -1; //Index of the front of the queue 14 | int main() 15 | { 16 | int choice; //Variable to store the choice of the user 17 | while (1) //Loop to continue the program 18 | { 19 | printf("******** Choices ********\n"); 20 | printf("\n Warning: Do Not Enter Any Other Symbols and Alphabets Except Numbers\n\n"); 21 | printf("1. Insert element to queue \n"); 22 | printf("2. Delete element from queue \n"); 23 | printf("3. Display all elements of queue \n"); 24 | printf("4. Quit \n\n"); 25 | printf("Enter your choice : "); 26 | scanf("%d", &choice); 27 | 28 | switch (choice) //Switch case to perform the required operation 29 | { 30 | case 1: //Insert element to queue 31 | insert(); 32 | printf("\n"); 33 | break; 34 | case 2: //Delete element from queue 35 | delete (); 36 | printf("\n"); 37 | break; 38 | case 3: //Display all elements of queue 39 | display(); 40 | printf("\n"); 41 | break; 42 | case 4: //Quit 43 | exit(0); 44 | break; 45 | default: //Invalid choice 46 | printf("Wrong choice \n"); 47 | printf("\n"); 48 | break; 49 | } 50 | } 51 | return 0; 52 | } 53 | 54 | // ------------------Insert Function which helps to insert elements in array queue---------------- 55 | 56 | void insert() 57 | { 58 | int Value; 59 | if (rear == MAX - 1) 60 | printf("Queue is FULL!!! Insertion is not possible!!!\n"); 61 | else 62 | { 63 | if (front == -1) 64 | front = 0; 65 | printf("Insert the element in queue : "); 66 | scanf("%d", &Value); 67 | rear = rear + 1; 68 | queue[rear] = Value; 69 | } 70 | } 71 | 72 | 73 | 74 | // ------------------Delete Function which helps to Delete elements From array queue---------------- 75 | void delete () 76 | { 77 | if (front == -1 || front > rear) 78 | { 79 | printf("Queue is EMPTY!!! Deletion is not possible!!! \n"); 80 | return; 81 | } 82 | else 83 | { 84 | printf("Element deleted from queue is : %d\n", queue[front]); 85 | front = front + 1; 86 | } 87 | } 88 | 89 | // ------------------Display Function which helps to Display all the elements of array queue---------------- 90 | void display() 91 | { 92 | int i; 93 | if (front == -1) 94 | printf("Queue is EMPTY!!! \n"); 95 | else 96 | { 97 | printf("All Elements of Queue is : "); 98 | for (i = front; i <= rear; i++) 99 | printf("%d ", queue[i]); 100 | printf("\n"); 101 | } 102 | } -------------------------------------------------------------------------------- /Queue Using Array/README.md: -------------------------------------------------------------------------------- 1 | # Implementation of Queue Data Structure Using Array 2 | 3 | ## What is Queue? 4 | A queue is a data structure in which whatever comes first will go out first. It follows the “FIFO (First-In-First-Out)” policy. In Queue, the insertion is done from one end known as the rear end or the tail of the queue, whereas the deletion is done from another end known as the front end or the head of the queue. 5 | 6 | ## Operations on Queue 7 | - Enqueue: The enqueue operation is used to insert the element at the rear end of the queue. 8 | - Dequeue: The dequeue operation performs the deletion from the front-end of the queue. 9 | - Peek: This is the third operation that returns the element, which is pointed by the front pointer in the queue but does not delete it. 10 | - Queue overflow (isfull): When the Queue is completely full, then it shows the overflow condition. 11 | - Queue underflow (isempty): When the Queue is empty, i.e., no elements are in the Queue then it throws the underflow condition. 12 | 13 | ## Implementation of Queue Using Array in C language 14 | 15 |

Queue data structure using array can be implemented as follows...

16 | Step 1. Include all the header files which are used in the program and define a constant 'MAX' with specific value.
17 | Step 2. Declare all the user defined functions which are used in queue implementation.
18 | Step 3. Create a one dimensional array with above defined MAX (int queue[MAX])
19 | Step 4. Define two integer variables 'front' and 'rear' and initialize both with '-1'.
20 | Step 5. Then implement main method by displaying menu of operations list and make suitable function calls to perform operation selected by the user on queue.
21 | 22 | ## Algorithm for Queue Implementation Using Array 23 | ### Algorithm to insert any element in a queue (Enqueue) 24 | - Check if the queue is already full by comparing rear to max - 1. if so, then return an overflow error. 25 | - If the item is to be inserted as the first element in the list, in that case set the value of front and rear to 0 and insert the element at the rear end. 26 | - Otherwise keep increasing the value of rear and insert each element one by one having rear as the index. 27 | ### Algorithm to delete any element from a queue (Dequeue) 28 | - If, the value of front is -1 or value of front is greater than rear , write an underflow message and exit. 29 | - Otherwise, keep increasing the value of front and return the item stored at the front end of the queue at each time. 30 | 31 | 32 | -------------------------------------------------------------------------------- /Queue using Linked List/Queue_Using_Linked_List.c: -------------------------------------------------------------------------------- 1 | // C Program to Implement Queue Data Structure using Linked List 2 | 3 | #include 4 | #include 5 | 6 | struct node // Structure for node 7 | { 8 | int info; 9 | struct node *ptr; 10 | } * front, *rear, *temp, *front1; 11 | 12 | int frontelement(); // Function to return front element 13 | void enq(int data); // Function to insert element in queue 14 | void deq(); // Function to delete element from queue 15 | void empty(); // Function to check if queue is empty 16 | void display(); // Function to display queue 17 | void create(); // Function to create queue 18 | void queuesize(); // Function to return size of queue 19 | 20 | int count = 0; // Variable to store size of queue 21 | 22 | void main() 23 | { 24 | int no, ch, e; // no = element to be inserted, ch = choice, e = element to be deleted 25 | 26 | printf("\n 1 - Enque"); 27 | printf("\n 2 - Deque"); 28 | printf("\n 3 - Front element"); 29 | printf("\n 4 - Empty"); 30 | printf("\n 5 - Exit"); 31 | printf("\n 6 - Display"); 32 | printf("\n 7 - Queue size"); 33 | create(); 34 | while (1) // Infinite loop 35 | { 36 | printf("\n Enter choice : "); 37 | scanf("%d", &ch); 38 | switch (ch) 39 | { 40 | case 1: // Insert element in queue 41 | printf("Enter data : "); 42 | scanf("%d", &no); 43 | enq(no); 44 | break; 45 | case 2: // Delete element from queue 46 | deq(); 47 | break; 48 | case 3: // Return front element 49 | e = frontelement(); 50 | if (e != 0) 51 | printf("Front element : %d", e); 52 | else 53 | printf("\n No front element in Queue as queue is empty"); 54 | break; 55 | case 4: // Check if queue is empty 56 | empty(); 57 | break; 58 | case 5: // Exit 59 | exit(0); 60 | case 6: // Display queue 61 | display(); 62 | break; 63 | case 7: // Return size of queue 64 | queuesize(); 65 | break; 66 | default: // Invalid choice 67 | printf("Wrong choice, Please enter correct choice "); 68 | break; 69 | } 70 | } 71 | } 72 | 73 | /* Create an empty queue */ 74 | void create() 75 | { 76 | front = rear = NULL; 77 | } 78 | 79 | /* Returns queue size */ 80 | void queuesize() 81 | { 82 | printf("\n Queue size : %d", count); 83 | } 84 | 85 | /* Enqueing the queue */ 86 | void enq(int data) 87 | { 88 | if (rear == NULL) 89 | { 90 | rear = (struct node *)malloc(1 * sizeof(struct node)); 91 | rear->ptr = NULL; 92 | rear->info = data; 93 | front = rear; 94 | } 95 | else 96 | { 97 | temp = (struct node *)malloc(1 * sizeof(struct node)); 98 | rear->ptr = temp; 99 | temp->info = data; 100 | temp->ptr = NULL; 101 | 102 | rear = temp; 103 | } 104 | count++; 105 | } 106 | 107 | /* Displaying the queue elements */ 108 | void display() 109 | { 110 | front1 = front; 111 | 112 | if ((front1 == NULL) && (rear == NULL)) 113 | { 114 | printf("Queue is empty"); 115 | return; 116 | } 117 | while (front1 != rear) 118 | { 119 | printf("%d ", front1->info); 120 | front1 = front1->ptr; 121 | } 122 | if (front1 == rear) 123 | printf("%d", front1->info); 124 | } 125 | 126 | /* Dequeing the queue */ 127 | void deq() 128 | { 129 | front1 = front; 130 | 131 | if (front1 == NULL) 132 | { 133 | printf("\n Error: Trying to display elements from empty queue"); 134 | return; 135 | } 136 | else if (front1->ptr != NULL) 137 | { 138 | front1 = front1->ptr; 139 | printf("\n Dequed value : %d", front->info); 140 | free(front); 141 | front = front1; 142 | } 143 | else 144 | { 145 | printf("\n Dequed value : %d", front->info); 146 | free(front); 147 | front = NULL; 148 | rear = NULL; 149 | } 150 | count--; 151 | } 152 | 153 | /* Returns the front element of queue */ 154 | int frontelement() 155 | { 156 | if ((front != NULL) && (rear != NULL)) 157 | return (front->info); 158 | else 159 | return 0; 160 | } 161 | 162 | /* Display if queue is empty or not */ 163 | void empty() 164 | { 165 | if ((front == NULL) && (rear == NULL)) 166 | printf("\n Queue empty"); 167 | else 168 | printf("Queue not empty"); 169 | } -------------------------------------------------------------------------------- /Queue using Linked List/README.md: -------------------------------------------------------------------------------- 1 | # Implementation of Queue Data Structure using Linked List 2 | 3 | ## What is a Queue? 4 | A Queue is a linear structure which follows a particular order in which the operations are performed.A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. 5 | 6 | ## What is Linked List 7 | 8 | Linked List is a linear data structure just like arrays but unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers. 9 | 10 | ## Applications of Linked List data structure 11 | 12 | 1. Implementation of stacks and queues 13 | 2. Implementation of graphs : Adjacency list representation of graphs is most popular which is uses linked list to store adjacent vertices. 14 | 3. Dynamic memory allocation : We use linked list of free blocks. 15 | 4. Maintaining directory of names 16 | 5. Performing arithmetic operations on long integers 17 | 6. Manipulation of polynomials by storing constants in the node of linked list representing sparse matrices 18 | 19 | ## Basic Operations on Queue 20 | ### EnQueue(): Inserts an element at the rear of the Queue. 21 | - Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively difficult to implement than that of stacks. 22 | The following steps should be taken to enqueue (insert) data into a queue − 23 | 24 | Step 1 − Check if the queue is full.
25 | Step 2 − If the queue is full, produce overflow error and exit.
26 | Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
27 | Step 4 − Add data element to the queue location, where the rear is pointing.
28 | Step 5 − return success. 29 | 30 | 31 | #### Algorithm for Enqueue operation 32 | 33 | procedure enqueue(data) 34 | 35 | if queue is full 36 | return overflow 37 | endif 38 | 39 | rear ← rear + 1 40 | queue[rear] ← data 41 | return true 42 | 43 | end procedure 44 | 45 | ### DeQueue(): Removes an element from the front of the Queue. 46 | - The following steps are taken to perform dequeue operation − 47 | Step 1 − Check if the queue is empty.
48 | Step 2 − If the queue is empty, produce underflow error and exit.
49 | Step 3 − If the queue is not empty, access the data where front is pointing.
50 | Step 4 − Increment front pointer to point to the next available data element.
51 | Step 5 − Return success. 52 | #### Algorithm for Dequeue operation 53 | 54 | procedure dequeue 55 | 56 | if queue is empty 57 | return underflow 58 | end if 59 | 60 | data = queue[front] 61 | front ← front + 1 62 | return true 63 | 64 | end procedure 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms using C 2 | 3 | 4 | I'm hugly thankful to my classmates, who agreed to share their assigments along with the source code, so I'm able to collect all the different types of Data Structures in C along with theri implementation and compalied them in the form of this repo. 5 | 6 | ## List of Data Structures 7 | 8 | Data Structure 9 | 10 | | Type of Data Structure | Subcategory | 11 | |:--- | :---- | 12 | | Searching | [Binary Search](https://github.com/SubhanRaj/DS-Algo/tree/main/Binary%20Search) | 13 | | | [Linear Search](https://github.com/SubhanRaj/DS-Algo/tree/main/Linear%20Search) | 14 | | Sorting | [Bubble Sort](https://github.com/SubhanRaj/DS-Algo/tree/main/Bubble%20Sort) | 15 | | | [Heap Sort](https://github.com/SubhanRaj/DS-Algo/tree/main/Heap%20Sort) | 16 | | | [Selection Sort](https://github.com/SubhanRaj/DS-Algo/tree/main/Selection%20Sort) | 17 | | | [Insertion Sort](https://github.com/SubhanRaj/DS-Algo/tree/main/Insertion%20Sort) | 18 | | Queue | [Circular Queue Using Array](https://github.com/SubhanRaj/DS-Algo/tree/main/Circular%20Queue%20using%20Array) | 19 | | | [Circular Queue Using Linked List](https://github.com/SubhanRaj/DS-Algo/tree/main/Circular%20Queue%20Using%20Linked%20List)| 20 | | | [Queue Using Array](https://github.com/SubhanRaj/DS-Algo/tree/main/Queue%20Using%20Array) | 21 | | | [Queue Using Linked List](https://github.com/SubhanRaj/DS-Algo/tree/main/Queue%20using%20Linked%20List) | 22 | | Stack | [Stack Using Array](https://github.com/SubhanRaj/DS-Algo/tree/main/Stack%20using%20Array) | 23 | | | [Stack Using Linked List](https://github.com/SubhanRaj/DS-Algo/tree/main/Stack%20using%20Linked%20List) | 24 | | 2D Array or Matrix | [Addition of 2D Array](https://github.com/SubhanRaj/DS-Algo/tree/main/Addition%20of%202D%20Array) | 25 | | | [Multiplication of 2D Array](https://github.com/SubhanRaj/DS-Algo/tree/main/Multiplication%20of%202D%20Array) | 26 | | | [Transpose of 2D Array](https://github.com/SubhanRaj/DS-Algo/tree/main/Transpose%20of%202D%20Array)| 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Selection Sort/README.md: -------------------------------------------------------------------------------- 1 | # Selection Sorting 2 | 3 | Selection Sort algorithm is used to arrange a list of elements in a particular order (Ascending or Descending). In selection sort, the first element in the list is selected and it is compared repeatedly with all the remaining elements in the list. If any element is smaller than the selected element (for Ascending order), then both are swapped so that first position is filled with the smallest element in the sorted order. Next, we select the element at a second position in the list and it is compared with all the remaining elements in the list. If any element is smaller than the selected element, then both are swapped. This procedure is repeated until the entire list is sorted. 4 | 5 | ## Step by Step Process 6 | 7 | The selection sort algorithm is performed using the following steps... 8 | 9 | - Step 1 - Select the first element of the list (i.e., Element at first position in the list). 10 | - Step 2: Compare the selected element with all the other elements in the list. 11 | - Step 3: In every comparision, if any element is found smaller than the selected element (for Ascending order), then both are swapped. 12 | - Step 4: Repeat the same procedure with element in the next position in the list till the entire list is sorted. 13 | 14 | ## Complexity of the Selection Sort Algorithm 15 | 16 | To sort an unsorted list with 'n' number of elements, we need to make ((n-1)+(n-2)+(n-3)+......+1) = (n (n-1))/2 number of comparisions in the worst case. If the list is already sorted then it requires 'n' number of comparisions. 17 | 18 | - **Worst Case** : O(n2) 19 | - **Best Case** : Ω(n2) 20 | - **Average Case** : Θ(n2) -------------------------------------------------------------------------------- /Selection Sort/selection_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | 7 | int size, i, j, temp, list[100]; 8 | 9 | printf("Enter the size of the List: "); 10 | scanf("%d", &size); 11 | 12 | printf("Enter %d integer values: ", size); 13 | for (i = 0; i < size; i++) 14 | scanf("%d", &list[i]); 15 | 16 | //Selection sort logic 17 | 18 | for (i = 0; i < size; i++) 19 | { 20 | for (j = i + 1; j < size; j++) 21 | { 22 | if (list[i] > list[j]) 23 | { 24 | temp = list[i]; 25 | list[i] = list[j]; 26 | list[j] = temp; 27 | } 28 | } 29 | } 30 | 31 | printf("List after sorting is: "); 32 | for (i = 0; i < size; i++) 33 | printf(" %d", list[i]); 34 | } -------------------------------------------------------------------------------- /Stack using Array/README.md: -------------------------------------------------------------------------------- 1 | # Stack Using Array 2 | 3 | Stack is a linear data structure that follows a particular order in which the operations are performed.The order may be LIFO(Last In First Out) or FILO(First In Last Out). 4 | 5 | Mainly the following three basic operations are performed in the stack: 6 | 7 | - Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. 8 | - Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. 9 | - Peek or Top: Returns the top element of the stack. 10 | - isEmpty: Returns true if the stack is empty, else false. 11 | 12 | ## Time Complexities of operations on stack: 13 | push(), pop(), isEmpty() and peek() all take O(1) time. We do not run any loop in any of these operations. 14 | 15 | ## Space Complexity of operations on stack: 16 | - The space complexity of push(), pop() and isEmpty() is O(1). 17 | - The space complexity of peek() is O(n). 18 | - The space complexity of the entire program is O(n). 19 | 20 | ## Applications of stack: 21 | - Balancing of symbols 22 | - Infix to Postfix /Prefix conversion 23 | - Redo-undo features at many places like editors, photoshop. 24 | - Forward and backward feature in web browsers 25 | - Used in many algorithms like Tower of Hanoi, tree traversals, stock span problem, histogram problem. 26 | - Backtracking is one of the algorithm designing techniques. Some examples of backtracking are the Knight-Tour problem, N-Queen problem, find your way through a maze, and game-like chess or checkers in all these problems we dive into someway if that way is not efficient we come back to the previous state and go into some another path. To get back from a current state we need to store the previous state for that purpose we need a stack. 27 | - In Graph Algorithms like Topological Sorting and Strongly Connected Components 28 | - In Memory management, any modern computer uses a stack as the primary management for a running purpose. Each program that is running in a- computer system has its own memory allocations 29 | - String reversal is also another application of stack. Here one by one each character gets inserted into the stack. So the first character of the string is on the bottom of the stack and the last element of a string is on the top of the stack. After Performing the pop operations on the stack we get a string in reverse order. 30 | 31 | ## Implementation of Stack using Array 32 | 33 | ### Creating an empty Stack 34 | 35 | - Step 1 - Include all the header files which are used in the program and define a constant 'SIZE' with specific value. 36 | - Step 2 - Declare all the functions used in stack implementation. 37 | - Step 3 - Create a one dimensional array with fixed size (int stack[SIZE]) 38 | - Step 4 - Define a integer variable 'top' and initialize with '-1'. (int top = -1) 39 | - Step 5 - In main method, display menu with list of operations and make suitable function calls to perform operation selected by the user on the stack. 40 | 41 | ### push(value) - Inserting value into the stack 42 | 43 | In a stack, push() is a function used to insert an element into the stack. In a stack, the new element is always inserted at top position. Push function takes one integer value as parameter and inserts that value into the stack. We can use the following steps to push an element on to the stack... 44 | 45 | - Step 1 - Check whether stack is FULL. (top == SIZE-1) 46 | - Step 2 - If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and terminate the function. 47 | - Step 3 - If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value (stack[top] = value). 48 | ### pop() - Delete a value from the Stack 49 | 50 | In a stack, pop() is a function used to delete an element from the stack. In a stack, the element is always deleted from top position. Pop function does not take any value as parameter. We can use the following steps to pop an element from the stack... 51 | 52 | - Step 1 - Check whether stack is EMPTY. (top == -1) 53 | - Step 2 - If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and terminate the function. 54 | - Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--). 55 | 56 | ### display() - Displays the elements of a Stack 57 | 58 | We can use the following steps to display the elements of a stack... 59 | 60 | - Step 1 - Check whether stack is EMPTY. (top == -1) 61 | - Step 2 - If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function. 62 | - Step 3 - If it is NOT EMPTY, then define a variable 'i' and initialize with top. Display stack[i] value and decrement i value by one (i--). 63 | - Step 3 - Repeat above step until i value becomes '0'. -------------------------------------------------------------------------------- /Stack using Array/stack_using_array.c: -------------------------------------------------------------------------------- 1 | // Implementation of stack data structure using array 2 | 3 | #include 4 | #include 5 | 6 | #define SIZE 10 // Defaine the size of the stack 7 | 8 | void push(int); // Function to push the element in the stack 9 | void pop(); // Function to pop the element from the stack 10 | void display(); // Function to display the stack 11 | 12 | int stack[SIZE], top = -1; // Declare the stack and top 13 | 14 | void main() 15 | { 16 | int value, choice; 17 | while (1) 18 | { 19 | printf("\n\n***** MENU *****\n"); 20 | printf("1. Push\n2. Pop\n3. Display\n4. Exit"); 21 | printf("\nEnter your choice: "); 22 | scanf("%d", &choice); 23 | switch (choice) 24 | { 25 | case 1: // Push the element in the stack 26 | printf("Enter the value to be insert: "); 27 | scanf("%d", &value); 28 | push(value); 29 | break; 30 | case 2: // Pop the element from the stack 31 | pop(); 32 | break; 33 | case 3: // Display the stack 34 | display(); 35 | break; 36 | case 4: // Exit the program 37 | exit(0); 38 | default: 39 | printf("\nWrong selection!!! Try again!!!"); 40 | } 41 | } 42 | } 43 | void push(int value) 44 | { 45 | if (top == SIZE - 1) 46 | printf("\nStack is Full!!! Insertion is not possible!!!"); 47 | else 48 | { 49 | top++; 50 | stack[top] = value; 51 | printf("\nInsertion success!!!"); 52 | } 53 | } 54 | void pop() 55 | { 56 | if (top == -1) 57 | printf("\nStack is Empty!!! Deletion is not possible!!!"); 58 | else 59 | { 60 | printf("\nDeleted : %d", stack[top]); 61 | top--; 62 | } 63 | } 64 | void display() 65 | { 66 | if (top == -1) 67 | printf("\nStack is Empty!!!"); 68 | else 69 | { 70 | int i; 71 | printf("\nStack elements are:\n"); 72 | for (i = top; i >= 0; i--) 73 | printf("%d\n", stack[i]); 74 | } 75 | } -------------------------------------------------------------------------------- /Stack using Linked List/README.md: -------------------------------------------------------------------------------- 1 | # Stack Using Linked List 2 | 3 | A stack data structure can be implemented by using a linked list data structure. The stack implemented using linked list can work for an unlimited number of values. That means, stack implemented using linked list works for the variable size of data. So, there is no need to fix the size at the beginning of the implementation. The Stack implemented using linked list can organize as many data values as we want. 4 | 5 | In linked list implementation of a stack, every new element is inserted as 'top' element. That means every newly inserted element is pointed by 'top'. Whenever we want to remove an element from the stack, simply remove the node which is pointed by 'top' by moving 'top' to its previous node in the list. The next field of the first element must be always NULL. 6 | 7 | ## Stack Operations using Linked List 8 | 9 | To implement a stack using a linked list, we need to set the following things before implementing actual operations. 10 | 11 | - Step 1 - Include all the header files which are used in the program. And declare all the user defined functions. 12 | - Step 2 - Define a 'Node' structure with two members data and next. 13 | - Step 3 - Define a Node pointer 'top' and set it to NULL. 14 | - Step 4 - Implement the main method by displaying Menu with list of operations and make suitable function calls in the main method. 15 | 16 | ### push(value) - Inserting an element into the Stack 17 | 18 | We can use the following steps to insert a new node into the stack... 19 | 20 | - Step 1 - Create a newNode with given value. 21 | - Step 2 - Check whether stack is Empty (top == NULL) 22 | - Step 3 - If it is Empty, then set newNode → next = NULL. 23 | - Step 4 - If it is Not Empty, then set newNode → next = top. 24 | - Step 5 - Finally, set top = newNode. 25 | 26 | ### pop() - Deleting an Element from a Stack 27 | 28 | We can use the following steps to delete a node from the stack... 29 | 30 | - Step 1 - Check whether stack is Empty (top == NULL). 31 | - Step 2 - If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate the function 32 | - Step 3 - If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'. 33 | - Step 4 - Then set 'top = top → next'. 34 | - Step 5 - Finally, delete 'temp'. (free(temp)). 35 | 36 | ### display() - Displaying stack of elements 37 | 38 | We can use the following steps to display the elements (nodes) of a stack... 39 | 40 | - Step 1 - Check whether stack is Empty (top == NULL). 41 | - Step 2 - If it is Empty, then display 'Stack is Empty!!!' and terminate the function. 42 | - Step 3 - If it is Not Empty, then define a Node pointer 'temp' and initialize with top. 43 | - Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same until temp reaches to the first node in the stack. (temp → next != NULL). 44 | - Step 5 - Finally! Display 'temp → data ---> NULL'. -------------------------------------------------------------------------------- /Stack using Linked List/stack_using_linked_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node // Node for linked list 5 | { 6 | int data; 7 | struct Node *next; 8 | } *top = NULL; 9 | 10 | void push(int); // Push function 11 | void pop(); // Pop function 12 | void display(); // Display function 13 | 14 | void main() 15 | { 16 | int choice, value; // choice for menu, value for push 17 | printf("\n:: Stack using Linked List ::\n"); 18 | while (1) 19 | { 20 | printf("\n****** MENU ******\n"); 21 | printf("1. Push\n2. Pop\n3. Display\n4. Exit\n"); 22 | printf("Enter your choice: "); 23 | scanf("%d", &choice); 24 | switch (choice) 25 | { 26 | case 1: // Push 27 | printf("Enter the value to be insert: "); 28 | scanf("%d", &value); 29 | push(value); 30 | break; 31 | case 2: // Pop 32 | pop(); 33 | break; 34 | case 3: // Display 35 | display(); 36 | break; 37 | case 4: 38 | exit(0); 39 | default: 40 | printf("\nWrong selection!!! Please try again!!!\n"); 41 | } 42 | } 43 | } 44 | void push(int value) 45 | { 46 | struct Node *newNode; // New node for linked list 47 | newNode = (struct Node *)malloc(sizeof(struct Node)); // Allocate memory for new node 48 | newNode->data = value; // Assign value to new node 49 | if (top == NULL) 50 | newNode->next = NULL; 51 | else 52 | newNode->next = top; 53 | top = newNode; 54 | printf("\nInsertion is Success!!!\n"); 55 | } 56 | void pop() 57 | { 58 | if (top == NULL) 59 | printf("\nStack is Empty!!!\n"); 60 | else 61 | { 62 | struct Node *temp = top; 63 | printf("\nDeleted element: %d", temp->data); 64 | top = temp->next; 65 | free(temp); 66 | } 67 | } 68 | void display() 69 | { 70 | if (top == NULL) 71 | printf("\nStack is Empty!!!\n"); 72 | else 73 | { 74 | struct Node *temp = top; 75 | while (temp->next != NULL) 76 | { 77 | printf("%d--->", temp->data); 78 | temp = temp->next; 79 | } 80 | printf("%d--->NULL", temp->data); 81 | } 82 | } -------------------------------------------------------------------------------- /Substraction of 2D Array/subsraction_of_matric.c: -------------------------------------------------------------------------------- 1 | // C program to subtract two matrices 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | int i, j, m, n, k; 8 | printf("Enter the number of rows and columns of matrix 1: "); 9 | scanf("%d%d", &m, &n); 10 | printf("Enter the number of rows and columns of matrix 2: "); 11 | scanf("%d%d", &k, &n); 12 | if (m != k || n != k) 13 | { 14 | printf("\nThe matrices are not of same size.\n"); 15 | return 0; 16 | } 17 | int a[m][n], b[m][n], c[m][n]; 18 | printf("Enter the elements of matrix 1: \n"); 19 | for (i = 0; i < m; i++) 20 | for (j = 0; j < n; j++) 21 | scanf("%d", &a[i][j]); 22 | printf("Enter the elements of matrix 2: \n"); 23 | for (i = 0; i < m; i++) 24 | for (j = 0; j < n; j++) 25 | scanf("%d", &b[i][j]); 26 | for (i = 0; i < m; i++) 27 | for (j = 0; j < n; j++) 28 | c[i][j] = a[i][j] - b[i][j]; 29 | printf("The resultant matrix is: \n"); 30 | for (i = 0; i < m; i++) 31 | { 32 | for (j = 0; j < n; j++) 33 | printf("%d ", c[i][j]); 34 | printf("\n"); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Transpose of 2D Array/transpose_of_2d_array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SubhanRaj/DS-Algo/dd27fd61226c1456af8ed497823ed148452bc23c/Transpose of 2D Array/transpose_of_2d_array -------------------------------------------------------------------------------- /Transpose of 2D Array/transpose_of_2d_array.c: -------------------------------------------------------------------------------- 1 | // C Program to transpose a 2D array 2 | #include 3 | 4 | int main() 5 | { 6 | int m, n, i, j, matrix[10][10], transpose[10][10]; // m = number of rows, n = number of columns, i, j = loop counters and matrix and transpose are 2D arrays 7 | printf("Enter rows and columns :\n"); // Prompts user to enter rows and columns 8 | scanf("%d%d", &m, &n); // Stores the number of rows and columns in m and n respectively 9 | printf("Enter elements of the matrix\n"); 10 | for (i = 0; i < m; i++) // Loops through the rows 11 | for (j = 0; j < n; j++) // Loops through the columns 12 | scanf("%d", &matrix[i][j]); // Stores the elements of the matrix in matrix 13 | // Transpose of the matrix 14 | for (i = 0; i < m; i++) 15 | for (j = 0; j < n; j++) 16 | transpose[j][i] = matrix[i][j]; // Stores the transpose of the matrix in transpose 17 | printf("Transpose of the matrix:\n"); 18 | for (i = 0; i < n; i++) 19 | { 20 | for (j = 0; j < m; j++) 21 | printf("%d\t", transpose[i][j]); // Prints the transpose of the matrix 22 | printf("\n"); 23 | } 24 | return 0; 25 | } --------------------------------------------------------------------------------