diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 00000000..f9128470 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x86", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "C:/MinGW/bin/gcc.exe", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x86", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..2f014ea6 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": ".", + "program": "build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..bb879da5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/Data Structure Using C/array/array.c b/Data Structure Using C/array/array.c new file mode 100644 index 00000000..0a8c35d2 --- /dev/null +++ b/Data Structure Using C/array/array.c @@ -0,0 +1,93 @@ +#include +#define size 50 + +int main(){ + int n=5,arr[size]={1,2,3,4,5}; + int choice,ele,pos,i; + int low=1,high=n,mid,flag=0; + while(1){ + printf("1.Traverse\n2.Insert\n3.Delete\n4.Sequentciatly Search\n5.Binary Search\n6.Exit\n"); + printf("Enter Your Choice: "); + scanf("%d",&choice); + switch(choice){ + case 1: + for(i=0;i=pos;i--){ + arr[i+1]=arr[i]; + printf("POS: %d I: %d ARR[i]: %d ARR[i+1]: %d\n",pos,i,arr[i],arr[i+1]); + } + arr[pos]=ele; + n++; + for(int i=0;iarr[mid]){ + low=mid+1; + }else{ + printf("Search Succesfully! %d founded",ele); + flag=1; + break; + } + } + if(flag==0){ + printf("%d is not found.",ele); + } + break; + case 6: + return 0; + default: + printf("Please! Enter valid choice."); + + } +printf("\n\n"); + } + return 0; +} \ No newline at end of file diff --git a/Data Structure Using C/linked-list/linked-list-doubly.c b/Data Structure Using C/linked-list/linked-list-doubly.c new file mode 100644 index 00000000..8bbaf468 --- /dev/null +++ b/Data Structure Using C/linked-list/linked-list-doubly.c @@ -0,0 +1,136 @@ +#include +#include + +// Define the structure of a node +struct Node { + int data; + struct Node* next; +}; + +struct Node* head = NULL; // Head pointer to the singly linked list + +// Function to traverse and display the list +void traverse() { + if (head == NULL) { + printf("List is empty.\n"); + } else { + struct Node* temp = head; + printf("List: "); + while (temp != NULL) { + printf("%d -> ", temp->data); + temp = temp->next; + } + printf("NULL\n"); + } +} + +// Function to insert a node at the end +void insertNode(int data) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = data; + newNode->next = NULL; + + if (head == NULL) { + head = newNode; // If the list is empty + } else { + struct Node* temp = head; + while (temp->next != NULL) { + temp = temp->next; + } + temp->next = newNode; // Insert at the end + } + printf("%d inserted into the list.\n", data); +} + +// Function to delete a node by value +void deleteNode(int value) { + if (head == NULL) { + printf("List is empty. Nothing to delete.\n"); + return; + } + struct Node *temp = head, *prev = NULL; + + // Check if the head node needs to be deleted + if (temp != NULL && temp->data == value) { + head = temp->next; + free(temp); + printf("%d deleted from the list.\n", value); + return; + } + + // Traverse to find the node to delete + while (temp != NULL && temp->data != value) { + prev = temp; + temp = temp->next; + } + + if (temp == NULL) { // Value not found + printf("Value %d not found in the list.\n", value); + return; + } + + prev->next = temp->next; // Unlink the node + free(temp); + printf("%d deleted from the list.\n", value); +} + +// Function to search for a node +void searchNode(int value) { + struct Node* temp = head; + int position = 1; + + while (temp != NULL) { + if (temp->data == value) { + printf("%d found at position %d.\n", value, position); + return; + } + temp = temp->next; + position++; + } + printf("%d not found in the list.\n", value); +} + +// Main function +int main() { + int choice, value; + + printf("SINGLY LINKED LIST IMPLEMENTATION\n\n"); + while (1) { + printf("Menu:\n"); + printf("1. Traverse\n"); + printf("2. Insert Node\n"); + printf("3. Delete Node\n"); + printf("4. Search Node\n"); + printf("5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + traverse(); + break; + case 2: + printf("Enter value to insert: "); + scanf("%d", &value); + insertNode(value); + break; + case 3: + printf("Enter value to delete: "); + scanf("%d", &value); + deleteNode(value); + break; + case 4: + printf("Enter value to search: "); + scanf("%d", &value); + searchNode(value); + break; + case 5: + printf("Exiting...\n"); + return 0; + default: + printf("Invalid choice. Try again.\n"); + } + printf("\n"); + } + return 0; +} diff --git a/Data Structure Using C/linked-list/linked-list-singly.c b/Data Structure Using C/linked-list/linked-list-singly.c new file mode 100644 index 00000000..8bbaf468 --- /dev/null +++ b/Data Structure Using C/linked-list/linked-list-singly.c @@ -0,0 +1,136 @@ +#include +#include + +// Define the structure of a node +struct Node { + int data; + struct Node* next; +}; + +struct Node* head = NULL; // Head pointer to the singly linked list + +// Function to traverse and display the list +void traverse() { + if (head == NULL) { + printf("List is empty.\n"); + } else { + struct Node* temp = head; + printf("List: "); + while (temp != NULL) { + printf("%d -> ", temp->data); + temp = temp->next; + } + printf("NULL\n"); + } +} + +// Function to insert a node at the end +void insertNode(int data) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = data; + newNode->next = NULL; + + if (head == NULL) { + head = newNode; // If the list is empty + } else { + struct Node* temp = head; + while (temp->next != NULL) { + temp = temp->next; + } + temp->next = newNode; // Insert at the end + } + printf("%d inserted into the list.\n", data); +} + +// Function to delete a node by value +void deleteNode(int value) { + if (head == NULL) { + printf("List is empty. Nothing to delete.\n"); + return; + } + struct Node *temp = head, *prev = NULL; + + // Check if the head node needs to be deleted + if (temp != NULL && temp->data == value) { + head = temp->next; + free(temp); + printf("%d deleted from the list.\n", value); + return; + } + + // Traverse to find the node to delete + while (temp != NULL && temp->data != value) { + prev = temp; + temp = temp->next; + } + + if (temp == NULL) { // Value not found + printf("Value %d not found in the list.\n", value); + return; + } + + prev->next = temp->next; // Unlink the node + free(temp); + printf("%d deleted from the list.\n", value); +} + +// Function to search for a node +void searchNode(int value) { + struct Node* temp = head; + int position = 1; + + while (temp != NULL) { + if (temp->data == value) { + printf("%d found at position %d.\n", value, position); + return; + } + temp = temp->next; + position++; + } + printf("%d not found in the list.\n", value); +} + +// Main function +int main() { + int choice, value; + + printf("SINGLY LINKED LIST IMPLEMENTATION\n\n"); + while (1) { + printf("Menu:\n"); + printf("1. Traverse\n"); + printf("2. Insert Node\n"); + printf("3. Delete Node\n"); + printf("4. Search Node\n"); + printf("5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + traverse(); + break; + case 2: + printf("Enter value to insert: "); + scanf("%d", &value); + insertNode(value); + break; + case 3: + printf("Enter value to delete: "); + scanf("%d", &value); + deleteNode(value); + break; + case 4: + printf("Enter value to search: "); + scanf("%d", &value); + searchNode(value); + break; + case 5: + printf("Exiting...\n"); + return 0; + default: + printf("Invalid choice. Try again.\n"); + } + printf("\n"); + } + return 0; +} diff --git a/Data Structure Using C/queue/queue.c b/Data Structure Using C/queue/queue.c new file mode 100644 index 00000000..ea128971 --- /dev/null +++ b/Data Structure Using C/queue/queue.c @@ -0,0 +1,89 @@ +#include +#define SIZE 100 + +int queue[SIZE]; +int n = 5; +int rear = -1; +int front = -1; + + +void qTraverse() { + if (front == -1 || front > rear) { + printf("Queue is empty.\n"); + } else { + printf("Queue: "); + for (int i = front; i <= rear; i++) { + printf("%d ", queue[i]); + } + printf("\n"); + } +} + + +void enqueue(int ele) { + if (rear == n - 1) { + printf("Queue Overflow. Cannot enqueue %d.\n", ele); + } else { + if (front == -1) { + front = 0; + } + rear++; + queue[rear] = ele; + printf("%d is added to the queue.\n", ele); + qTraverse(); + } +} + + +void dequeue() { + if (front == -1 || front > rear) { + printf("Queue Underflow. Cannot dequeue.\n"); + } else { + int ele = queue[front]; + front++; + printf("%d is dequeued from the queue.\n", ele); + if (front > rear) { + front = rear = -1; + } + qTraverse(); + } +} + + +int main() { + int ele, choice; + + printf("QUEUE IMPLEMENTATION\n\n"); + + while (1) { + printf("Menu:\n"); + printf("1. TRAVERSE\n"); + printf("2. ENQUEUE\n"); + printf("3. DEQUEUE\n"); + printf("4. EXIT\n"); + printf("Enter Your Choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + qTraverse(); + break; + case 2: + printf("Enter the element to enqueue: "); + scanf("%d", &ele); + enqueue(ele); + break; + case 3: + dequeue(); + break; + case 4: + printf("Exiting the program.\n"); + return 0; + default: + printf("Invalid choice. Please try again.\n"); + } + printf("\n"); + } + return 0; +} + diff --git a/Data Structure Using C/sorts/sorts.c b/Data Structure Using C/sorts/sorts.c new file mode 100644 index 00000000..54f29827 --- /dev/null +++ b/Data Structure Using C/sorts/sorts.c @@ -0,0 +1,263 @@ +#include +#include + +// Function prototypes for sorting algorithms +void bubbleSort(int arr[], int n); +void selectionSort(int arr[], int n); +void insertionSort(int arr[], int n); +void mergeSort(int arr[], int l, int r); +void quickSort(int arr[], int low, int high); +void radixSort(int arr[], int n); +void heapSort(int arr[], int n); +void displayArray(int arr[], int n); + +// Helper function prototypes +void merge(int arr[], int l, int m, int r); +int partition(int arr[], int low, int high); +void heapify(int arr[], int n, int i); +int getMax(int arr[], int n); +void countSortForRadix(int arr[], int n, int exp); + +// Main function to demonstrate all sorting algorithms +int main() { + int choice, n, i; + printf("Enter the number of elements: "); + scanf("%d", &n); + int arr[n], original[n]; + + printf("Enter the elements of the array:\n"); + for (i = 0; i < n; i++) { + scanf("%d", &arr[i]); + original[i] = arr[i]; // Save the original array for reuse + } + + while (1) { + // Menu for sorting algorithms + printf("\nSorting Algorithms Menu:\n"); + printf("1. Bubble Sort\n"); + printf("2. Selection Sort\n"); + printf("3. Insertion Sort\n"); + printf("4. Merge Sort\n"); + printf("5. Quick Sort\n"); + printf("6. Radix Sort\n"); + printf("7. Heap Sort\n"); + printf("8. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + // Reset array to original + for (i = 0; i < n; i++) { + arr[i] = original[i]; + } + + switch (choice) { + case 1: + bubbleSort(arr, n); + printf("Array after Bubble Sort: "); + displayArray(arr, n); + break; + case 2: + selectionSort(arr, n); + printf("Array after Selection Sort: "); + displayArray(arr, n); + break; + case 3: + insertionSort(arr, n); + printf("Array after Insertion Sort: "); + displayArray(arr, n); + break; + case 4: + mergeSort(arr, 0, n - 1); + printf("Array after Merge Sort: "); + displayArray(arr, n); + break; + case 5: + quickSort(arr, 0, n - 1); + printf("Array after Quick Sort: "); + displayArray(arr, n); + break; + case 6: + radixSort(arr, n); + printf("Array after Radix Sort: "); + displayArray(arr, n); + break; + case 7: + heapSort(arr, n); + printf("Array after Heap Sort: "); + displayArray(arr, n); + break; + case 8: + printf("Exiting the program.\n"); + return 0; + default: + printf("Invalid choice. Try again.\n"); + } + } + return 0; +} + +// Function to display the array +void displayArray(int arr[], int n) { + for (int i = 0; i < n; i++) { + printf("%d ", arr[i]); + } + printf("\n"); +} + +// Bubble Sort +void bubbleSort(int arr[], int n) { + for (int i = 0; i < n - 1; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } +} + +// Selection Sort +void selectionSort(int arr[], int n) { + for (int i = 0; i < n - 1; i++) { + int minIdx = i; + for (int j = i + 1; j < n; j++) { + if (arr[j] < arr[minIdx]) { + minIdx = j; + } + } + int temp = arr[i]; + arr[i] = arr[minIdx]; + arr[minIdx] = temp; + } +} + +// Insertion Sort +void insertionSort(int arr[], int n) { + for (int i = 1; i < n; i++) { + int key = arr[i]; + int j = i - 1; + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = key; + } +} + +// Merge Sort +void mergeSort(int arr[], int l, int r) { + if (l < r) { + int m = l + (r - l) / 2; + mergeSort(arr, l, m); + mergeSort(arr, m + 1, r); + merge(arr, l, m, r); + } +} + +// Merge function for Merge Sort +void merge(int arr[], int l, int m, int r) { + int n1 = m - l + 1; + int n2 = r - m; + int left[n1], right[n2]; + + for (int i = 0; i < n1; i++) left[i] = arr[l + i]; + for (int i = 0; i < n2; i++) right[i] = arr[m + 1 + i]; + + int i = 0, j = 0, k = l; + while (i < n1 && j < n2) { + arr[k++] = (left[i] <= right[j]) ? left[i++] : right[j++]; + } + + while (i < n1) arr[k++] = left[i++]; + while (j < n2) arr[k++] = right[j++]; +} + +// Quick Sort +void quickSort(int arr[], int low, int high) { + if (low < high) { + int pi = partition(arr, low, high); + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } +} + +// Partition function for Quick Sort +int partition(int arr[], int low, int high) { + int pivot = arr[high]; + int i = low - 1; + + for (int j = low; j < high; j++) { + if (arr[j] < pivot) { + i++; + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + int temp = arr[i + 1]; + arr[i + 1] = arr[high]; + arr[high] = temp; + + return i + 1; +} + +// Radix Sort +void radixSort(int arr[], int n) { + int max = getMax(arr, n); + for (int exp = 1; max / exp > 0; exp *= 10) { + countSortForRadix(arr, n, exp); + } +} + +// Get maximum value in the array +int getMax(int arr[], int n) { + int max = arr[0]; + for (int i = 1; i < n; i++) { + if (arr[i] > max) { + max = arr[i]; + } + } + return max; +} + +// Count sort for Radix Sort +void countSortForRadix(int arr[], int n, int exp) { + int output[n]; + int count[10] = {0}; + + for (int i = 0; i < n; i++) count[(arr[i] / exp) % 10]++; + for (int i = 1; i < 10; i++) count[i] += count[i - 1]; + for (int i = n - 1; i >= 0; i--) { + output[count[(arr[i] / exp) % 10] - 1] = arr[i]; + count[(arr[i] / exp) % 10]--; + } + for (int i = 0; i < n; i++) arr[i] = output[i]; +} + +// Heap Sort +void heapSort(int arr[], int n) { + for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); + for (int i = n - 1; i > 0; i--) { + int temp = arr[0]; + arr[0] = arr[i]; + arr[i] = temp; + heapify(arr, i, 0); + } +} + +// Heapify function for Heap Sort +void heapify(int arr[], int n, int i) { + int largest = i; + int left = 2 * i + 1; + int right = 2 * i + 2; + + if (left < n && arr[left] > arr[largest]) largest = left; + if (right < n && arr[right] > arr[largest]) largest = right; + if (largest != i) { + int temp = arr[i]; + arr[i] = arr[largest]; + arr[largest] = temp; + heapify(arr, n, largest); + } +} diff --git a/Data Structure Using C/string/string.c b/Data Structure Using C/string/string.c new file mode 100644 index 00000000..fd6fc223 --- /dev/null +++ b/Data Structure Using C/string/string.c @@ -0,0 +1,29 @@ +#include +#define size 50 + +int main() +{ + char str[size]; + int choice; + int i = 0; + + printf("1.Length\n2.lower\n3.upper\n4.copy\n5.append\n6.reverse\n7.compare\n8.substr\n9.insert\n10.delete"); + scanf("%d", &choice); + + switch (choice) + { + case 1: + printf("Enter str: "); + scanf("%s", str); + // printf("%s",str); + + while (str[i] != '\0') + { + i++; + } + printf("Length of %s is: %d", str, i); + } + break; + + return 0; +} diff --git a/index.html b/index.html new file mode 100644 index 00000000..af4d5b14 --- /dev/null +++ b/index.html @@ -0,0 +1,12 @@ + + + + + + Learn About open source + + + Learn to make a pull request +
Feature
+ + \ No newline at end of file