├── .vscode └── settings.json ├── Arrays ├── binarySearch.c ├── compareStrings.c ├── diagonalMatrix.cpp ├── duplicatesUnsorted.c ├── insertElement.c ├── leftshift.c ├── lowerTriangleRowMajor.cpp ├── merge.c ├── missingElementsFasterMethod.c ├── multipleMissing.c ├── palindromeString.c ├── permuationOfStringBacktracking.c ├── reverseString.c ├── singleMissingElement.c ├── sparseMatrix.c └── validateString.c ├── Graphs ├── BFS-DFS ├── BFS-DFS.c └── Queue.h ├── Hashing ├── Chaining │ ├── Chains.h │ ├── main │ └── main.c └── Linear Probing │ ├── lp │ └── lp.c ├── Heap ├── heapSort └── heapSort.c ├── Linked List ├── Singly LL │ ├── Display.c │ ├── circularDsisplay.c │ ├── countAndSum.c │ ├── insertAtEnd.c │ ├── insertSorted.c │ ├── max.c │ └── search.c └── circularLL.c ├── Mathematical ├── binaryToDecimal.c ├── decimalToBinary.c ├── magicNumber.c ├── palindromeNumber.c └── specialNumber.c ├── Queue ├── QueueArray.cpp ├── QueueListSTL.cpp ├── QueueSTL.cpp └── firstNonRepeating.cpp ├── README.md ├── Sorting Techniques ├── bubbleSort.c ├── countSort.c ├── insertionSort.c ├── mergeSort.c ├── quickSort.c ├── radixSort ├── radixSort.c ├── selectionSort.c ├── shellSort └── shellSort.c ├── Stack ├── StackWIth2Queues.cpp ├── parenthesisMatch.c └── stackArray.c └── Trees ├── AVL ├── avl └── avl.c ├── BST ├── Operations └── Operations.c ├── Binary Tree ├── Queue.h ├── binaryTreeCreate └── binaryTreeCreate.c ├── Iterative Tree Traversal ├── Queue.h ├── Stack.h └── main.c ├── Queue.h ├── Stack.h ├── binaryTreeWithQueue.c ├── level.cpp ├── tree.cpp └── treeTraversal.c /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "git.ignoreLimitWarning": true, 3 | "files.associations": { 4 | "queue.h": "c", 5 | "stdlib.h": "c", 6 | "stack.h": "c" 7 | } 8 | } -------------------------------------------------------------------------------- /Arrays/binarySearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i, l, h, mid, n, search, a[100]; 5 | 6 | printf("Enter number of elements\n"); 7 | scanf("%d", &n); 8 | 9 | printf("Enter %d integers\n", n); 10 | 11 | for (i = 0; i < n; i++) 12 | scanf("%d", &a[i]); 13 | 14 | printf("Enter value to find\n"); 15 | scanf("%d", &search); 16 | 17 | l = 0; 18 | h = n - 1; 19 | mid = (l + h) / 2; 20 | 21 | while (l <= h) 22 | { 23 | if (a[mid] < search) 24 | l = mid + 1; 25 | else if (a[mid] == search) 26 | { 27 | printf("%d found at location %d.\n", search, mid + 1); 28 | break; 29 | } 30 | else 31 | h = mid - 1; 32 | 33 | mid = (l + h) / 2; 34 | } 35 | if (l > h) 36 | printf("Not found! %d isn't present in the list.\n", search); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Arrays/compareStrings.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | 6 | char a[] = "hello"; 7 | char b[] = "hellow"; 8 | int i, j; 9 | 10 | for (i = 0, j = 0; a[i] != '\0' || b[j] != '\0'; i++, j++) 11 | { 12 | 13 | if (a[i] != b[j]) 14 | break; 15 | } 16 | 17 | if (a[i] == b[j]) 18 | printf("They are EQUAL"); 19 | 20 | else if (a[i] < b[j]) 21 | printf("%s is smaller than %s \n", a, b); 22 | 23 | else 24 | 25 | printf(" %s is greater than %s \n", a, b); 26 | } 27 | -------------------------------------------------------------------------------- /Arrays/diagonalMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Diagonal 5 | { 6 | private: 7 | int n; 8 | int *a; 9 | 10 | public: 11 | Diagonal(int n) 12 | { 13 | this->n = n; 14 | a = new int[n]; 15 | } 16 | void set(int i, int j, int x); 17 | int get(int i, int j); 18 | void display(); 19 | ~Diagonal() { delete[] a; } 20 | }; 21 | 22 | void Diagonal::set(int i, int j, int x) 23 | { 24 | if (i == j) 25 | a[i - 1] = x; 26 | } 27 | 28 | int Diagonal ::get(int i, int j) 29 | { 30 | if (i == j) 31 | return a[i - 1]; 32 | else 33 | return 0; 34 | } 35 | 36 | void Diagonal ::display() 37 | { 38 | 39 | for (int i = 1; i <= n; i++) 40 | { 41 | for (int j = 1; j <= n; j++) 42 | { 43 | if (i == j) 44 | cout << a[i - 1] << " "; 45 | else 46 | cout << "0 "; 47 | } 48 | cout << endl; 49 | } 50 | } 51 | int main() 52 | { 53 | int d; 54 | cout << "Enter dimension: "; 55 | cin >> d; 56 | Diagonal dm(d); 57 | int x; 58 | cout << "Enter the elements:\n"; 59 | for (int i = 1; i <= d; i++) 60 | { 61 | for (int j = 1; j <= d; j++) 62 | { 63 | cin >> x; 64 | dm.set(i, j, x); 65 | } 66 | } 67 | dm.display(); 68 | 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Arrays/duplicatesUnsorted.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | 5 | int arr[10], hrr[9]; 6 | 7 | printf("Enter elements in array: \n"); 8 | for (int i = 0; i < 10; i++) 9 | { 10 | scanf("%d", &arr[i]); 11 | } 12 | 13 | for (int i = 0; i <= 8; i++) 14 | { 15 | hrr[i] = 0; 16 | } 17 | 18 | for (int i = 0; i < 10; i++) 19 | { 20 | hrr[arr[i]]++; 21 | } 22 | 23 | for (int i = 0; i <= 8; i++) 24 | { 25 | if (hrr[i] > 1) 26 | printf(" \n %d ", i); 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Arrays/insertElement.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n; 7 | 8 | printf("Enter no. of elements: "); 9 | scanf("%d", &n); 10 | 11 | int a[n]; 12 | 13 | for (int i = 0; i < n; i++) 14 | { 15 | printf("Enter elements: \n"); 16 | scanf("%d", &a[i]); 17 | } 18 | printf("Elements: \n"); 19 | 20 | for (int i = 0; i < n; i++) 21 | { 22 | printf("%d ", a[i]); 23 | } 24 | 25 | // insertion 26 | int x, index; 27 | 28 | printf(" \n Enter element to be inserted: \n "); 29 | scanf("%d", &x); 30 | 31 | printf("Enter index : \n"); 32 | scanf("%d", &index); 33 | 34 | int Length = sizeof(a) / sizeof(int); 35 | 36 | for (int i = n; i > index; i--) 37 | { 38 | 39 | a[i] = a[i - 1]; 40 | } 41 | a[index] = x; 42 | 43 | printf("After insertion: \n"); 44 | for (int i = 0; i < n + 1; i++) 45 | { 46 | printf("%d ", a[i]); 47 | } 48 | } -------------------------------------------------------------------------------- /Arrays/leftshift.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | //Initialize array 6 | int arr[] = {1, 2, 3, 4, 5}; 7 | //Calculate length of array arr 8 | int length = sizeof(arr) / sizeof(arr[0]); 9 | //n determine the number of times an array should be rotated 10 | int n = 3; 11 | 12 | //Displays original array 13 | printf("Original array: \n"); 14 | for (int i = 0; i < length; i++) 15 | { 16 | printf("%d ", arr[i]); 17 | } 18 | 19 | //Rotate the given array by n times toward left 20 | for (int i = 0; i < n; i++) 21 | { 22 | int j, first; 23 | //Stores the first element of the array 24 | first = arr[0]; 25 | 26 | for (j = 0; j < length - 1; j++) 27 | { 28 | //Shift element of array by one 29 | arr[j] = arr[j + 1]; 30 | } 31 | //First element of array will be added to the end 32 | arr[j] = first; 33 | } 34 | 35 | printf("\n"); 36 | 37 | //Displays resulting array after rotation 38 | printf("Array after left rotation: \n"); 39 | for (int i = 0; i < length; i++) 40 | { 41 | printf("%d ", arr[i]); 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Arrays/lowerTriangleRowMajor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class LowerT 5 | { 6 | private: 7 | int n; 8 | int *a; 9 | 10 | public: 11 | LowerT(int n) 12 | { 13 | this->n = n; 14 | a = new int[n]; 15 | } 16 | void set(int i, int j, int x); 17 | int get(int i, int j); 18 | void display(); 19 | ~LowerT() { delete[] a; } 20 | }; 21 | 22 | void LowerT::set(int i, int j, int x) 23 | { 24 | if (i >= j) 25 | a[i * (i - 1) / 2 + j - 1] = x; 26 | } 27 | 28 | int LowerT ::get(int i, int j) 29 | { 30 | if (i >= j) 31 | return a[i * (i - 1) / 2 + j - 1]; 32 | else 33 | return 0; 34 | } 35 | 36 | void LowerT ::display() 37 | { 38 | 39 | for (int i = 1; i <= n; i++) 40 | { 41 | for (int j = 1; j <= n; j++) 42 | { 43 | if (i >= j) 44 | cout << a[i * (i - 1) / 2 + j - 1] << " "; 45 | else 46 | cout << "0 "; 47 | } 48 | cout << endl; 49 | } 50 | } 51 | int main() 52 | { 53 | int d; 54 | cout << "Enter dimension: "; 55 | cin >> d; 56 | LowerT lt(d); 57 | int x; 58 | cout << "Enter the elements:\n"; 59 | for (int i = 1; i <= d; i++) 60 | { 61 | for (int j = 1; j <= d; j++) 62 | { 63 | cin >> x; 64 | lt.set(i, j, x); 65 | } 66 | } 67 | lt.display(); 68 | 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Arrays/merge.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int m, n; 6 | printf("Enter the value of m: "); //m 7 | scanf("%d", &m); 8 | 9 | printf("Enter the value of n: "); //n 10 | scanf("%d", &n); 11 | int a[m], b[n], c[m + n]; 12 | printf("Enter elements for 'a' \t: "); 13 | 14 | for (int i = 0; i < m; i++) 15 | scanf("%d", &a[i]); //a 16 | 17 | printf("Enter elements for b \t: "); 18 | for (int i = 0; i < n; i++) 19 | scanf("%d", &b[i]); //b 20 | 21 | int i = 0, j = 0, k = 0; 22 | 23 | while (i < m && j < n) 24 | { 25 | if (a[i] < b[j]) 26 | c[k++] = a[i++]; 27 | else 28 | c[k++] = b[j++]; 29 | } 30 | 31 | for (; i < m; i++) 32 | c[k++] = a[i]; 33 | 34 | for (; j < n; j++) 35 | c[k++] = b[j]; 36 | 37 | printf("Printing the sorted merged array: "); 38 | for (i = 0; i < (m + n); i++) 39 | { 40 | printf("%d ", c[i]); 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Arrays/missingElementsFasterMethod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | 7 | int arr[6]; 8 | int i; 9 | int l = 1, h = 1000; // h is a big number to accomodate the hrr array 10 | 11 | for (i = 0; i < 6; i++) 12 | { 13 | printf("Enter element:"); 14 | scanf("%d", &arr[i]); 15 | } 16 | int hrr[h]; 17 | for (i = l; i < h; i++) 18 | { 19 | hrr[i] = 0; 20 | } 21 | 22 | for (i = 0; i < 6; i++) 23 | { 24 | hrr[arr[i]]++; 25 | } 26 | 27 | printf("Missing elements: \n"); 28 | for (i = l; i <= h; i++) 29 | { 30 | if (hrr[i] == 0) 31 | printf("\n %d", i); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Arrays/multipleMissing.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(void) 4 | { 5 | int arr[6], i; 6 | for (i = 0; i < 6; i++) 7 | { 8 | printf("Enter element:"); 9 | scanf("%d", &arr[i]); 10 | } 11 | 12 | int diff; 13 | diff = arr[0] - 0; 14 | 15 | for (i = 0; i < 6; i++) 16 | { 17 | 18 | if (arr[i] - i != diff) 19 | { 20 | while (diff < arr[i] - i) 21 | { 22 | printf("%d \n", i + diff); 23 | diff++; 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Arrays/palindromeString.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | 5 | char a[] = "madam"; 6 | char b[6]; 7 | 8 | int i, j; 9 | 10 | // reversing the string 11 | for (i = 0; a[i] != '\0'; i++) 12 | { 13 | } 14 | i--; 15 | for (j = 0; i >= 0; i--, j++) 16 | { 17 | 18 | b[j] = a[i]; 19 | } 20 | b[j] = '\0'; 21 | 22 | // comparing the strings 23 | for (i = 0, j = 0; a[i] != '\0' || b[j] != '\0'; i++, j++) 24 | { 25 | 26 | if (a[i] != b[j]) 27 | break; 28 | } 29 | 30 | if (a[i] == b[j]) 31 | printf("Palindrome string \n"); 32 | 33 | else 34 | printf("Not a palindrome string \n"); 35 | } -------------------------------------------------------------------------------- /Arrays/permuationOfStringBacktracking.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void permu(char s[], int k) 4 | { 5 | static int a[10] = {0}; 6 | static char res[10]; 7 | 8 | int i; 9 | 10 | if (s[k] == '\0') 11 | { 12 | 13 | res[k] = '\0'; 14 | printf("%s \n", res); 15 | } 16 | 17 | else 18 | { 19 | for (i = 0; s[i] != '\0'; i++) 20 | { 21 | if (a[i] == 0) 22 | { 23 | res[k] = s[i]; 24 | a[i] = 1; 25 | permu(s, k + 1); 26 | a[i] = 0; 27 | } 28 | } 29 | } 30 | } 31 | 32 | main() 33 | { 34 | char s[] = "abc"; 35 | permu(s, 0); 36 | } -------------------------------------------------------------------------------- /Arrays/reverseString.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | 5 | char a[] = "python"; 6 | char b[7]; 7 | 8 | int i, j; 9 | for (i = 0; a[i] != '\0'; i++) 10 | { 11 | } 12 | i--; 13 | for (j = 0; i >= 0; i--, j++) 14 | { 15 | 16 | b[j] = a[i]; 17 | } 18 | b[j] = '\0'; 19 | 20 | printf("%s \n", b); 21 | } -------------------------------------------------------------------------------- /Arrays/singleMissingElement.c: -------------------------------------------------------------------------------- 1 | // missing element -> sum series 2 | 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | int n, i, j; 9 | printf("Enter no. of elements: \n"); 10 | scanf("%d", &n); 11 | int arr[n]; 12 | int sum = 0, s = 0; 13 | printf("Enter elements in the array: \n"); 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | scanf("%d", &arr[i]); 18 | s = s + arr[i]; 19 | } 20 | 21 | n = arr[n - 1]; 22 | 23 | sum = (n * (n + 1)) / 2; 24 | int diff; 25 | diff = sum - s; 26 | printf("Missing element is: %d", abs(diff)); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Arrays/sparseMatrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 20 3 | 4 | void read_matrix(int a[10][10], int row, int column); 5 | void print_sparse(int b[MAX][3]); 6 | void create_sparse(int a[10][10], int row, int column, int b[MAX][3]); 7 | 8 | int main() 9 | { 10 | int a[10][10], b[MAX][3], row, column; 11 | printf("\nEnter the size of matrix (rows, columns): "); 12 | scanf("%d%d", &row, &column); 13 | 14 | read_matrix(a, row, column); 15 | create_sparse(a, row, column, b); 16 | print_sparse(b); 17 | return 0; 18 | } 19 | 20 | void read_matrix(int a[10][10], int row, int column) 21 | { 22 | int i, j; 23 | printf("\nEnter elements of matrix\n"); 24 | for (i = 0; i < row; i++) 25 | { 26 | for (j = 0; j < column; j++) 27 | { 28 | printf("[%d][%d]: ", i, j); 29 | scanf("%d", &a[i][j]); 30 | } 31 | } 32 | } 33 | 34 | void create_sparse(int a[10][10], int row, int column, int b[MAX][3]) 35 | { 36 | int i, j, k; 37 | k = 1; 38 | b[0][0] = row; 39 | b[0][1] = column; 40 | for (i = 0; i < row; i++) 41 | { 42 | for (j = 0; j < column; j++) 43 | { 44 | if (a[i][j] != 0) 45 | { 46 | b[k][0] = i; 47 | b[k][1] = j; 48 | b[k][2] = a[i][j]; 49 | k++; 50 | } 51 | } 52 | b[0][2] = k - 1; 53 | } 54 | } 55 | 56 | void print_sparse(int b[MAX][3]) 57 | { 58 | int i, column; 59 | column = b[0][2]; 60 | printf("\nSparse form - list of 3 triples\n\n"); 61 | for (i = 0; i <= column; i++) 62 | { 63 | printf("%d\t%d\t%d\n", b[i][0], b[i][1], b[i][2]); 64 | } 65 | } -------------------------------------------------------------------------------- /Arrays/validateString.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int validate(char *name) 4 | { 5 | int i; 6 | 7 | for (i = 0; name[i] != '\0'; i++) 8 | { 9 | if (!(name[i] > 65 && name[i] <= 90) && !(name[i] >= 97 && name[i] <= 122) && (!name[i] >= 48 && name[i] <= 57)) 10 | { 11 | return 1; 12 | } 13 | } 14 | 15 | return 0; 16 | } 17 | 18 | int main() 19 | { 20 | 21 | char *name = "An?321"; 22 | 23 | if (validate(name)) 24 | { 25 | printf("It's a VALID string"); 26 | } 27 | else 28 | { 29 | printf("INVALID string"); 30 | } 31 | } -------------------------------------------------------------------------------- /Graphs/BFS-DFS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/proghead00/Data-Structures-and-Algorithms-In-C-Cpp/567eebfb181fb618c3ac9acf45043e442ed3b98b/Graphs/BFS-DFS -------------------------------------------------------------------------------- /Graphs/BFS-DFS.c: -------------------------------------------------------------------------------- 1 | 2 | #include "Queue.h" 3 | #include 4 | 5 | void BFS(int G[][7], int start, int n) 6 | { 7 | int i = start, j; 8 | int visited[7] = {0}; 9 | printf("%d ", i); 10 | visited[i] = 1; 11 | enqueue(i); 12 | while (!isEmpty()) 13 | { 14 | i = dequeue(); 15 | for (j = 1; j < n; j++) 16 | { 17 | if (G[i][j] == 1 && visited[j] == 0) 18 | { 19 | printf("%d ", j); 20 | visited[j] = 1; 21 | enqueue(j); 22 | } 23 | } 24 | } 25 | } 26 | void DFS(int G[][7], int start, int n) 27 | { 28 | 29 | static int visited[7] = {0}; 30 | int j; 31 | if (visited[start] == 0) 32 | { 33 | printf("%d ", start); 34 | visited[start] = 1; 35 | } 36 | for (j = 1; j < n; j++) 37 | { 38 | if (G[start][j] == 1 && visited[j] == 0) 39 | DFS(G, j, n); 40 | } 41 | } 42 | 43 | int main() 44 | { 45 | int G[7][7] = {{0, 0, 0, 0, 0, 0, 0}, 46 | {0, 0, 1, 1, 0, 0, 0}, 47 | {0, 1, 0, 0, 1, 0, 0}, 48 | {0, 1, 0, 0, 1, 0, 0}, 49 | {0, 0, 1, 1, 0, 1, 1}, 50 | {0, 0, 0, 0, 1, 0, 0}, 51 | {0, 0, 0, 0, 1, 0, 0}}; 52 | printf("BFS: "); 53 | BFS(G, 1, 7); 54 | 55 | printf("\n"); 56 | 57 | printf("DFS: "); 58 | DFS(G, 4, 7); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Graphs/Queue.h: -------------------------------------------------------------------------------- 1 | #ifndef Queue_h 2 | #define Queue_h 3 | #include 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | } *front = NULL, *rear = NULL; 10 | 11 | void enqueue(int x) 12 | { 13 | struct Node *t; 14 | t = (struct Node *)malloc(sizeof(struct Node)); 15 | if (t == NULL) 16 | printf("Queue is FUll\n"); 17 | else 18 | { 19 | t->data = x; 20 | t->next = NULL; 21 | if (front == NULL) 22 | front = rear = t; 23 | else 24 | { 25 | rear->next = t; 26 | rear = t; 27 | } 28 | } 29 | } 30 | int dequeue() 31 | { 32 | int x = -1; 33 | struct Node *t; 34 | if (front == NULL) 35 | printf("Queue is Empty\n"); 36 | else 37 | { 38 | x = front->data; 39 | t = front; 40 | } 41 | front = front->next; 42 | free(t); 43 | return x; 44 | } 45 | 46 | int isEmpty() 47 | { 48 | return front == NULL; 49 | } 50 | 51 | #endif /* Queue_h */ 52 | -------------------------------------------------------------------------------- /Hashing/Chaining/Chains.h: -------------------------------------------------------------------------------- 1 | #ifndef Chains_h 2 | #define Chains_h 3 | #include 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }; 10 | 11 | void SortedInsert(struct Node **H, int x) 12 | { 13 | struct Node *t, *q = NULL, *p = *H; 14 | t = (struct Node *)malloc(sizeof(struct Node)); 15 | t->data = x; 16 | t->next = NULL; 17 | if (*H == NULL) 18 | *H = t; 19 | else 20 | { 21 | while (p && p->data < x) 22 | { 23 | q = p; 24 | p = p->next; 25 | } 26 | if (p == *H) 27 | { 28 | t->next = *H; 29 | *H = t; 30 | } 31 | else 32 | { 33 | t->next = q->next; 34 | q->next = t; 35 | } 36 | } 37 | } 38 | struct Node *Search(struct Node *p, int key) 39 | { 40 | while (p != NULL) 41 | { 42 | if (key == p->data) 43 | 44 | { 45 | return p = p->next; 46 | } 47 | } 48 | return NULL; 49 | } 50 | #endif /* Chains_h */ 51 | -------------------------------------------------------------------------------- /Hashing/Chaining/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/proghead00/Data-Structures-and-Algorithms-In-C-Cpp/567eebfb181fb618c3ac9acf45043e442ed3b98b/Hashing/Chaining/main -------------------------------------------------------------------------------- /Hashing/Chaining/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Chains.h" 3 | 4 | int hash(int key) 5 | { 6 | return key % 10; 7 | } 8 | 9 | void Insert(struct Node *H[], int key) 10 | { 11 | int index = hash(key); 12 | SortedInsert(&H[index], key); 13 | } 14 | 15 | int main() 16 | { 17 | struct Node *HT[10]; 18 | struct Node *temp; 19 | int i; 20 | for (i = 0; i < 10; i++) 21 | HT[i] = NULL; 22 | 23 | Insert(HT, 12); 24 | Insert(HT, 22); 25 | Insert(HT, 42); 26 | temp = Search(HT[hash(12)], 12); 27 | printf("%d ", temp->data); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Hashing/Linear Probing/lp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/proghead00/Data-Structures-and-Algorithms-In-C-Cpp/567eebfb181fb618c3ac9acf45043e442ed3b98b/Hashing/Linear Probing/lp -------------------------------------------------------------------------------- /Hashing/Linear Probing/lp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define SIZE 10 3 | 4 | int hash(int key) 5 | { 6 | return key % SIZE; 7 | } 8 | int probe(int H[], int key) 9 | { 10 | int index = hash(key); 11 | int i = 0; 12 | while (H[(index + i) % SIZE] != 0) 13 | i++; 14 | return (index + i) % SIZE; 15 | } 16 | void Insert(int H[], int key) 17 | { 18 | int index = hash(key); 19 | 20 | if (H[index] != 0) 21 | index = probe(H, key); 22 | H[index] = key; 23 | } 24 | 25 | int Search(int H[], int key) 26 | { 27 | int index = hash(key); 28 | int i = 0; 29 | while (H[(index + i) % SIZE] != key) 30 | i++; 31 | return (index + i) % SIZE; 32 | } 33 | 34 | int main() 35 | { 36 | 37 | int HT[10] = {0}; 38 | Insert(HT, 12); 39 | 40 | Insert(HT, 25); 41 | Insert(HT, 35); 42 | Insert(HT, 26); 43 | printf("\nKey found at %d\n", Search(HT, 35)); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Heap/heapSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/proghead00/Data-Structures-and-Algorithms-In-C-Cpp/567eebfb181fb618c3ac9acf45043e442ed3b98b/Heap/heapSort -------------------------------------------------------------------------------- /Heap/heapSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Insert(int A[], int n) 4 | { 5 | int i = n, temp; 6 | temp = A[i]; 7 | 8 | while (i > 1 && temp > A[i / 2]) 9 | { 10 | A[i] = A[i / 2]; 11 | i = i / 2; 12 | } 13 | A[i] = temp; 14 | } 15 | 16 | int Delete(int A[], int n) 17 | { 18 | int i, j, x, temp, val; 19 | val = A[1]; 20 | x = A[n]; 21 | A[1] = A[n]; 22 | A[n] = val; 23 | i = 1; 24 | j = i * 2; 25 | 26 | while (j < n - 1) 27 | { 28 | if (A[j + 1] > A[j]) 29 | j = j + 1; 30 | if (A[i] < A[j]) 31 | { 32 | temp = A[i]; 33 | A[i] = A[j]; 34 | A[j] = temp; 35 | i = j; 36 | j = 2 * j; 37 | } 38 | else 39 | break; 40 | } 41 | return val; 42 | } 43 | 44 | int main() 45 | { 46 | int H[] = {0, 10, 20, 30, 25, 5, 40, 35}; 47 | // 40,25,35,10,5,20,30 48 | 49 | int i; 50 | for (i = 2; i <= 7; i++) 51 | Insert(H, i); 52 | for (i = 7; i > 1; i--) 53 | { 54 | Delete(H, i); 55 | } 56 | for (i = 1; i <= 7; i++) 57 | printf("%d ", H[i]); 58 | printf("\n"); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Linked List/Singly LL/Display.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | struct Node *next; 7 | int data; 8 | } *first = NULL; 9 | 10 | void create(int value) 11 | { 12 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 13 | if (t == NULL) 14 | { 15 | printf("Memory couldn't be allocated"); 16 | return; 17 | } 18 | 19 | t->data = value; 20 | t->next = NULL; 21 | 22 | if (first == NULL) 23 | { 24 | first = t; 25 | } 26 | else 27 | { 28 | struct Node *p = first; 29 | while (p->next != NULL) 30 | p = p->next; 31 | p->next = t; 32 | } 33 | } 34 | 35 | void display(struct Node *p) 36 | { 37 | while (p != NULL) 38 | { 39 | printf("%d ", p->data); 40 | p = p->next; 41 | } 42 | printf("\n"); 43 | } 44 | 45 | int main() 46 | { 47 | 48 | int n; 49 | printf("Enter size of the list: \n"); 50 | scanf("%d", &n); 51 | 52 | printf("Enter the elements: \n"); 53 | for (int i = 0; i < n; i++) 54 | { 55 | int element; 56 | scanf("%d", &element); 57 | create(element); 58 | } 59 | 60 | display(first); 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Linked List/Singly LL/circularDsisplay.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | struct Node *next; 7 | int data; 8 | } *first = NULL; 9 | 10 | void create(int value) 11 | { 12 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 13 | if (t == NULL) 14 | { 15 | printf("Memory could not be allocated"); 16 | return; 17 | } 18 | t->data = value; 19 | t->next = first; 20 | if (first == NULL) 21 | { 22 | first = t; 23 | first->next = first; 24 | } 25 | else 26 | { 27 | struct Node *p = first; 28 | while (p->next != first) 29 | p = p->next; 30 | p->next = t; 31 | } 32 | } 33 | 34 | void display(struct Node *p) 35 | { 36 | 37 | do 38 | { 39 | printf("%d ", p->data); 40 | p = p->next; 41 | } while (p != first); 42 | printf("\n"); 43 | } 44 | 45 | int main() 46 | { 47 | int n; 48 | printf("Enter size of the list: "); 49 | scanf("%d", &n); 50 | 51 | printf("Enter the elements: \n"); 52 | for (int i = 0; i < n; i++) 53 | { 54 | int element; 55 | scanf("%d", &element); 56 | create(element); 57 | } 58 | display(first); 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /Linked List/Singly LL/countAndSum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | struct Node *next; 7 | int data; 8 | 9 | } *first = NULL; 10 | 11 | void create(int value) 12 | { 13 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 14 | if (t == NULL) 15 | { 16 | printf("Memory couldn't be allocated"); 17 | return; 18 | } 19 | 20 | t->data = value; 21 | t->next = NULL; 22 | 23 | if (first == NULL) 24 | { 25 | first = t; 26 | } 27 | else 28 | { 29 | struct Node *p = first; 30 | while (p->next != NULL) 31 | p = p->next; 32 | p->next = t; 33 | } 34 | } 35 | 36 | void displayCountAndSum(struct Node *p) 37 | { 38 | int c = 0, s = 0; 39 | while (p != NULL) 40 | { 41 | printf("%d ", p->data); 42 | s = s + p->data; 43 | 44 | p = p->next; 45 | c++; 46 | } 47 | printf("\n Number of nodes: %d\n", c); 48 | printf("Sum: %d\n", s); 49 | } 50 | 51 | int main() 52 | { 53 | 54 | int n; 55 | printf("Enter size of the list: \n"); 56 | scanf("%d", &n); 57 | 58 | printf("Enter the elements: \n"); 59 | for (int i = 0; i < n; i++) 60 | { 61 | int element; 62 | scanf("%d", &element); 63 | create(element); 64 | } 65 | 66 | displayCountAndSum(first); 67 | 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Linked List/Singly LL/insertAtEnd.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | 7 | struct Node *next; 8 | int data; 9 | } *first = NULL; 10 | 11 | void create(int value) 12 | { 13 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 14 | 15 | if (t == NULL) 16 | { 17 | printf("Memory couldn't be allocated"); 18 | } 19 | t->data = value; 20 | t->next = NULL; 21 | 22 | if (first == NULL) 23 | { 24 | first = t; 25 | } 26 | 27 | else 28 | { 29 | 30 | struct Node *p; 31 | p = first; 32 | 33 | while (p->next != NULL) 34 | { 35 | p = p->next; 36 | } 37 | p->next = t; 38 | } 39 | } 40 | 41 | void display(struct Node *p) 42 | { 43 | while (p != NULL) 44 | { 45 | printf(" %d", p->data); 46 | p = p->next; 47 | } 48 | } 49 | 50 | void insertEnd(int num) 51 | { 52 | struct Node *t, *p; 53 | t = (struct Node *)malloc(sizeof(sizeof(struct Node))); 54 | if (t == NULL) 55 | { 56 | printf(" Memory can not be allocated."); 57 | } 58 | else 59 | { 60 | t->data = num; 61 | t->next = NULL; 62 | p = first; 63 | while (p->next != NULL) 64 | p = p->next; 65 | p->next = t; 66 | } 67 | } 68 | 69 | int main() 70 | { 71 | 72 | int n; 73 | printf("Enter size of the list: \n"); 74 | scanf("%d", &n); 75 | 76 | printf("Enter the elements: \n"); 77 | for (int i = 0; i < n; i++) 78 | { 79 | int element; 80 | scanf("%d", &element); 81 | create(element); 82 | } 83 | insertEnd(10); 84 | display(first); 85 | 86 | return 0; 87 | } -------------------------------------------------------------------------------- /Linked List/Singly LL/insertSorted.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | 9 | } *first = NULL; 10 | 11 | void create(int value) 12 | { 13 | 14 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 15 | t->data = value; 16 | t->next = NULL; 17 | 18 | if (first == NULL) 19 | first = t; 20 | 21 | else 22 | { 23 | 24 | struct Node *p = first; 25 | while (p->next != NULL) 26 | p = p->next; 27 | p->next = t; 28 | } 29 | } 30 | 31 | void display(struct Node *p) 32 | { 33 | while (p != NULL) 34 | { 35 | printf(" %d", p->data); 36 | p = p->next; 37 | } 38 | } 39 | 40 | void insertSorted(int x) 41 | { 42 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 43 | t->data = x; 44 | t->next = NULL; 45 | struct Node *p = first; 46 | struct Node *prev = NULL; 47 | while (t->data > p->data && p->next != NULL) 48 | { 49 | prev = p; 50 | p = p->next; 51 | } 52 | if (t->data > p->data) 53 | { 54 | prev = p; 55 | p = p->next; 56 | } 57 | if (prev == NULL) 58 | { 59 | t->next = first; 60 | first = t; 61 | } 62 | else 63 | { 64 | t->next = prev->next; 65 | prev->next = t; 66 | } 67 | } 68 | int main() 69 | { 70 | 71 | int n; 72 | printf("Enter size of the list: \n"); 73 | scanf("%d", &n); 74 | 75 | printf("Enter the elements: \n"); 76 | for (int i = 0; i < n; i++) 77 | { 78 | int element; 79 | scanf("%d", &element); 80 | create(element); 81 | } 82 | insertSorted(4); 83 | display(first); 84 | 85 | return 0; 86 | } -------------------------------------------------------------------------------- /Linked List/Singly LL/max.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | 7 | struct Node *next; 8 | int data; 9 | 10 | } *first = NULL; 11 | 12 | int create(int value) 13 | { 14 | 15 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 16 | if (t == NULL) 17 | { 18 | printf("Memory couldn't be allocated"); 19 | return; 20 | } 21 | t->data = value; 22 | t->next = NULL; 23 | 24 | if (first == NULL) 25 | { 26 | first = t; 27 | } 28 | else 29 | { 30 | struct Node *p = first; 31 | while (p->next != NULL) 32 | p = p->next; 33 | p->next = t; 34 | } 35 | } 36 | 37 | void display(struct Node *p) 38 | { 39 | while (p != NULL) 40 | { 41 | printf("%d ", p->data); 42 | p = p->next; 43 | } 44 | printf("\n"); 45 | } 46 | 47 | int Max(struct Node *p) 48 | { 49 | int max = "INT32_MIN"; 50 | 51 | while (p) 52 | { 53 | if (p->data > max) 54 | max = p->data; 55 | p = p->next; 56 | } 57 | return max; 58 | } 59 | 60 | int main() 61 | { 62 | 63 | int n; 64 | printf("Enter size of the list: \n"); 65 | scanf("%d", &n); 66 | 67 | printf("Enter the elements: \n"); 68 | for (int i = 0; i < n; i++) 69 | { 70 | int element; 71 | scanf("%d", &element); 72 | create(element); 73 | } 74 | 75 | display(first); 76 | 77 | return 0; 78 | } -------------------------------------------------------------------------------- /Linked List/Singly LL/search.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | 7 | struct Node *next; 8 | int data; 9 | } *first = NULL; 10 | 11 | void create(int value) 12 | { 13 | 14 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 15 | t->data = value; 16 | t->next = NULL; 17 | 18 | if (first == NULL) 19 | { 20 | first = t; 21 | } 22 | else 23 | { 24 | struct Node *p = first; 25 | while (p->next != NULL) 26 | { 27 | p = p->next; 28 | } 29 | p->next = t; 30 | } 31 | } 32 | 33 | void search(struct Node *p, int x) 34 | { 35 | p = first; 36 | 37 | while (p->next != NULL) 38 | { 39 | if (p->data == x) 40 | { 41 | printf("Found"); 42 | break; 43 | } 44 | p = p->next; 45 | } 46 | if (p->data != x) 47 | printf("Not found"); 48 | } 49 | 50 | int main() 51 | { 52 | 53 | int n; 54 | printf("Enter size of the list: \n"); 55 | scanf("%d", &n); 56 | 57 | printf("Enter the elements: \n"); 58 | for (int i = 0; i < n; i++) 59 | { 60 | int element; 61 | scanf("%d", &element); 62 | create(element); 63 | } 64 | struct Node *k; 65 | 66 | search(k, 10); 67 | 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Linked List/circularLL.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | struct Node *next; 7 | 8 | int data; 9 | } *first = NULL; 10 | 11 | void create(int value) 12 | { 13 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 14 | if (t == NULL) 15 | { 16 | printf("Memory could not be allocated"); 17 | return; 18 | } 19 | t->data = value; 20 | t->next = first; 21 | if (first == NULL) 22 | { 23 | first = t; 24 | first->next = first; 25 | } 26 | else 27 | { 28 | struct Node *p = first; 29 | while (p->next != first) 30 | p = p->next; 31 | p->next = t; 32 | } 33 | } 34 | 35 | void display(struct Node *p) 36 | { 37 | 38 | do 39 | { 40 | printf("%d ", p->data); 41 | p = p->next; 42 | } while (p != first); 43 | } 44 | 45 | int len() 46 | { 47 | int l = 0; 48 | 49 | struct Node *p = first; 50 | while (p->next != first) 51 | { 52 | l++; 53 | p = p->next; 54 | } 55 | return l; 56 | } 57 | 58 | void insAny(int value, int pos) 59 | { 60 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 61 | t->data = value; 62 | struct Node *p; 63 | p = first; 64 | 65 | if (pos == 1) 66 | { 67 | t->next = first; 68 | 69 | while (p->next != first) 70 | { 71 | p = p->next; 72 | } 73 | 74 | first = t; 75 | p->next = first; 76 | return; 77 | } 78 | --pos; 79 | while (--pos != 0) 80 | { 81 | 82 | p = p->next; 83 | } 84 | struct Node *q = p->next; 85 | p->next = t; 86 | t->next = q; 87 | } 88 | 89 | void delAny(int pos) 90 | { 91 | struct Node *p = first; 92 | struct Node *q; 93 | if (pos == 1) 94 | { 95 | while (p->next != first) 96 | { 97 | p = p->next; 98 | } 99 | 100 | q = first->next; 101 | p->next = q; 102 | free(first); 103 | first = q; 104 | } 105 | 106 | else if (pos == len()) 107 | { 108 | while (p->next->next != first) 109 | { 110 | p = p->next; 111 | } 112 | 113 | q = p->next; 114 | p->next = first; 115 | 116 | free(q); 117 | } 118 | 119 | else 120 | 121 | { 122 | --pos; 123 | while (--pos) 124 | { 125 | p = p->next; 126 | } 127 | q = p->next; 128 | 129 | p->next = p->next->next; 130 | free(q); 131 | } 132 | } 133 | 134 | void search(int key) 135 | { 136 | 137 | struct Node *p = first; 138 | 139 | while (p->next != first && p->data != key) 140 | { 141 | if (p->data == key) 142 | { 143 | printf("Found\n"); 144 | return; 145 | } 146 | 147 | p = p->next; 148 | } 149 | printf("Not found\n"); 150 | return; 151 | } 152 | 153 | void sortedInsert(int x) 154 | { 155 | 156 | struct Node *p = first; 157 | struct Node *prev = NULL; 158 | 159 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 160 | t->data = x; 161 | t->next = NULL; 162 | 163 | while (t->data > p->data && p->next != first) 164 | { 165 | prev = p; 166 | p = p->next; 167 | } 168 | if (t->data > p->data) 169 | { 170 | prev = p; 171 | p = p->next; 172 | } 173 | if (prev == NULL) 174 | { 175 | t->next = first; 176 | while (p->next != first) 177 | p = p->next; 178 | first = t; 179 | p->next = first; 180 | } 181 | else 182 | { 183 | t->next = prev->next; 184 | prev->next = t; 185 | } 186 | } 187 | int main() 188 | { 189 | 190 | int n; 191 | printf("Enter size of list: "); 192 | scanf("%d", &n); 193 | 194 | printf("Enter elements: \n "); 195 | for (int i = 0; i < n; i++) 196 | { 197 | int element; 198 | scanf("%d", &element); 199 | create(element); 200 | } 201 | len(); 202 | // insAny(10, 4); 203 | // search(4); 204 | // delAny(5); 205 | sortedInsert(1); 206 | display(first); 207 | } -------------------------------------------------------------------------------- /Mathematical/binaryToDecimal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n, f = 1, s = 0, i, r; 7 | 8 | printf("Enter binary number to convert: "); 9 | scanf("%d", &n); 10 | 11 | while (n != 0) 12 | { 13 | r = n % 10; 14 | n = n / 10; 15 | s = s + r * f; 16 | 17 | f = f * 2; 18 | } 19 | 20 | printf("Decimal form is: %d \n", s); 21 | } 22 | -------------------------------------------------------------------------------- /Mathematical/decimalToBinary.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n, f = 1, s = 0, i, r; 7 | 8 | printf("Enter decimal number to convert: "); 9 | scanf("%d", &n); 10 | 11 | while (n != 0) 12 | { 13 | r = n % 2; 14 | n = n / 2; 15 | s = s + r * f; 16 | 17 | f = f * 10; 18 | } 19 | 20 | printf("Binary form is: %d \n", s); 21 | } 22 | -------------------------------------------------------------------------------- /Mathematical/magicNumber.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | int n; 8 | int s = 0, r; 9 | printf("Enter number to check: "); 10 | scanf("%d", &n); 11 | 12 | while (n > 9) 13 | { 14 | s = 0; 15 | 16 | while (n != 0) 17 | { 18 | r = n % 10; 19 | n = n / 10; 20 | s = s + r; 21 | } 22 | n = s; 23 | } 24 | 25 | if (n == 1) 26 | printf("It's a Magic Number \n"); 27 | 28 | else 29 | { 30 | printf("It's NOT a Magic Number \n"); 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Mathematical/palindromeNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n, r, sum = 0; 7 | 8 | printf("Enter the number to check: "); 9 | scanf("%d", &n); 10 | 11 | int n1 = n; 12 | while (n > 0) 13 | { 14 | r = n % 10; 15 | sum = (sum * 10) + r; 16 | n = n / 10; 17 | } 18 | 19 | if (n1 == sum) 20 | { 21 | printf("It's a palindrome number \n"); 22 | } 23 | else 24 | { 25 | printf("It's not a palindrome number \n"); 26 | } 27 | } -------------------------------------------------------------------------------- /Mathematical/specialNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n, n1, i, r, sum = 0; 7 | 8 | printf("Enter the number to check: "); 9 | scanf("%d", &n); 10 | 11 | n1 = n; 12 | while (n > 0) 13 | { 14 | r = n % 10; 15 | int fact = 1; 16 | for (i = 1; i <= r; i++) 17 | { 18 | fact = fact * i; 19 | } 20 | sum = sum + fact; 21 | n = n / 10; 22 | } 23 | if (n1 == sum) 24 | { 25 | printf("It's a Special Number!\n"); 26 | } 27 | else 28 | { 29 | printf("It's NOT a Special Number\n"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Queue/QueueArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Queue 5 | { 6 | 7 | int *arr; 8 | int f, r, cs, ms; // front, rear, current size, max size 9 | 10 | public: 11 | Queue(int ds = 5) 12 | { 13 | arr = new int[ds]; 14 | cs = 0; 15 | ms = ds; 16 | f = 0; 17 | r = ms - 1; 18 | } 19 | 20 | bool full() 21 | { 22 | 23 | return cs == ms; 24 | } 25 | 26 | bool empty() 27 | { 28 | return cs == 0; 29 | } 30 | 31 | void push(int data) 32 | { 33 | if (!full()) 34 | { 35 | r = (r + 1) % ms; 36 | arr[r] = data; 37 | cs++; 38 | } 39 | } 40 | 41 | void pop() 42 | { 43 | if (!empty()) 44 | { 45 | f = (f + 1) % ms; 46 | cs--; 47 | } 48 | } 49 | 50 | int front() 51 | { 52 | 53 | return arr[f]; 54 | } 55 | ~Queue() 56 | { 57 | if (arr != NULL) 58 | { 59 | delete[] arr; 60 | arr = NULL; 61 | } 62 | } 63 | }; 64 | 65 | int main() 66 | { 67 | 68 | Queue q; 69 | 70 | for (int i = 0; i < 5; i++) 71 | { 72 | q.push(i); 73 | } 74 | 75 | q.pop(); 76 | q.push(7); 77 | 78 | while (!q.empty()) 79 | { 80 | cout << q.front() << " "; 81 | 82 | q.pop(); 83 | } 84 | 85 | return 0; 86 | } -------------------------------------------------------------------------------- /Queue/QueueListSTL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Queue 6 | { 7 | int cs; 8 | 9 | list l; 10 | 11 | public: 12 | Queue() { cs = 0; } 13 | 14 | bool empty() { return cs == 0; } 15 | 16 | void push(int data) 17 | { 18 | l.push_back(data); // push back at the tail 19 | cs++; 20 | } 21 | 22 | void pop() 23 | { 24 | if (!empty()) 25 | { 26 | cs--; 27 | l.pop_front(); // remove the element from the head of the queue 28 | } 29 | } 30 | 31 | int front() 32 | { 33 | 34 | return l.front(); // element present at the front of the list 35 | } 36 | }; 37 | 38 | int main() 39 | { 40 | Queue q; 41 | 42 | for (int i = 0; i < 10; i++) 43 | { 44 | q.push(i); 45 | } 46 | 47 | // remove elements while list is not empty 48 | while (!q.empty()) 49 | { 50 | cout << q.front() << " "; 51 | q.pop(); 52 | } 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Queue/QueueSTL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | queue q; 9 | 10 | for (int i = 0; i < 5; i++) 11 | { 12 | q.push(i); 13 | } 14 | while (!q.empty()) 15 | { 16 | cout << q.front() << " "; 17 | q.pop(); 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Queue/firstNonRepeating.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | const int MAX_CHAR = 26; 5 | 6 | // function to find first non repeating 7 | // character of sa stream 8 | void firstnonrepeating(char str[]) 9 | { 10 | queue q; 11 | int charCount[MAX_CHAR] = {0}; 12 | 13 | // traverse whole stream 14 | for (int i = 0; str[i]; i++) 15 | { 16 | 17 | // push each character in queue 18 | q.push(str[i]); 19 | 20 | // increment the frequency count 21 | charCount[str[i] - 'a']++; 22 | 23 | // check for the non pepeating character 24 | while (!q.empty()) 25 | { 26 | if (charCount[q.front() - 'a'] > 1) 27 | q.pop(); 28 | else 29 | { 30 | cout << q.front() << " "; 31 | break; 32 | } 33 | } 34 | 35 | if (q.empty()) 36 | cout << -1 << " "; 37 | } 38 | cout << endl; 39 | } 40 | 41 | int main() 42 | { 43 | char str[] = "aabc"; 44 | firstnonrepeating(str); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms In C/C++ and *other mathematical programs* 🤠 2 | 3 | ![data structures and algorithms](https://user-images.githubusercontent.com/55017730/103276184-dedb7a80-49eb-11eb-85af-7f6ddab54663.gif) 4 | 5 | 6 | -------------------------------------------------------------------------------- /Sorting Techniques/bubbleSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *x, int *y) 5 | { 6 | int temp = *x; 7 | *x = *y; 8 | *y = temp; 9 | } 10 | 11 | void bubble(int A[], int n) 12 | { 13 | int i, j, flag = 0; 14 | 15 | for (i = 0; i < n - 1; i++) 16 | { 17 | flag = 0; 18 | for (j = 0; j < n - i - 1; j++) 19 | { 20 | if (A[j] > A[j + 1]) 21 | { 22 | swap(&A[j], &A[j + 1]); 23 | flag = 1; 24 | } 25 | } 26 | if (flag == 0) 27 | break; 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | int n; 34 | 35 | printf("Enter size of the array: \n "); 36 | scanf("%d", &n); 37 | 38 | int a[n]; 39 | printf("Enter elements: \n"); 40 | 41 | for (int i = 0; i < n; i++) 42 | { 43 | scanf("%d", &a[i]); 44 | } 45 | 46 | bubble(a, n); 47 | 48 | printf("After sorting: \n"); 49 | for (int i = 0; i < n; i++) 50 | printf("%d ", a[i]); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Sorting Techniques/countSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int MAX(int A[], int n) 5 | { 6 | int max = "INT32_MIN"; 7 | int i; 8 | for (i = 0; i < n; i++) 9 | { 10 | if (A[i] > max) 11 | max = A[i]; 12 | } 13 | return max; 14 | } 15 | void CountSort(int A[], int n) 16 | { 17 | int i, j, max; 18 | max = MAX(A, n); 19 | int C[max]; 20 | // C = (int *)malloc(sizeof(int) * (max + 1)); 21 | for (i = 0; i < max + 1; i++) 22 | { 23 | C[i] = 0; 24 | } 25 | for (i = 0; i < n; i++) 26 | { 27 | C[A[i]]++; 28 | } 29 | 30 | i = 0; 31 | j = 0; 32 | while (j < max + 1) 33 | { 34 | if (C[j] > 0) 35 | { 36 | A[i++] = j; 37 | C[j]--; 38 | } 39 | else 40 | j++; 41 | } 42 | } 43 | int main() 44 | { 45 | int A[] = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3}, n = 10, i; 46 | 47 | CountSort(A, n); 48 | for (i = 0; i < 10; i++) 49 | printf("%d ", A[i]); 50 | printf("\n"); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Sorting Techniques/insertionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *x, int *y) 5 | { 6 | 7 | int temp = *x; 8 | *x = *y; 9 | *y = temp; 10 | } 11 | 12 | void ins(int a[], int n) 13 | { 14 | 15 | int i, j, x; 16 | 17 | for (i = 1; i < n; i++) 18 | { 19 | j = i - 1; 20 | x = a[i]; 21 | 22 | while (j > -1 && a[j] > x) 23 | { 24 | a[j + 1] = a[j]; 25 | 26 | j--; 27 | } 28 | 29 | a[j + 1] = x; 30 | } 31 | } 32 | int main() 33 | { 34 | int A[] = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3}, n = 10, i; 35 | ins(A, n); 36 | for (i = 0; i < 10; i++) 37 | printf("%d ", A[i]); 38 | printf("\n"); 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Sorting Techniques/mergeSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *x, int *y) 5 | { 6 | int temp = *x; 7 | *x = *y; 8 | *y = temp; 9 | } 10 | 11 | void Merge(int A[], int l, int mid, int h) 12 | { 13 | int i = l, j = mid + 1, k = l; 14 | int B[100]; 15 | 16 | while (i <= mid && j <= h) 17 | { 18 | if (A[i] < A[j]) 19 | B[k++] = A[i++]; 20 | else 21 | B[k++] = A[j++]; 22 | } 23 | for (; i <= mid; i++) 24 | B[k++] = A[i]; 25 | for (; j <= h; j++) 26 | B[k++] = A[j]; 27 | 28 | for (i = l; i <= h; i++) 29 | A[i] = B[i]; //copying all back to A 30 | } 31 | 32 | void MergeSort(int A[], int l, int h) 33 | { 34 | int mid; 35 | if (l < h) 36 | { 37 | mid = (l + h) / 2; 38 | 39 | MergeSort(A, l, mid); 40 | MergeSort(A, mid + 1, h); 41 | Merge(A, l, mid, h); 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | int A[] = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3}, i; 48 | 49 | MergeSort(A, 0, 9); 50 | 51 | for (i = 0; i < 10; i++) 52 | printf("%d ", A[i]); 53 | printf("\n"); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Sorting Techniques/quickSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *x, int *y) 5 | { 6 | int temp = *x; 7 | *x = *y; 8 | *y = temp; 9 | } 10 | 11 | int partition(int a[], int start, int end) 12 | { 13 | int pivot = a[end]; 14 | int pIndex = start; 15 | 16 | for (int i = start; i < end; i++) 17 | { 18 | if (a[i] <= pivot) 19 | { 20 | swap(&a[i], &a[pIndex]); 21 | pIndex++; 22 | } 23 | } 24 | swap(&a[pIndex], &a[end]); 25 | return pIndex; 26 | } 27 | 28 | void quicksort(int a[], int start, int end) 29 | { 30 | 31 | if (start < end) 32 | { 33 | 34 | int pindex = partition(a, start, end); 35 | quicksort(a, start, pindex - 1); 36 | quicksort(a, pindex + 1, end); 37 | } 38 | } 39 | 40 | int main() 41 | { 42 | int A[] = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3}; 43 | quicksort(A, 0, 9); 44 | 45 | for (int i = 0; i < 9; i++) 46 | printf("%d ", A[i]); 47 | printf("\n"); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Sorting Techniques/radixSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/proghead00/Data-Structures-and-Algorithms-In-C-Cpp/567eebfb181fb618c3ac9acf45043e442ed3b98b/Sorting Techniques/radixSort -------------------------------------------------------------------------------- /Sorting Techniques/radixSort.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int getMax(int array[], int n) 5 | { 6 | int max = array[0]; 7 | for (int i = 1; i < n; i++) 8 | if (array[i] > max) 9 | max = array[i]; 10 | return max; 11 | } 12 | 13 | // Using counting sort to sort the elements in the basis of significant places 14 | void countingSort(int array[], int size, int place) 15 | { 16 | int output[size + 1]; 17 | int max = (array[0] / place) % 10; 18 | 19 | for (int i = 1; i < size; i++) 20 | { 21 | if (((array[i] / place) % 10) > max) 22 | max = array[i]; 23 | } 24 | int count[max + 1]; 25 | 26 | for (int i = 0; i < max; ++i) 27 | count[i] = 0; 28 | 29 | // Calculate count of elements 30 | for (int i = 0; i < size; i++) 31 | count[(array[i] / place) % 10]++; 32 | 33 | // Calculate cummulative count 34 | for (int i = 1; i < 10; i++) 35 | count[i] += count[i - 1]; 36 | 37 | // Place the elements in sorted order 38 | for (int i = size - 1; i >= 0; i--) 39 | { 40 | output[count[(array[i] / place) % 10] - 1] = array[i]; 41 | count[(array[i] / place) % 10]--; 42 | } 43 | 44 | for (int i = 0; i < size; i++) 45 | array[i] = output[i]; 46 | } 47 | 48 | // Main function to implement radix sort 49 | void radixsort(int array[], int size) 50 | { 51 | // Get maximum element 52 | int max = getMax(array, size); 53 | 54 | // Apply counting sort to sort elements based on place value. 55 | for (int place = 1; max / place > 0; place *= 10) 56 | countingSort(array, size, place); 57 | } 58 | 59 | // Print an array 60 | void printArray(int array[], int size) 61 | { 62 | for (int i = 0; i < size; ++i) 63 | { 64 | printf("%d ", array[i]); 65 | } 66 | printf("\n"); 67 | } 68 | 69 | // Driver code 70 | int main() 71 | { 72 | int array[] = {121, 432, 564, 23, 1, 45, 788}; 73 | int n = sizeof(array) / sizeof(array[0]); 74 | radixsort(array, n); 75 | printArray(array, n); 76 | } -------------------------------------------------------------------------------- /Sorting Techniques/selectionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *x, int *y) 5 | { 6 | int temp = *x; 7 | *x = *y; 8 | *y = temp; 9 | } 10 | 11 | void selection(int a[], int n) 12 | { 13 | 14 | int i, j, k; 15 | 16 | for (i = 0; i < n - 1; i++) 17 | { 18 | for (j = k = i; j < n; j++) 19 | { 20 | if (a[j] < a[k]) 21 | k = j; // k will have the smallest element 22 | } 23 | 24 | swap(&a[i], &a[k]); 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | int A[] = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3}, n = 10, i; 31 | selection(A, n); 32 | 33 | for (i = 0; i < 10; i++) 34 | printf("%d ", A[i]); 35 | printf("\n"); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Sorting Techniques/shellSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/proghead00/Data-Structures-and-Algorithms-In-C-Cpp/567eebfb181fb618c3ac9acf45043e442ed3b98b/Sorting Techniques/shellSort -------------------------------------------------------------------------------- /Sorting Techniques/shellSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int *x, int *y) 5 | { 6 | int temp = *x; 7 | *x = *y; 8 | *y = temp; 9 | } 10 | 11 | void shellSort(int a[], int n) 12 | { 13 | 14 | for (int gap = n / 2; gap >= 1; gap /= 2) 15 | { 16 | { 17 | for (int j = gap; j < n; j++) 18 | { 19 | for (int i = j - gap; i >= 0; i -= gap) 20 | { 21 | if (a[i + gap] > a[i]) 22 | { 23 | break; 24 | } 25 | else 26 | swap(&a[i + gap], &a[i]); 27 | } 28 | } 29 | } 30 | } 31 | } 32 | int main() 33 | { 34 | int A[] = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3}, n = 10, i; 35 | 36 | shellSort(A, n); 37 | for (i = 0; i < 10; i++) 38 | printf("%d ", A[i]); 39 | printf("\n"); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Stack/StackWIth2Queues.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | template 6 | class Stack 7 | { 8 | 9 | queue q1, q2; 10 | 11 | public: 12 | void push(T x) 13 | { 14 | q1.push(x); 15 | } 16 | void pop() 17 | { 18 | 19 | // remove the last added element from q1 20 | if (q1.empty()) 21 | { 22 | return; 23 | } 24 | 25 | //extracting n-1 elements and adding to q2 26 | while (q1.size() > 1) 27 | { 28 | 29 | T element = q1.front(); 30 | q2.push(element); 31 | q1.pop(); 32 | } 33 | 34 | //removing the last element from q1 35 | q1.pop(); 36 | 37 | //swap names of q1 and q2 38 | swap(q1, q2); 39 | } 40 | 41 | T top() 42 | { 43 | 44 | while (q1.size() > 1) 45 | { 46 | T element = q1.front(); 47 | q2.push(element); 48 | q1.pop(); 49 | } 50 | 51 | // 1 element in q1 52 | T element = q1.front(); 53 | q1.pop(); 54 | q2.push(element); 55 | 56 | swap(q1, q2); 57 | return element; 58 | } 59 | 60 | int size() 61 | { 62 | 63 | return q1.size() + q2.size(); 64 | } 65 | 66 | bool empty() 67 | { 68 | return size() == 0; 69 | } 70 | }; 71 | 72 | int main() 73 | { 74 | 75 | Stack s; 76 | s.push(1); 77 | s.push(2); 78 | s.push(3); 79 | s.push(4); 80 | s.push(5); 81 | 82 | while (!s.empty()) 83 | { 84 | cout << s.top() << " "; 85 | s.pop(); 86 | } 87 | return 0; 88 | } -------------------------------------------------------------------------------- /Stack/parenthesisMatch.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | char data; 7 | 8 | struct Node *next; 9 | 10 | } *top = NULL; 11 | 12 | void push(char x) 13 | { 14 | struct Node *t = (struct Node *)malloc(sizeof(struct Node)); 15 | 16 | if (t == NULL) 17 | { 18 | printf("Stack is Full"); 19 | } 20 | 21 | else 22 | { 23 | t->data = x; 24 | t->next = top; 25 | top = t; 26 | } 27 | } 28 | 29 | char pop() 30 | { 31 | struct Node *t; 32 | char x = -1; 33 | 34 | if (top == NULL) 35 | { 36 | printf("Stack is empty\n"); 37 | } 38 | else 39 | { 40 | 41 | t = top; 42 | top = top->next; 43 | x = t->data; 44 | free(t); 45 | } 46 | 47 | return x; 48 | } 49 | 50 | void display() 51 | { 52 | struct Node *p; 53 | 54 | p = top; 55 | 56 | while (p != NULL) 57 | { 58 | printf("%d ", p->data); 59 | p = p->next; 60 | } 61 | printf("\n"); 62 | } 63 | 64 | int isBalanced(char *exp) 65 | { 66 | for (int i = 0; exp[i] != '\0'; i++) 67 | { 68 | if (exp[i] == '(') 69 | { 70 | push(exp[i]); 71 | } 72 | else 73 | { 74 | 75 | if (top == NULL) 76 | return 0; 77 | 78 | pop(); 79 | } 80 | } 81 | 82 | if (top == NULL) 83 | return 1; 84 | else 85 | return 0; 86 | } 87 | int main() 88 | { 89 | 90 | char *exp = "((a+b)*(c-d)))"; 91 | 92 | printf("%d ", isBalanced(exp)); 93 | 94 | return 0; 95 | } -------------------------------------------------------------------------------- /Stack/stackArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Stack 5 | { 6 | int size; 7 | int top; 8 | int *S; 9 | }; 10 | 11 | void create(struct Stack *st) 12 | { 13 | printf("Enter size of stack: "); 14 | scanf("%d", &st->size); 15 | st->top = -1; 16 | 17 | st->S = (int *)malloc(st->size * sizeof(int)); 18 | } 19 | 20 | void display(struct Stack st) 21 | { 22 | for (int i = st.top; i >= 0; i--) 23 | { 24 | printf("%d ", st.S[i]); 25 | printf("\n"); 26 | } 27 | } 28 | 29 | void push(struct Stack *st, int x) 30 | { 31 | if (st->top == st->size - 1) 32 | { 33 | printf("Stack overflow\n"); 34 | } 35 | 36 | else 37 | { 38 | st->top++; 39 | st->S[st->top] = x; 40 | } 41 | } 42 | 43 | int pop(struct Stack *st) 44 | { 45 | int x = -1; 46 | 47 | if (st->top == -1) 48 | { 49 | printf("Stack underflow"); 50 | } 51 | else 52 | { 53 | 54 | x = st->S[st->top--]; 55 | } 56 | return x; 57 | } 58 | 59 | int peek(struct Stack st, int index) 60 | { 61 | int x = -1; 62 | if (st.top - index + 1 < 0) 63 | printf("Invalid Index \n"); 64 | x = st.S[st.top - index + 1]; 65 | return x; 66 | } 67 | 68 | int main() 69 | { 70 | 71 | struct Stack st; 72 | create(&st); 73 | 74 | push(&st, 10); 75 | push(&st, 20); 76 | push(&st, 30); 77 | 78 | printf("%d\n", peek(st, 2)); 79 | 80 | display(st); 81 | 82 | return 0; 83 | } -------------------------------------------------------------------------------- /Trees/AVL/avl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/proghead00/Data-Structures-and-Algorithms-In-C-Cpp/567eebfb181fb618c3ac9acf45043e442ed3b98b/Trees/AVL/avl -------------------------------------------------------------------------------- /Trees/AVL/avl.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Node 4 | { 5 | struct Node *lchild; 6 | int data; 7 | int height; 8 | struct Node *rchild; 9 | } *root = NULL; 10 | 11 | int NodeHeight(struct Node *p) 12 | { 13 | int hl, hr; 14 | hl = p && p->lchild ? p->lchild->height : 0; 15 | hr = p && p->rchild ? p->rchild->height : 0; 16 | 17 | return hl > hr ? hl + 1 : hr + 1; 18 | } 19 | 20 | int BalanceFactor(struct Node *p) 21 | { 22 | int hl, hr; 23 | hl = p && p->lchild ? p->lchild->height : 0; 24 | hr = p && p->rchild ? p->rchild->height : 0; 25 | return hl - hr; 26 | } 27 | 28 | struct Node *LLRotation(struct Node *p) 29 | { 30 | struct Node *pl = p->lchild; 31 | struct Node *plr = pl->rchild; 32 | pl->rchild = p; 33 | p->lchild = plr; 34 | p->height = NodeHeight(p); 35 | pl->height = NodeHeight(pl); 36 | if (root == p) 37 | root = pl; 38 | 39 | return pl; 40 | } 41 | 42 | struct Node *LRRotation(struct Node *p) 43 | { 44 | struct Node *pl = p->lchild; 45 | struct Node *plr = pl->rchild; 46 | pl->rchild = plr->lchild; 47 | p->lchild = plr->rchild; 48 | plr->lchild = pl; 49 | plr->rchild = p; 50 | pl->height = NodeHeight(pl); 51 | p->height = NodeHeight(p); 52 | plr->height = NodeHeight(plr); 53 | if (root == p) 54 | root = plr; 55 | return plr; 56 | } 57 | struct Node *RRRotation(struct Node *p) 58 | { 59 | return NULL; 60 | } 61 | struct Node *RLRotation(struct Node *p) 62 | { 63 | return NULL; 64 | } 65 | struct Node *RInsert(struct Node *p, int key) 66 | { 67 | struct Node *t = NULL; 68 | if (p == NULL) 69 | { 70 | t = (struct Node *)malloc(sizeof(struct Node)); 71 | t->data = key; 72 | t->height = 1; 73 | t->lchild = t->rchild = NULL; 74 | return t; 75 | } 76 | if (key < p->data) 77 | p->lchild = RInsert(p->lchild, key); 78 | else if (key > p->data) 79 | p->rchild = RInsert(p->rchild, key); 80 | p->height = NodeHeight(p); 81 | if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == 1) 82 | 83 | return LLRotation(p); 84 | 85 | else if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == -1) 86 | return LRRotation(p); 87 | 88 | else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == -1) 89 | 90 | return RRRotation(p); 91 | else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == 1) 92 | 93 | return RLRotation(p); 94 | return p; 95 | } 96 | int main() 97 | { 98 | 99 | root = RInsert(root, 50); 100 | RInsert(root, 10); 101 | 102 | RInsert(root, 20); 103 | return 0; 104 | } -------------------------------------------------------------------------------- /Trees/BST/Operations: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/proghead00/Data-Structures-and-Algorithms-In-C-Cpp/567eebfb181fb618c3ac9acf45043e442ed3b98b/Trees/BST/Operations -------------------------------------------------------------------------------- /Trees/BST/Operations.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node *lchild; 8 | struct Node *rchild; 9 | } *root = NULL; 10 | 11 | struct Node *RInsert(struct Node *p, int key) 12 | { 13 | struct Node *t = NULL; 14 | 15 | if (p == NULL) 16 | { 17 | t = (struct Node *)malloc(sizeof(struct Node)); 18 | t->data = key; 19 | t->lchild = t->rchild = NULL; 20 | return t; 21 | } 22 | 23 | else if (key < p->data) 24 | { 25 | p->lchild = RInsert(p->lchild, key); 26 | } 27 | else if (key > p->data) 28 | { 29 | p->rchild = RInsert(p->rchild, key); 30 | } 31 | 32 | return p; 33 | } 34 | 35 | int height(struct Node *p) 36 | { 37 | int x, y; 38 | 39 | if (p == NULL) 40 | { 41 | return 0; 42 | } 43 | 44 | x = height(p->lchild); 45 | y = height(p->rchild); 46 | 47 | return x > y ? x + 1 : y + 1; 48 | } 49 | 50 | struct Node *inorderPredecessor(struct Node *p) 51 | { 52 | while (p && p->rchild != NULL) 53 | { 54 | p = p->rchild; 55 | } 56 | 57 | return p; 58 | } 59 | 60 | struct Node *inorderSuccessor(struct Node *p) 61 | { 62 | while (p && p->lchild != NULL) 63 | { 64 | p = p->lchild; 65 | } 66 | 67 | return p; 68 | } 69 | 70 | struct Node *Delete(struct Node *p, int key) 71 | 72 | { 73 | struct Node *q; 74 | if (p == NULL) 75 | 76 | { 77 | return NULL; 78 | } 79 | 80 | if (p->lchild == NULL && p->rchild == NULL) 81 | { 82 | if (p == root) 83 | { 84 | root = NULL; 85 | } 86 | free(p); 87 | return NULL; 88 | } 89 | 90 | if (key < p->data) 91 | { 92 | p->lchild = Delete(p->lchild, key); 93 | } 94 | 95 | else if (key > p->data) 96 | p->rchild = Delete(p->rchild, key); 97 | 98 | else 99 | { 100 | if (height(p->lchild) > height(p->rchild)) 101 | { 102 | q = inorderPredecessor(p->lchild); 103 | p->data = q->data; 104 | p->lchild = Delete(p->lchild, q->data); 105 | } 106 | else 107 | { 108 | q = inorderSuccessor(p->rchild); 109 | p->data = q->data; 110 | p->rchild = Delete(p->rchild, q->data); 111 | } 112 | } 113 | return p; 114 | } 115 | 116 | struct Node *Search(int key) 117 | { 118 | struct Node *t = root; 119 | 120 | while (t != NULL) 121 | { 122 | 123 | if (key == t->data) 124 | { 125 | return t; 126 | } 127 | 128 | else if (key < t->data) 129 | t = t->lchild; 130 | 131 | else 132 | t = t->rchild; 133 | } 134 | return NULL; 135 | } 136 | 137 | void Inorder(struct Node *p) 138 | { 139 | if (p) 140 | { 141 | Inorder(p->lchild); 142 | printf("%d ", p->data); 143 | Inorder(p->rchild); 144 | } 145 | } 146 | int main() 147 | { 148 | 149 | struct Node *temp; 150 | 151 | root = RInsert(root, 50); 152 | RInsert(root, 10); 153 | RInsert(root, 40); 154 | RInsert(root, 20); 155 | RInsert(root, 30); 156 | 157 | Delete(root, 30); 158 | 159 | Inorder(root); 160 | 161 | printf("\n"); 162 | temp = Search(30); 163 | if (temp != NULL) 164 | printf(" %d is found\n", temp->data); 165 | else 166 | printf("Element is not found\n"); 167 | return 0; 168 | } 169 | -------------------------------------------------------------------------------- /Trees/Binary Tree/Queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | struct Node *lchild; 7 | int data; 8 | struct Node *rchild; 9 | }; 10 | struct Queue 11 | { 12 | int size; 13 | int front; 14 | int rear; 15 | struct Node **Q; 16 | }; 17 | void create(struct Queue *q, int size) 18 | { 19 | q->size = size; 20 | q->front = q->rear = 0; 21 | q->Q = (struct Node **)malloc(q->size * sizeof(struct 22 | Node *)); 23 | } 24 | void enqueue(struct Queue *q, struct Node *x) 25 | { 26 | if ((q->rear + 1) % q->size == q->front) 27 | printf("Queue is Full"); 28 | else 29 | { 30 | q->rear = (q->rear + 1) % q->size; 31 | q->Q[q->rear] = x; 32 | } 33 | } 34 | struct Node *dequeue(struct Queue *q) 35 | { 36 | struct Node *x = NULL; 37 | 38 | if (q->front == q->rear) 39 | printf("Queue is Empty\n"); 40 | else 41 | { 42 | q->front = (q->front + 1) % q->size; 43 | x = q->Q[q->front]; 44 | } 45 | return x; 46 | } 47 | int isEmpty(struct Queue q) 48 | { 49 | return q.front == q.rear; 50 | } 51 | -------------------------------------------------------------------------------- /Trees/Binary Tree/binaryTreeCreate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/proghead00/Data-Structures-and-Algorithms-In-C-Cpp/567eebfb181fb618c3ac9acf45043e442ed3b98b/Trees/Binary Tree/binaryTreeCreate -------------------------------------------------------------------------------- /Trees/Binary Tree/binaryTreeCreate.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Queue.h" 4 | 5 | struct Node *root = NULL; 6 | void Treecreate() 7 | { 8 | struct Node *p, *t; 9 | int x; 10 | struct Queue q; 11 | create(&q, 100); 12 | printf("Enter root value "); 13 | scanf("%d", &x); 14 | root = (struct Node *)malloc(sizeof(struct Node)); 15 | root->data = x; 16 | root->lchild = root->rchild = NULL; 17 | enqueue(&q, root); 18 | while (!isEmpty(q)) 19 | { 20 | p = dequeue(&q); 21 | printf("Enter left child of %d ", p->data); 22 | scanf("%d", &x); 23 | if (x != -1) 24 | { 25 | t = (struct Node *)malloc(sizeof(struct 26 | Node)); 27 | t->data = x; 28 | t->lchild = t->rchild = NULL; 29 | p->lchild = t; 30 | enqueue(&q, t); 31 | } 32 | printf("Enter right child of %d ", p->data); 33 | scanf("%d", &x); 34 | if (x != -1) 35 | { 36 | t = (struct Node *)malloc(sizeof(struct 37 | Node)); 38 | t->data = x; 39 | t->lchild = t->rchild = NULL; 40 | p->rchild = t; 41 | enqueue(&q, t); 42 | } 43 | } 44 | } 45 | void Preorder(struct Node *p) 46 | { 47 | if (p) 48 | { 49 | printf("%d ", p->data); 50 | Preorder(p->lchild); 51 | Preorder(p->rchild); 52 | } 53 | } 54 | void Inorder(struct Node *p) 55 | { 56 | if (p) 57 | { 58 | Inorder(p->lchild); 59 | printf("%d ", p->data); 60 | Inorder(p->rchild); 61 | } 62 | } 63 | void Postorder(struct Node *p) 64 | { 65 | if (p) 66 | { 67 | Postorder(p->lchild); 68 | Postorder(p->rchild); 69 | printf("%d ", p->data); 70 | } 71 | } 72 | int main() 73 | { 74 | Treecreate(); 75 | printf("\nPre Order: "); 76 | Preorder(root); 77 | 78 | printf("\nPost Order: "); 79 | Postorder(root); 80 | 81 | return 0; 82 | } -------------------------------------------------------------------------------- /Trees/Iterative Tree Traversal/Queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Queue 4 | { 5 | int size; 6 | int front; 7 | int rear; 8 | int *Q; 9 | }; 10 | void create(struct Queue *q, int size) 11 | { 12 | q->size = size; 13 | q->front = q->rear = 0; 14 | q->Q = (int *)malloc(q->size * sizeof(int)); 15 | } 16 | void enqueue(struct Queue *q, int x) 17 | { 18 | if ((q->rear + 1) % q->size == q->front) 19 | printf("Queue is Full"); 20 | else 21 | { 22 | q->rear = (q->rear + 1) % q->size; 23 | q->Q[q->rear] = x; 24 | } 25 | } 26 | int dequeue(struct Queue *q) 27 | { 28 | int x = -1; 29 | if (q->front == q->rear) 30 | printf("Queue is Empty\n"); 31 | else 32 | 33 | { 34 | q->front = (q->front + 1) % q->size; 35 | x = q->Q[q->front]; 36 | 37 | return x; 38 | } 39 | } 40 | void Display(struct Queue q) 41 | { 42 | int i = q.front + 1; 43 | do 44 | { 45 | 46 | printf("%d ", q.Q[i]); 47 | i = (i + 1) % q.size; 48 | 49 | } while (i != (q.rear + 1) % q.size); 50 | 51 | printf("\n"); 52 | } 53 | -------------------------------------------------------------------------------- /Trees/Iterative Tree Traversal/Stack.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Stack 5 | { 6 | int size; 7 | int top; 8 | int *S; 9 | }; 10 | 11 | void create(struct Stack *st) 12 | { 13 | printf("Enter size of stack: "); 14 | scanf("%d", &st->size); 15 | st->top = -1; 16 | 17 | st->S = (int *)malloc(st->size * sizeof(int)); 18 | } 19 | 20 | void display(struct Stack st) 21 | { 22 | for (int i = st.top; i >= 0; i--) 23 | { 24 | printf("%d ", st.S[i]); 25 | printf("\n"); 26 | } 27 | } 28 | 29 | void push(struct Stack *st, int x) 30 | { 31 | if (st->top == st->size - 1) 32 | { 33 | printf("Stack overflow\n"); 34 | } 35 | 36 | else 37 | { 38 | st->top++; 39 | st->S[st->top] = x; 40 | } 41 | } 42 | 43 | int pop(struct Stack *st) 44 | { 45 | int x = -1; 46 | 47 | if (st->top == -1) 48 | { 49 | printf("Stack underflow"); 50 | } 51 | else 52 | { 53 | 54 | x = st->S[st->top--]; 55 | } 56 | return x; 57 | } 58 | 59 | int peek(struct Stack st, int index) 60 | { 61 | int x = -1; 62 | if (st.top - index + 1 < 0) 63 | printf("Invalid Index \n"); 64 | x = st.S[st.top - index + 1]; 65 | return x; 66 | } 67 | -------------------------------------------------------------------------------- /Trees/Iterative Tree Traversal/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Queue.h" 4 | #include "Stack.h" 5 | 6 | struct Node *root = NULL; 7 | void Treecreate() 8 | { 9 | struct Node *p, *t; 10 | int x; 11 | struct Queue q; 12 | create(&q, 100); 13 | printf("Enter root value "); 14 | scanf("%d", &x); 15 | root = (struct Node *)malloc(sizeof(struct Node)); 16 | root->data = x; 17 | root->lchild = root->rchild = NULL; 18 | enqueue(&q, root); 19 | while (!isEmpty(q)) 20 | { 21 | p = dequeue(&q); 22 | printf("eneter left child of %d ", p->data); 23 | scanf("%d", &x); 24 | if (x != -1) 25 | { 26 | t = (struct Node *)malloc(sizeof(struct Node)); 27 | t->data = x; 28 | t->lchild = t->rchild = NULL; 29 | p->lchild = t; 30 | enqueue(&q, t); 31 | } 32 | printf("Enter right child of %d ", p->data); 33 | scanf("%d", &x); 34 | if (x != -1) 35 | { 36 | t = (struct Node *)malloc(sizeof(struct Node)); 37 | t->data = x; 38 | t->lchild = t->rchild = NULL; 39 | p->rchild = t; 40 | enqueue(&q, t); 41 | } 42 | } 43 | } 44 | void IPreorder(struct Node *p) 45 | { 46 | struct Stack stk; 47 | Stackcreate(&stk, 100); 48 | 49 | while (p || !isEmptyStack(stk)) 50 | { 51 | if (p) 52 | { 53 | printf("%d ", p->data); 54 | push(&stk, p); 55 | p = p->lchild; 56 | } 57 | else 58 | { 59 | p = pop(&stk); 60 | p = p->rchild; 61 | } 62 | } 63 | } 64 | void IInorder(struct Node *p) 65 | { 66 | struct Stack stk; 67 | Stackcreate(&stk, 100); 68 | 69 | while (p || !isEmptyStack(stk)) 70 | { 71 | if (p) 72 | { 73 | push(&stk, p); 74 | p = p->lchild; 75 | } 76 | else 77 | { 78 | p = pop(&stk); 79 | printf("%d ", p->data); 80 | p = p->rchild; 81 | } 82 | } 83 | } 84 | int main() 85 | { 86 | 87 | Treecreate(); 88 | IPreOrder(root); 89 | IInorder(root); 90 | return 0; 91 | } -------------------------------------------------------------------------------- /Trees/Queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Queue 4 | { 5 | int size; 6 | int front; 7 | int rear; 8 | int *Q; 9 | }; 10 | void create(struct Queue *q, int size) 11 | { 12 | q->size = size; 13 | q->front = q->rear = 0; 14 | q->Q = (int *)malloc(q->size * sizeof(int)); 15 | } 16 | void enqueue(struct Queue *q, int x) 17 | { 18 | if ((q->rear + 1) % q->size == q->front) 19 | printf("Queue is Full"); 20 | else 21 | { 22 | q->rear = (q->rear + 1) % q->size; 23 | q->Q[q->rear] = x; 24 | } 25 | } 26 | int dequeue(struct Queue *q) 27 | { 28 | int x = -1; 29 | if (q->front == q->rear) 30 | printf("Queue is Empty\n"); 31 | else 32 | 33 | { 34 | q->front = (q->front + 1) % q->size; 35 | x = q->Q[q->front]; 36 | 37 | return x; 38 | } 39 | } 40 | void Display(struct Queue q) 41 | { 42 | int i = q.front + 1; 43 | do 44 | { 45 | 46 | printf("%d ", q.Q[i]); 47 | i = (i + 1) % q.size; 48 | 49 | } while (i != (q.rear + 1) % q.size); 50 | 51 | printf("\n"); 52 | } 53 | -------------------------------------------------------------------------------- /Trees/Stack.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Stack 5 | { 6 | int size; 7 | int top; 8 | int *S; 9 | }; 10 | 11 | void create(struct Stack *st) 12 | { 13 | printf("Enter size of stack: "); 14 | scanf("%d", &st->size); 15 | st->top = -1; 16 | 17 | st->S = (int *)malloc(st->size * sizeof(int)); 18 | } 19 | 20 | void display(struct Stack st) 21 | { 22 | for (int i = st.top; i >= 0; i--) 23 | { 24 | printf("%d ", st.S[i]); 25 | printf("\n"); 26 | } 27 | } 28 | 29 | void push(struct Stack *st, int x) 30 | { 31 | if (st->top == st->size - 1) 32 | { 33 | printf("Stack overflow\n"); 34 | } 35 | 36 | else 37 | { 38 | st->top++; 39 | st->S[st->top] = x; 40 | } 41 | } 42 | 43 | int pop(struct Stack *st) 44 | { 45 | int x = -1; 46 | 47 | if (st->top == -1) 48 | { 49 | printf("Stack underflow"); 50 | } 51 | else 52 | { 53 | 54 | x = st->S[st->top--]; 55 | } 56 | return x; 57 | } 58 | 59 | int peek(struct Stack st, int index) 60 | { 61 | int x = -1; 62 | if (st.top - index + 1 < 0) 63 | printf("Invalid Index \n"); 64 | x = st.S[st.top - index + 1]; 65 | return x; 66 | } 67 | -------------------------------------------------------------------------------- /Trees/binaryTreeWithQueue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node *root = NULL; 5 | 6 | struct Node 7 | { 8 | struct Node *rchild; 9 | 10 | struct Node *lchild; 11 | int data; 12 | }; 13 | 14 | struct Queue 15 | { 16 | int size, front, rear; 17 | struct Node **Q; 18 | }; 19 | 20 | void createQ(struct Queue *q, int size) 21 | { 22 | 23 | q->size = size; 24 | q->front = q->rear = 0; 25 | 26 | q->Q = (struct Node **)malloc(sizeof(struct Node *)); 27 | } 28 | 29 | void enq(struct Queue *q, struct Node *x) 30 | { 31 | 32 | if ((q->rear + 1) % q->size == q->front) 33 | { 34 | printf("Queue is FULL\n"); 35 | } 36 | 37 | else 38 | { 39 | q->rear = (q->rear + 1) % q->size; 40 | q->Q[q->rear] = x; 41 | } 42 | } 43 | 44 | struct Node *deq(struct Queue *q) 45 | { 46 | struct Node *x = NULL; 47 | 48 | if (q->front == q->rear) 49 | { 50 | printf("Queue is EMPTY\n"); 51 | } 52 | 53 | else 54 | { 55 | q->front = (q->front + 1) % q->size; 56 | 57 | x = q->Q[q->front]; 58 | } 59 | 60 | return x; 61 | } 62 | 63 | int isEmpty(struct Queue q) 64 | { 65 | return q.front == q.rear; 66 | } 67 | 68 | void Treecreate() 69 | { 70 | struct Node *p, *t; 71 | int x; 72 | struct Queue q; 73 | createQ(&q, 100); 74 | 75 | printf("Enter root value:"); 76 | scanf("%d", &x); 77 | 78 | root = (struct Node *)malloc(sizeof(struct Node)); 79 | root->data = x; 80 | root->lchild = root->rchild = NULL; 81 | enq(&q, root); 82 | 83 | while (!isEmpty(q)) 84 | { 85 | p = deq(&q); 86 | printf("Enter left child of %d: ", p->data); 87 | scanf("%d", &x); 88 | if (x != -1) 89 | { 90 | t = (struct Node *)malloc(sizeof(struct 91 | Node)); 92 | t->data = x; 93 | t->lchild = t->rchild = NULL; 94 | p->lchild = t; 95 | enq(&q, t); 96 | } 97 | 98 | printf("Enter right child of %d: ", p->data); 99 | scanf("%d", &x); 100 | 101 | if (x != -1) 102 | { 103 | t = (struct Node *)malloc(sizeof(struct 104 | Node)); 105 | t->data = x; 106 | t->lchild = t->rchild = NULL; 107 | p->rchild = t; 108 | enq(&q, t); 109 | } 110 | } 111 | } 112 | 113 | void Preorder(struct Node *p) 114 | { 115 | if (p) 116 | { 117 | printf("%d ", p->data); 118 | Preorder(p->lchild); 119 | Preorder(p->rchild); 120 | } 121 | } 122 | void Inorder(struct Node *p) 123 | { 124 | if (p) 125 | { 126 | Inorder(p->lchild); 127 | printf("%d ", p->data); 128 | Inorder(p->rchild); 129 | } 130 | } 131 | void Postorder(struct Node *p) 132 | { 133 | if (p) 134 | { 135 | Postorder(p->lchild); 136 | Postorder(p->rchild); 137 | printf("%d ", p->data); 138 | } 139 | } 140 | 141 | int main() 142 | { 143 | Treecreate(); 144 | Preorder(root); 145 | printf("\nPost Order "); 146 | Postorder(root); 147 | 148 | return 0; 149 | } -------------------------------------------------------------------------------- /Trees/level.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/proghead00/Data-Structures-and-Algorithms-In-C-Cpp/567eebfb181fb618c3ac9acf45043e442ed3b98b/Trees/level.cpp -------------------------------------------------------------------------------- /Trees/tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class node{ 6 | public: 7 | int data; 8 | node*left; 9 | node*right; 10 | 11 | node(int d){ 12 | data = d; 13 | left = NULL; 14 | right = NULL; 15 | } 16 | }; 17 | 18 | node* buildTree(){ 19 | int d; 20 | cin>>d; 21 | 22 | if(d==-1){ 23 | return NULL; 24 | } 25 | node * root = new node(d); 26 | root->left = buildTree(); 27 | root->right = buildTree(); 28 | return root; 29 | } 30 | void print(node *root){ 31 | if(root==NULL){ 32 | return; 33 | } 34 | //Otherwise, print root first followed by subtrees(children) 35 | cout<data<<" "; 36 | print(root->left); 37 | print(root->right); 38 | } 39 | 40 | void printIn(node*root){ 41 | if(root==NULL){ 42 | return; 43 | } 44 | //Otherwise Left Root Right 45 | printIn(root->left); 46 | cout<data<<" "; 47 | printIn(root->right); 48 | } 49 | 50 | void printPost(node*root){ 51 | if(root==NULL){ 52 | return; 53 | } 54 | printPost(root->left); 55 | printPost(root->right); 56 | cout<data<<" "; 57 | } 58 | 59 | int height(node*root){ 60 | if(root==NULL){ 61 | return 0; 62 | } 63 | int ls = height(root->left); 64 | int rs = height(root->right); 65 | return max(ls,rs) + 1; 66 | 67 | } 68 | 69 | void printKthLevel(node*root,int k){ 70 | if(root==NULL){ 71 | return; 72 | } 73 | if(k==1){ 74 | cout<data<<" "; 75 | return; 76 | } 77 | printKthLevel(root->left,k-1); 78 | printKthLevel(root->right,k-1); 79 | return; 80 | 81 | } 82 | 83 | void printAllLevels(node*root){ 84 | int H = height(root); 85 | 86 | for(int i=1;i<=H;i++){ 87 | printKthLevel(root,i); 88 | cout<left) + count(root->right); 98 | } 99 | 100 | void bfs(node *root){ 101 | queue q; 102 | q.push(root); 103 | q.push(NULL); 104 | 105 | while(!q.empty()){ 106 | node* f = q.front(); 107 | if(f==NULL){ 108 | cout<data<<","; 116 | q.pop(); 117 | 118 | if(f->left){ 119 | q.push(f->left); 120 | } 121 | if(f->right){ 122 | q.push(f->right); 123 | } 124 | } 125 | } 126 | return; 127 | } 128 | 129 | void mirror(node*root){ 130 | if(root==NULL){ 131 | return; 132 | } 133 | swap(root->left,root->right); 134 | mirror(root->left); 135 | mirror(root->right); 136 | return; 137 | } 138 | 139 | class Pair{ 140 | public: 141 | int height; 142 | int diameter; 143 | }; 144 | 145 | Pair fastDiameter(node*root){ 146 | Pair p; 147 | if(root==NULL){ 148 | p.diameter = p.height = 0; 149 | return p; 150 | } 151 | //Otherwise 152 | Pair left = fastDiameter(root->left); 153 | Pair right = fastDiameter(root->right); 154 | 155 | p.height = max(left.height,right.height) + 1; 156 | p.diameter = max(left.height+right.height, max(left.diameter,right.diameter)); 157 | return p; 158 | } 159 | 160 | int main(){ 161 | node* root = buildTree(); 162 | //cout< 3 | // #include 4 | // #include "Queue.h" 5 | // #include "Stack.h" 6 | 7 | // struct Node *root = NULL; 8 | 9 | // void Tcreate() 10 | // { 11 | 12 | // struct Node *p, *t; 13 | // int x; 14 | 15 | // struct Queue q; 16 | // create(&q, 100); 17 | 18 | // printf("Enter root value: "); 19 | // scanf("%d", &x); 20 | 21 | // root = (struct Node *)malloc(sizeof(struct Node)); 22 | 23 | // root->data = x; 24 | // root->lchild = root->rchild = NULL; 25 | // enqueue(&q, root); 26 | // } --------------------------------------------------------------------------------