diff --git a/Data Structure Using C/queue/circuler_queue.c b/Data Structure Using C/queue/circuler_queue.c new file mode 100644 index 00000000..da2a7214 --- /dev/null +++ b/Data Structure Using C/queue/circuler_queue.c @@ -0,0 +1,80 @@ +#include +#define MAX 5 // Size of circular queue + +int queue[MAX]; +int front = -1, rear = -1; + +// Function to insert (enqueue) element +void enqueue(int value) { + if ((front == 0 && rear == MAX - 1) || (rear + 1) % MAX == front) { + printf("Queue Overflow! Cannot insert %d\n", value); + } else { + if (front == -1) // First element + front = 0; + rear = (rear + 1) % MAX; + queue[rear] = value; + printf("%d inserted into queue.\n", value); + } +} + +// Function to remove (dequeue) element +void dequeue() { + if (front == -1) { + printf("Queue Underflow! Nothing to delete.\n"); + } else { + printf("%d deleted from queue.\n", queue[front]); + if (front == rear) { + // Queue became empty + front = rear = -1; + } else { + front = (front + 1) % MAX; + } + } +} + +// Function to display queue elements +void display() { + if (front == -1) { + printf("Queue is empty.\n"); + } else { + printf("Queue elements: "); + int i = front; + while (1) { + printf("%d ", queue[i]); + if (i == rear) break; + i = (i + 1) % MAX; + } + printf("\n"); + } +} + +// Main function +int main() { + int choice, value; + + while (1) { + printf("\n--- Circular Queue Menu ---\n"); + printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + printf("Enter value to insert: "); + scanf("%d", &value); + enqueue(value); + break; + case 2: + dequeue(); + break; + case 3: + display(); + break; + case 4: + printf("Exiting program...\n"); + return 0; + default: + printf("Invalid choice! Try again.\n"); + } + } +} diff --git a/Data Structure Using C/queue/normal_queue.c b/Data Structure Using C/queue/normal_queue.c new file mode 100644 index 00000000..3ec96d2d --- /dev/null +++ b/Data Structure Using C/queue/normal_queue.c @@ -0,0 +1,72 @@ +#include +#define MAX 5 // Maximum size of queue + +int queue[MAX]; +int front = -1, rear = -1; + +// Function to insert (enqueue) element in queue +void enqueue(int value) { + if (rear == MAX - 1) { + printf("Queue Overflow! Cannot insert %d\n", value); + } else { + if (front == -1) // first element + front = 0; + rear++; + queue[rear] = value; + printf("%d inserted into queue.\n", value); + } +} + +// Function to remove (dequeue) element from queue +void dequeue() { + if (front == -1 || front > rear) { + printf("Queue Underflow! Nothing to delete.\n"); + } else { + printf("%d deleted from queue.\n", queue[front]); + front++; + } +} + +// Function to display the queue +void display() { + if (front == -1 || front > rear) { + printf("Queue is empty.\n"); + } else { + printf("Queue elements: "); + for (int i = front; i <= rear; i++) { + printf("%d ", queue[i]); + } + printf("\n"); + } +} + +// Main function +int main() { + int choice, value; + + while (1) { + printf("\n--- Queue Menu ---\n"); + printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + printf("Enter value to insert: "); + scanf("%d", &value); + enqueue(value); + break; + case 2: + dequeue(); + break; + case 3: + display(); + break; + case 4: + printf("Exiting program...\n"); + return 0; + default: + printf("Invalid choice! Try again.\n"); + } + } +} diff --git a/Data Structure Using C/search/binarysearch.c b/Data Structure Using C/search/binarysearch.c new file mode 100644 index 00000000..9561ffc3 --- /dev/null +++ b/Data Structure Using C/search/binarysearch.c @@ -0,0 +1,45 @@ +#include + +int main() { + int arr[100], n, i, key; + int low, high, mid; + + // Input array size + printf("Enter the number of elements: "); + scanf("%d", &n); + + // Input sorted array + printf("Enter %d sorted elements:\n", n); + for (i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + // Input the element to search for + printf("Enter the element to search: "); + scanf("%d", &key); + + // Binary Search starts + low = 0; + high = n - 1; + int found = 0; + + while (low <= high) { + mid = (low + high) / 2; + + if (arr[mid] == key) { + printf("Element found at index %d (position %d)\n", mid , mid+1); + found = 1; + break; + } else if (arr[mid] < key) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + if (!found) { + printf("Element not found in the array.\n"); + } + + return 0; +} \ No newline at end of file diff --git a/Data Structure Using C/search/linearsearch.c b/Data Structure Using C/search/linearsearch.c new file mode 100644 index 00000000..fc3e6eb3 --- /dev/null +++ b/Data Structure Using C/search/linearsearch.c @@ -0,0 +1,36 @@ +#include + +int linearSearch(int arr[], int size, int target) { + int i; + for (i = 0; i < size; i++) { + if (arr[i] == target) { + return i; //this is if the element is found + } + } + return -1; //this will give not found if element not found +} + +int main() { + int arr[100], n, target, i; + + printf("Enter the number of elements: "); + scanf("%d", &n); + + printf("Enter %d elements:\n", n); + for (i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + printf("Enter the element to search for: "); + scanf("%d", &target); + + int result = linearSearch(arr, n, target); + + if (result != -1) { + printf("Element found at index %d (position %d)\n", result, result + 1); + } else { + printf("Element not found in the array.\n"); + } + + return 0; +} \ No newline at end of file diff --git a/Data Structure Using C/sort/bubblrsort.c b/Data Structure Using C/sort/bubblrsort.c new file mode 100644 index 00000000..3438c455 --- /dev/null +++ b/Data Structure Using C/sort/bubblrsort.c @@ -0,0 +1,30 @@ +#include + +void bubblesort(int arr[] , int n) { + for(int i=0 ; i arr[j+1]){ + int temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + } + } + } +} + +void display(int arr[] , int n){ + for(int i=0 ; i + +void insertionsort(int arr[] , int n){ + for(int i=1 ; i=0 && arr[pre]>curr){ + arr[pre+1]=arr[pre]; + pre--; + } + arr[pre+1]=curr; + } +} + +void display(int arr[] , int n){ + for(int i=0 ; i + +void merge(int arr[] , int st , int mid , int end){ + int temp[100]; + int i=st , j=mid+1 , k=0; + while(i<=mid && j<= end){ + if(arr[i] < arr[j]){ + temp[k++]=arr[i++]; + } + else{ + temp[k++]=arr[j++]; + } + } + while(i<=mid){ + temp[k++]=arr[i++]; + } + while(j<=end){ + temp[k++]=arr[j++]; + } + for(i=st , j=0 ; i<=end ; i++ , j++){ + arr[i]=temp[j]; + } + +} + +void mergesort(int arr[] , int st , int end){ + if(st < end){ + int mid = (st+end)/2; + mergesort(arr , st , mid); //left + mergesort(arr , mid+1 , end); //right + + merge(arr , st , mid , end); + } + +} + + + +void display(int arr[] , int n){ + for(int i=0 ; i + +void selectionsort(int arr[] , int n){ + for(int i=0 ; i