├── CIRCULAR-LINKED-LIST ├── CIRCULAR-DOUBLY │ ├── DeleteBEG.c │ ├── DeleteFromPOS.c │ ├── DeleteLast.c │ ├── InsertAnyPOS.c │ ├── InsertBeginning.c │ └── LastInsert.c └── CIRCULAR-SINGLY │ ├── BeginInsertList.c │ ├── DeleteAtAnyPosition.c │ ├── DeleteAtFirstList.c │ ├── DeleteAtLastList.c │ ├── InsertAtAnyPosition.c │ └── LastInsertList.c ├── DOUBLY-LINKED-LIST ├── BeginDeleteDoublyLinkedList.c ├── BeginInsertDoublyLinkedList.c ├── DeleteAtAnyPositionDoublyLinkedList.c ├── InsertAtAnyPositionDoublyLinkedList.c ├── LastDeleteDoublyLinkedList.c └── LastInsertDoublyLinkedList.c ├── DSA-LEARNING-RESOURCES ├── DS lab programs-20200416T114234Z-001.zip ├── DSA Important Questions Solution.pdf ├── DSA Tutorial.pdf ├── DSA_Notes_.pdf ├── Data Structure and Algorithms.pdf ├── Trees Question Solution.pdf └── ds-cheat-sheet.pdf ├── GRAPH ├── DF-and-BF-Search-in-Graph.c ├── Dijkstras-Algorithm-Shortest-Path.c ├── Floyd-Warshall-Algorithm-Shortest-Paths.c ├── Kruskals-Algorithm-Minimum-Spanning-Tree.c └── Prims-Algirithm-Minimum-Spanning-Tree.c ├── LICENSE ├── LINKED-LIST ├── DeleteAtAnyPositionLinkedList.c ├── DeleteAtAnyPositionLinkedList1.c ├── DeleteAtFirstLinkedList.c ├── DeleteAtFirstLinkedList1.c ├── DeleteAtLastLinkedList.c ├── InsertAtAnyPositionLinkedList.c ├── LastInsertLinkedList.c └── begininsertlinkedlist.c ├── LIST ├── DeleteFromAnyPosition.c ├── DeleteFromFirst.c ├── DeleteFromLast.c ├── InsertAtAnyPosition.c ├── InsertAtBegining.c └── InsertAtLast.c ├── QUEUE ├── LinearQueue.c └── circularqueue.c ├── README.md ├── RECURSION ├── Factorial.c ├── FibonacciSeries.c └── TowerOfHanoi.c ├── SEARCHING ├── HashTable-Chaining.c └── Linear-Binary-Search.c ├── SORTING └── All-Sorting-Algorithms-Implementation.c ├── STACK ├── PostfixToInfix.c ├── PrefixToInfix.c ├── StackImplementation.c ├── infixtopostfix.c └── infixtoprefix.c └── TREE ├── AVL-Deletion.c ├── AVL-Insertion.c ├── B-Tree-Implementation.c ├── BST-All-Traversal.c ├── BST-Inorder-Traversal.c ├── BST-Node-Deletion.c ├── BST-Search-Node.c ├── Binary-Expression-Tree.c ├── Binary-Search-Tree-Inseartion-Deletion.c ├── Binary-Search-Tree.c └── Huffman-Coding-Implementation.c /CIRCULAR-LINKED-LIST/CIRCULAR-DOUBLY/DeleteBEG.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | struct Node* prev; 8 | }; 9 | 10 | struct Node* head = NULL; 11 | 12 | struct Node* createNode(int data) { 13 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 14 | if (!newNode) { 15 | printf("Memory allocation failed.\n"); 16 | exit(1); 17 | } 18 | newNode->data = data; 19 | newNode->next = newNode->prev = NULL; 20 | return newNode; 21 | } 22 | 23 | void insertAtLast(int data) { 24 | struct Node* newNode = createNode(data); 25 | 26 | if (head == NULL) { 27 | head = newNode; 28 | head->next = head->prev = newNode; 29 | } else { 30 | newNode->prev = head->prev; 31 | newNode->next = head; 32 | head->prev->next = newNode; 33 | head->prev = newNode; 34 | } 35 | } 36 | 37 | void deleteFromBeginning() { 38 | if (head == NULL) { 39 | printf("List is empty.\n"); 40 | return; 41 | } 42 | 43 | if (head->next == head) { // Only one node in the list 44 | free(head); 45 | head = NULL; 46 | } else { 47 | struct Node* temp = head; 48 | head = head->next; 49 | head->prev = temp->prev; 50 | temp->prev->next = head; 51 | free(temp); 52 | } 53 | } 54 | 55 | void displayList() { 56 | if (head == NULL) { 57 | printf("List is empty.\n"); 58 | return; 59 | } 60 | 61 | struct Node* current = head; 62 | printf("Circular Doubly Linked List: "); 63 | do { 64 | printf("%d ", current->data); 65 | current = current->next; 66 | } while (current != head); 67 | printf("\n"); 68 | } 69 | 70 | int main() { 71 | insertAtLast(10); 72 | insertAtLast(20); 73 | insertAtLast(30); 74 | 75 | printf("Before deletion:\n"); 76 | displayList(); 77 | 78 | deleteFromBeginning(); 79 | 80 | printf("After deletion from beginning:\n"); 81 | displayList(); 82 | 83 | return 0; 84 | } 85 | 86 | -------------------------------------------------------------------------------- /CIRCULAR-LINKED-LIST/CIRCULAR-DOUBLY/DeleteFromPOS.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | struct Node* prev; 8 | }; 9 | 10 | struct Node* head = NULL; 11 | 12 | struct Node* createNode(int data) { 13 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 14 | if (!newNode) { 15 | printf("Memory allocation failed.\n"); 16 | exit(1); 17 | } 18 | newNode->data = data; 19 | newNode->next = newNode->prev = NULL; 20 | return newNode; 21 | } 22 | 23 | void insertAtLast(int data) { 24 | struct Node* newNode = createNode(data); 25 | 26 | if (head == NULL) { 27 | head = newNode; 28 | head->next = head->prev = newNode; 29 | } else { 30 | newNode->prev = head->prev; 31 | newNode->next = head; 32 | head->prev->next = newNode; 33 | head->prev = newNode; 34 | } 35 | } 36 | 37 | void deleteFromPosition(int position) { 38 | if (head == NULL) { 39 | printf("List is empty.\n"); 40 | return; 41 | } 42 | 43 | if (position <= 0) { 44 | printf("Invalid position.\n"); 45 | return; 46 | } 47 | int i; 48 | struct Node* current = head; 49 | for (i = 1; i < position; ++i) { 50 | current = current->next; 51 | if (current == head) { 52 | printf("Position out of range.\n"); 53 | return; 54 | } 55 | } 56 | 57 | if (current == head && head->next == head) { // Only one node in the list 58 | free(head); 59 | head = NULL; 60 | } else { 61 | current->prev->next = current->next; 62 | current->next->prev = current->prev; 63 | if (current == head) { 64 | head = current->next; 65 | } 66 | free(current); 67 | } 68 | } 69 | 70 | void displayList() { 71 | if (head == NULL) { 72 | printf("List is empty.\n"); 73 | return; 74 | } 75 | 76 | struct Node* current = head; 77 | printf("Circular Doubly Linked List: "); 78 | do { 79 | printf("%d ", current->data); 80 | current = current->next; 81 | } while (current != head); 82 | printf("\n"); 83 | } 84 | 85 | int main() { 86 | insertAtLast(922); 87 | insertAtLast(138); 88 | insertAtLast(203); 89 | 90 | printf("Before deletion:\n"); 91 | displayList(); 92 | 93 | int position; 94 | printf("Enter the position to delete: "); 95 | scanf("%d", &position); 96 | deleteFromPosition(position); 97 | 98 | printf("After deletion from position %d:\n", position); 99 | displayList(); 100 | 101 | return 0; 102 | } 103 | 104 | -------------------------------------------------------------------------------- /CIRCULAR-LINKED-LIST/CIRCULAR-DOUBLY/DeleteLast.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | struct Node* prev; 8 | }; 9 | 10 | struct Node* head = NULL; 11 | 12 | struct Node* createNode(int data) { 13 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 14 | if (!newNode) { 15 | printf("Memory allocation failed.\n"); 16 | exit(1); 17 | } 18 | newNode->data = data; 19 | newNode->next = newNode->prev = NULL; 20 | return newNode; 21 | } 22 | 23 | void insertAtLast(int data) { 24 | struct Node* newNode = createNode(data); 25 | 26 | if (head == NULL) { 27 | head = newNode; 28 | head->next = head->prev = newNode; 29 | } else { 30 | newNode->prev = head->prev; 31 | newNode->next = head; 32 | head->prev->next = newNode; 33 | head->prev = newNode; 34 | } 35 | } 36 | 37 | void deleteFromLast() { 38 | if (head == NULL) { 39 | printf("List is empty.\n"); 40 | return; 41 | } 42 | 43 | if (head->next == head) { // Only one node in the list 44 | free(head); 45 | head = NULL; 46 | } else { 47 | struct Node* temp = head->prev; 48 | head->prev = temp->prev; 49 | temp->prev->next = head; 50 | free(temp); 51 | } 52 | } 53 | 54 | void displayList() { 55 | if (head == NULL) { 56 | printf("List is empty.\n"); 57 | return; 58 | } 59 | 60 | struct Node* current = head; 61 | printf("Circular Doubly Linked List: "); 62 | do { 63 | printf("%d ", current->data); 64 | current = current->next; 65 | } while (current != head); 66 | printf("\n"); 67 | } 68 | 69 | int main() { 70 | insertAtLast(100); 71 | insertAtLast(200); 72 | insertAtLast(300); 73 | 74 | printf("Before deletion:\n"); 75 | displayList(); 76 | 77 | deleteFromLast(); 78 | 79 | printf("After deletion from last:\n"); 80 | displayList(); 81 | 82 | return 0; 83 | } 84 | 85 | -------------------------------------------------------------------------------- /CIRCULAR-LINKED-LIST/CIRCULAR-DOUBLY/InsertAnyPOS.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | struct Node* prev; 8 | }; 9 | 10 | struct Node* head = NULL; 11 | 12 | struct Node* createNode(int data) { 13 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 14 | if (!newNode) { 15 | printf("Memory allocation failed.\n"); 16 | exit(1); 17 | } 18 | newNode->data = data; 19 | newNode->next = newNode->prev = NULL; 20 | return newNode; 21 | } 22 | 23 | void insertAtPosition(int data, int position) { 24 | if (position <= 0) { 25 | printf("Invalid position.\n"); 26 | return; 27 | } 28 | 29 | struct Node* newNode = createNode(data); 30 | 31 | if (head == NULL) { 32 | head = newNode; 33 | head->next = head->prev = newNode; 34 | } else { 35 | int i; 36 | struct Node* current = head; 37 | for (i = 1; i < position - 1; ++i) { 38 | current = current->next; 39 | if (current == head) { 40 | printf("Position out of range.\n"); 41 | free(newNode); 42 | return; 43 | } 44 | } 45 | 46 | newNode->prev = current; 47 | newNode->next = current->next; 48 | current->next->prev = newNode; 49 | current->next = newNode; 50 | } 51 | } 52 | 53 | void displayList() { 54 | if (head == NULL) { 55 | printf("List is empty.\n"); 56 | return; 57 | } 58 | 59 | struct Node* current = head; 60 | printf("Circular Doubly Linked List: "); 61 | do { 62 | printf("%d ", current->data); 63 | current = current->next; 64 | } while (current != head); 65 | printf("\n"); 66 | } 67 | 68 | int main() { 69 | insertAtPosition(10, 1); 70 | insertAtPosition(30, 1); 71 | insertAtPosition(20, 2); 72 | 73 | printf("After initial insertions:\n"); 74 | displayList(); 75 | 76 | int position, data; 77 | printf("\nEnter the position to insert: "); 78 | scanf("%d", &position); 79 | printf("Enter the data to insert: "); 80 | scanf("%d", &data); 81 | 82 | insertAtPosition(data, position); 83 | 84 | printf("\nAfter insertion at position %d:\n", position); 85 | displayList(); 86 | 87 | return 0; 88 | } 89 | 90 | -------------------------------------------------------------------------------- /CIRCULAR-LINKED-LIST/CIRCULAR-DOUBLY/InsertBeginning.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | struct Node* prev; 8 | }; 9 | struct Node* head = NULL; 10 | struct Node* createNode(int data) { 11 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 12 | if (!newNode) { 13 | printf("Memory allocation failed.\n"); 14 | exit(1); 15 | } 16 | newNode->data = data; 17 | newNode->next = newNode->prev = NULL; 18 | return newNode; 19 | } 20 | void insertAtBeginning(int data) { 21 | struct Node* newNode = createNode(data); 22 | if (head == NULL) { 23 | head = newNode; 24 | head->next = head->prev = newNode; 25 | } else { 26 | newNode->next = head; 27 | newNode->prev = head->prev; 28 | head->prev->next = newNode; 29 | head->prev = newNode; 30 | head = newNode; 31 | } 32 | } 33 | void displayList() { 34 | if (head == NULL) { 35 | printf("List is empty.\n"); 36 | return; 37 | } 38 | struct Node* current = head; 39 | printf("Circular Doubly Linked List: "); 40 | do { 41 | printf("%d ", current->data); 42 | current = current->next; 43 | } while (current != head); 44 | printf("\n"); 45 | } 46 | int main() { 47 | insertAtBeginning(10); 48 | insertAtBeginning(20); 49 | insertAtBeginning(30); 50 | insertAtBeginning(40); 51 | insertAtBeginning(50); 52 | printf("After insertion:\n"); 53 | displayList(); 54 | 55 | return 0; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /CIRCULAR-LINKED-LIST/CIRCULAR-DOUBLY/LastInsert.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | struct Node* prev; 8 | }; 9 | 10 | struct Node* head = NULL; 11 | 12 | struct Node* createNode(int data) { 13 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 14 | if (!newNode) { 15 | printf("Memory allocation failed.\n"); 16 | exit(1); 17 | } 18 | newNode->data = data; 19 | newNode->next = newNode->prev = NULL; 20 | return newNode; 21 | } 22 | 23 | void insertAtLast(int data) { 24 | struct Node* newNode = createNode(data); 25 | 26 | if (head == NULL) { 27 | head = newNode; 28 | head->next = head->prev = newNode; 29 | } else { 30 | newNode->prev = head->prev; 31 | newNode->next = head; 32 | head->prev->next = newNode; 33 | head->prev = newNode; 34 | } 35 | } 36 | 37 | void displayList() { 38 | if (head == NULL) { 39 | printf("List is empty.\n"); 40 | return; 41 | } 42 | 43 | struct Node* current = head; 44 | printf("Circular Doubly Linked List: "); 45 | do { 46 | printf("%d ", current->data); 47 | current = current->next; 48 | } while (current != head); 49 | printf("\n"); 50 | } 51 | 52 | int main() { 53 | insertAtLast(10); 54 | insertAtLast(20); 55 | insertAtLast(30); 56 | 57 | printf("After insertion:\n"); 58 | displayList(); 59 | 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /CIRCULAR-LINKED-LIST/CIRCULAR-SINGLY/BeginInsertList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | }; 8 | 9 | struct Node *insertAtBeginning(struct Node *last, int data) { 10 | struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); 11 | newNode->data = data; 12 | 13 | if (last == NULL) { 14 | newNode->next = newNode; 15 | last = newNode; 16 | } else { 17 | newNode->next = last->next; 18 | last->next = newNode; 19 | } 20 | 21 | return last; 22 | } 23 | 24 | void displayList(struct Node *last) { 25 | if (last == NULL) { 26 | printf("List is empty.\n"); 27 | return; 28 | } 29 | 30 | struct Node *current = last->next; 31 | do { 32 | printf("%d -> ", current->data); 33 | current = current->next; 34 | } while (current != last->next); 35 | printf("\n"); 36 | } 37 | 38 | int main() { 39 | int i; 40 | struct Node *last = NULL; 41 | int nodeData, numNodes; 42 | 43 | printf("Enter the number of nodes: "); 44 | scanf("%d", &numNodes); 45 | 46 | for (i = 0; i < numNodes; i++) { 47 | printf("Enter data for node %d: ", i + 1); 48 | scanf("%d", &nodeData); 49 | last = insertAtBeginning(last, nodeData); 50 | } 51 | 52 | printf("Circular Linked List: "); 53 | displayList(last); 54 | 55 | return 0; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /CIRCULAR-LINKED-LIST/CIRCULAR-SINGLY/DeleteAtAnyPosition.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node { 5 | int info; 6 | struct node* next; 7 | }; 8 | struct node* last = NULL; 9 | 10 | void addatlast(int data) { 11 | struct node* temp; 12 | temp = (struct node*)malloc(sizeof(struct node)); 13 | if (last == NULL) { 14 | temp->info = data; 15 | temp->next = temp; 16 | last = temp; 17 | } else { 18 | temp->info = data; 19 | temp->next = last->next; 20 | last->next = temp; 21 | last = temp; 22 | } 23 | } 24 | 25 | void deleteAtPosition(int position) { 26 | if (last == NULL) { 27 | printf("\nList is empty.\n"); 28 | return; 29 | } 30 | 31 | if (position <= 0) { 32 | printf("\nInvalid position.\n"); 33 | return; 34 | } 35 | 36 | struct node* temp = last->next; 37 | struct node* previous = NULL; 38 | int count = 1; 39 | 40 | while (count < position && temp != last) { 41 | previous = temp; 42 | temp = temp->next; 43 | count++; 44 | } 45 | 46 | if (temp == last && count < position) { 47 | printf("\nPosition out of range.\n"); 48 | return; 49 | } 50 | 51 | if (previous == NULL) { 52 | struct node* temp2 = last->next; 53 | last->next = temp2->next; 54 | free(temp2); 55 | if (last->next == last) { 56 | free(last); 57 | last = NULL; 58 | } 59 | } else { 60 | previous->next = temp->next; 61 | if (temp == last) { 62 | last = previous; 63 | } 64 | free(temp); 65 | } 66 | } 67 | 68 | void viewList() { 69 | if (last == NULL) 70 | printf("\nList is empty\n"); 71 | else { 72 | struct node* temp; 73 | temp = last->next; 74 | do { 75 | printf("\nData = %d", temp->info); 76 | temp = temp->next; 77 | } while (temp != last->next); 78 | } 79 | } 80 | 81 | int main() { 82 | addatlast(10); 83 | addatlast(20); 84 | addatlast(30); 85 | 86 | printf("Before Deletion:\n"); 87 | viewList(); 88 | 89 | int position; 90 | printf("\nEnter the position of node to delete: "); 91 | scanf("%d", &position); 92 | deleteAtPosition(position); 93 | 94 | printf("\n\nAfter Deletion:\n"); 95 | viewList(); 96 | 97 | return 0; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /CIRCULAR-LINKED-LIST/CIRCULAR-SINGLY/DeleteAtFirstList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node { 5 | int info; 6 | struct node* next; 7 | }; 8 | 9 | struct node* last = NULL; 10 | 11 | void addatlast(int data) { 12 | struct node* temp; 13 | temp = (struct node*)malloc(sizeof(struct node)); 14 | if (last == NULL) { 15 | temp->info = data; 16 | temp->next = temp; 17 | last = temp; 18 | } else { 19 | temp->info = data; 20 | temp->next = last->next; 21 | last->next = temp; 22 | last = temp; 23 | } 24 | } 25 | 26 | void deletefirst() { 27 | struct node* temp; 28 | if (last == NULL) 29 | printf("\nList is empty.\n"); 30 | else { 31 | temp = last->next; 32 | last->next = temp->next; 33 | free(temp); 34 | } 35 | } 36 | 37 | void viewList() { 38 | if (last == NULL) 39 | printf("\nList is empty\n"); 40 | else { 41 | struct node* temp; 42 | temp = last->next; 43 | do { 44 | printf("\nData = %d", temp->info); 45 | temp = temp->next; 46 | } while (temp != last->next); 47 | } 48 | } 49 | 50 | int main() { 51 | addatlast(10); 52 | addatlast(20); 53 | addatlast(30); 54 | 55 | printf("Before deletion:\n"); 56 | viewList(); 57 | deletefirst(); 58 | printf("\n\nAfter deletion:\n"); 59 | viewList(); 60 | 61 | return 0; 62 | } 63 | 64 | -------------------------------------------------------------------------------- /CIRCULAR-LINKED-LIST/CIRCULAR-SINGLY/DeleteAtLastList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node { 5 | int info; 6 | struct node* next; 7 | }; 8 | struct node* last = NULL; 9 | 10 | void addatlast(int data) 11 | { 12 | struct node* temp; 13 | temp = (struct node*)malloc(sizeof(struct node)); 14 | if (last == NULL) { 15 | temp->info = data; 16 | temp->next = temp; 17 | last = temp; 18 | } 19 | else { 20 | temp->info = data; 21 | temp->next = last->next; 22 | last->next = temp; 23 | last = temp; 24 | } 25 | } 26 | void deletelast() 27 | { 28 | struct node* temp; 29 | if (last == NULL) 30 | printf("\nList is empty.\n"); 31 | 32 | temp = last->next; 33 | while (temp->next != last) 34 | temp = temp->next; 35 | temp->next = last->next; 36 | last = temp; 37 | } 38 | void viewList() 39 | { 40 | if (last == NULL) 41 | printf("\nList is empty\n"); 42 | else { 43 | struct node* temp; 44 | temp = last->next; 45 | do { 46 | printf("\nData = %d", temp->info); 47 | temp = temp->next; 48 | } while (temp != last->next); 49 | } 50 | } 51 | 52 | int main() 53 | { 54 | addatlast(10); 55 | addatlast(20); 56 | addatlast(30); 57 | 58 | printf("Before Deletion:\n"); 59 | viewList(); 60 | deletelast(); 61 | printf("\n\nAfter Deletion:\n"); 62 | viewList(); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /CIRCULAR-LINKED-LIST/CIRCULAR-SINGLY/InsertAtAnyPosition.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | }; 8 | 9 | void insertAtPosition(struct Node** head, int data, int position) { 10 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 11 | if (!newNode) { 12 | printf("Memory allocation failed.\n"); 13 | return; 14 | } 15 | newNode->data = data; 16 | 17 | if (*head == NULL) { 18 | newNode->next = newNode; 19 | *head = newNode; 20 | return; 21 | } 22 | int i; 23 | struct Node* current = *head; 24 | for (i = 1; i < position - 1; ++i) { 25 | current = current->next; 26 | if (current == *head) { 27 | printf("Position out of range.\n"); 28 | free(newNode); 29 | return; 30 | } 31 | } 32 | 33 | newNode->next = current->next; 34 | current->next = newNode; 35 | } 36 | 37 | void printList(struct Node* head) { 38 | if (head == NULL) { 39 | printf("List is empty.\n"); 40 | return; 41 | } 42 | 43 | struct Node* current = head; 44 | do { 45 | printf("%d ", current->data); 46 | current = current->next; 47 | } while (current != head); 48 | printf("\n"); 49 | } 50 | 51 | int main() { 52 | struct Node* head = NULL; 53 | int data, position; 54 | 55 | // Inserting elements 56 | insertAtPosition(&head, 10, 1); // 10 57 | insertAtPosition(&head, 20, 2); // 10 20 58 | insertAtPosition(&head, 30, 1); // 30 10 20 59 | insertAtPosition(&head, 40, 4); // 30 10 20 40 60 | 61 | // Printing the list 62 | printf("Circular Linked List: "); 63 | printList(head); 64 | 65 | // Getting user input for new insertion 66 | printf("Enter data to insert: "); 67 | scanf("%d", &data); 68 | printf("Enter position to insert at: "); 69 | scanf("%d", &position); 70 | 71 | // Inserting user input at specified position 72 | insertAtPosition(&head, data, position); 73 | 74 | // Printing the updated list 75 | printf("Updated Circular Linked List: "); 76 | printList(head); 77 | 78 | return 0; 79 | } 80 | 81 | -------------------------------------------------------------------------------- /CIRCULAR-LINKED-LIST/CIRCULAR-SINGLY/LastInsertList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Define the structure of a node 5 | struct Node { 6 | int data; 7 | struct Node *next; 8 | }; 9 | 10 | // Function to insert a node at the end of a circular linked list 11 | struct Node *insertAtEnd(struct Node *last, int data) { 12 | // Create a new node 13 | struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); 14 | newNode->data = data; 15 | 16 | // If the list is empty 17 | if (last == NULL) { 18 | newNode->next = newNode; // Point to itself 19 | last = newNode; // Make it the last node 20 | } else { 21 | newNode->next = last->next; // New node points to the current first node 22 | last->next = newNode; // Last node now points to the new first node 23 | last = newNode; // Update the last node to the newly added node 24 | } 25 | 26 | return last; // Return the updated last node 27 | } 28 | 29 | // Function to display the circular linked list 30 | void displayList(struct Node *last) { 31 | if (last == NULL) { 32 | printf("List is empty.\n"); 33 | return; 34 | } 35 | 36 | struct Node *current = last->next; 37 | do { 38 | printf("%d -> ", current->data); 39 | current = current->next; 40 | } while (current != last->next); 41 | printf("\n"); 42 | } 43 | 44 | int main() { 45 | int i; 46 | struct Node *last = NULL; // Initialize an empty circular linked list 47 | int numNodes, nodeData; 48 | 49 | printf("Enter the number of nodes: "); 50 | scanf("%d", &numNodes); 51 | 52 | for (i = 0; i < numNodes; i++) { 53 | printf("Enter data for node %d: ", i + 1); 54 | scanf("%d", &nodeData); 55 | last = insertAtEnd(last, nodeData); 56 | } 57 | 58 | // Display the list 59 | printf("Circular Linked List: "); 60 | displayList(last); 61 | 62 | return 0; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /DOUBLY-LINKED-LIST/BeginDeleteDoublyLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | struct Node* prev; 8 | }; 9 | 10 | struct Node* head = NULL; 11 | 12 | void insert(int data) { 13 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 14 | if (newNode == NULL) { 15 | printf("\nMemory allocation failed.\n"); 16 | return; 17 | } 18 | newNode->data = data; 19 | newNode->prev = NULL; 20 | newNode->next = head; 21 | 22 | if (head != NULL) { 23 | head->prev = newNode; 24 | } 25 | head = newNode; 26 | 27 | printf("\nNode inserted.\n"); 28 | } 29 | 30 | void deleteFromBeginning() { 31 | if (head == NULL) { 32 | printf("\nLinked list is empty. Nothing to delete.\n"); 33 | return; 34 | } 35 | 36 | struct Node* temp = head; 37 | head = head->next; 38 | if (head != NULL) { 39 | head->prev = NULL; 40 | } 41 | free(temp); 42 | 43 | printf("\nNode from the beginning deleted.\n"); 44 | } 45 | 46 | void display() { 47 | struct Node* current = head; 48 | printf("Doubly linked list: "); 49 | while (current != NULL) { 50 | printf("%d -> ", current->data); 51 | current = current->next; 52 | } 53 | printf("NULL\n"); 54 | } 55 | 56 | int main() { 57 | insert(30); 58 | insert(20); 59 | insert(10); 60 | 61 | display(); 62 | 63 | deleteFromBeginning(); 64 | display(); 65 | 66 | return 0; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /DOUBLY-LINKED-LIST/BeginInsertDoublyLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void beginsert(int); 4 | struct node 5 | { 6 | int data; 7 | struct node *prev; 8 | struct node *next; 9 | }; 10 | struct node *head, *temp; 11 | void main() 12 | { 13 | int choice,item; 14 | do{ 15 | printf("Enter the item which you want to insert: \n"); 16 | scanf("%d", &item); 17 | beginsert(item); 18 | printf("\nPress 0 to insert more: \n"); 19 | scanf("%d", &choice); 20 | }while(choice==0); 21 | printf("\n\nItems in the list are:"); 22 | //Print items in the node 23 | for (temp = head ; temp!=NULL ; temp=temp->next ) 24 | { 25 | printf("%d\t",temp -> data ); 26 | } 27 | } 28 | void beginsert(int item) 29 | { 30 | struct node *ptr = (struct node *)malloc(sizeof(struct node*)); 31 | if(ptr == NULL) 32 | { 33 | printf("\n ****************OVERFLOW**************** \n"); 34 | } 35 | else 36 | { 37 | ptr->data = item; 38 | ptr->next = head; 39 | ptr->prev = NULL; 40 | if (head != NULL) 41 | head->prev = ptr; 42 | head=ptr; 43 | printf("\n ****************Node Inserted**************** \n\n"); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /DOUBLY-LINKED-LIST/DeleteAtAnyPositionDoublyLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void beginsert(int); 5 | void deleteat(); 6 | void display(); 7 | 8 | struct node 9 | { 10 | int data; 11 | struct node *prev; 12 | struct node *next; 13 | }; 14 | 15 | struct node *head, *temp; 16 | 17 | void main() 18 | { 19 | int choice,item; 20 | do { 21 | printf("Enter the item which you want to insert: \n"); 22 | scanf("%d", &item); 23 | beginsert(item); 24 | printf("\nPress 0 to insert more: \n"); 25 | scanf("%d", &choice); 26 | } while(choice==0); 27 | 28 | printf("\n\nItems in the list before deletion are:"); 29 | display(); 30 | 31 | deleteat(); 32 | 33 | printf("\n\nItems in the list after deletion are:\t"); 34 | display(); 35 | } 36 | 37 | void beginsert(int item) 38 | { 39 | struct node *ptr = (struct node *)malloc(sizeof(struct node)); 40 | if(ptr == NULL) 41 | { 42 | printf("\n****************OVERFLOW****************\n"); 43 | } 44 | else 45 | { 46 | ptr->data = item; 47 | ptr->next = head; 48 | ptr->prev = NULL; 49 | if (head != NULL) 50 | head->prev = ptr; 51 | head = ptr; 52 | printf("\n****************Node Inserted****************\n\n"); 53 | } 54 | } 55 | 56 | void deleteat() 57 | { 58 | int pos, i; 59 | printf("\nEnter the position: \n"); 60 | scanf("%d",&pos); 61 | 62 | if (head == NULL) 63 | { 64 | printf("Underflow!\n"); 65 | } 66 | else 67 | { 68 | temp = head; 69 | for (i = 1; i < pos; i++) 70 | { 71 | temp = temp->next; 72 | } 73 | 74 | if (temp->next != NULL) // To handle deletion from the end 75 | temp->next->prev = temp->prev; 76 | 77 | if (temp->prev != NULL) // To handle deletion from the beginning 78 | temp->prev->next = temp->next; 79 | 80 | if (temp == head) // If the node to delete is the head 81 | head = temp->next; 82 | 83 | free(temp); 84 | printf("\nNode Deleted\n"); 85 | } 86 | } 87 | 88 | void display() 89 | { 90 | printf("\n\nItems in the list are:"); 91 | // Print items in the node 92 | for (temp = head; temp != NULL; temp = temp->next) 93 | { 94 | printf("%d\t", temp->data); 95 | } 96 | } 97 | 98 | -------------------------------------------------------------------------------- /DOUBLY-LINKED-LIST/InsertAtAnyPositionDoublyLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node *next, *prev; 7 | }; 8 | 9 | struct Node *head = NULL; 10 | 11 | void insert(int item, int loc) { 12 | struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); 13 | if (newNode == NULL) { 14 | printf("\nOVERFLOW"); 15 | return; 16 | } 17 | newNode->data = item; 18 | 19 | if (loc == 0) { 20 | newNode->next = head; 21 | newNode->prev = NULL; 22 | if (head != NULL) { 23 | head->prev = newNode; 24 | } 25 | head = newNode; 26 | } else { 27 | int i; 28 | struct Node *temp = head; 29 | for ( i = 0; i < loc - 1 && temp != NULL; i++) { 30 | temp = temp->next; 31 | } 32 | if (temp == NULL) { 33 | printf("\nCan't Insert\n"); 34 | free(newNode); 35 | return; 36 | } 37 | newNode->prev = temp; 38 | newNode->next = temp->next; 39 | temp->next = newNode; 40 | if (newNode->next != NULL) { 41 | newNode->next->prev = newNode; 42 | } 43 | } 44 | 45 | printf("\nNode inserted\n"); 46 | } 47 | 48 | void display() { 49 | struct Node *ptr = head; 50 | if (ptr == NULL) { 51 | printf("Linked List is empty.\n"); 52 | } else { 53 | while (ptr != NULL) { 54 | printf("%d ", ptr->data); 55 | ptr = ptr->next; 56 | } 57 | } 58 | } 59 | 60 | int main() { 61 | int choice, item, loc; 62 | do { 63 | printf("\nEnter the item you want to insert: "); 64 | scanf("%d", &item); 65 | printf("Enter the location: "); 66 | scanf("%d", &loc); 67 | insert(item, loc); 68 | 69 | printf("\nPress 0 to insert more, or any other key to exit: "); 70 | scanf("%d", &choice); 71 | } while (choice == 0); 72 | 73 | printf("\nDisplaying the items in the linked list:\n"); 74 | display(); 75 | 76 | return 0; 77 | } 78 | 79 | -------------------------------------------------------------------------------- /DOUBLY-LINKED-LIST/LastDeleteDoublyLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | struct Node* prev; 8 | }; 9 | 10 | struct Node* head = NULL; 11 | 12 | void insert(int data) { 13 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 14 | if (newNode == NULL) { 15 | printf("\nMemory allocation failed.\n"); 16 | return; 17 | } 18 | newNode->data = data; 19 | newNode->next = NULL; 20 | 21 | if (head == NULL) { 22 | newNode->prev = NULL; 23 | head = newNode; 24 | return; 25 | } 26 | 27 | struct Node* current = head; 28 | while (current->next != NULL) { 29 | current = current->next; 30 | } 31 | current->next = newNode; 32 | newNode->prev = current; 33 | 34 | printf("\nNode inserted.\n"); 35 | } 36 | 37 | void deleteFromEnd() { 38 | if (head == NULL) { 39 | printf("\nLinked list is empty. Nothing to delete.\n"); 40 | return; 41 | } 42 | 43 | struct Node* current = head; 44 | while (current->next != NULL) { 45 | current = current->next; 46 | } 47 | 48 | if (current->prev == NULL) { 49 | free(current); 50 | head = NULL; 51 | printf("\nLast node deleted.\n"); 52 | return; 53 | } 54 | 55 | current->prev->next = NULL; 56 | free(current); 57 | 58 | printf("\nLast node deleted.\n"); 59 | } 60 | 61 | void display() { 62 | struct Node* current = head; 63 | printf("Doubly linked list: "); 64 | while (current != NULL) { 65 | printf("%d -> ", current->data); 66 | current = current->next; 67 | } 68 | printf("NULL\n"); 69 | } 70 | 71 | int main() { 72 | insert(10); 73 | insert(20); 74 | insert(30); 75 | 76 | display(); 77 | 78 | deleteFromEnd(); 79 | display(); 80 | 81 | return 0; 82 | } 83 | 84 | -------------------------------------------------------------------------------- /DOUBLY-LINKED-LIST/LastInsertDoublyLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | struct Node* prev; 8 | }; 9 | 10 | struct Node* head = NULL; 11 | 12 | void lastInsert(int data) { 13 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 14 | if (newNode == NULL) { 15 | printf("\nMemory allocation failed.\n"); 16 | return; 17 | } 18 | newNode->data = data; 19 | newNode->next = NULL; 20 | 21 | if (head == NULL) { 22 | newNode->prev = NULL; 23 | head = newNode; 24 | } else { 25 | struct Node* temp = head; 26 | while (temp->next != NULL) { 27 | temp = temp->next; 28 | } 29 | temp->next = newNode; 30 | newNode->prev = temp; 31 | } 32 | 33 | printf("\nNode inserted.\n"); 34 | } 35 | 36 | void display() { 37 | struct Node* temp = head; 38 | 39 | if (temp == NULL) { 40 | printf("\nLinked List is empty\n"); 41 | } else { 42 | printf("\nLinked List: "); 43 | 44 | while (temp != NULL) { 45 | printf("%d ", temp->data); 46 | temp = temp->next; 47 | } 48 | 49 | printf("\n"); 50 | } 51 | } 52 | 53 | int main() 54 | { 55 | int choice, item; 56 | 57 | do { 58 | printf("\nEnter the item you want to insert: "); 59 | scanf("%d", &item); 60 | lastInsert(item); 61 | 62 | printf("\nPress 0 to insert more, or any other key to exit: "); 63 | scanf("%d", &choice); 64 | } while (choice == 0); 65 | 66 | // Display the linked list 67 | display(); 68 | 69 | return 0; 70 | } 71 | 72 | -------------------------------------------------------------------------------- /DSA-LEARNING-RESOURCES/DS lab programs-20200416T114234Z-001.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahendramahara/DSA-in-C-Programing/3d478d704a8607196d235c38ba6d0cdb66a5af4d/DSA-LEARNING-RESOURCES/DS lab programs-20200416T114234Z-001.zip -------------------------------------------------------------------------------- /DSA-LEARNING-RESOURCES/DSA Important Questions Solution.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahendramahara/DSA-in-C-Programing/3d478d704a8607196d235c38ba6d0cdb66a5af4d/DSA-LEARNING-RESOURCES/DSA Important Questions Solution.pdf -------------------------------------------------------------------------------- /DSA-LEARNING-RESOURCES/DSA Tutorial.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahendramahara/DSA-in-C-Programing/3d478d704a8607196d235c38ba6d0cdb66a5af4d/DSA-LEARNING-RESOURCES/DSA Tutorial.pdf -------------------------------------------------------------------------------- /DSA-LEARNING-RESOURCES/DSA_Notes_.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahendramahara/DSA-in-C-Programing/3d478d704a8607196d235c38ba6d0cdb66a5af4d/DSA-LEARNING-RESOURCES/DSA_Notes_.pdf -------------------------------------------------------------------------------- /DSA-LEARNING-RESOURCES/Data Structure and Algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahendramahara/DSA-in-C-Programing/3d478d704a8607196d235c38ba6d0cdb66a5af4d/DSA-LEARNING-RESOURCES/Data Structure and Algorithms.pdf -------------------------------------------------------------------------------- /DSA-LEARNING-RESOURCES/Trees Question Solution.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahendramahara/DSA-in-C-Programing/3d478d704a8607196d235c38ba6d0cdb66a5af4d/DSA-LEARNING-RESOURCES/Trees Question Solution.pdf -------------------------------------------------------------------------------- /DSA-LEARNING-RESOURCES/ds-cheat-sheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mahendramahara/DSA-in-C-Programing/3d478d704a8607196d235c38ba6d0cdb66a5af4d/DSA-LEARNING-RESOURCES/ds-cheat-sheet.pdf -------------------------------------------------------------------------------- /GRAPH/DF-and-BF-Search-in-Graph.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int vertex; 6 | struct Node* next; 7 | }; 8 | 9 | struct Graph { 10 | struct Node* adjacencyList[100]; 11 | int visited[100]; 12 | }; 13 | 14 | void initializeGraph(struct Graph* graph, int numVertices) { 15 | for (int i = 0; i < numVertices; ++i) { 16 | graph->adjacencyList[i] = NULL; 17 | graph->visited[i] = 0; 18 | } 19 | } 20 | 21 | void addEdge(struct Graph* graph, int src, int dest) { 22 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 23 | newNode->vertex = dest; 24 | newNode->next = graph->adjacencyList[src]; 25 | graph->adjacencyList[src] = newNode; 26 | 27 | newNode = (struct Node*)malloc(sizeof(struct Node)); 28 | newNode->vertex = src; 29 | newNode->next = graph->adjacencyList[dest]; 30 | graph->adjacencyList[dest] = newNode; 31 | } 32 | 33 | void dfs(struct Graph* graph, int startVertex) { 34 | int stack[100]; 35 | int top = -1; 36 | stack[++top] = startVertex; 37 | graph->visited[startVertex] = 1; 38 | while (top != -1) { 39 | int currentVertex = stack[top--]; 40 | printf("%d ", currentVertex); 41 | struct Node* current = graph->adjacencyList[currentVertex]; 42 | while (current != NULL) { 43 | int neighbor = current->vertex; 44 | if (!graph->visited[neighbor]) { 45 | stack[++top] = neighbor; 46 | graph->visited[neighbor] = 1; 47 | } 48 | current = current->next; 49 | } 50 | } 51 | } 52 | 53 | void bfs(struct Graph* graph, int startVertex) { 54 | int queue[100]; 55 | int front = 0, rear = -1; 56 | queue[++rear] = startVertex; 57 | graph->visited[startVertex] = 1; 58 | while (front <= rear) { 59 | int currentVertex = queue[front++]; 60 | printf("%d ", currentVertex); 61 | struct Node* current = graph->adjacencyList[currentVertex]; 62 | while (current != NULL) { 63 | int neighbor = current->vertex; 64 | if (!graph->visited[neighbor]) { 65 | queue[++rear] = neighbor; 66 | graph->visited[neighbor] = 1; 67 | } 68 | current = current->next; 69 | } 70 | } 71 | } 72 | 73 | int main() { 74 | struct Graph graph; 75 | int numVertices, numEdges; 76 | printf("Enter the number of vertices: "); 77 | scanf("%d", &numVertices); 78 | initializeGraph(&graph, numVertices); 79 | printf("Enter the number of edges: "); 80 | scanf("%d", &numEdges); 81 | printf("Enter the edges (format: src dest):\n"); 82 | for (int i = 0; i < numEdges; ++i) { 83 | int src, dest; 84 | scanf("%d %d", &src, &dest); 85 | addEdge(&graph, src, dest); 86 | } 87 | printf("\n\nDepth-First Search (DFS) starting from vertex 0:\n"); 88 | dfs(&graph, 0); 89 | 90 | // Resetting visited array for BFS 91 | for (int i = 0; i < numVertices; ++i) { 92 | graph.visited[i] = 0; 93 | } 94 | 95 | printf("\n\nBreadth-First Search (BFS) starting from vertex 0:\n"); 96 | bfs(&graph, 0); 97 | 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /GRAPH/Dijkstras-Algorithm-Shortest-Path.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define V 5 5 | 6 | void dijkstra(int graph[V][V], int src) { 7 | int dist[V]; 8 | int visited[V] = {0}; 9 | for (int i = 0; i < V; i++) { 10 | dist[i] = INT_MAX; 11 | } 12 | dist[src] = 0; 13 | for (int count = 0; count < V - 1; count++) { 14 | int u, min_dist = INT_MAX; 15 | for (int v = 0; v < V; v++) { 16 | if (!visited[v] && dist[v] < min_dist) { 17 | min_dist = dist[v]; 18 | u = v; 19 | } 20 | } 21 | visited[u] = 1; 22 | for (int v = 0; v < V; v++) { 23 | if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) { 24 | dist[v] = dist[u] + graph[u][v]; 25 | } 26 | } 27 | } 28 | printf("Vertex Distance from Source\n"); 29 | for (int i = 0; i < V; i++) { 30 | printf("%d \t\t %d\n", i, dist[i]); 31 | } 32 | } 33 | 34 | int main() { 35 | int graph[V][V] = { 36 | {0, 2, 0, 6, 0}, 37 | {2, 0, 3, 8, 5}, 38 | {0, 3, 0, 0, 7}, 39 | {6, 8, 0, 0, 9}, 40 | {0, 5, 7, 9, 0} 41 | }; 42 | int source = 0; 43 | dijkstra(graph, source); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /GRAPH/Floyd-Warshall-Algorithm-Shortest-Paths.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define V 4 4 | 5 | void floydWarshall(int graph[V][V]) { 6 | int dist[V][V]; 7 | for (int i = 0; i < V; i++) { 8 | for (int j = 0; j < V; j++) { 9 | dist[i][j] = graph[i][j]; 10 | } 11 | } 12 | for (int k = 0; k < V; k++) { 13 | for (int i = 0; i < V; i++) { 14 | for (int j = 0; j < V; j++) { 15 | if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j]) { 16 | dist[i][j] = dist[i][k] + dist[k][j]; 17 | } 18 | } 19 | } 20 | } 21 | printf("Shortest distances between all pairs of vertices:\n"); 22 | for (int i = 0; i < V; i++) { 23 | for (int j = 0; j < V; j++) { 24 | if (dist[i][j] == INT_MAX) { 25 | printf("INF\t"); 26 | } else { 27 | printf("%d\t", dist[i][j]); 28 | } 29 | } 30 | printf("\n"); 31 | } 32 | } 33 | 34 | int main() { 35 | int graph[V][V] = { 36 | {0, 5, INT_MAX, 10}, 37 | {INT_MAX, 0, 3, INT_MAX}, 38 | {INT_MAX, INT_MAX, 0, 1}, 39 | {INT_MAX, INT_MAX, INT_MAX, 0} 40 | }; 41 | floydWarshall(graph); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /GRAPH/Kruskals-Algorithm-Minimum-Spanning-Tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Edge { 5 | int src, dest, weight; 6 | }; 7 | 8 | struct Subset { 9 | int parent, rank; 10 | }; 11 | 12 | int find(struct Subset subsets[], int i); 13 | void Union(struct Subset subsets[], int x, int y); 14 | int compareEdges(const void* a, const void* b); 15 | void KruskalMST(struct Edge edges[], int V, int E); 16 | 17 | int find(struct Subset subsets[], int i) { 18 | if (subsets[i].parent != i) 19 | subsets[i].parent = find(subsets, subsets[i].parent); 20 | return subsets[i].parent; 21 | } 22 | 23 | void Union(struct Subset subsets[], int x, int y) { 24 | int xroot = find(subsets, x); 25 | int yroot = find(subsets, y); 26 | if (subsets[xroot].rank < subsets[yroot].rank) 27 | subsets[xroot].parent = yroot; 28 | else if (subsets[xroot].rank > subsets[yroot].rank) 29 | subsets[yroot].parent = xroot; 30 | else { 31 | subsets[yroot].parent = xroot; 32 | subsets[xroot].rank++; 33 | } 34 | } 35 | 36 | int compareEdges(const void* a, const void* b) { 37 | return ((struct Edge*)a)->weight - ((struct Edge*)b)->weight; 38 | } 39 | 40 | void KruskalMST(struct Edge edges[], int V, int E) { 41 | qsort(edges, E, sizeof(edges[0]), compareEdges); 42 | struct Subset subsets[V]; 43 | for (int v = 0; v < V; v++) { 44 | subsets[v].parent = v; 45 | subsets[v].rank = 0; 46 | } 47 | struct Edge result[V - 1]; 48 | int e = 0; 49 | for (int i = 0; i < E && e < V - 1; i++) { 50 | int x = find(subsets, edges[i].src); 51 | int y = find(subsets, edges[i].dest); 52 | if (x != y) { 53 | result[e++] = edges[i]; 54 | Union(subsets, x, y); 55 | } 56 | } 57 | printf("Edge Weight\n"); 58 | for (int i = 0; i < e; i++) 59 | printf("%d - %d %d\n", result[i].src, result[i].dest, result[i].weight); 60 | } 61 | 62 | int main() { 63 | int V = 4; 64 | int E = 5; 65 | struct Edge edges[] = { 66 | {0, 1, 2}, 67 | {0, 2, 1}, 68 | {1, 2, 4}, 69 | {1, 3, 5}, 70 | {2, 3, 6} 71 | }; 72 | printf("Minimum Spanning Tree (MST) using Kruskal's algorithm:\n"); 73 | KruskalMST(edges, V, E); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /GRAPH/Prims-Algirithm-Minimum-Spanning-Tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define V 5 4 | int minKey(int key[], int mstSet[]) { 5 | int min = INT_MAX, min_index; 6 | for (int v = 0; v < V; v++) { 7 | if (mstSet[v] == 0 && key[v] < min) { 8 | min = key[v]; 9 | min_index = v; 10 | } 11 | } 12 | return min_index; 13 | } 14 | void printMST(int parent[], int graph[V][V]) { 15 | printf("Edge Weight\n"); 16 | for (int i = 1; i < V; i++) 17 | printf("%d - %d %d \n", parent[i], i, graph[i][parent[i]]); 18 | } 19 | void primMST(int graph[V][V]) { 20 | int parent[V]; 21 | int key[V]; 22 | int mstSet[V]; 23 | for (int i = 0; i < V; i++) { 24 | key[i] = INT_MAX; 25 | mstSet[i] = 0; 26 | } 27 | key[0] = 0; 28 | parent[0] = -1; 29 | for (int count = 0; count < V - 1; count++) { 30 | int u = minKey(key, mstSet); 31 | mstSet[u] = 1; 32 | for (int v = 0; v < V; v++) { 33 | if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v]) { 34 | parent[v] = u; 35 | key[v] = graph[u][v]; 36 | } 37 | } 38 | } 39 | printMST(parent, graph); 40 | } 41 | int main() { 42 | int graph[V][V] = {{0, 2, 0, 6, 0}, 43 | {2, 0, 3, 8, 5}, 44 | {0, 3, 0, 0, 7}, 45 | {6, 8, 0, 0, 9}, 46 | {0, 5, 7, 9, 0}}; 47 | primMST(graph); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [2022] [Mahendra-Mahara] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LINKED-LIST/DeleteAtAnyPositionLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | }; 8 | 9 | struct Node* head = NULL; 10 | 11 | // Function to insert a node at the end of the linked list 12 | void insertAtEnd(int data) { 13 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 14 | newNode->data = data; 15 | newNode->next = NULL; 16 | 17 | if (head == NULL) { 18 | head = newNode; 19 | return; 20 | } 21 | 22 | struct Node* current = head; 23 | while (current->next != NULL) { 24 | current = current->next; 25 | } 26 | current->next = newNode; 27 | } 28 | 29 | // Function to delete a node from a specific position in the linked list 30 | void deleteFromPosition(int position) { 31 | if (position < 1 || head == NULL) { 32 | printf("Invalid position or linked list is empty.\n"); 33 | return; 34 | } 35 | 36 | if (position == 1) { 37 | struct Node* temp = head; 38 | head = head->next; 39 | free(temp); 40 | printf("Node at position %d deleted.\n", position); 41 | return; 42 | } 43 | 44 | struct Node* current = head; 45 | struct Node* previous = NULL; 46 | int currentPosition = 1; 47 | 48 | while (currentPosition < position && current != NULL) { 49 | previous = current; 50 | current = current->next; 51 | currentPosition++; 52 | } 53 | 54 | if (currentPosition != position) { 55 | printf("Position %d is out of bounds.\n", position); 56 | return; 57 | } 58 | 59 | previous->next = current->next; 60 | free(current); 61 | printf("Node at position %d deleted.\n", position); 62 | } 63 | 64 | // Function to display the linked list 65 | void display() { 66 | struct Node* current = head; 67 | printf("Linked list: "); 68 | while (current != NULL) { 69 | printf("%d -> ", current->data); 70 | current = current->next; 71 | } 72 | printf("NULL\n"); 73 | } 74 | 75 | int main() { 76 | insertAtEnd(10); 77 | insertAtEnd(20); 78 | insertAtEnd(30); 79 | insertAtEnd(40); 80 | 81 | display(); 82 | 83 | int position; 84 | printf("Enter the position of the node to delete: "); 85 | scanf("%d", &position); 86 | 87 | deleteFromPosition(position); 88 | display(); 89 | 90 | return 0; 91 | } 92 | 93 | -------------------------------------------------------------------------------- /LINKED-LIST/DeleteAtAnyPositionLinkedList1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void beginsert(int); 5 | void deleteatpos(); 6 | void display(); 7 | 8 | struct node 9 | { 10 | int data; 11 | struct node *next; 12 | }; 13 | 14 | struct node *head, *temp; 15 | 16 | void main() 17 | { 18 | int items[] = {10, 20, 30, 40, 50}; 19 | int i; 20 | int numItems = sizeof(items) / sizeof(items[0]); 21 | 22 | for (i = 0; i < numItems; i++) 23 | { 24 | beginsert(items[i]); 25 | } 26 | 27 | printf("\n\n Items in the list before deletion are: "); 28 | display(); 29 | 30 | deleteatpos(); 31 | 32 | printf("\n\n Items in the list after deletion are: "); 33 | display(); 34 | } 35 | 36 | void beginsert(int item) 37 | { 38 | struct node *ptr = (struct node *)malloc(sizeof(struct node)); 39 | if (ptr == NULL) 40 | { 41 | printf("\n ****************OVERFLOW**************** \n"); 42 | } 43 | else 44 | { 45 | ptr->data = item; 46 | ptr->next = head; 47 | head = ptr; 48 | } 49 | } 50 | void begdelete() 51 | { 52 | struct node *ptr; 53 | if (head == NULL) 54 | { 55 | printf("\n List is empty"); 56 | } 57 | else 58 | { 59 | ptr = head; 60 | head = ptr->next; 61 | free(ptr); 62 | printf("\n Node Deleted From The Beginning...."); 63 | } 64 | } 65 | void deleteatpos() 66 | { 67 | struct node *lod, *temp; 68 | int ps, i; 69 | printf("\n Enter the position you wants to delete:\n"); 70 | scanf("%d",&ps); 71 | if (ps==1) 72 | { 73 | begdelete(); 74 | } 75 | else 76 | { 77 | if(head == NULL) 78 | { 79 | printf("\nList Is Empty"); 80 | } 81 | else 82 | { 83 | temp=head; 84 | for(i=1;inext; 87 | } 88 | lod=temp->next; 89 | temp->next=lod->next; 90 | free(lod); 91 | } 92 | 93 | } 94 | } 95 | void display() 96 | { 97 | for (temp = head; temp != NULL; temp = temp->next) 98 | { 99 | printf("%d\t", temp->data); 100 | } 101 | } 102 | 103 | -------------------------------------------------------------------------------- /LINKED-LIST/DeleteAtFirstLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | }; 8 | 9 | struct Node* head = NULL; 10 | 11 | // Function to insert a node at the beginning of the linked list 12 | void insertAtBeginning(int data) { 13 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 14 | newNode->data = data; 15 | newNode->next = head; 16 | head = newNode; 17 | } 18 | 19 | // Function to delete the first node from the linked list 20 | void deleteFromBeginning() { 21 | if (head == NULL) { 22 | printf("Linked list is empty. Nothing to delete.\n"); 23 | return; 24 | } 25 | 26 | struct Node* temp = head; 27 | head = head->next; 28 | free(temp); 29 | printf("First node deleted.\n"); 30 | } 31 | 32 | // Function to display the linked list 33 | void display() { 34 | struct Node* current = head; 35 | printf("Linked list: "); 36 | while (current != NULL) { 37 | printf("%d -> ", current->data); 38 | current = current->next; 39 | } 40 | printf("NULL\n"); 41 | } 42 | 43 | int main() { 44 | insertAtBeginning(30); 45 | insertAtBeginning(20); 46 | insertAtBeginning(10); 47 | 48 | display(); 49 | 50 | deleteFromBeginning(); 51 | display(); 52 | 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /LINKED-LIST/DeleteAtFirstLinkedList1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void beginsert(int); 5 | void begdelete(); 6 | void display(); 7 | 8 | struct node 9 | { 10 | int data; 11 | struct node *next; 12 | }; 13 | 14 | struct node *head, *temp; 15 | 16 | void main() 17 | { 18 | int items[] = {10, 20, 30, 40, 50}; 19 | int i; 20 | int numItems = sizeof(items) / sizeof(items[0]); 21 | 22 | for (i = 0; i < numItems; i++) 23 | { 24 | beginsert(items[i]); 25 | } 26 | 27 | printf("\n\n Items in the list before deletion are: "); 28 | display(); 29 | 30 | begdelete(); 31 | 32 | printf("\n\n Items in the list after deletion are: "); 33 | display(); 34 | } 35 | 36 | void beginsert(int item) 37 | { 38 | struct node *ptr = (struct node *)malloc(sizeof(struct node)); 39 | if (ptr == NULL) 40 | { 41 | printf("\n ****************OVERFLOW**************** \n"); 42 | } 43 | else 44 | { 45 | ptr->data = item; 46 | ptr->next = head; 47 | head = ptr; 48 | } 49 | } 50 | 51 | void begdelete() 52 | { 53 | struct node *ptr; 54 | if (head == NULL) 55 | { 56 | printf("\n List is empty"); 57 | } 58 | else 59 | { 60 | ptr = head; 61 | head = ptr->next; 62 | free(ptr); 63 | printf("\n Node Deleted From The Beginning...."); 64 | } 65 | } 66 | 67 | void display() 68 | { 69 | for (temp = head; temp != NULL; temp = temp->next) 70 | { 71 | printf("%d\t", temp->data); 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /LINKED-LIST/DeleteAtLastLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | }; 8 | 9 | struct Node* head = NULL; 10 | 11 | // Function to insert a node at the end of the linked list 12 | void insertAtEnd(int data) { 13 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 14 | newNode->data = data; 15 | newNode->next = NULL; 16 | 17 | if (head == NULL) { 18 | head = newNode; 19 | return; 20 | } 21 | 22 | struct Node* current = head; 23 | while (current->next != NULL) { 24 | current = current->next; 25 | } 26 | current->next = newNode; 27 | } 28 | 29 | // Function to delete the last node from the linked list 30 | void deleteFromEnd() { 31 | if (head == NULL) { 32 | printf("Linked list is empty. Nothing to delete.\n"); 33 | return; 34 | } 35 | 36 | if (head->next == NULL) { 37 | free(head); 38 | head = NULL; 39 | printf("Last node deleted.\n"); 40 | return; 41 | } 42 | 43 | struct Node* current = head; 44 | struct Node* previous = NULL; 45 | 46 | while (current->next != NULL) { 47 | previous = current; 48 | current = current->next; 49 | } 50 | 51 | previous->next = NULL; 52 | free(current); 53 | printf("Last node deleted.\n"); 54 | } 55 | 56 | // Function to display the linked list 57 | void display() { 58 | struct Node* current = head; 59 | printf("Linked list: "); 60 | while (current != NULL) { 61 | printf("%d -> ", current->data); 62 | current = current->next; 63 | } 64 | printf("NULL\n"); 65 | } 66 | 67 | int main() { 68 | insertAtEnd(10); 69 | insertAtEnd(20); 70 | insertAtEnd(30); 71 | 72 | display(); 73 | 74 | deleteFromEnd(); 75 | display(); 76 | 77 | return 0; 78 | } 79 | 80 | -------------------------------------------------------------------------------- /LINKED-LIST/InsertAtAnyPositionLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void randominsert(int); 5 | void create(int); 6 | void display(); 7 | struct node 8 | { 9 | int data; 10 | struct node *next; 11 | }; 12 | struct node *head; 13 | 14 | void main() 15 | { 16 | int choice, item, loc; 17 | do 18 | { 19 | printf("\n Enter the item which you want to insert? \n"); 20 | scanf("%d", &item); 21 | if (head == NULL) 22 | { 23 | create(item); 24 | } 25 | else 26 | { 27 | randominsert(item); 28 | } 29 | printf("\n Press 0 To insert More? \n"); 30 | scanf("%d", &choice); 31 | } while (choice == 0); 32 | 33 | printf("\n Displaying the items in the linked list:\n"); 34 | display(); 35 | } 36 | 37 | void create(int item) 38 | { 39 | struct node *ptr = (struct node*)malloc(sizeof(struct node)); 40 | if (ptr == NULL) 41 | { 42 | printf("\n OVERFLOW"); 43 | } 44 | else 45 | { 46 | ptr->data = item; 47 | ptr->next = head; 48 | head = ptr; 49 | printf("\n Node inserted"); 50 | } 51 | } 52 | 53 | void randominsert(int item) 54 | { 55 | struct node *ptr = (struct node*)malloc(sizeof(struct node)); 56 | struct node *temp; 57 | int i, loc; 58 | if (ptr == NULL) 59 | { 60 | printf("\n OVERFLOW"); 61 | } 62 | else 63 | { 64 | printf("Enter The location: "); 65 | scanf("%d", &loc); 66 | ptr->data = item; 67 | temp = head; 68 | for (i = 0; i < loc - 1; i++) // Adjusted loop limit to be loc-1 for proper insertion 69 | { 70 | if (temp == NULL) 71 | { 72 | printf("\n Can't Insert \n"); 73 | return; 74 | } 75 | temp = temp->next; 76 | } 77 | ptr->next = temp->next; 78 | temp->next = ptr; 79 | printf("\n Node inserted"); 80 | } 81 | } 82 | 83 | void display() 84 | { 85 | struct node *ptr; 86 | ptr = head; 87 | if (ptr == NULL) 88 | { 89 | printf("Linked List is empty.\n"); 90 | } 91 | else 92 | { 93 | while (ptr != NULL) 94 | { 95 | printf("%d ", ptr->data); 96 | ptr = ptr->next; 97 | } 98 | } 99 | } 100 | 101 | -------------------------------------------------------------------------------- /LINKED-LIST/LastInsertLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void lastInsert(int); 5 | void display(); 6 | 7 | struct node { 8 | int data; 9 | struct node *next; 10 | }; 11 | 12 | struct node *head; 13 | 14 | int main() 15 | { 16 | int choice, item; 17 | 18 | do { 19 | printf("\nEnter the item you want to insert: "); 20 | scanf("%d", &item); 21 | lastInsert(item); 22 | 23 | printf("\nPress 0 to insert more, or any other key to exit: "); 24 | scanf("%d", &choice); 25 | } while (choice == 0); 26 | 27 | // Display the linked list 28 | display(); 29 | 30 | return 0; 31 | } 32 | 33 | void lastInsert(int item) 34 | { 35 | struct node *ptr = (struct node*)malloc(sizeof(struct node)); 36 | struct node *temp; 37 | 38 | if (ptr == NULL) { 39 | printf("\n LinkedList OVERFLOW"); 40 | } else { 41 | ptr->data = item; 42 | 43 | if (head == NULL) { 44 | ptr->next = NULL; 45 | head = ptr; 46 | printf("\n Node inserted "); 47 | } else { 48 | temp = head; 49 | 50 | while (temp->next != NULL) { 51 | temp = temp->next; 52 | } 53 | 54 | temp->next = ptr; 55 | ptr->next = NULL; 56 | printf("\n Node inserted"); 57 | } 58 | } 59 | } 60 | 61 | void display() { 62 | struct node *temp = head; 63 | 64 | if (temp == NULL) { 65 | printf("\nLinked List is empty\n"); 66 | } else { 67 | printf("\nLinked List: "); 68 | 69 | while (temp != NULL) { 70 | printf("%d ", temp->data); 71 | temp = temp->next; 72 | } 73 | 74 | printf("\n"); 75 | } 76 | } 77 | 78 | -------------------------------------------------------------------------------- /LINKED-LIST/begininsertlinkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void beginsert(int); 4 | struct node 5 | { 6 | int data; 7 | struct node *next; 8 | }; 9 | struct node *head, *temp; 10 | void main() 11 | { 12 | int choice,item; 13 | do{ 14 | printf("Enter the item which you want to insert: \n"); 15 | scanf("%d", &item); 16 | beginsert(item); 17 | printf("\nPress 0 to insert more: \n"); 18 | scanf("%d", &choice); 19 | }while(choice==0); 20 | printf("\n\nItems in the list are:"); 21 | //Print items in the node 22 | for (temp = head ; temp!=NULL ; temp=temp->next ) 23 | { 24 | printf("%d\t",temp -> data ); 25 | } 26 | } 27 | void beginsert(int item) 28 | { 29 | struct node *ptr = (struct node *)malloc(sizeof(struct node*)); 30 | if(ptr == NULL) 31 | { 32 | printf("\n ****************OVERFLOW**************** \n"); 33 | } 34 | else 35 | { 36 | ptr->data = item; 37 | ptr->next = head; 38 | head = ptr; 39 | printf("\n ****************Node Inserted**************** \n\n"); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /LIST/DeleteFromAnyPosition.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int array[10] = {500, 1000, 1500, 2000, 2500}; 5 | int position, i; 6 | 7 | // Print the original array 8 | printf("Original Array: "); 9 | for (i = 0; i < 5; i++) { 10 | printf("%d ", array[i]); 11 | } 12 | 13 | // Input the position to delete 14 | printf("\nEnter the position to delete: "); 15 | scanf("%d", &position); 16 | 17 | if (position < 0 || position >= 5) { 18 | printf("Invalid position!\n"); 19 | return 1; 20 | } 21 | 22 | // Shift elements to the left to overwrite the deleted element 23 | for (i = position; i < 4; i++) { 24 | array[i] = array[i + 1]; 25 | } 26 | 27 | // Decrement the size of the array 28 | int newSize = 4; 29 | 30 | // Print the updated array 31 | printf("\nArray After Deleted: "); 32 | for (i = 0; i < newSize; i++) { 33 | printf("%d ", array[i]); 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /LIST/DeleteFromFirst.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int array[10] = {10, 20, 30, 40, 50}; 5 | int i; 6 | 7 | // Print the original array 8 | printf("Original Array: "); 9 | for (i = 0; i < 5; i++) { 10 | printf("%d ", array[i]); 11 | } 12 | 13 | // Delete the element from the first position 14 | for (i = 0; i < 4; i++) { 15 | array[i] = array[i + 1]; 16 | } 17 | 18 | // Decrement the size of the array 19 | int newSize = 4; 20 | 21 | // Print the updated array 22 | printf("\nArray After Deleted: "); 23 | for (i = 0; i < newSize; i++) { 24 | printf("%d ", array[i]); 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /LIST/DeleteFromLast.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int array[10] = {60, 70, 80, 90, 100}; 5 | int i; 6 | 7 | // Print the original array 8 | printf("Original Array: "); 9 | for (i = 0; i < 5; i++) { 10 | printf("%d ", array[i]); 11 | } 12 | 13 | // Delete the element from the last position 14 | array[4] = 0; // Set the last element to 0 15 | 16 | // Decrement the size of the array 17 | int newSize = 4; 18 | 19 | // Print the updated array 20 | printf("\nArray After Deleted: "); 21 | for (i = 0; i < newSize; i++) { 22 | printf("%d ", array[i]); 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /LIST/InsertAtAnyPosition.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int array[10] = {500, 1000, 1500, 2000, 2500}; 5 | int newElement, position, i; 6 | 7 | // Print the original array 8 | printf("Original Array: "); 9 | for (i = 0; i < 5; i++) { 10 | printf("%d ", array[i]); 11 | } 12 | 13 | // Input the new element and position 14 | printf("\nEnter the new element: "); 15 | scanf("%d", &newElement); 16 | printf("Enter the position to insert: "); 17 | scanf("%d", &position); 18 | 19 | if (position < 0 || position > 5) { 20 | printf("Invalid position!\n"); 21 | return 1; 22 | } 23 | 24 | // Shift elements to the right to make space for the new element 25 | for (i = 4; i >= position; i--) { 26 | array[i + 1] = array[i]; 27 | } 28 | 29 | // Insert the new element at the specified position 30 | array[position] = newElement; 31 | 32 | // Print the updated array 33 | printf("Arrey After Updated: "); 34 | for (i = 0; i < 6; i++) { 35 | printf("%d ", array[i]); 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /LIST/InsertAtBegining.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int array[10] = {10, 20, 30, 40, 50}; 5 | int newElement, i; 6 | 7 | // Print the original array 8 | printf("Original Array: "); 9 | for (i = 0; i < 5; i++) { 10 | printf("%d ", array[i]); 11 | } 12 | 13 | // Input the new element to be inserted 14 | printf("\n\nEnter the new element: "); 15 | scanf("%d", &newElement); 16 | 17 | // Shift elements to the right to make space for the new element 18 | for (i = 4; i >= 0; i--) { 19 | array[i + 1] = array[i]; 20 | } 21 | 22 | // Insert the new element at the beginning 23 | array[0] = newElement; 24 | 25 | // Print the updated array 26 | printf("\nArray After Updated: "); 27 | for (i = 0; i < 6; i++) { 28 | printf("%d ", array[i]); 29 | } 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /LIST/InsertAtLast.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int array[10] = {60, 70, 80, 90, 100}; 5 | int newElement, i; 6 | 7 | // Print the original array 8 | printf("Original Array: "); 9 | for (i = 0; i < 5; i++) { 10 | printf("%d ", array[i]); 11 | } 12 | 13 | // Input the new element to be inserted 14 | printf("\nEnter the new element: "); 15 | scanf("%d", &newElement); 16 | 17 | // Insert the new element at the end 18 | array[5] = newElement; 19 | 20 | // Print the updated array 21 | printf("Arrey After Updated: "); 22 | for (i = 0; i < 6; i++) { 23 | printf("%d ", array[i]); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /QUEUE/LinearQueue.c: -------------------------------------------------------------------------------- 1 | // QUEUE OPERATION IN C 2 | #include 3 | # define SIZE 100 4 | void enqueue(); 5 | void dequeue(); 6 | void show(); 7 | int inp_arr[SIZE]; 8 | int Rear = - 1; 9 | int Front = - 1; 10 | main() 11 | { 12 | int ch; 13 | while (1) 14 | { 15 | printf("1.Enqueue Operation\n"); 16 | printf("2.Dequeue Operation\n"); 17 | printf("3.Display the Queue\n"); 18 | printf("4.Exit\n"); 19 | printf("Enter your choice of operations : "); 20 | scanf("%d", &ch); 21 | switch (ch) 22 | { 23 | case 1: 24 | enqueue(); 25 | break; 26 | case 2: 27 | dequeue(); 28 | break; 29 | case 3: 30 | show(); 31 | break; 32 | case 4: 33 | exit(0); 34 | default: 35 | printf("Incorrect choice \n"); 36 | } 37 | } 38 | } 39 | 40 | void enqueue() 41 | { 42 | int insert_item; 43 | if (Rear == SIZE - 1) 44 | printf("Overflow \n"); 45 | else 46 | { 47 | if (Front == - 1) 48 | 49 | Front = 0; 50 | printf("Element to be inserted in the Queue\n : "); 51 | scanf("%d", &insert_item); 52 | Rear = Rear + 1; 53 | inp_arr[Rear] = insert_item; 54 | } 55 | } 56 | 57 | void dequeue() 58 | { 59 | if (Front == - 1 || Front > Rear) 60 | { 61 | printf("Underflow \n"); 62 | return ; 63 | } 64 | else 65 | { 66 | printf("Element deleted from the Queue: %d\n", inp_arr[Front]); 67 | Front = Front + 1; 68 | } 69 | } 70 | 71 | void show() 72 | { 73 | int i; 74 | 75 | if (Front == - 1) 76 | printf("Empty Queue \n"); 77 | else 78 | { 79 | printf("Queue: \n"); 80 | for (i = Front; i <= Rear; i++) 81 | printf("%d ", inp_arr[i]); 82 | printf("\n"); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /QUEUE/circularqueue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define SIZE 5 3 | int items[SIZE]; 4 | int front =-1, rear= -1; 5 | // check the queue is full or not 6 | int isFull() 7 | { 8 | if((front == rear+1) || (front == 0 && rear == SIZE -1)) 9 | return 1; 10 | return 0; 11 | } 12 | // check the queue is empty or not 13 | int isEmpty() 14 | { 15 | if(front == -1) 16 | return 1; 17 | return 0; 18 | } 19 | // Adding an element 20 | void enQueue(int element) 21 | { 22 | if (isFull()) 23 | printf("\n Queue is full!! \n"); 24 | else{ 25 | if (front==-1) front =0; 26 | rear = (rear + 1) % SIZE; 27 | items[rear] = element; 28 | printf("\n Inserted -> %d", element); 29 | } 30 | } 31 | // Removing an element 32 | int deQueue() 33 | { 34 | int element; 35 | if(isEmpty()) 36 | { 37 | printf("\n Queue is Empty!! \n"); 38 | return (-1); 39 | } 40 | else{ 41 | element= items[front]; 42 | if(front == rear) 43 | { 44 | front= -1; 45 | rear= -1; 46 | } 47 | else{ 48 | front= (front +1)%SIZE; 49 | } 50 | printf("\n Deleted element -> %d \n", element); 51 | return (element); 52 | } 53 | } 54 | // display the queue 55 | void display(){ 56 | int i; 57 | if(isEmpty()) 58 | printf("\n Empty Queue \n"); 59 | else 60 | { 61 | printf("\n Front -> %d", front); 62 | printf("\n items -> "); 63 | for(i=front; i!=rear; i=(i+1)%SIZE) 64 | { 65 | printf("%d", items[i]); 66 | } 67 | printf("%d", items[i]); 68 | printf("\n Rear -> %d \n", rear); 69 | } 70 | } 71 | int main() 72 | { 73 | //Fails because front = -1 74 | deQueue(); 75 | enQueue(5); 76 | enQueue(6); 77 | enQueue(7); 78 | enQueue(8); 79 | enQueue(9); 80 | //Fails because front = 0 && rear == SIZE -1 81 | enQueue(1); 82 | display(); 83 | deQueue(); 84 | display(); 85 | enQueue(0); 86 | display(); 87 | //Fails because front = rear+1 88 | enQueue(2); 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in C Programming 2 | 3 | Welcome to the Data Structures and Algorithms in C Programming repository! This repository contains implementations of various data structures and algorithms in the C programming language. Whether you're learning about fundamental data structures or exploring advanced algorithms, you'll find practical examples and explanations here. 4 | 5 | ## Quick Access 6 | 7 | 1. [Circular Linked List](./CIRCULAR-LINKED-LIST) 8 | 2. [Doubly Linked List](./DOUBLY-LINKED-LIST) 9 | 3. [Graph](./GRAPH) 10 | 4. [Linked List](./LINKED-LIST) 11 | 5. [List](./LIST) 12 | 6. [Queue](./QUEUE) 13 | 7. [Recursion](./RECURSION) 14 | 8. [Searching](./SEARCHING) 15 | 9. [Sorting](./SORTING) 16 | 10. [Stack](./STACK) 17 | 11. [Tree](./TREE) 18 | 12. [DSA Learning Resources](./DSA-LEARNING-RESOURCES) 19 | 20 | ## Table of Contents 21 | 22 | 1. [Introduction](#introduction) 23 | 2. [Data Structures](#data-structures) 24 | - [Linked Lists](#linked-lists) 25 | - [Stacks](#stacks) 26 | - [Queues](#queues) 27 | - [Trees](#trees) 28 | - [Graphs](#graphs) 29 | 3. [Algorithms](#algorithms) 30 | - [Searching](#searching) 31 | - [Sorting](#sorting) 32 | - [Recursion](#recursion) 33 | - [Dynamic Programming](#dynamic-programming) 34 | - [Greedy Algorithms](#greedy-algorithms) 35 | - [Graph Algorithms](#graph-algorithms) 36 | 4. [Contributing](#contributing) 37 | 5. [License](#license) 38 | 39 | ## Introduction 40 | 41 | Understanding data structures and algorithms is fundamental to becoming a proficient programmer. This repository aims to provide a comprehensive collection of C implementations for various data structures and algorithms. Whether you're a student, a software engineer, or an enthusiast, you'll find valuable resources here to deepen your understanding and improve your coding skills. 42 | 43 | ## Data Structures 44 | 45 | ### Linked Lists 46 | 47 | Linked lists are linear data structures where elements are linked using pointers. They come in various forms, such as singly linked lists, doubly linked lists, and circular linked lists. In this repository, you'll find implementations of these linked list variants along with operations like insertion, deletion, and traversal. 48 | 49 | ### Stacks 50 | 51 | A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. It supports two main operations: push (to add an element) and pop (to remove the top element). Implementations of stack data structure with various functionalities are available here. 52 | 53 | ### Queues 54 | 55 | A queue is a linear data structure that follows the First In, First Out (FIFO) principle. It supports operations like enqueue (to add an element) and dequeue (to remove the front element). This repository contains implementations of queue data structures and their associated operations. 56 | 57 | ### Trees 58 | 59 | Trees are hierarchical data structures consisting of nodes connected by edges. Common types of trees include binary trees, binary search trees (BSTs), AVL trees, and B-trees. You'll find implementations of these tree variants and their operations like insertion, deletion, and traversal. 60 | 61 | ### Graphs 62 | 63 | Graphs are non-linear data structures consisting of nodes (vertices) and edges connecting these nodes. They are widely used to model relationships between objects. This repository includes implementations of various graph algorithms like breadth-first search (BFS), depth-first search (DFS), Dijkstra's algorithm, and more. 64 | 65 | ## Algorithms 66 | 67 | ### Searching 68 | 69 | Searching algorithms are used to find a particular element in a data structure. Common searching algorithms include linear search, binary search, and interpolation search. You'll find C implementations of these algorithms along with their explanations and complexities. 70 | 71 | ### Sorting 72 | 73 | Sorting algorithms are used to arrange elements in a specific order, such as ascending or descending. Common sorting algorithms include bubble sort, insertion sort, selection sort, merge sort, quick sort, and heap sort. This repository provides C implementations of these sorting algorithms and their comparisons. 74 | 75 | ### Recursion 76 | 77 | Recursion is a programming technique where a function calls itself to solve smaller instances of a problem. It is commonly used in algorithms like factorial calculation, Fibonacci sequence generation, and binary tree traversal. You'll find recursive implementations of various algorithms in this repository. 78 | 79 | ### Dynamic Programming 80 | 81 | Dynamic programming is a method for solving complex problems by breaking them down into simpler subproblems and solving each subproblem only once. It is used to optimize algorithms by storing the results of subproblems to avoid redundant computations. This repository includes dynamic programming solutions to classic problems like the knapsack problem, longest common subsequence, and more. 82 | 83 | ### Greedy Algorithms 84 | 85 | Greedy algorithms make locally optimal choices at each step with the hope of finding a global optimum. They are often used for optimization problems where a sequence of choices must be made. This repository contains C implementations of greedy algorithms like Kruskal's algorithm, Prim's algorithm, and Dijkstra's algorithm. 86 | 87 | ### Graph Algorithms 88 | 89 | Graph algorithms are used to solve problems related to graphs, such as finding the shortest path, detecting cycles, and determining connectivity. This repository provides C implementations of fundamental graph algorithms like BFS, DFS, topological sorting, and more. 90 | 91 | ## Getting Started 92 | 93 | To get started with using the scripts in this repository, follow these steps: 94 | 95 | 1. Clone this repository to your local machine using the following command: 96 | 97 | ``` 98 | git clone https://github.com/mahendramahara/DSA-in-C-Programing.git 99 | ``` 100 | 101 | 2. Navigate to the directory containing the script you are interested in. 102 | 3. Follow the instructions provided in the README.md file of that directory to set up and run the script. 103 | 104 | 105 | ## License 106 | 107 | This repository is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. 108 | 109 | --- 110 | 111 | Happy Coding! 🚀 112 | -------------------------------------------------------------------------------- /RECURSION/Factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to calculate factorial using recursion 4 | unsigned long long factorial(int n) { 5 | if (n == 0 || n == 1) { 6 | return 1; 7 | } else { 8 | return n * factorial(n - 1); 9 | } 10 | } 11 | 12 | int main() { 13 | int num; 14 | 15 | printf("Enter a positive integer: "); 16 | scanf("%d", &num); 17 | 18 | if (num < 0) { 19 | printf("Factorial is not defined for negative numbers.\n"); 20 | } 21 | else { 22 | unsigned long long result = factorial(num); 23 | printf("Factorial of %d is %llu\n", num, result); 24 | } 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /RECURSION/FibonacciSeries.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int fibonacci(int n) { 5 | if (n == 0) { 6 | return 0; 7 | } 8 | else if (n == 1) { 9 | return 1; 10 | } 11 | else { 12 | return fibonacci(n - 1) + fibonacci(n - 2); 13 | } 14 | } 15 | 16 | int main() { 17 | int num; 18 | printf("Enter The Number: "); 19 | scanf("%d", &num); 20 | 21 | if (num < 0) { 22 | printf("Fibonacci is not defined for negative numbers.\n"); 23 | } else { 24 | int fib = fibonacci(num); 25 | printf("Fibonacci of %d is %d\n", num, fib); 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /RECURSION/TowerOfHanoi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void hanoi(int n, char rodFrom, char rodMiddle, char rodTo); 4 | int main() 5 | { 6 | hanoi(3, 'A', 'B', 'C'); 7 | return 0; 8 | } 9 | 10 | void hanoi(int n, char rodFrom, char rodMiddle, char rodTo){ 11 | if(n==1){ 12 | printf(" Disk 1 moved from %c to %c \n",rodFrom,rodTo); 13 | return; 14 | } 15 | hanoi(n-1,rodFrom,rodTo,rodMiddle); 16 | printf(" Disk %d moved from %c to %c \n",n,rodFrom,rodTo); 17 | hanoi(n-1,rodMiddle,rodFrom,rodTo); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /SEARCHING/HashTable-Chaining.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 10 4 | 5 | struct KeyValue { 6 | int key; 7 | int value; 8 | struct KeyValue* next; 9 | }; 10 | 11 | struct HashTable { 12 | struct KeyValue *table[SIZE]; 13 | }; 14 | 15 | int hashFunction(int key) { 16 | return key % SIZE; 17 | } 18 | 19 | void insert(struct HashTable *hashTable, int key, int value) { 20 | int index = hashFunction(key); 21 | if (hashTable->table[index] == NULL) { 22 | struct KeyValue *newPair = (struct KeyValue *)malloc(sizeof(struct KeyValue)); 23 | newPair->key = key; 24 | newPair->value = value; 25 | newPair->next = NULL; 26 | hashTable->table[index] = newPair; 27 | } else { 28 | struct KeyValue *current = hashTable->table[index]; 29 | while (current->next != NULL) { 30 | current = current->next; 31 | } 32 | struct KeyValue *newPair = (struct KeyValue *)malloc(sizeof(struct KeyValue)); 33 | newPair->key = key; 34 | newPair->value = value; 35 | newPair->next = NULL; 36 | current->next = newPair; 37 | } 38 | } 39 | 40 | int search(struct HashTable *hashTable, int key) { 41 | int index = hashFunction(key); 42 | struct KeyValue *current = hashTable->table[index]; 43 | while (current != NULL) { 44 | if (current->key == key) { 45 | return current->value; 46 | } 47 | current = current->next; 48 | } 49 | return -1; 50 | } 51 | 52 | int main() { 53 | struct HashTable hashTable; 54 | for (int i = 0; i < SIZE; i++) { 55 | hashTable.table[i] = NULL; 56 | } 57 | insert(&hashTable, 12, 100); 58 | insert(&hashTable, 25, 200); 59 | insert(&hashTable, 35, 300); 60 | int value1 = search(&hashTable, 12); 61 | int value2 = search(&hashTable, 25); 62 | int value3 = search(&hashTable, 35); 63 | int value4 = search(&hashTable, 99); 64 | printf("Value for key 12: %d\n", value1); 65 | printf("Value for key 25: %d\n", value2); 66 | printf("Value for key 35: %d\n", value3); 67 | printf("Value for key 99: %d\n", value4); 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /SEARCHING/Linear-Binary-Search.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int linearSearch(int arr[], int n, int target); 4 | int binarySearch(int arr[], int n, int target); 5 | 6 | int binarySearch(int arr[], int n, int target) { 7 | int low = 0; 8 | int high = n - 1; 9 | while (low <= high) { 10 | int mid = low + (high - low) / 2; 11 | if (arr[mid] == target) { 12 | return mid; 13 | } 14 | else if (arr[mid] < target) { 15 | low = mid + 1; 16 | } 17 | else { 18 | high = mid - 1; 19 | } 20 | } 21 | return -1; 22 | } 23 | 24 | void printSearchResult(int result, int target) { 25 | if (result != -1) { 26 | printf("Element %d found at index %d.\n", target, result); 27 | } else { 28 | printf("Element %d not found in the array.\n", target); 29 | } 30 | } 31 | 32 | int linearSearch(int arr[], int n, int target) { 33 | for (int i = 0; i < n; i++) { 34 | if (arr[i] == target) { 35 | return i; 36 | } 37 | } 38 | return -1; 39 | } 40 | 41 | int main() { 42 | int arr[] = {2, 3, 4, 10, 40}; 43 | int n = sizeof(arr) / sizeof(arr[0]); 44 | int target = 10; 45 | int target2 = 3; 46 | printf("Sorted array: "); 47 | for (int i = 0; i < n; i++) { 48 | printf("%d ", arr[i]); 49 | } 50 | printf("\n"); 51 | int result = binarySearch(arr, n, target); 52 | printf("\n\nUsing Binary Search:\n"); 53 | printSearchResult(result, target); 54 | int result2 = linearSearch(arr, n, target2); 55 | printf("\n\nUsing Linear Search:\n"); 56 | printSearchResult(result, target2); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /SORTING/All-Sorting-Algorithms-Implementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | void selectionSort(int arr[], int n); 3 | void insertionSort(int arr[], int n); 4 | void bubbleSort(int arr[], int n); 5 | void merge(int arr[], int l, int m, int r); 6 | void mergeSort(int arr[], int l, int r); 7 | void quickSort(int arr[], int low, int high); 8 | int partition(int arr[], int low, int high); 9 | int getMaxDigits(int arr[], int n); 10 | void countingSort(int arr[], int n, int exp); 11 | void radixSort(int arr[], int n); 12 | void heapify(int arr[], int n, int i); 13 | void heapSort(int arr[], int n); 14 | void shellSort(int arr[], int n); 15 | 16 | // Function to perform selection sort 17 | void selectionSort(int arr[], int n) { 18 | int i, j, minIndex, temp; 19 | // Traverse the entire array 20 | for (i = 0; i < n - 1; i++) { 21 | // Assume the current index is the minimum 22 | minIndex = i; 23 | // Find the index of the minimum element in the unsorted part 24 | for (j = i + 1; j < n; j++) { 25 | if (arr[j] < arr[minIndex]) { 26 | minIndex = j; 27 | } 28 | } 29 | // Swap the found minimum element with the first element 30 | temp = arr[minIndex]; 31 | arr[minIndex] = arr[i]; 32 | arr[i] = temp; 33 | } 34 | } 35 | 36 | // Function to perform Insertion Sort 37 | void insertionSort(int arr[], int n) { 38 | int i, key, j; 39 | for (i = 1; i < n; i++) { 40 | key = arr[i]; 41 | j = i - 1; 42 | // Move elements of arr[0..i-1] that are greater than key to one position ahead of their current position 43 | while (j >= 0 && arr[j] > key) { 44 | arr[j + 1] = arr[j]; 45 | j = j - 1; 46 | } 47 | // Insert the key into its correct position 48 | arr[j + 1] = key; 49 | } 50 | } 51 | 52 | // Function to perform Bubble Sort 53 | void bubbleSort(int arr[], int n) 54 | { 55 | int i, j, temp; 56 | int swapped; 57 | for (i = 0; i < n - 1; i++) { 58 | swapped = 0; // Flag to check if any swapping occurs in the inner loop 59 | // Last i elements are already in place, so no need to check them 60 | for (j = 0; j < n - 1 - i; j++) { 61 | // Compare adjacent elements 62 | if (arr[j] > arr[j + 1]) { 63 | // Swap them if they are in the wrong order 64 | temp = arr[j]; 65 | arr[j] = arr[j + 1]; 66 | arr[j + 1] = temp; 67 | swapped = 1; // Set the flag to indicate swapping occurred 68 | } 69 | } 70 | // If no swapping occurred in the inner loop, the array is already sorted 71 | if (swapped == 0) { 72 | break; 73 | } 74 | } 75 | } 76 | 77 | // Function to merge two subarrays of arr[] 78 | // First subarray is arr[l..m] 79 | // Second subarray is arr[m+1..r] 80 | void merge(int arr[], int l, int m, int r) { 81 | int i, j, k; 82 | int n1 = m - l + 1; 83 | int n2 = r - m; 84 | // Create temporary arrays 85 | int L[n1], R[n2]; 86 | // Copy data to temporary arrays L[] and R[] 87 | for (i = 0; i < n1; i++) 88 | L[i] = arr[l + i]; 89 | for (j = 0; j < n2; j++) 90 | R[j] = arr[m + 1 + j]; 91 | // Merge the temporary arrays back into arr[l..r] 92 | i = 0; // Initial index of first subarray 93 | j = 0; // Initial index of second subarray 94 | k = l; // Initial index of merged subarray 95 | while (i < n1 && j < n2) { 96 | if (L[i] <= R[j]) { 97 | arr[k] = L[i]; 98 | i++; 99 | } else { 100 | arr[k] = R[j]; 101 | j++; 102 | } 103 | k++; 104 | } 105 | // Copy the remaining elements of L[], if there are any 106 | while (i < n1) { 107 | arr[k] = L[i]; 108 | i++; 109 | k++; 110 | } 111 | // Copy the remaining elements of R[], if there are any 112 | while (j < n2) { 113 | arr[k] = R[j]; 114 | j++; 115 | k++; 116 | } 117 | } 118 | // Main function to perform merge sort 119 | void mergeSort(int arr[], int l, int r) { 120 | if (l < r) { 121 | // Same as (l+r)/2, but avoids overflow for large l and r 122 | int m = l + (r - l) / 2; 123 | // Sort first and second halves 124 | mergeSort(arr, l, m); 125 | mergeSort(arr, m + 1, r); 126 | // Merge the sorted halves 127 | merge(arr, l, m, r); 128 | } 129 | } 130 | 131 | // Function to partition the array and return the index of the pivot 132 | int partition(int arr[], int low, int high) { 133 | int pivot = arr[high]; // Choose the last element as the pivot 134 | int i = low - 1; // Index of the smaller element 135 | for (int j = low; j < high; j++) { 136 | // If the current element is smaller than or equal to the pivot 137 | if (arr[j] <= pivot) { 138 | i++; 139 | // Swap arr[i] and arr[j] 140 | int temp = arr[i]; 141 | arr[i] = arr[j]; 142 | arr[j] = temp; 143 | } 144 | } 145 | // Swap arr[i+1] and arr[high] (put the pivot in its correct place) 146 | int temp = arr[i + 1]; 147 | arr[i + 1] = arr[high]; 148 | arr[high] = temp; 149 | return i + 1; 150 | } 151 | // Function to perform Quick Sort 152 | void quickSort(int arr[], int low, int high) { 153 | if (low < high) { 154 | // Partition the array and get the index of the pivot 155 | int pi = partition(arr, low, high); 156 | // Recursively sort the subarrays 157 | quickSort(arr, low, pi - 1); 158 | quickSort(arr, pi + 1, high); 159 | } 160 | } 161 | 162 | // Function to find the maximum number of digits in an array 163 | int getMaxDigits(int arr[], int n) { 164 | int max = arr[0]; 165 | for (int i = 1; i < n; i++) { 166 | if (arr[i] > max) { 167 | max = arr[i]; 168 | } 169 | } 170 | 171 | // Count the number of digits in the maximum number 172 | int digits = 0; 173 | while (max > 0) { 174 | digits++; 175 | max /= 10; 176 | } 177 | return digits; 178 | } 179 | // Using counting sort to sort the elements based on a particular digit 180 | void countingSort(int arr[], int n, int exp) { 181 | const int BASE = 10; 182 | int output[n]; 183 | int count[BASE]; 184 | // Initialize the count array 185 | for (int i = 0; i < BASE; i++) { 186 | count[i] = 0; 187 | } 188 | // Count the occurrences of each digit at the current place value 189 | for (int i = 0; i < n; i++) { 190 | count[(arr[i] / exp) % BASE]++; 191 | } 192 | // Update count[i] to store the position of the digit in the output array 193 | for (int i = 1; i < BASE; i++) { 194 | count[i] += count[i - 1]; 195 | } 196 | // Build the output array 197 | for (int i = n - 1; i >= 0; i--) { 198 | output[count[(arr[i] / exp) % BASE] - 1] = arr[i]; 199 | count[(arr[i] / exp) % BASE]--; 200 | } 201 | // Copy the output array back to the original array 202 | for (int i = 0; i < n; i++) 203 | { 204 | arr[i] = output[i]; 205 | } 206 | } 207 | 208 | // Function to perform Radix Sort 209 | void radixSort(int arr[], int n) { 210 | int maxDigits = getMaxDigits(arr, n); 211 | // Apply counting sort for each digit place value 212 | for (int exp = 1; maxDigits / exp > 0; exp *= 10) { 213 | countingSort(arr, n, exp); 214 | } 215 | } 216 | // Function to perform heapify on a subtree rooted at index i 217 | void heapify(int arr[], int n, int i) { 218 | int largest = i; // Initialize largest as root 219 | int left = 2 * i + 1; 220 | int right = 2 * i + 2; 221 | // If left child is larger than root 222 | if (left < n && arr[left] > arr[largest]) { 223 | largest = left; 224 | } 225 | // If right child is larger than root 226 | if (right < n && arr[right] > arr[largest]) { 227 | largest = right; 228 | } 229 | 230 | // If largest is not the root, swap and recursively heapify the affected subtree 231 | if (largest != i) { 232 | int temp = arr[i]; 233 | arr[i] = arr[largest]; 234 | arr[largest] = temp; 235 | heapify(arr, n, largest); 236 | } 237 | } 238 | // Function to perform heap sort 239 | void heapSort(int arr[], int n) { 240 | // Build a max heap 241 | for (int i = n / 2 - 1; i >= 0; i--) { 242 | heapify(arr, n, i); 243 | } 244 | // Extract elements one by one from the heap 245 | for (int i = n - 1; i > 0; i--) { 246 | // Swap the root (maximum element) with the last element 247 | int temp = arr[0]; 248 | arr[0] = arr[i]; 249 | arr[i] = temp; 250 | // Reduce the size of the heap and heapify the root 251 | heapify(arr, i, 0); 252 | } 253 | } 254 | 255 | 256 | 257 | 258 | // Function to perform shell sort 259 | void shellSort(int arr[], int n) { 260 | for (int gap = n / 2; gap > 0; gap /= 2) { 261 | for (int i = gap; i < n; i++) { 262 | int temp = arr[i]; 263 | int j; 264 | for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { 265 | arr[j] = arr[j - gap]; 266 | } 267 | arr[j] = temp; 268 | } 269 | } 270 | } 271 | // Function to print an array 272 | void printArray(int arr[], int size) 273 | { 274 | for (int i = 0; i < size; i++) { 275 | printf("%d ", arr[i]); 276 | } 277 | printf("\n"); 278 | } 279 | // Driver program to test the selectionSort function 280 | int main() { 281 | int arr[] = {24, 25, 12, 22, 11}; 282 | int n = sizeof(arr) / sizeof(arr[0]); 283 | 284 | printf("Unsorted array: \n"); 285 | printArray(arr, n); 286 | 287 | selectionSort(arr, n); 288 | printf("\n\nSorted array using Selection Sort: \n"); 289 | printArray(arr, n); 290 | // Resetting the array 291 | int arr2[] = {24, 25, 12, 22, 11}; 292 | insertionSort(arr2, n); 293 | printf("\n\nSorted array using Insertion Sort: \n"); 294 | printArray(arr2, n); 295 | // Resetting the array 296 | int arr3[] = {24, 25, 12, 22, 11}; 297 | bubbleSort(arr3, n); 298 | printf("\n\nSorted array using Bubble Sort: \n"); 299 | printArray(arr3, n); 300 | // Resetting the array 301 | int arr4[] = {24, 25, 12, 22, 11}; 302 | mergeSort(arr4, 0, n - 1); 303 | printf("\n\nSorted array using Merge Sort: \n"); 304 | printArray(arr4, n); 305 | // Resetting the array 306 | int arr5[] = {24, 25, 12, 22, 11}; 307 | quickSort(arr5, 0, n - 1); 308 | printf("\n\nSorted array using Quick Sort: \n"); 309 | printArray(arr5, n); 310 | // Resetting the array 311 | int arr6[] = {24, 25, 12, 22, 11}; 312 | radixSort(arr6, n); 313 | printf("\n\nSorted array using Radix Sort: \n"); 314 | printArray(arr6, n); 315 | // Resetting the array 316 | int arr7[] = {24, 25, 12, 22, 11}; 317 | heapSort(arr7, n); 318 | printf("\n\nSorted array using Heap Sort: \n"); 319 | printArray(arr7, n); 320 | // Resetting the array 321 | int arr8[] = {24, 25, 12, 22, 11}; 322 | shellSort(arr8, n); 323 | printf("\n\nSorted array using Shell Sort: \n"); 324 | printArray(arr8, n); 325 | return 0; 326 | } 327 | -------------------------------------------------------------------------------- /STACK/PostfixToInfix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX 100 6 | 7 | char stack[MAX][MAX]; 8 | int top = -1; 9 | 10 | int isOperator(char c) { 11 | return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^'); 12 | } 13 | 14 | void push(char *str) { 15 | strcpy(stack[++top], str); 16 | } 17 | 18 | void pop(char *str) { 19 | strcpy(str, stack[top--]); 20 | } 21 | 22 | void postfixtoinfix(char *postfix, char *infix) { 23 | int len = strlen(postfix); 24 | int i; 25 | 26 | for ( i = 0; i < len; i++) { 27 | if (isOperator(postfix[i])) { 28 | char operand2[MAX], operand1[MAX]; 29 | char temp[MAX]; 30 | 31 | pop(operand2); 32 | pop(operand1); 33 | 34 | sprintf(temp, "(%s%c%s)", operand1, postfix[i], operand2); 35 | push(temp); 36 | } else { 37 | char temp[MAX]; 38 | temp[0] = postfix[i]; 39 | temp[1] = '\0'; 40 | push(temp); 41 | } 42 | } 43 | 44 | pop(infix); 45 | } 46 | 47 | int main() { 48 | char postfix[MAX], infix[MAX]; 49 | 50 | printf("Enter the postfix expression: "); 51 | fgets(postfix, sizeof(postfix), stdin); 52 | postfix[strlen(postfix) - 1] = '\0'; 53 | // Remove the newline character from fgets 54 | 55 | postfixtoinfix(postfix, infix); 56 | 57 | printf("Infix expression is: %s\n", infix); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /STACK/PrefixToInfix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX 100 6 | 7 | char stack[MAX][MAX]; 8 | int top = -1; 9 | 10 | int isOperator(char c) { 11 | return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^'); 12 | } 13 | 14 | void push(char *str) { 15 | strcpy(stack[++top], str); 16 | } 17 | 18 | void pop(char *str) { 19 | strcpy(str, stack[top--]); 20 | } 21 | 22 | void prefixtoinfix(char *prefix, char *infix) { 23 | int len = strlen(prefix); 24 | int i; 25 | 26 | for ( i = len - 1; i >= 0; i--) { 27 | if (isOperator(prefix[i])) { 28 | char operand1[MAX], operand2[MAX]; 29 | char temp[MAX]; 30 | 31 | pop(operand1); 32 | pop(operand2); 33 | 34 | sprintf(temp, "(%s%c%s)", operand1, prefix[i], operand2); 35 | push(temp); 36 | } else { 37 | char temp[MAX]; 38 | temp[0] = prefix[i]; 39 | temp[1] = '\0'; 40 | push(temp); 41 | } 42 | } 43 | 44 | pop(infix); 45 | } 46 | 47 | int main() { 48 | char prefix[MAX], infix[MAX]; 49 | 50 | printf("Enter the prefix expression: "); 51 | fgets(prefix, sizeof(prefix), stdin); 52 | prefix[strlen(prefix) - 1] = '\0'; 53 | // Remove the newline character from fgets 54 | 55 | prefixtoinfix(prefix, infix); 56 | 57 | printf("Infix expression is: %s\n", infix); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /STACK/StackImplementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_SIZE 20 3 | 4 | int topOfStack = -1; 5 | int stack[MAX_SIZE]; 6 | int main() 7 | { 8 | while (1) 9 | { 10 | int keyPress; 11 | printf("\n\nPress a key according to the operation you want.\n1. Push\n2. Pop\n3. Display\n: "); 12 | scanf("%d", &keyPress); 13 | switch (keyPress) 14 | { 15 | case 1: 16 | push(); 17 | break; 18 | case 2: 19 | pop(); 20 | break; 21 | case 3: 22 | display(); 23 | break; 24 | 25 | default: 26 | printf("Enter a vaild option!"); 27 | break; 28 | } 29 | } 30 | } 31 | 32 | void push() 33 | { 34 | if (topOfStack == 20) 35 | { 36 | printf("\nStack Overflow!\nCan't add any more items."); 37 | } 38 | else 39 | { 40 | printf("\nEnter the element to push into the stack: "); 41 | scanf("%d", &stack[++topOfStack]); 42 | printf("\nPushed successfully!"); 43 | } 44 | } 45 | 46 | void pop() 47 | { 48 | if (topOfStack == -1) 49 | { 50 | printf("\nStack Underflow!"); 51 | } 52 | else 53 | { 54 | printf("%d", stack[topOfStack--]); 55 | } 56 | } 57 | 58 | void display() 59 | { 60 | int i; 61 | if (topOfStack == -1) 62 | { 63 | printf("\nStack Underflow!"); 64 | } 65 | else 66 | { 67 | printf("\nStack:\n"); 68 | for ( i = topOfStack; i > -1; i--) 69 | { 70 | printf("%d\n", stack[i]); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /STACK/infixtopostfix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | char stack[100]; 4 | int top = -1; 5 | 6 | void push(char x) 7 | { 8 | stack[++top] = x; // Pushes a character onto the stack 9 | } 10 | 11 | char pop() 12 | { 13 | if (top == -1) 14 | return -1; // Checks if the stack is empty and returns -1 15 | else 16 | return stack[top--]; // Pops the topmost character from the stack and returns it 17 | } 18 | 19 | int priority(char x) 20 | { 21 | if (x == '(' || x == ')') 22 | return 0; // Returns priority 0 for parentheses 23 | if (x == '+' || x == '-') 24 | return 1; // Returns priority 1 for addition and subtraction 25 | if (x == '*' || x == '/') 26 | return 2; // Returns priority 2 for multiplication and division 27 | if (x == '^') 28 | return 3; // Returns priority 3 for exponentiation 29 | else 30 | return 0; // Returns priority 0 for other characters 31 | } 32 | 33 | int main() 34 | { 35 | char exp[100]; 36 | char *e, x; 37 | printf("Enter The Expression:"); 38 | scanf("%s", &exp); // Reads the infix expression from the user 39 | printf("\n"); 40 | e = exp; 41 | 42 | while (*e != '\0') 43 | { 44 | if (isalnum(*e)) 45 | printf("%c", *e); // Prints alphanumeric characters directly 46 | else if (*e == '(') 47 | push(*e); // Pushes an opening parenthesis onto the stack 48 | else if (*e == ')') 49 | { 50 | while ((x = pop()) != '(') 51 | printf("%c", x); // Pops and prints characters until matching opening parenthesis is found 52 | } 53 | else 54 | { 55 | while (priority(stack[top]) >= priority(*e)) 56 | printf("%c", pop()); // Pops and prints characters with higher or equal priority 57 | push(*e); // Pushes the current character onto the stack 58 | } 59 | e++; 60 | } 61 | 62 | while (top != -1) 63 | { 64 | printf("%c", pop()); // Pops and prints remaining characters from the stack 65 | } 66 | 67 | return 0; 68 | } 69 | 70 | -------------------------------------------------------------------------------- /STACK/infixtoprefix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define MAX 100 6 | 7 | int top = -1; 8 | char stack[MAX]; 9 | // checking if stack is full 10 | int isFull () 11 | { 12 | return top == MAX - 1; 13 | } 14 | // checking is stack is empty 15 | int isEmpty () 16 | { 17 | return top == -1; 18 | } 19 | void push (char item) 20 | { 21 | if (isFull ()) 22 | return; 23 | top++; 24 | stack[top] = item; 25 | } 26 | // Function to remove an item from stack. It decreases top by 1 27 | int pop () 28 | { 29 | if (isEmpty ()) 30 | return INT_MIN; 31 | // decrements top and returns what has been popped 32 | return stack[top--]; 33 | } 34 | // Function to return the top from stack without removing it 35 | int peek () 36 | { 37 | if (isEmpty ()) 38 | return INT_MIN; 39 | return stack[top]; 40 | } 41 | // A utility function to check if the given character is operand 42 | int checkIfOperand (char ch) 43 | { 44 | return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); 45 | } 46 | // Fucntion to compare precedence 47 | // If we return larger value means higher precedence 48 | int precedence (char ch) 49 | { 50 | switch (ch) 51 | { 52 | case '+': 53 | case '-': 54 | return 1; 55 | case '*': 56 | case '/': 57 | return 2; 58 | case '^': 59 | return 3; 60 | } 61 | return -1; 62 | } 63 | // The driver function for infix to postfix conversion 64 | int getPostfix (char *expression) 65 | { 66 | int i, j; 67 | for (i = 0, j = -1; expression[i]; ++i) 68 | { 69 | // If the character is an operand, add it to the expression 70 | if (checkIfOperand (expression[i])) 71 | expression[++j] = expression[i]; 72 | // If the character is an opening parenthesis, push it to the stack 73 | else if (expression[i] == '(') 74 | push (expression[i]); 75 | // If the character is a closing parenthesis, pop elements from the stack and add them to the expression until an opening parenthesis is encountered 76 | else if (expression[i] == ')') 77 | { 78 | while (!isEmpty (stack) && peek (stack) != '(') 79 | expression[++j] = pop (stack); 80 | if (!isEmpty (stack) && peek (stack) != '(') 81 | return -1; // invalid expression 82 | else 83 | pop (stack); 84 | } 85 | else // if an operator 86 | { 87 | // Pop operators from the stack and add them to the expression until an operator with lower precedence is encountered 88 | while (!isEmpty (stack) 89 | && precedence (expression[i]) <= precedence (peek (stack))) 90 | expression[++j] = pop (stack); 91 | push (expression[i]); 92 | } 93 | } 94 | 95 | // Once all initial expression characters are traversed, 96 | // add all remaining elements from stack to expression 97 | while (!isEmpty (stack)) 98 | expression[++j] = pop (stack); 99 | expression[++j] = '\0'; 100 | } 101 | // Function to reverse a string 102 | void reverse (char *exp) 103 | { 104 | int size = strlen (exp); 105 | int j = size, i = 0; 106 | char temp[size]; 107 | temp[j--] = '\0'; 108 | while (exp[i] != '\0') 109 | { 110 | temp[j] = exp[i]; 111 | j--; 112 | i++; 113 | } 114 | strcpy (exp, temp); 115 | } 116 | // Function to change opening and closing brackets 117 | void brackets (char *exp) 118 | { 119 | int i = 0; 120 | while (exp[i] != '\0') 121 | { 122 | if (exp[i] == '(') 123 | exp[i] = ')'; 124 | else if (exp[i] == ')') 125 | exp[i] = '('; 126 | i++; 127 | } 128 | } 129 | // Function to convert infix expression to prefix 130 | void InfixtoPrefix (char *exp) 131 | { 132 | int size = strlen (exp); 133 | // Reverse the string 134 | reverse (exp); 135 | // Change the brackets 136 | brackets (exp); 137 | // Get the postfix expression 138 | getPostfix (exp); 139 | // Reverse the string again to get the prefix expression 140 | reverse (exp); 141 | } 142 | 143 | int main () 144 | { 145 | char expression[100]; 146 | printf ("Enter The Expression: "); 147 | scanf("%s",&expression); 148 | printf ("Your Expression Is: %s\n", expression); 149 | InfixtoPrefix (expression); 150 | printf ("The prefix Expression is: "); 151 | printf ("%s\n", expression); 152 | 153 | return 0; 154 | } 155 | 156 | -------------------------------------------------------------------------------- /TREE/AVL-Deletion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | // Create Node 4 | struct Node { 5 | int key; 6 | struct Node *left; 7 | struct Node *right; 8 | int height; 9 | }; 10 | int max(int a, int b); 11 | // Calculate height 12 | int height(struct Node *N) { 13 | if (N == NULL) 14 | return 0; 15 | return N->height; 16 | } 17 | int max(int a, int b) { 18 | return (a > b) ? a : b; 19 | } 20 | // Create a node 21 | struct Node *newNode(int key) { 22 | struct Node *node = (struct Node *) 23 | malloc(sizeof(struct Node)); 24 | node->key = key; 25 | node->left = NULL; 26 | node->right = NULL; 27 | node->height = 1; 28 | return (node); 29 | } 30 | // Right rotate 31 | struct Node *rightRotate(struct Node *y) { 32 | struct Node *x = y->left; 33 | struct Node *T2 = x->right; 34 | x->right = y; 35 | y->left = T2; 36 | y->height = max(height(y->left), height(y->right)) + 1; 37 | x->height = max(height(x->left), height(x->right)) + 1; 38 | return x; 39 | } 40 | // Left rotate 41 | struct Node *leftRotate(struct Node *x) { 42 | struct Node *y = x->right; 43 | struct Node *T2 = y->left; 44 | y->left = x; 45 | x->right = T2; 46 | x->height = max(height(x->left), height(x->right)) + 1; 47 | y->height = max(height(y->left), height(y->right)) + 1; 48 | return y; 49 | } 50 | // Get the balance factor 51 | int getBalance(struct Node *N) { 52 | if (N == NULL) 53 | return 0; 54 | return height(N->left) - height(N->right); 55 | } 56 | // Insert node 57 | struct Node *insertNode(struct Node *node, int key) { 58 | // Find the correct position to insertNode the node and insertNode it 59 | if (node == NULL) 60 | return (newNode(key)); 61 | if (key < node->key) 62 | node->left = insertNode(node->left, key); 63 | else if (key > node->key) 64 | node->right = insertNode(node->right, key); 65 | else 66 | return node; 67 | // Update the balance factor of each node and 68 | // Balance the tree 69 | node->height = 1 + max(height(node->left), 70 | height(node->right)); 71 | int balance = getBalance(node); 72 | if (balance > 1 && key < node->left->key) 73 | return rightRotate(node); 74 | if (balance < -1 && key > node->right->key) 75 | return leftRotate(node); 76 | if (balance > 1 && key > node->left->key) { 77 | node->left = leftRotate(node->left); 78 | return rightRotate(node); 79 | } 80 | if (balance < -1 && key < node->right->key) { 81 | node->right = rightRotate(node->right); 82 | return leftRotate(node); 83 | } 84 | return node; 85 | } 86 | struct Node *minValueNode(struct Node *node) { 87 | struct Node *current = node; 88 | while (current->left != NULL) 89 | current = current->left; 90 | return current; 91 | } 92 | // Delete a nodes 93 | struct Node *deleteNode(struct Node *root, int key) { 94 | // Find the node and delete it 95 | if (root == NULL) 96 | return root; 97 | if (key < root->key) 98 | root->left = deleteNode(root->left, key); 99 | else if (key > root->key) 100 | root->right = deleteNode(root->right, key); 101 | else { 102 | if ((root->left == NULL) || (root->right == NULL)) { 103 | struct Node *temp = root->left ? root->left : root->right; 104 | if (temp == NULL) { 105 | temp = root; 106 | root = NULL; 107 | } else 108 | *root = *temp; 109 | free(temp); 110 | } else { 111 | struct Node *temp = minValueNode(root->right); 112 | root->key = temp->key; 113 | root->right = deleteNode(root->right, temp->key); 114 | } 115 | } 116 | 117 | if (root == NULL) 118 | return root; 119 | // Update the balance factor of each node and 120 | // balance the tree 121 | root->height = 1 + max(height(root->left), 122 | height(root->right)); 123 | int balance = getBalance(root); 124 | if (balance > 1 && getBalance(root->left) >= 0) 125 | return rightRotate(root); 126 | if (balance > 1 && getBalance(root->left) < 0) { 127 | root->left = leftRotate(root->left); 128 | return rightRotate(root); 129 | } 130 | if (balance < -1 && getBalance(root->right) <= 0) 131 | return leftRotate(root); 132 | if (balance < -1 && getBalance(root->right) > 0) { 133 | root->right = rightRotate(root->right); 134 | return leftRotate(root); 135 | } 136 | return root; 137 | } 138 | // Print the tree 139 | void printPreOrder(struct Node *root) { 140 | if (root != NULL) { 141 | printf("%d ", root->key); 142 | printPreOrder(root->left); 143 | printPreOrder(root->right); 144 | } 145 | } 146 | 147 | int main() { 148 | struct Node *root = NULL; 149 | root = insertNode(root, 2); 150 | root = insertNode(root, 1); 151 | root = insertNode(root, 7); 152 | root = insertNode(root, 4); 153 | root = insertNode(root, 5); 154 | root = insertNode(root, 3); 155 | root = insertNode(root, 8); 156 | printPreOrder(root); 157 | root = deleteNode(root, 3); 158 | printf("\nAfter deletion: "); 159 | printPreOrder(root); 160 | return 0; 161 | } 162 | -------------------------------------------------------------------------------- /TREE/AVL-Insertion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | // Create Node 4 | struct Node { 5 | int key; 6 | struct Node *left; 7 | struct Node *right; 8 | int height; 9 | }; 10 | int max(int a, int b); 11 | // Calculate height 12 | int height(struct Node *N) { 13 | if (N == NULL) 14 | return 0; 15 | return N->height; 16 | } 17 | int max(int a, int b) { 18 | return (a > b) ? a : b; 19 | } 20 | // Create a node 21 | struct Node *newNode(int key) { 22 | struct Node *node = (struct Node *) 23 | malloc(sizeof(struct Node)); 24 | node->key = key; 25 | node->left = NULL; 26 | node->right = NULL; 27 | node->height = 1; 28 | return (node); 29 | } 30 | // Right rotate 31 | struct Node *rightRotate(struct Node *y) { 32 | struct Node *x = y->left; 33 | struct Node *T2 = x->right; 34 | x->right = y; 35 | y->left = T2; 36 | y->height = max(height(y->left), height(y->right)) + 1; 37 | x->height = max(height(x->left), height(x->right)) + 1; 38 | return x; 39 | } 40 | // Left rotate 41 | struct Node *leftRotate(struct Node *x) { 42 | struct Node *y = x->right; 43 | struct Node *T2 = y->left; 44 | y->left = x; 45 | x->right = T2; 46 | x->height = max(height(x->left), height(x->right)) + 1; 47 | y->height = max(height(y->left), height(y->right)) + 1; 48 | return y; 49 | } 50 | // Get the balance factor 51 | int getBalance(struct Node *N) { 52 | if (N == NULL) 53 | return 0; 54 | return height(N->left) - height(N->right); 55 | } 56 | // Insert node 57 | struct Node *insertNode(struct Node *node, int key) { 58 | // Find the correct position to insertNode the node and insertNode it 59 | if (node == NULL) 60 | return (newNode(key)); 61 | if (key < node->key) 62 | node->left = insertNode(node->left, key); 63 | else if (key > node->key) 64 | node->right = insertNode(node->right, key); 65 | else 66 | return node; 67 | // Update the balance factor of each node and 68 | // Balance the tree 69 | node->height = 1 + max(height(node->left), 70 | height(node->right)); 71 | int balance = getBalance(node); 72 | if (balance > 1 && key < node->left->key) 73 | return rightRotate(node); 74 | if (balance < -1 && key > node->right->key) 75 | return leftRotate(node); 76 | if (balance > 1 && key > node->left->key) { 77 | node->left = leftRotate(node->left); 78 | return rightRotate(node); 79 | } 80 | if (balance < -1 && key < node->right->key) { 81 | node->right = rightRotate(node->right); 82 | return leftRotate(node); 83 | } return node; 84 | } 85 | struct Node *minValueNode(struct Node *node) { 86 | struct Node *current = node; 87 | 88 | while (current->left != NULL) 89 | current = current->left; 90 | return current; 91 | } 92 | // Print the tree 93 | void printPreOrder(struct Node *root) { 94 | if (root != NULL) { 95 | printf("%d ", root->key); 96 | printPreOrder(root->left); 97 | printPreOrder(root->right); 98 | } 99 | } 100 | int main() { 101 | struct Node *root = NULL; 102 | root = insertNode(root, 2); 103 | root = insertNode(root, 1); 104 | root = insertNode(root, 7); 105 | root = insertNode(root, 4); 106 | root = insertNode(root, 5); 107 | root = insertNode(root, 3); 108 | root = insertNode(root, 8); 109 | printPreOrder(root); 110 | return 0; 111 | } 112 | -------------------------------------------------------------------------------- /TREE/B-Tree-Implementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 3 4 | #define MIN 2 5 | struct BTreeNode { 6 | int val[MAX + 1], count; 7 | struct BTreeNode *link[MAX + 1]; 8 | }; struct BTreeNode *root; 9 | // Create a node 10 | struct BTreeNode *createNode(int val, struct BTreeNode *child) { 11 | struct BTreeNode *newNode; 12 | newNode = (struct BTreeNode *)malloc(sizeof(struct BTreeNode)); 13 | newNode->val[1] = val; 14 | newNode->count = 1; 15 | newNode->link[0] = root; 16 | newNode->link[1] = child; 17 | return newNode; } 18 | // Insert node 19 | void insertNode(int val, int pos, struct BTreeNode *node, 20 | struct BTreeNode *child) { 21 | int j = node->count; 22 | while (j > pos) { 23 | node->val[j + 1] = node->val[j]; 24 | node->link[j + 1] = node->link[j]; 25 | j--; 26 | } 27 | node->val[j + 1] = val; 28 | node->link[j + 1] = child; 29 | node->count++; 30 | } 31 | // Split node 32 | void splitNode(int val, int *pval, int pos, struct BTreeNode *node, 33 | struct BTreeNode *child, struct BTreeNode **newNode) { 34 | int median, j; 35 | if (pos > MIN) 36 | median = MIN + 1; 37 | else 38 | median = MIN; 39 | *newNode = (struct BTreeNode *)malloc(sizeof(struct BTreeNode)); 40 | j = median + 1; 41 | while (j <= MAX) { 42 | (*newNode)->val[j - median] = node->val[j]; 43 | (*newNode)->link[j - median] = node->link[j]; 44 | j++; 45 | } 46 | node->count = median; 47 | (*newNode)->count = MAX - median; 48 | if (pos <= MIN) { 49 | insertNode(val, pos, node, child); 50 | } else { 51 | insertNode(val, pos - median, *newNode, child); 52 | } 53 | *pval = node->val[node->count]; 54 | (*newNode)->link[0] = node->link[node->count]; 55 | node->count--; 56 | } 57 | 58 | // Set the value 59 | int setValue(int val, int *pval, 60 | struct BTreeNode *node, struct BTreeNode **child) { 61 | int pos; 62 | if (!node) { 63 | *pval = val; 64 | *child = NULL; 65 | return 1; 66 | } 67 | if (val < node->val[1]) { 68 | pos = 0; 69 | } else { 70 | for (pos = node->count; 71 | (val < node->val[pos] && pos > 1); pos--) ; 72 | if (val == node->val[pos]) { 73 | printf("Duplicates are not permitted\n"); 74 | return 0; 75 | } 76 | } 77 | if (setValue(val, pval, node->link[pos], child)) { 78 | if (node->count < MAX) { 79 | insertNode(*pval, pos, node, *child); 80 | } else { 81 | splitNode(*pval, pval, pos, node, *child, child); 82 | return 1; 83 | } 84 | } 85 | return 0; 86 | } 87 | // Insert the value 88 | void insert(int val) { 89 | int flag, i; 90 | struct BTreeNode *child; 91 | flag = setValue(val, &i, root, &child); 92 | if (flag) 93 | root = createNode(i, child); 94 | } 95 | // Search node 96 | void search(int val, int *pos, struct BTreeNode *myNode) { 97 | if (!myNode) { 98 | return; } 99 | if (val < myNode->val[1]) { 100 | *pos = 0; 101 | } else { 102 | for (*pos = myNode->count; 103 | (val < myNode->val[*pos] && *pos > 1); (*pos)--) 104 | ; 105 | if (val == myNode->val[*pos]) { 106 | printf("%d is found", val); 107 | return; 108 | } 109 | } 110 | search(val, pos, myNode->link[*pos]); 111 | return; } 112 | // Traverse then nodes 113 | void traversal(struct BTreeNode *myNode) { 114 | int i; 115 | if (myNode) { 116 | for (i = 0; i < myNode->count; i++) { 117 | traversal(myNode->link[i]); 118 | printf("%d ", myNode->val[i + 1]); 119 | } 120 | traversal(myNode->link[i]); 121 | } 122 | } 123 | int main() { 124 | int val, ch; 125 | insert(8); 126 | insert(9); 127 | insert(10); 128 | insert(11); 129 | insert(15); 130 | insert(16); 131 | insert(17); 132 | insert(18); 133 | insert(20); 134 | insert(23); 135 | traversal(root); 136 | printf("\n"); 137 | search(11, &ch, root); 138 | } 139 | 140 | -------------------------------------------------------------------------------- /TREE/BST-All-Traversal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Node { 4 | int data; 5 | struct Node *left, *right; 6 | }; 7 | struct Node *newNode(int data) { 8 | struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); 9 | temp->data = data; 10 | temp->left = temp->right = NULL; 11 | return temp; 12 | } 13 | void inorder(struct Node *root) { 14 | if (root != NULL) { 15 | inorder(root->left); 16 | printf("%d ", root->data); 17 | inorder(root->right); 18 | } 19 | } 20 | void preorder(struct Node *root) { 21 | if (root != NULL) { 22 | printf("%d ", root->data); 23 | preorder(root->left); 24 | preorder(root->right); 25 | } 26 | } 27 | // Postorder traversal of the binary tree 28 | void postorder(struct Node *root) { 29 | if (root != NULL) { 30 | postorder(root->left); 31 | postorder(root->right); 32 | printf("%d ", root->data); 33 | } 34 | } 35 | int main() { 36 | // Constructing a binary tree 37 | struct Node *root = newNode(1); 38 | root->left = newNode(2); 39 | root->right = newNode(3); 40 | root->left->left = newNode(4); 41 | root->left->right = newNode(5); 42 | // Print the traversals 43 | printf("Inorder traversal: "); 44 | inorder(root); 45 | printf("\nPreorder traversal: "); 46 | preorder(root); 47 | printf("\nPostorder traversal: "); 48 | postorder(root); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /TREE/BST-Inorder-Traversal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int key; 6 | struct node *left, *right; 7 | }; 8 | // Create a node 9 | struct node *newNode(int item) 10 | { 11 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 12 | temp->key = item; 13 | temp->left = temp->right = NULL; 14 | return temp; 15 | } 16 | // Inorder Traversal 17 | void inorder(struct node *root) { 18 | if (root != NULL) { 19 | // Traverse left 20 | inorder(root->left); 21 | // Traverse root 22 | printf("%d -> ", root->key); 23 | // Traverse right 24 | inorder(root->right); 25 | } 26 | } 27 | 28 | // Insert a node 29 | struct node *insert(struct node *node, int key) { 30 | // Return a new node if the tree is empty 31 | if (node == NULL) return newNode(key); 32 | 33 | // Traverse to the right place and insert the node 34 | if (key < node->key) 35 | node->left = insert(node->left, key); 36 | else 37 | node->right = insert(node->right, key); 38 | 39 | return node; 40 | } 41 | 42 | // Find the inorder successor 43 | struct node *minValueNode(struct node *node) { 44 | struct node *current = node; 45 | 46 | // Find the leftmost leaf 47 | while (current && current->left != NULL) 48 | current = current->left; 49 | 50 | return current; 51 | } 52 | 53 | // Driver code 54 | int main() 55 | { 56 | struct node *root = NULL; 57 | root = insert(root, 8); 58 | root = insert(root, 3); 59 | root = insert(root, 1); 60 | root = insert(root, 6); 61 | root = insert(root, 7); 62 | root = insert(root, 10); 63 | root = insert(root, 14); 64 | root = insert(root, 4); 65 | 66 | printf("Inorder traversal: "); 67 | inorder(root); 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /TREE/BST-Node-Deletion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node { 5 | int key; 6 | struct node *left, *right; 7 | }; 8 | 9 | // Create a node 10 | struct node *newNode(int item) { 11 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 12 | temp->key = item; 13 | temp->left = temp->right = NULL; 14 | return temp; 15 | } 16 | 17 | // Inorder Traversal 18 | void inorder(struct node *root) { 19 | if (root != NULL) { 20 | // Traverse left 21 | inorder(root->left); 22 | 23 | // Traverse root 24 | printf("%d -> ", root->key); 25 | 26 | // Traverse right 27 | inorder(root->right); 28 | } 29 | } 30 | // Insert a node 31 | struct node *insert(struct node *node, int key) { 32 | // Return a new node if the tree is empty 33 | if (node == NULL) return newNode(key); 34 | 35 | // Traverse to the right place and insert the node 36 | if (key < node->key) 37 | node->left = insert(node->left, key); 38 | else 39 | node->right = insert(node->right, key); 40 | 41 | return node; 42 | } 43 | 44 | // Find the inorder successor 45 | struct node *minValueNode(struct node *node) { 46 | struct node *current = node; 47 | 48 | // Find the leftmost leaf 49 | while (current && current->left != NULL) 50 | current = current->left; 51 | 52 | return current; 53 | } 54 | 55 | // Deleting a node 56 | struct node *deleteNode(struct node *root, int key) { 57 | // Return if the tree is empty 58 | if (root == NULL) return root; 59 | 60 | // Find the node to be deleted 61 | if (key < root->key) 62 | root->left = deleteNode(root->left, key); 63 | else if (key > root->key) 64 | root->right = deleteNode(root->right, key); 65 | 66 | else { 67 | // If the node is with only one child or no child 68 | if (root->left == NULL) { 69 | struct node *temp = root->right; 70 | free(root); 71 | return temp; 72 | } else if (root->right == NULL) { 73 | struct node *temp = root->left; 74 | free(root); 75 | return temp; 76 | } 77 | 78 | // If the node has two children 79 | struct node *temp = minValueNode(root->right); 80 | 81 | // Place the inorder successor in position of the node to be deleted 82 | root->key = temp->key; 83 | 84 | // Delete the inorder successor 85 | root->right = deleteNode(root->right, temp->key); 86 | } 87 | return root; 88 | } 89 | // Driver code 90 | int main() { 91 | struct node *root = NULL; 92 | root = insert(root, 8); 93 | root = insert(root, 3); 94 | root = insert(root, 1); 95 | root = insert(root, 6); 96 | root = insert(root, 7); 97 | root = insert(root, 10); 98 | root = insert(root, 14); 99 | root = insert(root, 4); 100 | 101 | printf("Inorder traversal: "); 102 | inorder(root); 103 | 104 | printf("\nAfter deleting 10\n"); 105 | root = deleteNode(root, 10); 106 | printf("Inorder traversal: "); 107 | inorder(root); 108 | } 109 | -------------------------------------------------------------------------------- /TREE/BST-Search-Node.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Structure for a binary search tree node 5 | struct Node { 6 | int key; 7 | struct Node *left, *right; 8 | }; 9 | 10 | // Function to create a new node with the given key 11 | struct Node *newNode(int key) { 12 | struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); 13 | temp->key = key; 14 | temp->left = temp->right = NULL; 15 | return temp; 16 | } 17 | 18 | // Function to search for a key in the BST 19 | struct Node *search(struct Node *root, int key) { 20 | // Base cases: root is null or key is present at the root 21 | if (root == NULL || root->key == key) 22 | return root; 23 | 24 | // Key is greater than root's key, search in the right subtree 25 | if (key > root->key) 26 | return search(root->right, key); 27 | 28 | // Key is smaller than root's key, search in the left subtree 29 | return search(root->left, key); 30 | } 31 | 32 | struct Node *insert(struct Node *root, int key) { 33 | // If the tree is empty, return a new node 34 | if (root == NULL) 35 | return newNode(key); 36 | 37 | // Otherwise, recur down the tree 38 | if (key < root->key) 39 | root->left = insert(root->left, key); 40 | else if (key > root->key) 41 | root->right = insert(root->right, key); 42 | 43 | // Return the (unchanged) node pointer 44 | return root; 45 | } 46 | 47 | // Driver code to test the search 48 | int main() { 49 | struct Node *root = NULL; 50 | 51 | // Insert nodes into the binary search tree 52 | root = insert(root, 50); 53 | insert(root, 30); 54 | insert(root, 20); 55 | insert(root, 40); 56 | insert(root, 70); 57 | insert(root, 60); 58 | insert(root, 80); 59 | 60 | // Search for a key in the binary search tree 61 | int keyToSearch = 40; 62 | struct Node *result = search(root, keyToSearch); 63 | 64 | // Display the result 65 | if (result != NULL) 66 | printf("Key %d found in the BST.\n", keyToSearch); 67 | else 68 | printf("Key %d not found in the BST.\n", keyToSearch); 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /TREE/Binary-Expression-Tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /* The below structure node is defined as a node of a binary tree consists 4 | of left child and the right child, along with the pointer next which points to the next node */ 5 | struct node 6 | { 7 | char info ; 8 | struct node* l ; 9 | struct node* r ; 10 | struct node* nxt ; 11 | }; 12 | struct node *head=NULL; 13 | /* Helper function that allocates a new node with the 14 | given data and NULL left and right pointers. */ 15 | struct node* newnode(char data) 16 | { 17 | struct node* node = (struct node*) malloc ( sizeof ( struct node ) ) ; 18 | node->info = data ; 19 | node->l = NULL ; 20 | node->r = NULL ; 21 | node->nxt = NULL ; 22 | return ( node ) ; 23 | } 24 | void Inorder(struct node* node) { 25 | if ( node == NULL) 26 | return ; 27 | else 28 | { 29 | /* first recur on left child */ 30 | Inorder ( node->l ) ; 31 | /* then print the data of node */ 32 | printf ( "%c " , node->info ) ; 33 | /* now recur on right child */ 34 | Inorder ( node->r ) ; 35 | } 36 | } 37 | void push ( struct node* x ) 38 | { 39 | if ( head == NULL ) 40 | head = x ; 41 | else 42 | { 43 | ( x )->nxt = head ; 44 | head = x ; 45 | } 46 | } 47 | struct node* pop() 48 | { 49 | // Poping out the top most [pointed with head] element 50 | struct node* n = head ; 51 | head = head->nxt ; 52 | return n ; 53 | } 54 | int main() 55 | { 56 | char t[] = { 'X' , 'Y' , 'Z' , '*' , '+' , 'W' , '/' } ; 57 | int n = sizeof(t) / sizeof(t[0]) ; 58 | int i ; 59 | struct node *p , *q , *s ; 60 | for ( i = 0 ; i < n ; i++ ) { 61 | // if read character is operator then popping two 62 | // other elements from stack and making a binary 63 | // tree 64 | if ( t[i] == '+' || t[i] == '-' || t[i] == '*' || t[i] == '/' || t[i] == '^' ) { 65 | s = newnode ( t [ i ] ) ; 66 | p = pop() ; 67 | q = pop() ; 68 | s->l = q ; 69 | s->r = p; 70 | push(s); 71 | } 72 | else { 73 | s = newnode ( t [ i ] ) ; 74 | push ( s ) ; 75 | } 76 | } 77 | // printf("\nBinary Expression Tree of: %s", t); 78 | printf ( "\n The Inorder Traversal of Expression Tree: " ) ; 79 | Inorder ( s ) ; 80 | return 0 ; 81 | } 82 | -------------------------------------------------------------------------------- /TREE/Binary-Search-Tree-Inseartion-Deletion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | // Structure for a binary tree node 4 | struct Node 5 | { 6 | int data; 7 | struct Node *left, *right; 8 | }; 9 | // Function to create a new node with the given data 10 | struct Node *newNode(int data) 11 | { 12 | struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); 13 | temp->data = data; 14 | temp->left = temp->right = NULL; 15 | return temp; 16 | } 17 | // Function to insert a new node with the given data into the binary tree 18 | struct Node *insert(struct Node *root, int data) 19 | { 20 | // If the tree is empty, return a new node 21 | if (root == NULL) 22 | return newNode(data); 23 | // Otherwise, move down the tree recursively 24 | if (data < root->data) 25 | root->left = insert(root->left, data); 26 | else if (data > root->data) 27 | root->right = insert(root->right, data); 28 | // Return the (unchanged) node pointer 29 | return root; 30 | } 31 | // Function to delete a node with the given key from the binary tree 32 | struct Node *deleteNode(struct Node *root, int key) { 33 | if (root == NULL) 34 | return root; 35 | // Recur down the tree 36 | if (key < root->data) 37 | root->left = deleteNode(root->left, key); 38 | else if (key > root->data) 39 | root->right = deleteNode(root->right, key); 40 | else { 41 | // Node with only one child or no child 42 | if (root->left == NULL) { 43 | struct Node *temp = root->right; 44 | free(root); 45 | return temp; 46 | } else if (root->right == NULL) { 47 | struct Node *temp = root->left; 48 | free(root); 49 | return temp; 50 | } 51 | // Node with two children: Get the inorder successor (smallest 52 | // in the right subtree) 53 | struct Node *temp = root->right; 54 | while (temp->left != NULL) 55 | temp = temp->left; 56 | // Copy the inorder successor's data to this node 57 | root->data = temp->data; 58 | // Delete the inorder successor 59 | root->right = deleteNode(root->right, temp->data); 60 | } 61 | return root; 62 | } 63 | // Inorder traversal of the binary tree 64 | void inorder(struct Node *root) 65 | { 66 | if (root != NULL) { 67 | inorder(root->left); 68 | printf("%d ", root->data); 69 | inorder(root->right); 70 | } 71 | } 72 | // Driver code to test the deletion 73 | int main() 74 | { 75 | struct Node *root = NULL; 76 | // Insert nodes into the binary tree 77 | root = insert(root, 50); 78 | insert(root, 30); 79 | insert(root, 20); 80 | insert(root, 40); 81 | insert(root, 70); 82 | insert(root, 60); 83 | insert(root, 80); 84 | // Print the inorder traversal of the binary tree before deletion 85 | printf("Inorder traversal before deletion: "); 86 | inorder(root); 87 | // Delete a node (e.g., node with key 20) 88 | int keyToDelete = 20; 89 | root = deleteNode(root, keyToDelete); 90 | // Print the inorder traversal of the binary tree after deletion 91 | printf("\nInorder traversal after deletion of %d: ", keyToDelete); 92 | inorder(root); 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /TREE/Binary-Search-Tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Node { 4 | int data; 5 | struct Node *left, *right; 6 | }; 7 | // Function to create a new node with the given data 8 | struct Node *newNode(int data) { 9 | struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); 10 | temp->data = data; 11 | temp->left = temp->right = NULL; 12 | return temp; 13 | } 14 | // Function to insert a new node with the given data into the binary tree 15 | struct Node *insert(struct Node *root, int data) { 16 | // If the tree is empty, return a new node 17 | if (root == NULL) 18 | return newNode(data); 19 | // Otherwise, move down the tree recursively 20 | if (data < root->data) 21 | root->left = insert(root->left, data); 22 | else if (data > root->data) 23 | root->right = insert(root->right, data); 24 | // Return the (unchanged) node pointer 25 | return root; 26 | } 27 | // Inorder traversal of the binary tree 28 | void inorder(struct Node *root) { 29 | if (root != NULL) { 30 | inorder(root->left); 31 | printf("%d ", root->data); 32 | inorder(root->right); 33 | } 34 | } 35 | // Driver code to test the insertion 36 | int main() { 37 | struct Node *root = NULL; 38 | // Insert nodes into the binary tree 39 | root = insert(root, 50); 40 | insert(root, 30); 41 | insert(root, 20); 42 | insert(root, 40); 43 | insert(root, 70); 44 | insert(root, 60); 45 | insert(root, 80); 46 | // Print the inorder traversal of the binary tree 47 | printf("Inorder traversal: "); 48 | inorder(root); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /TREE/Huffman-Coding-Implementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX_TREE_HT 50 4 | struct MinHNode { 5 | char item; 6 | unsigned freq; 7 | struct MinHNode *left, *right; 8 | }; 9 | struct MinHeap { 10 | unsigned size; 11 | unsigned capacity; 12 | struct MinHNode **array; 13 | }; 14 | // Create nodes 15 | struct MinHNode *newNode(char item, unsigned freq) { 16 | struct MinHNode *temp = (struct MinHNode *)malloc(sizeof(struct MinHNode)); 17 | temp->left = temp->right = NULL; 18 | temp->item = item; 19 | temp->freq = freq; 20 | return temp; 21 | } 22 | // Create min heap 23 | struct MinHeap *createMinH(unsigned capacity) { 24 | struct MinHeap *minHeap = (struct MinHeap *)malloc(sizeof(struct MinHeap)); 25 | minHeap->size = 0; 26 | minHeap->capacity = capacity; 27 | minHeap->array = (struct MinHNode **)malloc(minHeap->capacity * sizeof(struct MinHNode *)); 28 | return minHeap; 29 | } 30 | 31 | // Function to swap 32 | void swapMinHNode(struct MinHNode **a, struct MinHNode **b) { 33 | struct MinHNode *t = *a; 34 | *a = *b; 35 | *b = t; 36 | } 37 | // Heapify 38 | void minHeapify(struct MinHeap *minHeap, int idx) { 39 | int smallest = idx; 40 | int left = 2 * idx + 1; 41 | int right = 2 * idx + 2; 42 | if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq) 43 | smallest = left; 44 | if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq) 45 | smallest = right; 46 | if (smallest != idx) { 47 | swapMinHNode(&minHeap->array[smallest], &minHeap->array[idx]); 48 | minHeapify(minHeap, smallest); 49 | } 50 | } 51 | // Check if size if 1 52 | int checkSizeOne(struct MinHeap *minHeap) { 53 | return (minHeap->size == 1); 54 | } 55 | // Extract min 56 | struct MinHNode *extractMin(struct MinHeap *minHeap) { 57 | struct MinHNode *temp = minHeap->array[0]; 58 | minHeap->array[0] = minHeap->array[minHeap->size - 1]; 59 | --minHeap->size; 60 | minHeapify(minHeap, 0); 61 | return temp; 62 | } 63 | // Insertion function 64 | void insertMinHeap(struct MinHeap *minHeap, struct MinHNode *minHeapNode) { 65 | ++minHeap->size; 66 | int i = minHeap->size - 1; 67 | while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) { 68 | minHeap->array[i] = minHeap->array[(i - 1) / 2]; 69 | i = (i - 1) / 2; 70 | } 71 | minHeap->array[i] = minHeapNode; 72 | } 73 | void buildMinHeap(struct MinHeap *minHeap) { 74 | int n = minHeap->size - 1; 75 | int i; 76 | for (i = (n - 1) / 2; i >= 0; --i) 77 | minHeapify(minHeap, i); 78 | } 79 | int isLeaf(struct MinHNode *root) { 80 | return !(root->left) && !(root->right); 81 | } 82 | struct MinHeap *createAndBuildMinHeap(char item[], int freq[], int size) { 83 | struct MinHeap *minHeap = createMinH(size); 84 | for (int i = 0; i < size; ++i) 85 | minHeap->array[i] = newNode(item[i], freq[i]); 86 | minHeap->size = size; 87 | buildMinHeap(minHeap); 88 | return minHeap; 89 | } 90 | struct MinHNode *buildHuffmanTree(char item[], int freq[], int size) { 91 | struct MinHNode *left, *right, *top; 92 | struct MinHeap *minHeap = createAndBuildMinHeap(item, freq, size); 93 | while (!checkSizeOne(minHeap)) { 94 | left = extractMin(minHeap); 95 | right = extractMin(minHeap); 96 | top = newNode('$', left->freq + right->freq); 97 | top->left = left; 98 | top->right = right; 99 | insertMinHeap(minHeap, top); 100 | } 101 | return extractMin(minHeap); 102 | } 103 | // Print the array 104 | void printArray(int arr[], int n) { 105 | int i; 106 | for (i = 0; i < n; ++i) 107 | printf("%d", arr[i]); 108 | printf("\n"); 109 | } 110 | void printHCodes(struct MinHNode *root, int arr[], int top) { 111 | if (root->left) { 112 | arr[top] = 0; 113 | printHCodes(root->left, arr, top + 1); 114 | } 115 | if (root->right) { 116 | arr[top] = 1; 117 | printHCodes(root->right, arr, top + 1); 118 | } 119 | if (isLeaf(root)) { 120 | printf(" %c | ", root->item); 121 | printArray(arr, top); 122 | } 123 | } 124 | // Wrapper function 125 | void HuffmanCodes(char item[], int freq[], int size) { 126 | struct MinHNode *root = buildHuffmanTree(item, freq, size); 127 | int arr[MAX_TREE_HT], top = 0; 128 | printHCodes(root, arr, top); 129 | } 130 | int main() { 131 | char arr[] = {'A', 'B', 'C', 'D'}; 132 | int freq[] = {5, 1, 6, 3}; 133 | int size = sizeof(arr) / sizeof(arr[0]); 134 | printf(" Char | Huffman code "); 135 | printf("\n--------------------\n"); 136 | HuffmanCodes(arr, freq, size); 137 | } 138 | --------------------------------------------------------------------------------