├── 00_DSA NOTES ├── 001 time complexity.pdf ├── 01.pdf ├── 02_Asymptotic_Notations.pdf ├── 03_Best_Worst_Average.pdf ├── 04_ADTs.pdf ├── 05_Array_Operations.pdf ├── 06_LinearVsBinary.pdf ├── 07_LinkedLists.pdf ├── 08_Linked_List_Deletion.pdf ├── 09_Circular_Linked_Lists.pdf ├── 10_DoublyLinkedLists.pdf └── 11_Introduction_To_Stack.pdf ├── 01_Arrays ├── 01_arrayrotation.c ├── 01_arrayrotation.exe ├── 02_reversingarray.c ├── 02_reversingarray.exe ├── 1.c ├── 1.exe ├── ADTarrays.c ├── ADTarrays.exe ├── binarysearch.c ├── binarysearch.exe ├── deletioninarray.c ├── deletioninarray.exe ├── insert1.c ├── insert1.exe ├── insertion.c ├── insertion.exe ├── linearsearch copy.c ├── linearsearch.c ├── linearsearch.exe └── linearsearch1.c ├── 02_Linked list ├── .vscode │ └── settings.json ├── 01_creating.c ├── 01_creating.exe ├── 01_creatingcopy.c ├── case1.c ├── case1.exe ├── case2.c ├── case2.exe ├── case3.c ├── case3.exe ├── case4.c ├── case4.exe ├── circularLL.c ├── circularLL.exe ├── deleteCase1.c ├── deleteCase1.exe ├── deleteCase2.c ├── deleteCase2.exe ├── deleteCase3.c ├── deleteCase3.exe ├── deleteCase4.c ├── deleteCase4.exe ├── mylinklist.c └── mylinklist.exe ├── 03_Stack using array ├── peek_using_array.c ├── peek_using_array.exe ├── pop_using_array.c ├── pop_using_array.exe ├── push_using_array.c ├── push_using_array.exe ├── stack_bottom.c ├── stack_bottom.exe ├── stack_top.c ├── stack_top.exe ├── stack_using_array.c ├── stack_using_array.exe └── tempCodeRunnerFile.c ├── 04_Stack using Linked list ├── 00stack_using_LL.c ├── 00stack_using_LL.exe ├── 01pop_in_stack.c ├── 01pop_in_stack.exe ├── 02pop_in_stack.c ├── 02pop_in_stack.exe ├── 03stacktop.c ├── 03stacktop.exe ├── 04_stackbottom.c ├── 04_stackbottom.exe ├── peek_operation.c ├── peek_operation.exe └── stack_using_LL.exe ├── 05_parantheisi matching in stack ├── .vscode │ └── settings.json ├── Multiple_PM.c ├── Multiple_PM.exe ├── PM_using_Array.c └── PM_using_Array.exe ├── 06_infix to postfix in stack ├── infix_to_postfix.c └── infix_to_postfix.exe ├── 07_queue using array ├── .vscode │ └── settings.json ├── 01_queue_using_array.c ├── 01_queue_using_array.exe ├── QueueOperations.c └── QueueOperations.exe ├── 08_circular queue using array ├── Dynamic_allocation.c ├── Dynamic_allocation.exe ├── circular_queue.c └── circular_queue.exe ├── 09_queue using linked list ├── Queue_using_LL.c └── Queue_using_LL.exe ├── 10_doubly ended queue ├── .vscode │ └── settings.json ├── DE_queue.c ├── DE_queue.exe └── reference.c ├── 11_Sorting Algorithms ├── 01_BubbleSort.c ├── 01_BubbleSort.exe ├── 01_BubbleSort_2.c ├── 01_BubbleSort_2.exe ├── 02_InsertionSort.c ├── 02_InsertionSort.exe ├── 02_selectionSort.exe ├── 03_selectionSort.c ├── 03_selectionSort.exe ├── 04_QuickSort.c ├── 04_QuickSort.exe ├── 05_MergeSort.c ├── 05_MergeSort.exe ├── 06_CountSort.c └── 06_CountSort.exe ├── 12_Trees and binary trees ├── .vscode │ └── settings.json ├── 01_PreOrder.c ├── 01_PreOrder.exe ├── 02_PostOrder.c ├── 02_PostOrder.exe ├── 03_InOrder.c ├── 03_InOrder.exe ├── 10_INSERT.c ├── 10_INSERT.exe ├── 11_DELETE.c ├── 11_DELETE.exe ├── BST.c ├── BST.exe ├── BST_iterative_search.c ├── BST_iterative_search.exe ├── BST_search.c ├── BST_search.exe ├── binaryTree_01.c ├── binaryTree_01.exe ├── binaryTree_02.c └── binaryTree_02.exe └── README.md /00_DSA NOTES/001 time complexity.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/001 time complexity.pdf -------------------------------------------------------------------------------- /00_DSA NOTES/01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/01.pdf -------------------------------------------------------------------------------- /00_DSA NOTES/02_Asymptotic_Notations.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/02_Asymptotic_Notations.pdf -------------------------------------------------------------------------------- /00_DSA NOTES/03_Best_Worst_Average.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/03_Best_Worst_Average.pdf -------------------------------------------------------------------------------- /00_DSA NOTES/04_ADTs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/04_ADTs.pdf -------------------------------------------------------------------------------- /00_DSA NOTES/05_Array_Operations.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/05_Array_Operations.pdf -------------------------------------------------------------------------------- /00_DSA NOTES/06_LinearVsBinary.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/06_LinearVsBinary.pdf -------------------------------------------------------------------------------- /00_DSA NOTES/07_LinkedLists.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/07_LinkedLists.pdf -------------------------------------------------------------------------------- /00_DSA NOTES/08_Linked_List_Deletion.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/08_Linked_List_Deletion.pdf -------------------------------------------------------------------------------- /00_DSA NOTES/09_Circular_Linked_Lists.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/09_Circular_Linked_Lists.pdf -------------------------------------------------------------------------------- /00_DSA NOTES/10_DoublyLinkedLists.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/10_DoublyLinkedLists.pdf -------------------------------------------------------------------------------- /00_DSA NOTES/11_Introduction_To_Stack.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/00_DSA NOTES/11_Introduction_To_Stack.pdf -------------------------------------------------------------------------------- /01_Arrays/01_arrayrotation.c: -------------------------------------------------------------------------------- 1 | #include 2 | //void RotatebyOne(int arr[], int n); 3 | void DisplayArray(int arr[], int n) 4 | { 5 | for (int i = 0; i < n; i++) 6 | { 7 | printf("%d ", arr[i]); 8 | } 9 | } 10 | 11 | // Function to left Rotate arr[] of size n by 1 12 | void RotatebyOne(int arr[], int n) 13 | { 14 | int temp = arr[0], i; 15 | for (i = 0; i < n - 1; i++) 16 | { 17 | 18 | arr[i] = arr[i + 1]; 19 | } 20 | arr[n - 1] = temp; 21 | } 22 | 23 | void LeftRotateByD(int arr[], int d, int n) 24 | { 25 | for (int i = 0; i < d; i++) 26 | { 27 | RotatebyOne(arr, n); 28 | } 29 | } 30 | int main() 31 | { 32 | int arr[] = {1, 2, 3, 4, 5, 6}; 33 | DisplayArray(arr, 6); 34 | printf("\nAfer rotation\n"); 35 | LeftRotateByD(arr, 3, 6); 36 | DisplayArray(arr, 6); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /01_Arrays/01_arrayrotation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/01_Arrays/01_arrayrotation.exe -------------------------------------------------------------------------------- /01_Arrays/02_reversingarray.c: -------------------------------------------------------------------------------- 1 | #include 2 | void DisplayArray(int arr[], int n) 3 | { 4 | for (int i = 0; i < n; i++) 5 | { 6 | printf("%d ", arr[i]); 7 | } 8 | } 9 | void reverse(int arr[], int start, int end) 10 | { 11 | int temp; 12 | while (start < end) 13 | { 14 | temp = arr[start]; 15 | arr[start] = arr[end]; 16 | arr[end] = temp; 17 | start++; 18 | end--; 19 | } 20 | } 21 | void LeftRotateByD(int arr[], int d, int n) 22 | { 23 | 24 | if (d == 0) 25 | return; 26 | // in case the rotating factor is 27 | // greater than array length 28 | d = d % n; 29 | 30 | reverse(arr, 0, d - 1); 31 | reverse(arr, d, n - 1); 32 | reverse(arr, 0, n - 1); 33 | } 34 | int main() 35 | { 36 | int arr[] = {1, 2, 3, 4, 5, 6}; 37 | DisplayArray(arr, 6); 38 | LeftRotateByD(arr, 2, 6); 39 | printf("\nAfter rotation\n"); 40 | DisplayArray(arr, 6); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /01_Arrays/02_reversingarray.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/01_Arrays/02_reversingarray.exe -------------------------------------------------------------------------------- /01_Arrays/1.c: -------------------------------------------------------------------------------- 1 | // C program to rotate an array by 2 | // d elements 3 | #include 4 | 5 | /* Function to left Rotate arr[] of size n by 1*/ 6 | void leftRotatebyOne(int arr[], int n); 7 | 8 | /*Function to left rotate arr[] of size n by d*/ 9 | void leftRotate(int arr[], int d, int n) 10 | { 11 | int i; 12 | for (i = 0; i < d; i++) 13 | leftRotatebyOne(arr, n); 14 | } 15 | 16 | void leftRotatebyOne(int arr[], int n) 17 | { 18 | int temp = arr[0], i; 19 | for (i = 0; i < n - 1; i++) 20 | arr[i] = arr[i + 1]; 21 | arr[n - 1] = temp; 22 | } 23 | 24 | /* utility function to print an array */ 25 | void printArray(int arr[], int n) 26 | { 27 | int i; 28 | for (i = 0; i < n; i++) 29 | printf("%d ", arr[i]); 30 | } 31 | 32 | /* Driver program to test above functions */ 33 | int main() 34 | { 35 | int arr[] = {1, 2, 3, 4, 5, 6, 7}; 36 | printArray(arr, 7); 37 | leftRotate(arr, 2, 7); 38 | printf("\nafter rotation\n"); 39 | printArray(arr, 7); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /01_Arrays/1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/01_Arrays/1.exe -------------------------------------------------------------------------------- /01_Arrays/ADTarrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct myArray 4 | { 5 | int total_size; 6 | int used_size; 7 | int *ptr; 8 | }; 9 | 10 | void createArray(struct myArray *a, int tSize, int uSize) 11 | { 12 | // (*a).total_size = tSize ; 13 | // (*a).used_size = uSize ; 14 | // (*a).ptr = (int *)malloc(tSize * sizeof(int)); 15 | a->total_size = tSize; 16 | a->used_size = uSize; 17 | a->ptr = (int *)malloc(tSize * sizeof(int)); 18 | } 19 | 20 | void show(struct myArray *a) 21 | { 22 | for (int i = 0; i < a->used_size; i++) 23 | { 24 | printf("%d\n", (a->ptr)[i]); 25 | } 26 | } 27 | 28 | void setVal(struct myArray *a) 29 | { 30 | int n; 31 | for (int i = 0; i < a->used_size; i++) 32 | { 33 | printf("Enter elements %d : ", i); 34 | scanf("%d", &n); 35 | (a->ptr)[i] = n; 36 | } 37 | } 38 | 39 | int main() 40 | { 41 | struct myArray marks; 42 | createArray(&marks, 20, 3); 43 | printf("Running setVal\n"); 44 | setVal(&marks); 45 | printf("Running show\n"); 46 | show(&marks); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /01_Arrays/ADTarrays.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/01_Arrays/ADTarrays.exe -------------------------------------------------------------------------------- /01_Arrays/binarysearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | int BinarySearch(int arr[], int size, int element) 3 | { 4 | int low, mid, high; 5 | low = 0; 6 | high = size - 1; 7 | while (low <= high) 8 | { 9 | mid = (low + high) / 2; 10 | if (arr[mid] == element) 11 | { 12 | return mid; 13 | } 14 | if (arr[mid] < element) 15 | { 16 | low = mid + 1; 17 | } 18 | else 19 | { 20 | high = mid - 1; 21 | } 22 | } 23 | return -1; 24 | } 25 | 26 | int main() 27 | { 28 | //index of elements= 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 29 | int arr[100] = {12, 40, 58, 81, 95, 123, 145}; 30 | int element = 12; 31 | int size = sizeof(arr) / sizeof(int); 32 | 33 | int SearchIndex = BinarySearch(arr, size, element); 34 | printf("The element %d was found at index %d \n", element, SearchIndex); 35 | // printf((BinarySearch==-1)?"Element not found":"The element %d was found at index %d \n", element, SearchIndex); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /01_Arrays/binarysearch.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/01_Arrays/binarysearch.exe -------------------------------------------------------------------------------- /01_Arrays/deletioninarray.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //*****CODE FOR TRAVERSAL******** 4 | 5 | void display(int arr[], int n) 6 | { 7 | for (int i = 0; i < n; i++) 8 | { 9 | printf("%d ", arr[i]); 10 | } 11 | printf("\n"); 12 | } 13 | 14 | //*********CODE FOR DELETION OF ELEMENT IN AN ARRAY************* 15 | 16 | void IndexDelete(int arr[], int size, int index) 17 | { 18 | 19 | for (int i = index; i < size - 1; i++) 20 | { 21 | arr[i] = arr[i + 1]; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | //array index - 0 1 2 3 4 28 | int arr[100] = {56, 24, 98, 12, 89}; 29 | int size = 5, element = 10, index = 1; 30 | display(arr, size); 31 | IndexDelete(arr, size, index); 32 | 33 | size -= 1; 34 | display(arr, size); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /01_Arrays/deletioninarray.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/01_Arrays/deletioninarray.exe -------------------------------------------------------------------------------- /01_Arrays/insert1.c: -------------------------------------------------------------------------------- 1 | #include 2 | void display(int arr[], int n) 3 | { 4 | for (int i = 0; i < n; i++) 5 | { 6 | printf("%d ", arr[i]); 7 | } 8 | printf("\n"); 9 | } 10 | int IndexInsert(int arr[], int size, int element, int capacity, int index) 11 | { 12 | if (size >= capacity) 13 | { 14 | return -1; 15 | } 16 | 17 | for (int i = size - 1; i >= index; i--) 18 | { 19 | arr[i + 1] = arr[i]; 20 | } 21 | arr[index] = element; 22 | return 1; 23 | } 24 | 25 | int main() 26 | { 27 | //array index - 0 1 2 3 4 28 | int arr[100] = {56, 24, 98, 12, 89}; 29 | 30 | int size = 10; //size of used array 31 | 32 | int element = 10; //element to be inserted inside the array 33 | 34 | int index = 1; //elemetnt to be inserted on required index 35 | 36 | display(arr, size); // DISPLAYS OUR ARRAY 37 | 38 | IndexInsert(arr, size, element, 100, index); //INSERTION FUNCTION 39 | 40 | size += 1; //AFTER INSERTION INCEREASE THE SIZE OF ARRAY BY ONE 41 | 42 | if (IndexInsert(arr, size, element, 100, index) == 1) 43 | { 44 | display(arr, size); 45 | printf("\nInsertion done successfully\n"); 46 | } 47 | else 48 | { 49 | printf("\nUsed size of array is greater than actual size of array! \nHence, insertion not done"); 50 | } 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /01_Arrays/insert1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/01_Arrays/insert1.exe -------------------------------------------------------------------------------- /01_Arrays/insertion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //*****CODE FOR TRAVERSAL******** 4 | 5 | void display(int arr[], int n) 6 | { 7 | for (int i = 0; i < n; i++) 8 | { 9 | printf("%d ", arr[i]); 10 | } 11 | printf("\n"); 12 | } 13 | 14 | //*********CODE FOR INSERTION OF ELEMENT IN AN ARRAY************* 15 | 16 | int IndexInsert(int arr[], int size, int element, int capacity, int index) 17 | { 18 | if (size >= capacity) 19 | { 20 | return -1; 21 | } 22 | 23 | for (int i = size - 1; i >= index; i--) 24 | { 25 | arr[i + 1] = arr[i]; 26 | } 27 | arr[index] = element; 28 | return 1; 29 | } 30 | 31 | int main() 32 | { 33 | //array index - 0 1 2 3 4 34 | int arr[100] = {56, 24, 98, 12, 89}; 35 | int size = 5, element = 10, index = 1; 36 | display(arr, size); 37 | IndexInsert(arr, size, element, 100, index); 38 | 39 | size += 1; 40 | display(arr, size); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /01_Arrays/insertion.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/01_Arrays/insertion.exe -------------------------------------------------------------------------------- /01_Arrays/linearsearch copy.c: -------------------------------------------------------------------------------- 1 | #include 2 | int LinearSearch(int arr[], int size, int element) 3 | { 4 | for (int i = 0; i < size; i++) 5 | { 6 | if (arr[i] == element) 7 | { 8 | return 1; 9 | } 10 | } 11 | } 12 | int main() 13 | { 14 | //index of elements= 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15 | int arr[100] = {12, 40, 58, 81, 95, 23, 45, 64, 94, 65, 684, 6534, 98}; 16 | int element = 684; 17 | int size = sizeof(arr) / sizeof(int); 18 | 19 | int SearchIndex = LinearSearch(arr, size, element); 20 | printf("The element %d was found at index %d \n", element, SearchIndex); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /01_Arrays/linearsearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | int LinearSearch(int arr[], int size, int element) 3 | { 4 | for (int i = 0; i < size; i++) 5 | { 6 | if (arr[i] == element) 7 | { 8 | return i; 9 | } 10 | }return -1; 11 | } 12 | int main() 13 | { 14 | //index of elements= 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15 | int arr[100] = {12, 40, 58, 81, 95, 23, 45, 64, 94, 65, 684, 6534, 98}; 16 | int element = 684; 17 | int size = sizeof(arr) / sizeof(int); 18 | 19 | int SearchIndex = LinearSearch(arr, size, element); 20 | printf("The element %d was found at index %d \n", element, SearchIndex); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /01_Arrays/linearsearch.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/01_Arrays/linearsearch.exe -------------------------------------------------------------------------------- /01_Arrays/linearsearch1.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/01_Arrays/linearsearch1.c -------------------------------------------------------------------------------- /02_Linked list/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "stdio.h": "c" 4 | } 5 | } -------------------------------------------------------------------------------- /02_Linked list/01_creating.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | void TraverseLinkedList(struct node *ptr) 10 | { 11 | while (ptr != NULL) 12 | { 13 | printf(" %d ", ptr->data); 14 | ptr = ptr->next; 15 | } 16 | } 17 | int main() 18 | { 19 | struct node *head; 20 | struct node *second; 21 | struct node *third; 22 | struct node *fourth; 23 | struct node *fifth; 24 | 25 | head = (struct node *)malloc(sizeof(struct node *)); 26 | second = (struct node *)malloc(sizeof(struct node *)); 27 | third = (struct node *)malloc(sizeof(struct node *)); 28 | fourth = (struct node *)malloc(sizeof(struct node *)); 29 | fifth = (struct node *)malloc(sizeof(struct node *)); 30 | 31 | head->data = 7; 32 | head->next = second; 33 | 34 | second->data = 17; 35 | second->next = third; 36 | 37 | third->data = 27; 38 | third->next = fourth; 39 | 40 | fourth->data = 37; 41 | fourth->next = fifth; 42 | 43 | fifth->data = 47; 44 | fifth->next = NULL; 45 | TraverseLinkedList(head); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /02_Linked list/01_creating.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/02_Linked list/01_creating.exe -------------------------------------------------------------------------------- /02_Linked list/01_creatingcopy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | void TraverseLinkedList(struct node *ptr) 10 | { 11 | while (ptr != NULL) 12 | { 13 | printf("%d ", ptr->data); 14 | ptr = ptr->next; 15 | } 16 | } 17 | int main() 18 | { 19 | struct node *head; 20 | struct node *second; 21 | struct node *third; 22 | struct node *fourth; 23 | struct node *fifth; 24 | 25 | head = (struct node *)malloc(sizeof(struct node *)); 26 | second = (struct node *)malloc(sizeof(struct node *)); 27 | third = (struct node *)malloc(sizeof(struct node *)); 28 | fourth = (struct node *)malloc(sizeof(struct node *)); 29 | fifth = (struct node *)malloc(sizeof(struct node *)); 30 | 31 | head->data = 7; 32 | head->next = second; 33 | 34 | second->data = 17; 35 | second->next = third; 36 | 37 | third->data = 27; 38 | third->next = fourth; 39 | 40 | fourth->data = 37; 41 | fourth->next = fifth; 42 | 43 | fifth->data = 47; 44 | fifth->next = NULL; 45 | TraverseLinkedList(head); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /02_Linked list/case1.c: -------------------------------------------------------------------------------- 1 | // Insertion at the Head node 2 | 3 | #include 4 | #include 5 | struct node 6 | { 7 | int data; 8 | struct node *next; 9 | }; 10 | 11 | // function to print/traverse our linked list 12 | void TraverseLinkedList(struct node *ptr) 13 | { 14 | while (ptr != NULL) 15 | { 16 | printf("%d ", ptr->data); 17 | ptr = ptr->next; 18 | } 19 | } 20 | // case 1 21 | struct node *InsertionAtFirst(struct node *head, int data) 22 | { 23 | struct node *ptr = (struct node *)malloc(sizeof(struct node *)); 24 | ptr->data = data; 25 | ptr->next = head; 26 | 27 | return ptr; 28 | } 29 | int main() 30 | { 31 | struct node *head; 32 | struct node *second; 33 | struct node *third; 34 | struct node *fourth; 35 | struct node *fifth; 36 | 37 | head = (struct node *)malloc(sizeof(struct node *)); 38 | second = (struct node *)malloc(sizeof(struct node *)); 39 | third = (struct node *)malloc(sizeof(struct node *)); 40 | fourth = (struct node *)malloc(sizeof(struct node *)); 41 | fifth = (struct node *)malloc(sizeof(struct node *)); 42 | 43 | head->data = 7; 44 | head->next = second; 45 | 46 | second->data = 17; 47 | second->next = third; 48 | 49 | third->data = 27; 50 | third->next = fourth; 51 | 52 | fourth->data = 37; 53 | fourth->next = fifth; 54 | 55 | fifth->data = 47; 56 | fifth->next = NULL; 57 | printf("Linked list before insertion\n"); 58 | TraverseLinkedList(head); 59 | head = InsertionAtFirst(head, 14); 60 | printf("\nLinked list after insertion\n"); 61 | TraverseLinkedList(head); 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /02_Linked list/case1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/02_Linked list/case1.exe -------------------------------------------------------------------------------- /02_Linked list/case2.c: -------------------------------------------------------------------------------- 1 | // Insertion at the Head node 2 | 3 | #include 4 | #include 5 | struct node 6 | { 7 | int data; 8 | struct node *next; 9 | }; 10 | 11 | // function to print/traverse our linked list 12 | void TraverseLinkedList(struct node *ptr) 13 | { 14 | while (ptr != NULL) 15 | { 16 | printf("%d ", ptr->data); 17 | ptr = ptr->next; 18 | } 19 | } 20 | // case 1 21 | struct node *InsertionAtIndex(struct node *head, int data, int index) 22 | { 23 | struct node *ptr = (struct node *)malloc(sizeof(struct node *)); 24 | ptr->data = data; 25 | struct node *p = head; 26 | int i = 0; 27 | while (i != (index - 1)) 28 | { 29 | p = p->next; 30 | i++; 31 | } 32 | ptr->next = p->next; 33 | p->next = ptr; 34 | 35 | return head; 36 | } 37 | int main() 38 | { 39 | struct node *head; 40 | struct node *second; 41 | struct node *third; 42 | struct node *fourth; 43 | struct node *fifth; 44 | 45 | head = (struct node *)malloc(sizeof(struct node *)); 46 | second = (struct node *)malloc(sizeof(struct node *)); 47 | third = (struct node *)malloc(sizeof(struct node *)); 48 | fourth = (struct node *)malloc(sizeof(struct node *)); 49 | fifth = (struct node *)malloc(sizeof(struct node *)); 50 | 51 | head->data = 7; 52 | head->next = second; 53 | 54 | second->data = 17; 55 | second->next = third; 56 | 57 | third->data = 27; 58 | third->next = fourth; 59 | 60 | fourth->data = 37; 61 | fourth->next = fifth; 62 | 63 | fifth->data = 47; 64 | fifth->next = NULL; 65 | printf("Linked list before insertion\n"); 66 | TraverseLinkedList(head); 67 | // head = InsertionAtIndex(head, 14, 3); 68 | head = InsertionAtIndex(head, 16, 3); 69 | printf("\nLinked list after insertion\n"); 70 | TraverseLinkedList(head); 71 | 72 | return 0; 73 | } -------------------------------------------------------------------------------- /02_Linked list/case2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/02_Linked list/case2.exe -------------------------------------------------------------------------------- /02_Linked list/case3.c: -------------------------------------------------------------------------------- 1 | // Insertion at the Head node 2 | 3 | #include 4 | #include 5 | struct node 6 | { 7 | int data; 8 | struct node *next; 9 | }; 10 | 11 | // function to print/traverse our linked list 12 | void TraverseLinkedList(struct node *ptr) 13 | { 14 | while (ptr != NULL) 15 | { 16 | printf("%d ", ptr->data); 17 | ptr = ptr->next; 18 | } 19 | } 20 | // case 1 21 | struct node * InsertionAtEnd(struct node *head, int data) 22 | { 23 | struct node *ptr = (struct node *)malloc(sizeof(struct node *)); 24 | ptr->data = data; 25 | struct node *p = head; 26 | 27 | while (p->next != NULL) 28 | { 29 | p = p->next; 30 | } 31 | p->next = ptr; 32 | ptr->next = NULL; 33 | return head; 34 | } 35 | int main() 36 | { 37 | struct node *head; 38 | struct node *second; 39 | struct node *third; 40 | struct node *fourth; 41 | struct node *fifth; 42 | 43 | head = (struct node *)malloc(sizeof(struct node *)); 44 | second = (struct node *)malloc(sizeof(struct node *)); 45 | third = (struct node *)malloc(sizeof(struct node *)); 46 | fourth = (struct node *)malloc(sizeof(struct node *)); 47 | fifth = (struct node *)malloc(sizeof(struct node *)); 48 | 49 | head->data = 7; 50 | head->next = second; 51 | 52 | second->data = 17; 53 | second->next = third; 54 | 55 | third->data = 27; 56 | third->next = fourth; 57 | 58 | fourth->data = 37; 59 | fourth->next = fifth; 60 | 61 | fifth->data = 47; 62 | fifth->next = NULL; 63 | 64 | printf("Linked list before insertion\n"); 65 | TraverseLinkedList(head); 66 | // head = InsertionAtIndex(head, 14, 3); 67 | head = InsertionAtEnd(head, 16); 68 | printf("\nLinked list after insertion\n"); 69 | TraverseLinkedList(head); 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /02_Linked list/case3.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/02_Linked list/case3.exe -------------------------------------------------------------------------------- /02_Linked list/case4.c: -------------------------------------------------------------------------------- 1 | // Insertion at the Head node 2 | 3 | #include 4 | #include 5 | struct node 6 | { 7 | int data; 8 | struct node *next; 9 | }; 10 | 11 | // function to print/traverse our linked list 12 | void TraverseLinkedList(struct node *ptr) 13 | { 14 | while (ptr != NULL) 15 | { 16 | printf("%d ", ptr->data); 17 | ptr = ptr->next; 18 | } 19 | } 20 | // case 1 21 | struct node *InsertionAtNode(struct node *head, struct node *prevNode, int data) 22 | { 23 | struct node *ptr = (struct node *)malloc(sizeof(struct node *)); 24 | ptr->data = data; 25 | 26 | ptr->next = prevNode->next; 27 | prevNode->next = ptr; 28 | return head; 29 | } 30 | int main() 31 | { 32 | struct node *head; 33 | struct node *second; 34 | struct node *third; 35 | struct node *fourth; 36 | struct node *fifth; 37 | 38 | head = (struct node *)malloc(sizeof(struct node *)); 39 | second = (struct node *)malloc(sizeof(struct node *)); 40 | third = (struct node *)malloc(sizeof(struct node *)); 41 | fourth = (struct node *)malloc(sizeof(struct node *)); 42 | fifth = (struct node *)malloc(sizeof(struct node *)); 43 | 44 | head->data = 7; 45 | head->next = second; 46 | 47 | second->data = 17; 48 | second->next = third; 49 | 50 | third->data = 27; 51 | third->next = fourth; 52 | 53 | fourth->data = 37; 54 | fourth->next = fifth; 55 | 56 | fifth->data = 47; 57 | fifth->next = NULL; 58 | printf("Linked list before insertion\n"); 59 | TraverseLinkedList(head); 60 | 61 | head = InsertionAtNode(head, fourth,16); 62 | printf("\nLinked list after insertion\n"); 63 | TraverseLinkedList(head); 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /02_Linked list/case4.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/02_Linked list/case4.exe -------------------------------------------------------------------------------- /02_Linked list/circularLL.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | void TraverseLinkedList(struct node *ptr) 10 | { 11 | int head; 12 | do 13 | { 14 | printf(" %d ", ptr->data); 15 | ptr = ptr->next; 16 | } while (ptr != head); 17 | } 18 | int main() 19 | { 20 | struct node *head; 21 | struct node *second; 22 | struct node *third; 23 | struct node *fourth; 24 | struct node *fifth; 25 | 26 | head = (struct node *)malloc(sizeof(struct node *)); 27 | second = (struct node *)malloc(sizeof(struct node *)); 28 | third = (struct node *)malloc(sizeof(struct node *)); 29 | fourth = (struct node *)malloc(sizeof(struct node *)); 30 | fifth = (struct node *)malloc(sizeof(struct node *)); 31 | 32 | head->data = 7; 33 | head->next = second; 34 | 35 | second->data = 17; 36 | second->next = third; 37 | 38 | third->data = 27; 39 | third->next = fourth; 40 | 41 | fourth->data = 37; 42 | fourth->next = fifth; 43 | dgh 44 | fifth->next = head; 45 | dgh 46 | fifth->data = 47; 47 | fifth->next = head; 48 | fifth->next = head; 49 | TraverseLinkedList(head); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /02_Linked list/circularLL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/02_Linked list/circularLL.exe -------------------------------------------------------------------------------- /02_Linked list/deleteCase1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | void TraverseLinkedList(struct node *ptr) 10 | { 11 | while (ptr != NULL) 12 | { 13 | printf("%d ", ptr->data); 14 | ptr = ptr->next; 15 | } 16 | } 17 | struct node *DeletionAtFirst(struct node *head) 18 | { 19 | struct node *ptr = head; 20 | head = head->next; 21 | free(ptr); 22 | return head; 23 | } 24 | int main() 25 | { 26 | struct node *head; 27 | struct node *second; 28 | struct node *third; 29 | struct node *fourth; 30 | struct node *fifth; 31 | 32 | head = (struct node *)malloc(sizeof(struct node *)); 33 | second = (struct node *)malloc(sizeof(struct node *)); 34 | third = (struct node *)malloc(sizeof(struct node *)); 35 | fourth = (struct node *)malloc(sizeof(struct node *)); 36 | fifth = (struct node *)malloc(sizeof(struct node *)); 37 | 38 | head->data = 7; 39 | head->next = second; 40 | 41 | second->data = 17; 42 | second->next = third; 43 | 44 | third->data = 27; 45 | third->next = fourth; 46 | 47 | fourth->data = 37; 48 | fourth->next = fifth; 49 | 50 | fifth->data = 47; 51 | fifth->next = NULL; 52 | 53 | printf("Linked list before deletion\n"); 54 | TraverseLinkedList(head); 55 | head = DeletionAtFirst(head); 56 | printf("\nLinked list after deletion\n"); 57 | TraverseLinkedList(head); 58 | 59 | return 0; 60 | } -------------------------------------------------------------------------------- /02_Linked list/deleteCase1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/02_Linked list/deleteCase1.exe -------------------------------------------------------------------------------- /02_Linked list/deleteCase2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | void TraverseLinkedList(struct node *ptr) 10 | { 11 | while (ptr != NULL) 12 | { 13 | printf("%d ", ptr->data); 14 | ptr = ptr->next; 15 | } 16 | } 17 | struct node *DeletionAtIndex(struct node *head, int index) 18 | { 19 | struct node *p = head; 20 | struct node *q = head->next; 21 | for (int i = 0; i < index - 1; i++) 22 | { 23 | p = p->next; 24 | q = q->next; 25 | } 26 | p->next = q->next; 27 | free(q); 28 | 29 | return head; 30 | } 31 | int main() 32 | { 33 | struct node *head; 34 | struct node *second; 35 | struct node *third; 36 | struct node *fourth; 37 | struct node *fifth; 38 | 39 | head = (struct node *)malloc(sizeof(struct node *)); 40 | second = (struct node *)malloc(sizeof(struct node *)); 41 | third = (struct node *)malloc(sizeof(struct node *)); 42 | fourth = (struct node *)malloc(sizeof(struct node *)); 43 | fifth = (struct node *)malloc(sizeof(struct node *)); 44 | 45 | head->data = 7; 46 | head->next = second; 47 | 48 | second->data = 17; 49 | second->next = third; 50 | 51 | third->data = 27; 52 | third->next = fourth; 53 | 54 | fourth->data = 37; 55 | fourth->next = fifth; 56 | 57 | fifth->data = 47; 58 | fifth->next = NULL; 59 | 60 | printf("Linked list before deletion\n"); 61 | TraverseLinkedList(head); 62 | head = DeletionAtIndex(head, 2); 63 | printf("\nLinked list after deletion\n"); 64 | 65 | TraverseLinkedList(head); 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /02_Linked list/deleteCase2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/02_Linked list/deleteCase2.exe -------------------------------------------------------------------------------- /02_Linked list/deleteCase3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | void TraverseLinkedList(struct node *ptr) 10 | { 11 | while (ptr != NULL) 12 | { 13 | printf(" %d ", ptr->data); 14 | ptr = ptr->next; 15 | } 16 | } 17 | struct node *DeletionAtLast(struct node *head) 18 | { 19 | struct node *p = head; 20 | struct node *q = head->next; 21 | while (q->next != NULL) 22 | { 23 | p = p->next; 24 | q = q->next; 25 | } 26 | 27 | p->next = NULL; 28 | free(q); 29 | 30 | return head; 31 | } 32 | int main() 33 | { 34 | struct node *head; 35 | struct node *second; 36 | struct node *third; 37 | struct node *fourth; 38 | struct node *fifth; 39 | 40 | head = (struct node *)malloc(sizeof(struct node *)); 41 | second = (struct node *)malloc(sizeof(struct node *)); 42 | third = (struct node *)malloc(sizeof(struct node *)); 43 | fourth = (struct node *)malloc(sizeof(struct node *)); 44 | fifth = (struct node *)malloc(sizeof(struct node *)); 45 | 46 | head->data = 7; 47 | head->next = second; 48 | 49 | second->data = 17; 50 | second->next = third; 51 | 52 | third->data = 27; 53 | third->next = fourth; 54 | 55 | fourth->data = 37; 56 | fourth->next = fifth; 57 | 58 | fifth->data = 47; 59 | fifth->next = NULL; 60 | 61 | printf("Linked list before deletion\n"); 62 | TraverseLinkedList(head); 63 | head = DeletionAtLast(head); 64 | printf("\nLinked list after deletion\n"); 65 | TraverseLinkedList(head); 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /02_Linked list/deleteCase3.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/02_Linked list/deleteCase3.exe -------------------------------------------------------------------------------- /02_Linked list/deleteCase4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | void TraverseLinkedList(struct node *ptr) 10 | { 11 | while (ptr != NULL) 12 | { 13 | printf(" %d ", ptr->data); 14 | ptr = ptr->next; 15 | } 16 | } 17 | struct node *DeletionAtValue(struct node *head, int value) 18 | { 19 | struct node *p = head; 20 | struct node *q = head->next; 21 | while (q->data != value && q->data != NULL) 22 | { 23 | p = p->next; 24 | q = q->next; 25 | } 26 | if (q->data == value) 27 | { 28 | 29 | p->next = q->next; 30 | free(q); 31 | } 32 | 33 | return head; 34 | } 35 | int main() 36 | { 37 | struct node *head; 38 | struct node *second; 39 | struct node *third; 40 | struct node *fourth; 41 | struct node *fifth; 42 | 43 | head = (struct node *)malloc(sizeof(struct node *)); 44 | second = (struct node *)malloc(sizeof(struct node *)); 45 | third = (struct node *)malloc(sizeof(struct node *)); 46 | fourth = (struct node *)malloc(sizeof(struct node *)); 47 | fifth = (struct node *)malloc(sizeof(struct node *)); 48 | 49 | head->data = 7; 50 | head->next = second; 51 | 52 | second->data = 17; 53 | second->next = third; 54 | 55 | third->data = 27; 56 | third->next = fourth; 57 | 58 | fourth->data = 37; 59 | fourth->next = fifth; 60 | 61 | fifth->data = 47; 62 | fifth->next = NULL; 63 | 64 | printf("Linked list before deletion\n"); 65 | TraverseLinkedList(head); 66 | head = DeletionAtValue(head, 3); 67 | printf("\nLinked list after deletion\n"); 68 | TraverseLinkedList(head); 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /02_Linked list/deleteCase4.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/02_Linked list/deleteCase4.exe -------------------------------------------------------------------------------- /02_Linked list/mylinklist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | void printlist(struct node *ptr) 10 | { 11 | while (ptr != NULL) 12 | { 13 | printf(" %d ", ptr->data); 14 | ptr = ptr->next; 15 | } 16 | } 17 | struct node *First(struct node *head, int data) 18 | { 19 | struct node *ptr = (struct node *)malloc(sizeof(struct node *)); 20 | ptr->data = ptr; 21 | ptr->next = head; 22 | return head; 23 | } 24 | 25 | struct node *Last(struct node *head, int data) 26 | { 27 | struct node *ptr = (struct node *)malloc(sizeof(struct node *)); 28 | ptr->data = ptr; 29 | struct node *p = head; 30 | while (p->next != NULL) 31 | { 32 | p = p->next; 33 | } 34 | p->next = ptr->next; 35 | ptr->next = NULL; 36 | 37 | return head; 38 | } 39 | 40 | int main() 41 | { 42 | struct node *head; 43 | struct node *second; 44 | struct node *third; 45 | struct node *fourth; 46 | head = (struct node *)malloc(sizeof(struct node)); 47 | second = (struct node *)malloc(sizeof(struct node)); 48 | third = (struct node *)malloc(sizeof(struct node)); 49 | fourth = (struct node *)malloc(sizeof(struct node)); 50 | 51 | head->data = 1; 52 | head->next = second; 53 | 54 | second->data = 2; 55 | second->next = third; 56 | 57 | third->data = 3; 58 | third->next = fourth; 59 | 60 | fourth->data = 4; 61 | fourth->next = NULL; 62 | 63 | head = First(head, 10); 64 | head = Last(head, 12); 65 | printlist(head); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /02_Linked list/mylinklist.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/02_Linked list/mylinklist.exe -------------------------------------------------------------------------------- /03_Stack using array/peek_using_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct stack 4 | { 5 | int size; 6 | int top; 7 | int *arr; 8 | }; 9 | 10 | int isFull(struct stack *ptr) 11 | { 12 | if (ptr->top >= (ptr->size - 1)) 13 | { 14 | return 1; 15 | } 16 | else 17 | { 18 | return 0; 19 | } 20 | } 21 | 22 | void push(struct stack *ptr, int value) 23 | { 24 | if (isFull(ptr)) 25 | { 26 | printf("Stack Overflow ! Can't Push %d \n", value); 27 | } 28 | else 29 | { 30 | ptr->top++; 31 | ptr->arr[ptr->top] = value; 32 | } 33 | } 34 | 35 | int peek(struct stack *sp, int i) 36 | { 37 | int ArrPosition = sp->top - i + 1; 38 | if (ArrPosition < 0) 39 | { 40 | printf("Invalid position in stack!"); 41 | } 42 | else 43 | { 44 | return sp->arr[ArrPosition]; 45 | } 46 | } 47 | int main() 48 | { 49 | struct stack *sp = (struct stack *)malloc(sizeof(struct stack)); 50 | sp->size = 20; 51 | sp->top = -1; 52 | sp->arr = (int *)malloc(sp->size * sizeof(int)); 53 | 54 | printf("%d\n", isFull(sp)); 55 | push(sp, 1); 56 | push(sp, 2); 57 | push(sp, 3); 58 | push(sp, 4); 59 | push(sp, 5); 60 | push(sp, 6); 61 | push(sp, 7); 62 | push(sp, 8); 63 | push(sp, 9); 64 | push(sp, 10); 65 | push(sp, 11); 66 | push(sp, 12); 67 | printf("%d\n", isFull(sp)); 68 | 69 | for (int j = 1; j <= sp->top + 1; j++) 70 | { 71 | printf("The element at position %d is %d\n", j, peek(sp, j)); 72 | } 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /03_Stack using array/peek_using_array.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/03_Stack using array/peek_using_array.exe -------------------------------------------------------------------------------- /03_Stack using array/pop_using_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct stack 4 | { 5 | int size; 6 | int top; 7 | int *arr; 8 | }; 9 | 10 | int isEmpty(struct stack *ptr) 11 | { 12 | if (ptr->top == - 1) 13 | { 14 | return 1; 15 | } 16 | else 17 | { 18 | return 0; 19 | } 20 | } 21 | 22 | void pop(struct stack *ptr) 23 | { 24 | if (isEmpty(ptr)) 25 | { 26 | printf("Stack Underflow ! Can't Pop.\n"); 27 | return -1; 28 | } 29 | else 30 | { 31 | int val =ptr -> arr[ptr->top]; 32 | ptr->top--; 33 | return val; 34 | } 35 | } 36 | int main() 37 | { 38 | struct stack *sp=(struct stack *)malloc(sizeof(struct stack)); 39 | sp->size = 6; 40 | sp->top = -1; 41 | sp->arr = (int *)malloc(sp->size * sizeof(int)); 42 | 43 | printf("%d\n", isEmpty(sp)); 44 | pop(sp); 45 | pop(sp); 46 | pop(sp); 47 | pop(sp); 48 | pop(sp); 49 | printf("%d\n", isEmpty(sp)); 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /03_Stack using array/pop_using_array.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/03_Stack using array/pop_using_array.exe -------------------------------------------------------------------------------- /03_Stack using array/push_using_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct stack 4 | { 5 | int size; 6 | int top; 7 | int *arr; 8 | }; 9 | 10 | int isFull(struct stack *ptr) 11 | { 12 | if (ptr->top >= (ptr->size - 1)) 13 | { 14 | return 1; 15 | } 16 | else 17 | { 18 | return 0; 19 | } 20 | } 21 | 22 | void push(struct stack *ptr, int value) 23 | { 24 | if (isFull(ptr)) 25 | { 26 | printf("Stack Overflow ! Can't Push %d \n", value); 27 | } 28 | else 29 | { 30 | ptr->top++; 31 | ptr->arr[ptr->top] = value; 32 | } 33 | } 34 | int main() 35 | { 36 | struct stack *sp=(struct stack *)malloc(sizeof(struct stack)); 37 | sp->size = 6; 38 | sp->top = -1; 39 | sp->arr = (int *)malloc(sp->size * sizeof(int)); 40 | 41 | printf("%d\n", isFull(sp)); 42 | push(sp,26); 43 | push(sp,26); 44 | push(sp,26); 45 | push(sp,26); 46 | push(sp,26); 47 | push(sp,26); 48 | push(sp,26); 49 | push(sp,26); 50 | push(sp,26); 51 | push(sp,26); 52 | push(sp,26); 53 | push(sp,26); 54 | printf("%d\n", isFull(sp)); 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /03_Stack using array/push_using_array.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/03_Stack using array/push_using_array.exe -------------------------------------------------------------------------------- /03_Stack using array/stack_bottom.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct stack 4 | { 5 | int size; 6 | int top; 7 | int *arr; 8 | }; 9 | 10 | int isFull(struct stack *ptr) 11 | { 12 | if (ptr->top >= (ptr->size - 1)) 13 | { 14 | return 1; 15 | } 16 | else 17 | { 18 | return 0; 19 | } 20 | } 21 | 22 | void push(struct stack *ptr, int value) 23 | { 24 | if (isFull(ptr)) 25 | { 26 | printf("Stack Overflow ! Can't Push %d \n", value); 27 | } 28 | else 29 | { 30 | ptr->top++; 31 | ptr->arr[ptr->top] = value; 32 | } 33 | } 34 | int stackBottom(struct stack *sp) 35 | { 36 | return sp->arr[0]; 37 | } 38 | int main() 39 | { 40 | struct stack *sp = (struct stack *)malloc(sizeof(struct stack)); 41 | sp->size = 16; 42 | sp->top = -1; 43 | sp->arr = (int *)malloc(sp->size * sizeof(int)); 44 | 45 | // printf("%d\n", isFull(sp)); 46 | push(sp, 1); 47 | push(sp, 2); 48 | push(sp, 3); 49 | push(sp, 4); 50 | push(sp, 5); 51 | push(sp, 6); 52 | push(sp, 7); 53 | push(sp, 8); 54 | push(sp, 9); 55 | push(sp, 10); 56 | push(sp, 11); 57 | push(sp, 12); 58 | // printf("%d\n", isFull(sp)); 59 | printf( "The Bottom most element of the stack is %d\n",stackBottom(sp)); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /03_Stack using array/stack_bottom.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/03_Stack using array/stack_bottom.exe -------------------------------------------------------------------------------- /03_Stack using array/stack_top.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct stack 4 | { 5 | int size; 6 | int top; 7 | int *arr; 8 | }; 9 | 10 | int isFull(struct stack *ptr) 11 | { 12 | if (ptr->top >= (ptr->size - 1)) 13 | { 14 | return 1; 15 | } 16 | else 17 | { 18 | return 0; 19 | } 20 | } 21 | 22 | void push(struct stack *ptr, int value) 23 | { 24 | if (isFull(ptr)) 25 | { 26 | printf("Stack Overflow ! Can't Push %d \n", value); 27 | } 28 | else 29 | { 30 | ptr->top++; 31 | ptr->arr[ptr->top] = value; 32 | } 33 | } 34 | int stackTop(struct stack *sp) 35 | { 36 | return sp->arr[sp->top]; 37 | } 38 | int main() 39 | { 40 | struct stack *sp = (struct stack *)malloc(sizeof(struct stack)); 41 | sp->size = 16; 42 | sp->top = -1; 43 | sp->arr = (int *)malloc(sp->size * sizeof(int)); 44 | 45 | // printf("%d\n", isFull(sp)); 46 | push(sp, 1); 47 | push(sp, 2); 48 | push(sp, 3); 49 | push(sp, 4); 50 | push(sp, 5); 51 | push(sp, 6); 52 | push(sp, 7); 53 | push(sp, 8); 54 | push(sp, 9); 55 | push(sp, 10); 56 | push(sp, 11); 57 | push(sp, 12); 58 | // printf("%d\n", isFull(sp)); 59 | printf( "The top most element of the stack is %d\n",stackTop(sp)); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /03_Stack using array/stack_top.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/03_Stack using array/stack_top.exe -------------------------------------------------------------------------------- /03_Stack using array/stack_using_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct stack 4 | { 5 | int size; 6 | int top; 7 | int *arr; 8 | }; 9 | int isEmpty(struct stack *ptr) 10 | { 11 | if (ptr->top == -1) 12 | { 13 | return 1; 14 | } 15 | else 16 | { 17 | return 0; 18 | } 19 | } 20 | int isFull(struct stack *ptr) 21 | { 22 | if (ptr->top >= (ptr->size - 1)) 23 | { 24 | return 1; 25 | } 26 | else 27 | { 28 | return 0; 29 | } 30 | } 31 | 32 | int main() 33 | { 34 | struct stack *s; 35 | s->size = 6; 36 | s->top = -1; 37 | s->arr = (int *)malloc(s->size * sizeof(int)); 38 | 39 | s->arr[0] = 7; 40 | s->top=s->top+1; 41 | s->arr[1] = 12; 42 | s->top=s->top+1; 43 | s->arr[2] = 12; 44 | s->top=s->top+1; 45 | s->arr[3] = 34; 46 | s->top=s->top+1; 47 | s->arr[4] = 23; 48 | s->top=s->top+1; 49 | // s->arr[5] = 54; 50 | // s->top=s->top+1; 51 | // s->arr[6] = 99; 52 | // s->top=s->top+1; 53 | // s->arr[7] = 99; 54 | // s->top=s->top+1; 55 | 56 | 57 | // if (isEmpty(s)) 58 | // { 59 | // printf("The stack is empty\n"); 60 | // } 61 | // else 62 | // { 63 | // printf("The stack is not empty\n"); 64 | // } 65 | // return 0; 66 | // } 67 | 68 | if (isFull(s) == 1) 69 | { 70 | printf("The stack is Full\n"); 71 | } 72 | else 73 | { 74 | printf("The stack is not Full\n"); 75 | } 76 | return 0; 77 | } -------------------------------------------------------------------------------- /03_Stack using array/stack_using_array.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/03_Stack using array/stack_using_array.exe -------------------------------------------------------------------------------- /03_Stack using array/tempCodeRunnerFile.c: -------------------------------------------------------------------------------- 1 | // s->arr[6] = 99; 2 | // s->top=s->top+1; 3 | // s->arr[7] = 99; 4 | // s->top=s->top+1; 5 | -------------------------------------------------------------------------------- /04_Stack using Linked list/00stack_using_LL.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | void LinkedListTraversal(struct node *ptr) 10 | { 11 | while (ptr != NULL) 12 | { 13 | printf("|__%d__|\n", ptr->data); 14 | ptr = ptr->next; 15 | } 16 | } 17 | 18 | int isEmpty(struct node *top) 19 | { 20 | if (top == NULL) 21 | { 22 | return 1; 23 | } 24 | else 25 | { 26 | return 0; 27 | } 28 | } 29 | 30 | int isFull(struct node *top) 31 | { 32 | struct node *p = (struct node *)malloc(sizeof(struct node)); 33 | if (p == NULL) 34 | { 35 | return 1; 36 | } 37 | else 38 | { 39 | return 0; 40 | } 41 | } 42 | 43 | struct node *push(struct node *top, int x) 44 | { 45 | if (isFull(top)) 46 | { 47 | printf("Stack Overflow !\n"); 48 | } 49 | else 50 | { 51 | struct node *n = (struct node *)malloc(sizeof(struct node)); 52 | n->data = x; 53 | n->next = top; 54 | top = n; 55 | return top; 56 | } 57 | } 58 | int main() 59 | { 60 | struct node *top = NULL; 61 | printf("\n\n"); 62 | top=push(top,1); 63 | top=push(top,2); 64 | top=push(top,3); 65 | top=push(top,4); 66 | top=push(top,5); 67 | top=push(top,6); 68 | top=push(top,7); 69 | top=push(top,8); 70 | top=push(top,9); 71 | 72 | 73 | LinkedListTraversal(top); 74 | printf("\n\n"); 75 | 76 | return 0; 77 | } -------------------------------------------------------------------------------- /04_Stack using Linked list/00stack_using_LL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/04_Stack using Linked list/00stack_using_LL.exe -------------------------------------------------------------------------------- /04_Stack using Linked list/01pop_in_stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | void LinkedListTraversal(struct node *ptr) 10 | { 11 | while (ptr != NULL) 12 | { 13 | printf("|__%d__|\n", ptr->data); 14 | ptr = ptr->next; 15 | } 16 | } 17 | 18 | int isEmpty(struct node *top) 19 | { 20 | if (top == NULL) 21 | { 22 | return 1; 23 | } 24 | else 25 | { 26 | return 0; 27 | } 28 | } 29 | 30 | int isFull(struct node *top) 31 | { 32 | struct node *p = (struct node *)malloc(sizeof(struct node)); 33 | if (p == NULL) 34 | { 35 | return 1; 36 | } 37 | else 38 | { 39 | return 0; 40 | } 41 | } 42 | 43 | struct node *push(struct node *top, int x) 44 | { 45 | if (isFull(top)) 46 | { 47 | printf("Stack Overflow !\n"); 48 | } 49 | else 50 | { 51 | struct node *n = (struct node *)malloc(sizeof(struct node)); 52 | n->data = x; 53 | n->next = top; 54 | top = n; 55 | return top; 56 | } 57 | } 58 | int pop(struct node **top) 59 | { 60 | if (isEmpty(*top)) 61 | { 62 | printf("Stack Underflow !,\nPop cannot be performed\n"); 63 | } 64 | else 65 | { 66 | struct node *n = *top; 67 | *top = (*top)->next; 68 | int x = n->data; 69 | free(n); 70 | printf("Pop operation performed successfully ! :) \n"); 71 | return x; 72 | } 73 | } 74 | int main() 75 | { 76 | struct node *top = NULL; 77 | top = push(top, 1); 78 | top = push(top, 2); 79 | top = push(top, 3); 80 | top = push(top, 4); 81 | top = push(top, 5); 82 | // top = push(top, 6); 83 | printf("\n\n"); 84 | 85 | LinkedListTraversal(top); 86 | printf("\n\n"); 87 | 88 | // printf("Pop operation performed successfully ! :) \n"); 89 | int element = pop(&top); 90 | LinkedListTraversal(top); 91 | printf("\n\n"); 92 | 93 | printf("The popped element is %d \n", element); 94 | printf("\n\n"); 95 | 96 | return 0; 97 | } -------------------------------------------------------------------------------- /04_Stack using Linked list/01pop_in_stack.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/04_Stack using Linked list/01pop_in_stack.exe -------------------------------------------------------------------------------- /04_Stack using Linked list/02pop_in_stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | struct node *top = NULL; 10 | 11 | void LinkedListTraversal(struct node *ptr) 12 | { 13 | while (ptr != NULL) 14 | { 15 | printf("|__%d__|\n", ptr->data); 16 | ptr = ptr->next; 17 | } 18 | } 19 | 20 | int isEmpty(struct node *top) 21 | { 22 | if (top == NULL) 23 | { 24 | return 1; 25 | } 26 | else 27 | { 28 | return 0; 29 | } 30 | } 31 | 32 | int isFull(struct node *top) 33 | { 34 | struct node *p = (struct node *)malloc(sizeof(struct node)); 35 | if (p == NULL) 36 | { 37 | return 1; 38 | } 39 | else 40 | { 41 | return 0; 42 | } 43 | } 44 | 45 | struct node *push(struct node *top, int x) 46 | { 47 | if (isFull(top)) 48 | { 49 | printf("Stack Overflow !\n"); 50 | } 51 | else 52 | { 53 | struct node *n = (struct node *)malloc(sizeof(struct node)); 54 | n->data = x; 55 | n->next = top; 56 | top = n; 57 | return top; 58 | } 59 | } 60 | int pop(struct node *tp) 61 | { 62 | if (isEmpty(tp)) 63 | { 64 | printf("Stack Underflow !\n"); 65 | } 66 | else 67 | { 68 | struct node *n = tp; 69 | top = tp->next; 70 | int x = n->data; 71 | free(n); 72 | return x; 73 | } 74 | } 75 | int main() 76 | { 77 | 78 | top = push(top, 1); 79 | top = push(top, 2); 80 | top = push(top, 3); 81 | top = push(top, 4); 82 | top = push(top, 5); 83 | top = push(top, 6); 84 | 85 | LinkedListTraversal(top); 86 | printf("Pop operation performed successfully ! :) \n"); 87 | int element = pop(top); 88 | LinkedListTraversal(top); 89 | printf("The popped element is %d \n", element); 90 | return 0; 91 | } -------------------------------------------------------------------------------- /04_Stack using Linked list/02pop_in_stack.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/04_Stack using Linked list/02pop_in_stack.exe -------------------------------------------------------------------------------- /04_Stack using Linked list/03stacktop.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | struct node *top = NULL; 9 | 10 | void LinkedListTraversal(struct node *ptr) 11 | { 12 | while (ptr != NULL) 13 | { 14 | printf("|__%d__|\n", ptr->data); 15 | ptr = ptr->next; 16 | } 17 | } 18 | 19 | int isEmpty(struct node *top) 20 | { 21 | if (top == NULL) 22 | { 23 | return 1; 24 | } 25 | else 26 | { 27 | return 0; 28 | } 29 | } 30 | 31 | int isFull(struct node *top) 32 | { 33 | struct node *p = (struct node *)malloc(sizeof(struct node)); 34 | if (p == NULL) 35 | { 36 | return 1; 37 | } 38 | else 39 | { 40 | return 0; 41 | } 42 | } 43 | 44 | struct node *push(struct node *top, int x) 45 | { 46 | if (isFull(top)) 47 | { 48 | printf("Stack Overflow !\n"); 49 | } 50 | else 51 | { 52 | struct node *n = (struct node *)malloc(sizeof(struct node)); 53 | n->data = x; 54 | n->next = top; 55 | top = n; 56 | return top; 57 | } 58 | } 59 | 60 | int peek(int pos) 61 | { 62 | struct node *ptr = top; 63 | for (int i = 0; (i < pos - 1 && ptr != NULL); i++) 64 | { 65 | ptr = ptr->next; 66 | } 67 | if (ptr != NULL) 68 | { 69 | return ptr->data; 70 | } 71 | else 72 | { 73 | return -1; 74 | } 75 | } 76 | int stacktop(struct node * ptr){ 77 | return top->data; 78 | } 79 | int main() 80 | { 81 | 82 | 83 | printf("\n\n"); 84 | top = push(top, 1); // 6 85 | top = push(top, 2); // 5 86 | top = push(top, 3); // 4 87 | top = push(top, 4); // 3 88 | top = push(top, 5); // 2 89 | top = push(top, 6); // 1 90 | // top = push(top, 7); 91 | // top = push(top, 8); 92 | // top = push(top, 9); 93 | 94 | LinkedListTraversal(top); 95 | printf("\n\n"); 96 | printf("The Topmost element is %d \n", stacktop(top)); 97 | return 0; 98 | } -------------------------------------------------------------------------------- /04_Stack using Linked list/03stacktop.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/04_Stack using Linked list/03stacktop.exe -------------------------------------------------------------------------------- /04_Stack using Linked list/04_stackbottom.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | struct node *top = NULL; 9 | 10 | void LinkedListTraversal(struct node *ptr) 11 | { 12 | while (ptr != NULL) 13 | { 14 | printf("|__%d__|\n", ptr->data); 15 | ptr = ptr->next; 16 | } 17 | } 18 | 19 | int isEmpty(struct node *top) 20 | { 21 | if (top == NULL) 22 | { 23 | return 1; 24 | } 25 | else 26 | { 27 | return 0; 28 | } 29 | } 30 | 31 | int isFull(struct node *top) 32 | { 33 | struct node *p = (struct node *)malloc(sizeof(struct node)); 34 | if (p == NULL) 35 | { 36 | return 1; 37 | } 38 | else 39 | { 40 | return 0; 41 | } 42 | } 43 | 44 | struct node *push(struct node *top, int x) 45 | { 46 | if (isFull(top)) 47 | { 48 | printf("Stack Overflow !\n"); 49 | } 50 | else 51 | { 52 | struct node *n = (struct node *)malloc(sizeof(struct node)); 53 | n->data = x; 54 | n->next = top; 55 | top = n; 56 | return top; 57 | } 58 | } 59 | 60 | int peek(int pos) 61 | { 62 | struct node *ptr = top; 63 | for (int i = 0; (i < pos - 1 && ptr != NULL); i++) 64 | { 65 | ptr = ptr->next; 66 | } 67 | if (ptr != NULL) 68 | { 69 | return ptr->data; 70 | } 71 | else 72 | { 73 | return -1; 74 | } 75 | } 76 | int stackBottom(struct node *ptr) 77 | { 78 | struct node *p = top; 79 | while (p->next != NULL) 80 | { 81 | p = p->next; 82 | } 83 | 84 | return p->data; 85 | } 86 | int main() 87 | { 88 | 89 | printf("\n\n"); 90 | top = push(top, 1); // 6 91 | top = push(top, 2); // 5 92 | top = push(top, 3); // 4 93 | top = push(top, 4); // 3 94 | top = push(top, 5); // 2 95 | top = push(top, 6); // 1 96 | // top = push(top, 7); 97 | // top = push(top, 8); 98 | // top = push(top, 9); 99 | 100 | LinkedListTraversal(top); 101 | printf("\n\n"); 102 | printf("The bottom most element is %d \n", stackBottom(top)); 103 | return 0; 104 | } -------------------------------------------------------------------------------- /04_Stack using Linked list/04_stackbottom.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/04_Stack using Linked list/04_stackbottom.exe -------------------------------------------------------------------------------- /04_Stack using Linked list/peek_operation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | struct node *top = NULL; 9 | 10 | void LinkedListTraversal(struct node *ptr) 11 | { 12 | while (ptr != NULL) 13 | { 14 | printf("|__%d__|\n", ptr->data); 15 | ptr = ptr->next; 16 | } 17 | } 18 | 19 | int isEmpty(struct node *top) 20 | { 21 | if (top == NULL) 22 | { 23 | return 1; 24 | } 25 | else 26 | { 27 | return 0; 28 | } 29 | } 30 | 31 | int isFull(struct node *top) 32 | { 33 | struct node *p = (struct node *)malloc(sizeof(struct node)); 34 | if (p == NULL) 35 | { 36 | return 1; 37 | } 38 | else 39 | { 40 | return 0; 41 | } 42 | } 43 | 44 | struct node *push(struct node *top, int x) 45 | { 46 | if (isFull(top)) 47 | { 48 | printf("Stack Overflow !\n"); 49 | } 50 | else 51 | { 52 | struct node *n = (struct node *)malloc(sizeof(struct node)); 53 | n->data = x; 54 | n->next = top; 55 | top = n; 56 | return top; 57 | } 58 | } 59 | 60 | int peek(int pos) 61 | { 62 | struct node *ptr = top; 63 | for (int i = 0; (i < pos - 1 && ptr != NULL); i++) 64 | { 65 | ptr = ptr->next; 66 | } 67 | if (ptr != NULL) 68 | { 69 | return ptr->data; 70 | } 71 | else 72 | { 73 | return -1; 74 | } 75 | } 76 | int main() 77 | { 78 | 79 | 80 | printf("\n\n"); 81 | top = push(top, 1); // 6 82 | top = push(top, 2); // 5 83 | top = push(top, 3); // 4 84 | top = push(top, 4); // 3 85 | top = push(top, 5); // 2 86 | top = push(top, 6); // 1 87 | // top = push(top, 7); 88 | // top = push(top, 8); 89 | // top = push(top, 9); 90 | 91 | LinkedListTraversal(top); 92 | printf("\n\n"); 93 | printf("The Element at third position is %d \n", peek(3)); 94 | return 0; 95 | } -------------------------------------------------------------------------------- /04_Stack using Linked list/peek_operation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/04_Stack using Linked list/peek_operation.exe -------------------------------------------------------------------------------- /04_Stack using Linked list/stack_using_LL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/04_Stack using Linked list/stack_using_LL.exe -------------------------------------------------------------------------------- /05_parantheisi matching in stack/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "stdio.h": "c", 4 | "cmath": "c" 5 | } 6 | } -------------------------------------------------------------------------------- /05_parantheisi matching in stack/Multiple_PM.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct stack 5 | { 6 | int size; 7 | int top; 8 | char *arr; 9 | }; 10 | 11 | int isEmpty(struct stack *ptr) 12 | { 13 | 14 | if (ptr->top == -1) 15 | { 16 | return 1; 17 | } 18 | return 0; 19 | } 20 | 21 | int isFull(struct stack *ptr) 22 | { 23 | if (ptr->top == ptr->size - 1) 24 | { 25 | return 1; 26 | } 27 | return 0; 28 | } 29 | 30 | void push(struct stack *ptr, int value) 31 | { 32 | if (isFull(ptr)) 33 | { 34 | printf("Stack overflow !\n %d cannot be pushed.\n", value); 35 | } 36 | else 37 | { 38 | ptr->top++; 39 | ptr->arr[ptr->top] = value; 40 | } 41 | } 42 | 43 | char pop(struct stack *ptr) 44 | { 45 | if (isEmpty(ptr)) 46 | { 47 | printf("Stack Underflow !\nElement cannot be popped.\n"); 48 | } 49 | else 50 | { 51 | char val = ptr->arr[ptr->top]; 52 | ptr->top--; 53 | return val; 54 | } 55 | } 56 | 57 | int Match(char a, char b) 58 | { 59 | if (a == '(' && b == ')') 60 | { 61 | return 1; 62 | } 63 | if (a == '[' && b == ']') 64 | { 65 | return 1; 66 | } 67 | if (a == '{' && b == '}') 68 | { 69 | return 1; 70 | } 71 | return 0; 72 | } 73 | 74 | int Multiple_PM(char * exp) 75 | { 76 | struct stack *sp; 77 | sp->top = -1; 78 | sp->size = 20; 79 | sp->arr = (char *)malloc(sp->size * sizeof(char)); 80 | char popped_char; 81 | for (int i = 0; exp[i] != '\0'; i++) 82 | { 83 | if (exp[i] == '(' || exp[i] == '[' || exp[i] == '{') 84 | { 85 | push(sp, exp[i]); 86 | } 87 | else if (exp[i] == ')' || exp[i] == ']' || exp[i] == '}') 88 | { 89 | if (isEmpty(sp)) 90 | { 91 | return 0; 92 | } 93 | popped_char = pop(sp); 94 | if (!Match(popped_char, exp[i])) 95 | { 96 | return 0; 97 | } 98 | } 99 | } 100 | if (isEmpty(sp)) 101 | { 102 | return 1; 103 | } 104 | return 0; 105 | } 106 | 107 | int main() 108 | { 109 | char *exp = "[][{[{}6*(56)34]}]"; 110 | if (Multiple_PM(exp)) 111 | { 112 | printf("\nParenthesis are matching successfully\n\n"); 113 | } 114 | else 115 | { 116 | printf("\nParenthesis are not matching\n\n"); 117 | } 118 | 119 | return 0; 120 | } -------------------------------------------------------------------------------- /05_parantheisi matching in stack/Multiple_PM.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/05_parantheisi matching in stack/Multiple_PM.exe -------------------------------------------------------------------------------- /05_parantheisi matching in stack/PM_using_Array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct stack 5 | { 6 | int top; 7 | int size; 8 | char *arr; 9 | }; 10 | 11 | int isEmpty(struct stack *ptr) 12 | { 13 | if (ptr->top == -1) 14 | { 15 | return 1; 16 | } 17 | return 0; 18 | } 19 | 20 | int isFull(struct stack *ptr) 21 | { 22 | if (ptr->top == ptr->size - 1) 23 | { 24 | return 1; 25 | } 26 | return 0; 27 | } 28 | 29 | void push(struct stack *ptr, char val) 30 | { 31 | if (isFull(ptr)) 32 | { 33 | printf("Stack Overflow !\n Cannot push %d to the stack.\n", val); 34 | } 35 | else 36 | { 37 | ptr->top++; 38 | ptr->arr[ptr->top] = val; 39 | } 40 | } 41 | 42 | char pop(struct stack *ptr) 43 | { 44 | if (isEmpty(ptr)) 45 | { 46 | printf("Stack Underflow !\n Pop cannot be done.\n"); 47 | return -1; 48 | } 49 | else 50 | { 51 | char val = ptr->arr[ptr->top]; 52 | ptr->top--; 53 | return val; 54 | } 55 | } 56 | 57 | int PM_match(char *exp) 58 | { 59 | //initialiazing our stack as sp 60 | struct stack *sp; 61 | sp->size = 100; 62 | sp->top = -1; 63 | sp->arr = (char *)malloc(sp->size * sizeof(char)); 64 | 65 | for (int i = 0; exp[i] != '\0'; i++) 66 | { 67 | if (exp[i] == '(') 68 | { 69 | push(sp, '('); 70 | } 71 | else if (exp[i] == ')') 72 | { 73 | if (isEmpty(sp)) 74 | { 75 | return 0; 76 | } 77 | pop(sp); 78 | } 79 | } 80 | if (isEmpty(sp)) 81 | { 82 | return 1; 83 | } 84 | else 85 | { 86 | return 0; 87 | } 88 | } 89 | 90 | int main() 91 | { 92 | char *exp = "(8*9(((())))"; 93 | if (PM_match(exp)) 94 | { 95 | printf("The parenthesis is matching successfully\n"); 96 | } 97 | else 98 | { 99 | printf("The parenthesis is not matching \n"); 100 | } 101 | return 0; 102 | } -------------------------------------------------------------------------------- /05_parantheisi matching in stack/PM_using_Array.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/05_parantheisi matching in stack/PM_using_Array.exe -------------------------------------------------------------------------------- /06_infix to postfix in stack/infix_to_postfix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct stack 6 | { 7 | int size; 8 | int top; 9 | char *arr; 10 | }; 11 | 12 | int StackTop(struct stack *ptr) 13 | { 14 | return ptr->arr[ptr->top]; 15 | } 16 | 17 | int isEmpty(struct stack *ptr) 18 | { 19 | if (ptr->top == -1) 20 | { 21 | return 1; 22 | } 23 | return 0; 24 | } 25 | 26 | int isFull(struct stack *ptr) 27 | { 28 | if (ptr->top == ptr->size - 1) 29 | { 30 | return 1; 31 | } 32 | return 0; 33 | } 34 | 35 | void push(struct stack *ptr, char value) 36 | { 37 | if (isFull(ptr)) 38 | { 39 | printf("Stack Overflow !.\n %d element cannot be pushed. ", value); 40 | } 41 | else 42 | { 43 | ptr->top++; 44 | ptr->arr[ptr->top] = value; 45 | } 46 | } 47 | 48 | char pop(struct stack *ptr) 49 | { 50 | if (isEmpty(ptr)) 51 | { 52 | printf("Stack Underflow ! Connot pop element from the stack"); 53 | return -1; 54 | } 55 | else 56 | { 57 | char value = ptr->arr[ptr->top]; 58 | ptr->top--; 59 | return value; 60 | } 61 | } 62 | 63 | int IsOperator(char ch) 64 | { 65 | if (ch == '+' || ch == '-' || ch == '*' || ch == '/') 66 | return 1; 67 | return 0; 68 | } 69 | 70 | int precedence(char ch) 71 | { 72 | if (ch == '*' || ch == '/') 73 | return 3; 74 | else if (ch == '+' || ch == '-') 75 | return 2; 76 | else 77 | return 0; 78 | } 79 | 80 | char *InfixToPostfix(char *infix) 81 | { 82 | //initializing our stack 83 | struct stack *sp = (struct stack *)malloc(sizeof(struct stack)); 84 | sp->size = 20; 85 | sp->top = -1; 86 | sp->arr = (char *)malloc(sp->size * sizeof(char)); 87 | 88 | char *postfix = (char *)malloc((strlen(infix) + 1) * sizeof(char)); 89 | int i = 0; // track infix traversal 90 | int j = 0; // track postfix addition 91 | 92 | while (infix[i] != '\0') 93 | { 94 | if (!IsOperator(infix[i])) 95 | { 96 | postfix[j] = infix[i]; 97 | i++; 98 | j++; 99 | } 100 | else 101 | { 102 | if (precedence(infix[i]) > precedence(StackTop(sp))) 103 | { 104 | push(sp, infix[i]); 105 | i++; 106 | } 107 | else 108 | { 109 | postfix[j] = pop(sp); 110 | j++; 111 | } 112 | } 113 | } 114 | while (!isEmpty(sp)) 115 | { 116 | postfix[j] = pop(sp); 117 | j++; 118 | } 119 | postfix[j] = '\0'; 120 | return postfix; 121 | } 122 | 123 | int main() 124 | { 125 | char *infix = "a*b+k*f"; 126 | printf("\nThe postfic conversion is %s \n\n", InfixToPostfix(infix)); 127 | return 0; 128 | } -------------------------------------------------------------------------------- /06_infix to postfix in stack/infix_to_postfix.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/06_infix to postfix in stack/infix_to_postfix.exe -------------------------------------------------------------------------------- /07_queue using array/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "stdio.h": "c" 4 | } 5 | } -------------------------------------------------------------------------------- /07_queue using array/01_queue_using_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Queue 5 | { 6 | int size; 7 | int f; 8 | int r; 9 | int *arr; 10 | }; 11 | int isFull(struct Queue *q) 12 | { 13 | if (q->f == q->size - 1) 14 | { 15 | return 1; 16 | } 17 | return 0; 18 | } 19 | int isEmpty(struct Queue *q) 20 | { 21 | if (q->f == q->r) 22 | { 23 | return 1; 24 | } 25 | return 0; 26 | } 27 | void enqueue(struct Queue *q, int value) 28 | { 29 | if (isFull(q)) 30 | { 31 | printf("Queue overflow !\n Cannot insert %d to the queue\n", value); 32 | } 33 | else 34 | { 35 | q->r++; 36 | q->arr[q->r] = value; 37 | printf("| %d ", value); 38 | } 39 | } 40 | 41 | int dequeue(struct Queue *q) 42 | { 43 | int a = -1; 44 | if (isEmpty(q)) 45 | { 46 | printf("The queue is empty.\n"); 47 | return 0; 48 | } 49 | else 50 | { 51 | q->f++; 52 | a = q->arr[q->f]; 53 | } 54 | return a; 55 | } 56 | int main() 57 | { 58 | struct Queue q; 59 | q.size = 10; 60 | q.f = q.r = -1; 61 | q.arr = (int *)malloc(q.size * sizeof(int)); 62 | 63 | enqueue(&q, 12); 64 | enqueue(&q, 1); 65 | enqueue(&q, 116); 66 | enqueue(&q, 212); 67 | 68 | if (isEmpty(&q)) 69 | { 70 | printf("The Queue is Empty.\n"); 71 | } 72 | if (isFull(&q)) 73 | { 74 | printf("The Queue is Full.\n"); 75 | } 76 | 77 | return 0; 78 | } -------------------------------------------------------------------------------- /07_queue using array/01_queue_using_array.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/07_queue using array/01_queue_using_array.exe -------------------------------------------------------------------------------- /07_queue using array/QueueOperations.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Queue 5 | { 6 | int size; 7 | int f; 8 | int r; 9 | int *arr; 10 | }; 11 | int isFull(struct Queue *q) 12 | { 13 | if (q->r == q->size - 1) 14 | { 15 | return 1; 16 | } 17 | return 0; 18 | } 19 | int isEmpty(struct Queue *q) 20 | { 21 | if (q->f == q->r) 22 | { 23 | return 1; 24 | } 25 | return 0; 26 | } 27 | void enqueue(struct Queue *q, int value) 28 | { 29 | if (isFull(q)) 30 | { 31 | printf("\nQueue overflow !\n Cannot insert %d to the queue\n", value); 32 | } 33 | else 34 | { 35 | q->r++; 36 | q->arr[q->r] = value; 37 | printf("| %d ", value); 38 | } 39 | } 40 | 41 | int dequeue(struct Queue *q) 42 | { 43 | int a = -1; 44 | if (isEmpty(q)) 45 | { 46 | printf("The queue is empty.\n"); 47 | return 0; 48 | } 49 | else 50 | { 51 | q->f++; 52 | a = q->arr[q->f]; 53 | } 54 | return a; 55 | } 56 | int main() 57 | { 58 | struct Queue q; 59 | q.size = 10; 60 | q.f = q.r = -1; 61 | q.arr = (int *)malloc(q.size * sizeof(int)); 62 | 63 | // enqueue(&q, 12); 64 | // enqueue(&q, 1); 65 | // enqueue(&q, 116); 66 | // enqueue(&q, 212); 67 | 68 | // printf("\nThe Dequeued element is %d\n",dequeue(&q)); 69 | // printf("The Dequeued element is %d\n",dequeue(&q)); 70 | // printf("The Dequeued element is %d\n",dequeue(&q)); 71 | // printf("The Dequeued element is %d\n",dequeue(&q)); 72 | enqueue(&q, 312); 73 | enqueue(&q, 211); 74 | enqueue(&q, 16); 75 | enqueue(&q, 22); 76 | enqueue(&q, 152); 77 | enqueue(&q, 100); 78 | enqueue(&q, 567); 79 | enqueue(&q, 434); 80 | enqueue(&q, 512); 81 | enqueue(&q, 410); 82 | enqueue(&q, 1410); 83 | 84 | if (isEmpty(&q)) 85 | { 86 | printf("The Queue is Empty.\n"); 87 | } 88 | if (isFull(&q)) 89 | { 90 | printf("\nThe Queue is Full.\n"); 91 | } 92 | 93 | return 0; 94 | } -------------------------------------------------------------------------------- /07_queue using array/QueueOperations.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/07_queue using array/QueueOperations.exe -------------------------------------------------------------------------------- /08_circular queue using array/Dynamic_allocation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct circular_queue 5 | { 6 | int size; 7 | int f; 8 | int r; 9 | int *arr; 10 | }; 11 | 12 | int isEmpty(struct circular_queue *cq) 13 | { 14 | if (cq->r == cq->f) 15 | { 16 | return 1; 17 | } 18 | return 0; 19 | } 20 | int isFull(struct circular_queue *cq) 21 | { 22 | if ((cq->r + 1) % (cq->size)==cq->f) 23 | { 24 | return 1; 25 | } 26 | return 0; 27 | } 28 | void enqueue(struct circular_queue *cq, int value) 29 | { 30 | if (isFull(cq)) 31 | { 32 | printf("Queue overflow !\nThe circular queue is full.\n"); 33 | } 34 | else 35 | { 36 | cq->r = (cq->r + 1) % cq->size; 37 | cq->arr[cq->r] = value; 38 | printf("| %d |", value); 39 | } 40 | } 41 | int dequeue(struct circular_queue *cq) 42 | { 43 | int a = -1; 44 | if (isEmpty(cq)) 45 | { 46 | printf("Queue Underflow!\n The circular queue is empty\n"); 47 | } 48 | else 49 | { 50 | cq->f = (cq->f + 1) % cq->size; 51 | a = cq->arr[cq->f]; 52 | } 53 | } 54 | int main() 55 | { 56 | struct circular_queue *q; 57 | q=(struct circular_queue *)malloc(sizeof(struct circular_queue )); 58 | q->size = 1+15; 59 | q->f = q->r = 0; 60 | q->arr = (int *)malloc(q->size * sizeof(int)); 61 | 62 | enqueue(q, 1); 63 | enqueue(q, 2); 64 | enqueue(q, 3); 65 | enqueue(q, 4); 66 | enqueue(q, 5); 67 | printf("\ndequeued element is %d\n", dequeue(q)); 68 | printf("dequeued element is %d\n", dequeue(q)); 69 | printf("dequeued element is %d\n", dequeue(q)); 70 | enqueue(q, 6); 71 | enqueue(q, 7); 72 | enqueue(q, 8); 73 | enqueue(q, 9); 74 | enqueue(q, 10); 75 | enqueue(q, 11); 76 | enqueue(q, 12); 77 | printf("\ndequeued element is %d\n", dequeue(q)); 78 | printf("dequeued element is %d\n", dequeue(q)); 79 | 80 | if (isEmpty(q)) 81 | { 82 | printf("Queue is empty\n"); 83 | } 84 | if (isFull(q)) 85 | { 86 | printf("Queue is full\n"); 87 | } 88 | return 0; 89 | } -------------------------------------------------------------------------------- /08_circular queue using array/Dynamic_allocation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/08_circular queue using array/Dynamic_allocation.exe -------------------------------------------------------------------------------- /08_circular queue using array/circular_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct circular_queue 5 | { 6 | int size; 7 | int f; 8 | int r; 9 | int *arr; 10 | }; 11 | 12 | int isEmpty(struct circular_queue *cq) 13 | { 14 | if (cq->r == cq->f) 15 | { 16 | return 1; 17 | } 18 | return 0; 19 | } 20 | int isFull(struct circular_queue *cq) 21 | { 22 | if ((cq->r + 1) % (cq->size) == cq->f) 23 | { 24 | return 1; 25 | } 26 | return 0; 27 | } 28 | void enqueue(struct circular_queue *cq, int value) 29 | { 30 | if (isFull(cq)) 31 | { 32 | printf("Queue overflow !\nThe circular queue is full.\n"); 33 | } 34 | else 35 | { 36 | cq->r = (cq->r + 1) % cq->size; 37 | cq->arr[cq->r] = value; 38 | printf("| %d ", value); 39 | } 40 | } 41 | int dequeue(struct circular_queue *cq) 42 | { 43 | int a = -1; 44 | if (isEmpty(cq)) 45 | { 46 | printf("Queue Underflow!\n The circular queue is empty\n"); 47 | } 48 | else 49 | { 50 | cq->f = (cq->f + 1) % cq->size; 51 | a = cq->arr[cq->f]; 52 | } 53 | } 54 | int main() 55 | { 56 | struct circular_queue q; 57 | 58 | q.size = 1 + 15; 59 | q.f = q.r = 0; 60 | q.arr = (int *)malloc(q.size * sizeof(int)); 61 | 62 | enqueue(&q, 1); 63 | enqueue(&q, 2); 64 | enqueue(&q, 3); 65 | enqueue(&q, 4); 66 | enqueue(&q, 5); 67 | printf("\ndequeued element is %d\n", dequeue(&q)); 68 | printf("dequeued element is %d\n", dequeue(&q)); 69 | printf("dequeued element is %d\n", dequeue(&q)); 70 | enqueue(&q, 6); 71 | enqueue(&q, 7); 72 | enqueue(&q, 8); 73 | enqueue(&q, 9); 74 | enqueue(&q, 10); 75 | enqueue(&q, 11); 76 | enqueue(&q, 12); 77 | printf("\ndequeued element is %d\n", dequeue(&q)); 78 | printf("dequeued element is %d\n", dequeue(&q)); 79 | 80 | if (isEmpty(&q)) 81 | { 82 | printf("Queue is empty\n"); 83 | } 84 | if (isFull(&q)) 85 | { 86 | printf("Queue is full\n"); 87 | } 88 | return 0; 89 | } -------------------------------------------------------------------------------- /08_circular queue using array/circular_queue.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/08_circular queue using array/circular_queue.exe -------------------------------------------------------------------------------- /09_queue using linked list/Queue_using_LL.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node *f = NULL; 5 | struct node *r = NULL; 6 | 7 | struct node 8 | { 9 | int data; 10 | struct node *next; 11 | }; 12 | 13 | void Print(struct node *ptr) 14 | { 15 | while (ptr != NULL) 16 | { 17 | printf(" %d ", ptr->data); 18 | ptr = ptr->next; 19 | } 20 | } 21 | 22 | void enqueue(int value) 23 | { 24 | struct node *n = (struct node *)malloc(sizeof(struct node)); 25 | if (n == NULL) 26 | { 27 | printf("\nThe queue is Full\n"); 28 | } 29 | else 30 | { 31 | n->data = value; 32 | n->next = NULL; //because the element is added at the last position of the linked list 33 | if (f == NULL) 34 | { 35 | f = r = n; 36 | } 37 | else 38 | { 39 | r->next = n; 40 | r = n; 41 | } 42 | } 43 | } 44 | 45 | int dequeue() 46 | { 47 | int value = -1; 48 | struct node *ptr=f; 49 | if (f == NULL) 50 | { 51 | printf("The queue is Empty\n"); 52 | } 53 | else 54 | { 55 | f = f->next; 56 | value = ptr->data; 57 | free(ptr); 58 | } 59 | return value; 60 | } 61 | int main() 62 | { 63 | Print(f); 64 | enqueue(8); 65 | enqueue(9); 66 | enqueue(10); 67 | enqueue(11); 68 | enqueue(12); 69 | Print(f); 70 | printf("\nDequeued element is %d\n",dequeue()); 71 | printf("Dequeued element is %d\n",dequeue()); 72 | printf("Dequeued element is %d\n",dequeue()); 73 | Print(f); 74 | return 0; 75 | } -------------------------------------------------------------------------------- /09_queue using linked list/Queue_using_LL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/09_queue using linked list/Queue_using_LL.exe -------------------------------------------------------------------------------- /10_doubly ended queue/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "stdio.h": "c" 4 | } 5 | } -------------------------------------------------------------------------------- /10_doubly ended queue/DE_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct DE_queue 5 | { 6 | int size; 7 | int f; 8 | int r; 9 | int *arr; 10 | }; 11 | 12 | int isEmpty(struct DE_queue *q) 13 | { 14 | if (q->f == q->r) 15 | return 1; 16 | return 0; 17 | } 18 | 19 | int isFull(struct DE_queue *q) 20 | { 21 | if (q->r == q->size - 1) 22 | return 1; 23 | return 0; 24 | } 25 | 26 | void enqueueF(struct DE_queue *q, int value) 27 | { 28 | if (isFull(q)) 29 | { 30 | printf("The queue is Full\n"); 31 | } 32 | else 33 | { 34 | if (q->f == -1) 35 | { 36 | printf("cannot insert %d \n", value); 37 | } 38 | else 39 | { 40 | q->arr[q->f] = value; 41 | q->f--; 42 | // printf("| %d |", value); 43 | } 44 | } 45 | } 46 | 47 | void enqueueR(struct DE_queue *q, int value) 48 | { 49 | if (isFull(q)) 50 | { 51 | printf("The queue is Full\n"); 52 | } 53 | else 54 | { 55 | q->r++; 56 | q->arr[q->r] = value; 57 | // printf("| %d |", value); 58 | } 59 | } 60 | 61 | int dequeueF(struct DE_queue *q) 62 | { 63 | int a = -1; 64 | if (isEmpty(q)) 65 | { 66 | printf("The queue is empty\n"); 67 | } 68 | else 69 | { 70 | q->f++; 71 | a = q->arr[q->f]; 72 | } 73 | return a; 74 | } 75 | 76 | int dequeueR(struct DE_queue *q) 77 | { 78 | int a = -1; 79 | if (isEmpty(q)) 80 | { 81 | printf("The queue is empty\n"); 82 | } 83 | else 84 | { 85 | a = q->arr[q->r]; 86 | q->r--; 87 | } 88 | return a; 89 | } 90 | 91 | void print(struct DE_queue *q) 92 | { 93 | if (isEmpty(q)) 94 | { 95 | printf("\nThe queue is empty\n"); 96 | } 97 | else 98 | { 99 | printf("\nThe queue is : "); 100 | for (int i = (q->f + 1); i <= q->r; i++) 101 | { 102 | printf(" %d ", q->arr[i]); 103 | } 104 | } 105 | } 106 | 107 | int main() 108 | { 109 | struct DE_queue q; 110 | q.size = 20; 111 | q.f = q.r = -1; 112 | q.arr = (int *)malloc(q.size * sizeof(int)); 113 | enqueueR(&q, 18); 114 | enqueueR(&q, 8); 115 | enqueueR(&q, 14); 116 | enqueueR(&q, 33); 117 | 118 | print(&q); //printing the queue 119 | 120 | printf("\nDequeued element from front is %d\n", dequeueF(&q)); 121 | printf("Dequeued element from front is %d\n", dequeueF(&q)); 122 | printf("Dequeued element from front is %d\n", dequeueF(&q)); 123 | 124 | print(&q); //printing the queue 125 | 126 | enqueueF(&q, 15); 127 | enqueueF(&q, 29); 128 | 129 | print(&q); //printing the queue 130 | 131 | printf("\nDequeued element from Rear is %d\n", dequeueR(&q)); 132 | printf("Dequeued element from Rear is %d\n", dequeueR(&q)); 133 | 134 | print(&q); //printing the queue 135 | 136 | enqueueR(&q, 15); 137 | enqueueR(&q, 29); 138 | 139 | print(&q); //printing the queue 140 | 141 | printf("\nDequeued element from front is %d\n", dequeueF(&q)); 142 | printf("Dequeued element from front is %d\n", dequeueF(&q)); 143 | printf("Dequeued element from front is %d\n", dequeueF(&q)); 144 | 145 | print(&q); //printing the queue 146 | return 0; 147 | } -------------------------------------------------------------------------------- /10_doubly ended queue/DE_queue.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/10_doubly ended queue/DE_queue.exe -------------------------------------------------------------------------------- /10_doubly ended queue/reference.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Queue 5 | { 6 | int size; 7 | int f; 8 | int r; 9 | int *arr; 10 | }; 11 | int isFull(struct Queue *q) 12 | { 13 | if (q->r == q->size - 1) 14 | { 15 | return 1; 16 | } 17 | return 0; 18 | } 19 | int isEmpty(struct Queue *q) 20 | { 21 | if (q->f == q->r) 22 | { 23 | return 1; 24 | } 25 | return 0; 26 | } 27 | void enqueue(struct Queue *q, int value) 28 | { 29 | if (isFull(q)) 30 | { 31 | printf("\nQueue overflow !\n Cannot insert %d to the queue\n", value); 32 | } 33 | else 34 | { 35 | q->r++; 36 | q->arr[q->r] = value; 37 | printf("| %d ", value); 38 | } 39 | } 40 | 41 | int dequeue(struct Queue *q) 42 | { 43 | int a = -1; 44 | if (isEmpty(q)) 45 | { 46 | printf("The queue is empty.\n"); 47 | return 0; 48 | } 49 | else 50 | { 51 | q->f++; 52 | a = q->arr[q->f]; 53 | } 54 | return a; 55 | } 56 | int main() 57 | { 58 | struct Queue q; 59 | q.size = 10; 60 | q.f = q.r = -1; 61 | q.arr = (int *)malloc(q.size * sizeof(int)); 62 | 63 | // enqueue(&q, 12); 64 | // enqueue(&q, 1); 65 | // enqueue(&q, 116); 66 | // enqueue(&q, 212); 67 | 68 | // printf("\nThe Dequeued element is %d\n",dequeue(&q)); 69 | // printf("The Dequeued element is %d\n",dequeue(&q)); 70 | // printf("The Dequeued element is %d\n",dequeue(&q)); 71 | // printf("The Dequeued element is %d\n",dequeue(&q)); 72 | enqueue(&q, 312); 73 | enqueue(&q, 211); 74 | enqueue(&q, 16); 75 | enqueue(&q, 22); 76 | enqueue(&q, 152); 77 | enqueue(&q, 100); 78 | enqueue(&q, 567); 79 | enqueue(&q, 434); 80 | enqueue(&q, 512); 81 | enqueue(&q, 410); 82 | enqueue(&q, 1410); 83 | 84 | if (isEmpty(&q)) 85 | { 86 | printf("The Queue is Empty.\n"); 87 | } 88 | if (isFull(&q)) 89 | { 90 | printf("\nThe Queue is Full.\n"); 91 | } 92 | 93 | return 0; 94 | } -------------------------------------------------------------------------------- /11_Sorting Algorithms/01_BubbleSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void PrintArray(int *A, int n) 4 | { 5 | for (int i = 0; i < n; i++) 6 | { 7 | printf(" %d ", A[i]); 8 | } 9 | printf("\n"); 10 | } 11 | 12 | void BubbleSort(int *A, int n) 13 | { 14 | int temp; 15 | for (int i = 0; i < n - 1; i++) // this loop for number of passes 16 | { 17 | for (int j = 0; j < n - 1 - i; j++) // this loop for number of swaps 18 | { 19 | if (A[j] > A[j + 1]) 20 | { 21 | temp=A[j]; 22 | A[j]=A[j+1]; 23 | A[j+1]=temp; 24 | } 25 | } 26 | } 27 | } 28 | int main() 29 | { 30 | int A[] = {11, 23, 56, 54, 32, 98, 65, 234, 1, 12, 34}; 31 | int n = 11; 32 | printf("Array before Sorting\n"); 33 | PrintArray(A, n); 34 | BubbleSort(A, n); 35 | printf("Array after Sorting\n"); 36 | PrintArray(A, n); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /11_Sorting Algorithms/01_BubbleSort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/11_Sorting Algorithms/01_BubbleSort.exe -------------------------------------------------------------------------------- /11_Sorting Algorithms/01_BubbleSort_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void PrintArray(int *a, int n) 4 | { 5 | for (int i = 0; i < n; i++) 6 | { 7 | printf(" %d ", a[i]); 8 | } 9 | printf("\n"); 10 | } 11 | 12 | void BubbleSortAdaptive(int *a, int n) 13 | { 14 | 15 | int temp; 16 | int IsSorted = 0; 17 | for (int i = 0; i < n - 1; i++) // this loop for number of passes 18 | { 19 | printf("Working on pass number %d \n", i + 1); 20 | IsSorted = 1; 21 | for (int j = 0; j < n - 1 - i; j++) // this loop for number of swaps 22 | { 23 | if (a[j] > a[j + 1]) 24 | { 25 | temp = a[j]; 26 | a[j] = a[j + 1]; 27 | a[j + 1] = temp; 28 | IsSorted = 0; 29 | } 30 | } 31 | if (IsSorted) 32 | { 33 | return; 34 | } 35 | } 36 | } 37 | int main() 38 | { 39 | int a[] = {1, 2, 3, 4, 45, 67, 68, 89}; 40 | int n = 8; 41 | printf("Array before sorting\n"); 42 | PrintArray(a, n); 43 | BubbleSortAdaptive(a, n); 44 | printf("Array after sorting\n"); 45 | PrintArray(a, n); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /11_Sorting Algorithms/01_BubbleSort_2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/11_Sorting Algorithms/01_BubbleSort_2.exe -------------------------------------------------------------------------------- /11_Sorting Algorithms/02_InsertionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void PrintArray(int *a, int n) 4 | { 5 | for (int i = 0; i < n; i++) 6 | { 7 | printf(" %d ", a[i]); 8 | } 9 | printf("\n"); 10 | } 11 | 12 | void insertionSort(int *a, int n) 13 | { 14 | int key; 15 | int j; 16 | for (int i = 1; i <= n - 1; i++) 17 | { 18 | key = a[i]; 19 | j = i - 1; 20 | while (j >= 0 && a[j] > key) 21 | { 22 | a[j + 1] = a[j]; 23 | j--; 24 | } 25 | a[j + 1] = key; 26 | } 27 | } 28 | int main() 29 | { 30 | int a[] = {12, 143, 21, 134, 65, 212, 76, 43, 65, 123, 456, 653, 578, 431, 6, 3412, 7, 6543, 67, 563, 67, 7895, 54,634 , 56, 67, 462, 633, 642, 89}; 31 | int n = 35; 32 | printf("Array before sorting\n"); 33 | PrintArray(a, n); 34 | insertionSort(a, n); 35 | printf("Array after sorting\n"); 36 | PrintArray(a, n); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /11_Sorting Algorithms/02_InsertionSort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/11_Sorting Algorithms/02_InsertionSort.exe -------------------------------------------------------------------------------- /11_Sorting Algorithms/02_selectionSort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/11_Sorting Algorithms/02_selectionSort.exe -------------------------------------------------------------------------------- /11_Sorting Algorithms/03_selectionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void PrintArray(int *a, int n) 4 | { 5 | for (int i = 0; i < n; i++) 6 | { 7 | printf(" %d ", a[i]); 8 | } 9 | printf("\n"); 10 | } 11 | 12 | void SelectionSort(int *a, int n) 13 | { 14 | printf("Running selection sort.....\n"); 15 | int indexofMin, temp; 16 | for (int i = 0; i < n - 1; i++) 17 | { 18 | indexofMin = i; 19 | for (int j = i + 1; j < n; j++) 20 | { 21 | if (a[j] < a[indexofMin]) 22 | { 23 | indexofMin = j; 24 | } 25 | temp = a[i]; 26 | a[i] = a[indexofMin]; 27 | a[indexofMin] = temp; 28 | } 29 | } 30 | } 31 | int main() 32 | { 33 | // 00 01 02 03 04 05 06 07 08 09 ---->index of array 34 | // 32, 01, 54, 12, 56, 23, 78, 54, 90, 11 ----> input array 35 | int a[] = {32, 1, 54, 12, 56, 23, 78, 54, 90, 11}; 36 | int n = 10; 37 | PrintArray(a, n); 38 | SelectionSort(a, n); 39 | PrintArray(a, n); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /11_Sorting Algorithms/03_selectionSort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/11_Sorting Algorithms/03_selectionSort.exe -------------------------------------------------------------------------------- /11_Sorting Algorithms/04_QuickSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void PrintArray(int *a, int n) 4 | { 5 | for (int i = 0; i < n; i++) 6 | { 7 | printf(" %d ", a[i]); 8 | } 9 | printf("\n"); 10 | } 11 | 12 | int partition(int a[], int low, int high) 13 | { 14 | int pivot = a[low]; 15 | int i = low + 1; 16 | int j = high; 17 | int temp; 18 | do 19 | { 20 | while (a[i] <= pivot) 21 | { 22 | i++; 23 | } 24 | while (a[j] > pivot) 25 | { 26 | j--; 27 | } 28 | if (i < j) 29 | { 30 | temp = a[i]; 31 | a[i] = a[j]; 32 | a[j] = temp; 33 | } 34 | 35 | } while (i < j); 36 | temp = a[low]; 37 | a[low] = a[j]; 38 | a[j] = temp; 39 | return j; 40 | } 41 | 42 | void QuickSort(int a[], int low, int high) 43 | { 44 | 45 | int partitionIndex; 46 | if (low < high) 47 | { 48 | PrintArray(a, 10); 49 | partitionIndex = partition(a, low, high); 50 | QuickSort(a, low, partitionIndex - 1); 51 | QuickSort(a, partitionIndex + 1, high); 52 | } 53 | } 54 | int main() 55 | { 56 | int a[] = {10, 2, 5, 9, 6, 3, 1, 8, 4, 7}; 57 | int n = 10; 58 | PrintArray(a, n); 59 | printf("Performing QuickSort.....\n"); 60 | QuickSort(a, 0, n - 1); 61 | PrintArray(a, n); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /11_Sorting Algorithms/04_QuickSort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/11_Sorting Algorithms/04_QuickSort.exe -------------------------------------------------------------------------------- /11_Sorting Algorithms/05_MergeSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void PrintArray(int *a, int n) 4 | { 5 | printf("\n"); 6 | for (int i = 0; i < n; i++) 7 | { 8 | printf(" %d ", a[i]); 9 | } 10 | printf("\n\n"); 11 | } 12 | 13 | void merge(int *a, int mid, int low, int high) 14 | { 15 | int i = low; 16 | int j = mid + 1; 17 | int k = low; 18 | int b[high + 1]; 19 | while (i <= mid && j <= high) 20 | { 21 | if (a[i] < a[j]) 22 | { 23 | b[k] = a[i]; 24 | i++; 25 | k++; 26 | } 27 | else 28 | { 29 | b[k] = a[j]; 30 | j++; 31 | k++; 32 | } 33 | } 34 | while (i <= mid) 35 | { 36 | b[k] = a[i]; 37 | k++; 38 | i++; 39 | } 40 | 41 | while (j <= high) 42 | { 43 | b[k] = a[j]; 44 | k++; 45 | j++; 46 | } 47 | for (int i = low; i <= high; i++) 48 | { 49 | a[i] = b[i]; 50 | } 51 | } 52 | 53 | void MergeSort(int a[], int low, int high) 54 | { 55 | int mid; 56 | if (low < high) 57 | { 58 | // PrintArray(a, 10); 59 | mid = (low + high) / 2; 60 | MergeSort(a, low, mid); 61 | MergeSort(a, mid + 1, high); 62 | merge(a, mid, low, high); 63 | } 64 | } 65 | 66 | int main() 67 | { 68 | int a[] = {10, 2, 5, 9, 6, 3, 1, 8, 4, 7}; 69 | int n = 10; 70 | PrintArray(a, n); 71 | printf("Performing Merge Sort........\n"); 72 | MergeSort(a, 0, 9); 73 | PrintArray(a, n); 74 | return 0; 75 | } -------------------------------------------------------------------------------- /11_Sorting Algorithms/05_MergeSort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/11_Sorting Algorithms/05_MergeSort.exe -------------------------------------------------------------------------------- /11_Sorting Algorithms/06_CountSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void PrintArray(int *a, int n) 5 | { 6 | for (int i = 0; i < n; i++) 7 | { 8 | printf(" %d ", a[i]); 9 | } 10 | printf("\n"); 11 | } 12 | 13 | int maximium(int a[], int n) 14 | { 15 | int max = INT_MIN; 16 | for (int i = 0; i < n; i++) 17 | { 18 | if (max < a[i]) 19 | { 20 | max = a[i]; 21 | } 22 | } 23 | return max; 24 | } 25 | 26 | void CountSort(int *a, int n) 27 | { 28 | int i, j; 29 | 30 | //finding the maximum element in array a 31 | int max = maximium(a, n); 32 | 33 | //creating the count array 34 | int *count = (int *)malloc((max + 1) * sizeof(int)); 35 | 36 | //initialize all elements in count array to zero 37 | for (i = 0; i < max + 1; i++) 38 | { 39 | count[i] = 0; 40 | } 41 | 42 | //increment the corresponding index in array by one 43 | for (i = 0; i < n; i++) 44 | { 45 | count[a[i]] = count[a[i]] + 1; 46 | } 47 | i = 0; //counter for count array 48 | j = 0; //counter for given array 49 | 50 | while (i <= max) 51 | { 52 | if (count[i] > 0) 53 | { 54 | a[j] = i; 55 | count[i] = count[i] - 1; 56 | j++; 57 | } 58 | else 59 | { 60 | i++; 61 | } 62 | } 63 | } 64 | 65 | int main() 66 | { 67 | int a[] = {10, 2, 5, 9, 6, 3, 1, 8, 4, 7}; 68 | int n = 10; 69 | PrintArray(a, n); 70 | printf("Performing Count Sort........\n"); 71 | CountSort(a, n); 72 | PrintArray(a, n); 73 | return 0; 74 | } -------------------------------------------------------------------------------- /11_Sorting Algorithms/06_CountSort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/11_Sorting Algorithms/06_CountSort.exe -------------------------------------------------------------------------------- /12_Trees and binary trees/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "stdio.h": "c" 4 | }, 5 | "editor.fontLigatures": true, 6 | "editor.mouseWheelZoom": true, 7 | "code-runner.runInTerminal": true, 8 | "code-runner.saveAllFilesBeforeRun": true, 9 | "code-runner.saveFileBeforeRun": true, 10 | } -------------------------------------------------------------------------------- /12_Trees and binary trees/01_PreOrder.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *left; 8 | struct node *right; 9 | }; 10 | 11 | struct node* CreateNode(int data) 12 | { 13 | struct node *n; 14 | n = (struct node *)malloc(sizeof(struct node)); 15 | n->data = data; 16 | n->left = NULL; 17 | n->right = NULL; 18 | return n; 19 | } 20 | 21 | void PreOrder(struct node *root) 22 | { 23 | if (root != NULL) 24 | { 25 | printf(" %d ", root->data); 26 | PreOrder(root->left); 27 | PreOrder(root->right); 28 | } 29 | } 30 | int main() 31 | { 32 | struct node *p = CreateNode(2); 33 | struct node *p1 = CreateNode(6); 34 | struct node *p2 = CreateNode(3); 35 | struct node *p3 = CreateNode(9); 36 | struct node *p4 = CreateNode(8); 37 | struct node *p5 = CreateNode(5); 38 | struct node *p6 = CreateNode(23); 39 | 40 | p->left = p1; 41 | p->right = p2; 42 | 43 | p1->left = p3; 44 | p1->right = p4; 45 | 46 | p2->left = p5; 47 | p2->right = p6; 48 | 49 | PreOrder(p); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /12_Trees and binary trees/01_PreOrder.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/12_Trees and binary trees/01_PreOrder.exe -------------------------------------------------------------------------------- /12_Trees and binary trees/02_PostOrder.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *left; 8 | struct node *right; 9 | }; 10 | 11 | struct node *CreateNode(int data) 12 | { 13 | struct node *n; 14 | n = (struct node *)malloc(sizeof(struct node)); 15 | n->data = data; 16 | n->left = NULL; 17 | n->right = NULL; 18 | return n; 19 | } 20 | 21 | void PostOrder(struct node *root) 22 | { 23 | if (root != NULL) 24 | { 25 | PostOrder(root->left); 26 | PostOrder(root->right); 27 | printf(" %d ", root->data); 28 | } 29 | } 30 | int main() 31 | { 32 | struct node *p = CreateNode(2); 33 | struct node *p1 = CreateNode(6); 34 | struct node *p2 = CreateNode(3); 35 | struct node *p3 = CreateNode(9); 36 | struct node *p4 = CreateNode(8); 37 | struct node *p5 = CreateNode(5); 38 | struct node *p6 = CreateNode(22); 39 | 40 | p->left = p1; 41 | p->right = p2; 42 | 43 | p1->left = p3; 44 | p1->right = p4; 45 | 46 | p2->left = p5; 47 | p2->right = p6; 48 | 49 | PostOrder(p); 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /12_Trees and binary trees/02_PostOrder.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/12_Trees and binary trees/02_PostOrder.exe -------------------------------------------------------------------------------- /12_Trees and binary trees/03_InOrder.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *left; 8 | struct node *right; 9 | }; 10 | 11 | struct node *CreateNode(int data) 12 | { 13 | struct node *n; 14 | n = (struct node *)malloc(sizeof(struct node)); 15 | n->data = data; 16 | n->left = NULL; 17 | n->right = NULL; 18 | return n; 19 | } 20 | 21 | void InOrder(struct node *root) 22 | { 23 | if (root != NULL) 24 | { 25 | InOrder(root->left); 26 | printf(" %d ", root->data); 27 | InOrder(root->right); 28 | } 29 | } 30 | int main() 31 | { 32 | struct node *p = CreateNode(2); 33 | struct node *p1 = CreateNode(6); 34 | struct node *p2 = CreateNode(3); 35 | struct node *p3 = CreateNode(9); 36 | struct node *p4 = CreateNode(8); 37 | struct node *p5 = CreateNode(5); 38 | struct node *p6 = CreateNode(23); 39 | 40 | p->left = p1; 41 | p->right = p2; 42 | 43 | p1->left = p3; 44 | p1->right = p4; 45 | 46 | p2->left = p5; 47 | p2->right = p6; 48 | 49 | InOrder(p); 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /12_Trees and binary trees/03_InOrder.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/12_Trees and binary trees/03_InOrder.exe -------------------------------------------------------------------------------- /12_Trees and binary trees/10_INSERT.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *left; 8 | struct node *right; 9 | }; 10 | 11 | struct node *CreateNode(int data) 12 | { 13 | struct node *n; 14 | n = (struct node *)malloc(sizeof(struct node)); 15 | n->data = data; 16 | n->left = NULL; 17 | n->right = NULL; 18 | return n; 19 | } 20 | void InOrder(struct node * root){ 21 | if (root!=NULL) 22 | { 23 | InOrder(root->left); 24 | printf(" %d ",root->data); 25 | InOrder(root->right); 26 | } 27 | 28 | } 29 | 30 | void Insert(struct node *root, int key) 31 | { 32 | struct node *prev = NULL; 33 | while (root != NULL) 34 | { 35 | prev = root; 36 | if (key == root->data) 37 | { 38 | printf("Cannot Insert %d, Already exists in the tree\n", key); 39 | return; 40 | } 41 | else if (key < root->data) 42 | { 43 | root = root->left; 44 | } 45 | else 46 | { 47 | root = root->right; 48 | } 49 | } 50 | struct node *new = CreateNode(key); 51 | if (key < prev->data) 52 | { 53 | prev->left = new; 54 | } 55 | else 56 | { 57 | prev->right = new; 58 | } 59 | } 60 | int main() 61 | { 62 | // Constructing the root node - Using Function (Recommended) 63 | struct node *p = CreateNode(5); 64 | struct node *p1 = CreateNode(3); 65 | struct node *p2 = CreateNode(6); 66 | struct node *p3 = CreateNode(1); 67 | struct node *p4 = CreateNode(4); 68 | // Finally The tree looks like this: 69 | // 5 70 | // / \ 71 | // 3 6 72 | // / \ 73 | // 1 4 74 | 75 | // Linking the root node with left and right children 76 | p->left = p1; 77 | p->right = p2; 78 | p1->left = p3; 79 | p1->right = p4; 80 | 81 | Insert(p, 2); 82 | // printf("%d", p->right->right->data); 83 | InOrder(p); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /12_Trees and binary trees/10_INSERT.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/12_Trees and binary trees/10_INSERT.exe -------------------------------------------------------------------------------- /12_Trees and binary trees/11_DELETE.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *left; 8 | struct node *right; 9 | }; 10 | 11 | struct node *CreateNode(int data) 12 | { 13 | struct node *n; 14 | n = (struct node *)malloc(sizeof(struct node)); 15 | n->data = data; 16 | n->left = NULL; 17 | n->right = NULL; 18 | return n; 19 | } 20 | void InOrder(struct node *root) 21 | { 22 | if (root != NULL) 23 | { 24 | InOrder(root->left); 25 | printf(" %d ", root->data); 26 | InOrder(root->right); 27 | } 28 | } 29 | struct node *InOrderPredecessor(struct node *root) 30 | { 31 | root = root->left; 32 | while (root->right != NULL) 33 | { 34 | root = root->right; 35 | } 36 | return root; 37 | } 38 | struct node *delete (struct node *root, int key) 39 | { 40 | struct node *ipre; 41 | if (root == NULL) 42 | { 43 | return NULL; 44 | } 45 | if (root->left == NULL && root->right == NULL) 46 | { 47 | free(root); 48 | return NULL; 49 | } 50 | if (key < root->data) 51 | { 52 | root->left = delete (root->left, key); 53 | } 54 | else if (key > root->data) 55 | { 56 | root->right = delete (root->right, key); 57 | } 58 | else 59 | { 60 | ipre = InOrderPredecessor(root); 61 | root->data = ipre->data; 62 | root->left = delete (root->left, ipre->data); 63 | } 64 | } 65 | int main() 66 | { 67 | struct node *p0 = CreateNode(50); 68 | struct node *p1 = CreateNode(30); 69 | struct node *p2 = CreateNode(80); 70 | struct node *p3 = CreateNode(20); 71 | struct node *p4 = CreateNode(40); 72 | struct node *p5 = CreateNode(70); 73 | struct node *p6 = CreateNode(90); 74 | // struct node *p0=CreateNode(50); 75 | 76 | // 50 77 | // / \ 78 | // 30 80 79 | // / \ / \ 80 | // 20 40 70 90 81 | 82 | p0->left = p1; 83 | p0->right = p2; 84 | 85 | p1->left = p3; 86 | p1->right = p4; 87 | 88 | p2->left = p5; 89 | p2->right = p6; 90 | printf("\nPerforming InOrder traversal.........\n\n"); 91 | InOrder(p0); 92 | printf("\nThe root node is %d\n",p0->data); 93 | printf("\n"); 94 | delete(p0,50); 95 | InOrder(p0); 96 | printf("\nThe root node is %d\n",p0->data); 97 | 98 | // 50 99 | // / \ 100 | // 30 80 101 | // / \ / \ 102 | // 20 40 70 90 103 | 104 | return 0; 105 | } -------------------------------------------------------------------------------- /12_Trees and binary trees/11_DELETE.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/12_Trees and binary trees/11_DELETE.exe -------------------------------------------------------------------------------- /12_Trees and binary trees/BST.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *left; 8 | struct node *right; 9 | }; 10 | 11 | struct node *CreateNode(int data) 12 | { 13 | struct node *n; 14 | n = (struct node *)malloc(sizeof(struct node)); 15 | n->data = data; 16 | n->left = NULL; 17 | n->right = NULL; 18 | return n; 19 | } 20 | 21 | void InOrder(struct node *root) 22 | { 23 | if (root != NULL) 24 | { 25 | InOrder(root->left); 26 | printf(" %d ", root->data); 27 | InOrder(root->right); 28 | } 29 | } 30 | 31 | int IsBST(struct node *root) 32 | { 33 | static struct node *prev = NULL; 34 | if (root != NULL) 35 | { 36 | if (!IsBST(root->left)) 37 | { 38 | return 0; 39 | } 40 | if (prev != NULL && root->data <= prev->data) 41 | { 42 | return 0; 43 | } 44 | prev = root; 45 | return IsBST(root->right); 46 | } 47 | else 48 | { 49 | return 1; 50 | } 51 | } 52 | 53 | int main() 54 | { 55 | struct node *p0 = CreateNode(9); 56 | struct node *p1 = CreateNode(4); 57 | struct node *p2 = CreateNode(11); 58 | struct node *p3 = CreateNode(2); 59 | struct node *p4 = CreateNode(7); 60 | struct node *p5 = CreateNode(15); 61 | struct node *p6 = CreateNode(5); 62 | struct node *p7 = CreateNode(8); 63 | struct node *p8 = CreateNode(14); 64 | 65 | p0->left = p1; 66 | p0->right = p2; 67 | 68 | p1->left = p3; 69 | p1->right = p4; 70 | 71 | p2->left = NULL; 72 | p2->right = p5; 73 | 74 | p4->left = p6; 75 | p4->right = p7; 76 | 77 | p5->left = p8; 78 | p5->right = NULL; 79 | 80 | printf("Performing InOrder traversal...........\n"); 81 | InOrder(p0); 82 | printf("\n"); 83 | if (IsBST(p0)) 84 | printf("The given tree is a Binary search tree\n"); 85 | else 86 | printf("The given tree is Not a Binary search tree\n"); 87 | 88 | return 0; 89 | } -------------------------------------------------------------------------------- /12_Trees and binary trees/BST.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/12_Trees and binary trees/BST.exe -------------------------------------------------------------------------------- /12_Trees and binary trees/BST_iterative_search.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *left; 8 | struct node *right; 9 | }; 10 | 11 | struct node *CreateNode(int data) 12 | { 13 | struct node *n; 14 | n=(struct node *)malloc(sizeof(struct node)); 15 | n->data = data; 16 | n->left = NULL; 17 | n->right = NULL; 18 | return n; 19 | } 20 | struct node *IterSearch(struct node *root, int key) 21 | { 22 | while (root != NULL) 23 | { 24 | if (key == root->data) 25 | { 26 | return root; 27 | } 28 | else if (key < root->data) 29 | { 30 | root = root->left; 31 | } 32 | else 33 | { 34 | root = root->right; 35 | } 36 | } 37 | return NULL; 38 | } 39 | int main() 40 | { 41 | struct node *p0 = CreateNode(50); 42 | struct node *p1 = CreateNode(30); 43 | struct node *p2 = CreateNode(80); 44 | struct node *p3 = CreateNode(20); 45 | struct node *p4 = CreateNode(40); 46 | struct node *p5 = CreateNode(70); 47 | struct node *p6 = CreateNode(90); 48 | // struct node *p0=CreateNode(50); 49 | 50 | p0->left = p1; 51 | p0->right = p2; 52 | 53 | p1->left = p3; 54 | p1->right = p4; 55 | 56 | p2->left = p5; 57 | p2->right = p6; 58 | // printf("\nPerforming InOrder traversal.........\n\n"); 59 | // InOrder(p0); 60 | 61 | struct node *n = IterSearch(p0, 30); 62 | if (n != NULL) 63 | { 64 | printf("\nCongrulation :) \n Element found - %d \n", n->data); 65 | } 66 | else 67 | printf("\nElement not found :( \n"); 68 | return 0; 69 | } -------------------------------------------------------------------------------- /12_Trees and binary trees/BST_iterative_search.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/12_Trees and binary trees/BST_iterative_search.exe -------------------------------------------------------------------------------- /12_Trees and binary trees/BST_search.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *left; 8 | struct node *right; 9 | }; 10 | 11 | struct node *CreateNode(int data) 12 | { 13 | struct node *n; 14 | n = (struct node *)malloc(sizeof(struct node)); 15 | n->data = data; 16 | n->left = NULL; 17 | n->right = NULL; 18 | return n; 19 | } 20 | 21 | void InOrder(struct node *root) 22 | { 23 | if (root != NULL) 24 | { 25 | InOrder(root->left); 26 | printf(" %d ", root->data); 27 | InOrder(root->right); 28 | } 29 | 30 | } 31 | 32 | struct node *search(struct node *root, int key) 33 | { 34 | if (root == NULL) 35 | { 36 | return NULL; 37 | } 38 | if (key == root->data) 39 | { 40 | return root; 41 | } 42 | else if (key < root->data) 43 | { 44 | return search(root->left, key); 45 | } 46 | else 47 | { 48 | return search(root->right, key); 49 | } 50 | } 51 | int main() 52 | { 53 | struct node *p0 = CreateNode(50); 54 | struct node *p1 = CreateNode(30); 55 | struct node *p2 = CreateNode(80); 56 | struct node *p3 = CreateNode(20); 57 | struct node *p4 = CreateNode(40); 58 | struct node *p5 = CreateNode(70); 59 | struct node *p6 = CreateNode(90); 60 | // struct node *p0=CreateNode(50); 61 | 62 | p0->left = p1; 63 | p0->right = p2; 64 | 65 | p1->left = p3; 66 | p1->right = p4; 67 | 68 | p2->left = p5; 69 | p2->right = p6; 70 | printf("\nPerforming InOrder traversal.........\n\n"); 71 | InOrder(p0); 72 | 73 | struct node *n=search(p0,300); 74 | if (n!=NULL) 75 | { 76 | printf("\nCongrulation :) \n Element found - %d \n",n->data); 77 | } 78 | else 79 | printf("\nElement not found :( \n"); 80 | return 0; 81 | } -------------------------------------------------------------------------------- /12_Trees and binary trees/BST_search.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/12_Trees and binary trees/BST_search.exe -------------------------------------------------------------------------------- /12_Trees and binary trees/binaryTree_01.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *left; 8 | struct node *right; 9 | }; 10 | 11 | int main() 12 | { 13 | struct node *p; 14 | p = (struct node *)malloc(sizeof(struct node)); 15 | p->data = 3; 16 | p->left = NULL; 17 | p->right = NULL; 18 | 19 | struct node *p1; 20 | p1 = (struct node *)malloc(sizeof(struct node)); 21 | p1->data = 6; 22 | p1->left = NULL; 23 | p1->right = NULL; 24 | 25 | struct node *p2; 26 | p2 = (struct node *)malloc(sizeof(struct node)); 27 | p2->data = 7; 28 | p2->left = NULL; 29 | p2->right = NULL; 30 | 31 | p->left = p1; 32 | p->right = p2; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /12_Trees and binary trees/binaryTree_01.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/12_Trees and binary trees/binaryTree_01.exe -------------------------------------------------------------------------------- /12_Trees and binary trees/binaryTree_02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //creating a binary tree in c language 5 | //recommended to use this method 6 | 7 | struct node 8 | { 9 | int data; 10 | struct node *left; 11 | struct node *right; 12 | }; 13 | 14 | struct node *CreateNode(int data) 15 | { 16 | struct node *n; 17 | n = (struct node *)malloc(sizeof(struct node)); 18 | n->data = data; 19 | n->left = NULL; 20 | n->right = NULL; 21 | return n; 22 | } 23 | int main() 24 | { 25 | struct node *p = CreateNode(6); 26 | struct node *p1 = CreateNode(7); 27 | struct node *p2 = CreateNode(70); 28 | struct node *p3 = CreateNode(91); 29 | struct node *p4 = CreateNode(21); 30 | struct node *p5 = CreateNode(26); 31 | 32 | p->left = p1; 33 | p->right = p2; 34 | 35 | p1->left = p3; 36 | p1->right = p4; 37 | 38 | p2->left = NULL; 39 | p2->right = p5; 40 | 41 | 42 | 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /12_Trees and binary trees/binaryTree_02.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushSoni86/DSA-with-c-programming/6bc0f2bf301faf2a7516d45afa0b7e857241be8b/12_Trees and binary trees/binaryTree_02.exe -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DSA-with-c-programming 2 | --------------------------------------------------------------------------------