diff --git a/Assignment18/8/23.c b/Assignment18/8/23.c new file mode 100644 index 0000000..3e1e6a2 --- /dev/null +++ b/Assignment18/8/23.c @@ -0,0 +1,77 @@ +#include +#include + +// Function to perform counting sort on a specific digit +void countingSort(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]++;//find the last element + } + + for (int i = 1; i < 10; i++) + { + count[i] += count[i - 1];//counting sort + } + + 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]; + } +} + +// Radix Sort function +void radixSort(int arr[], int n, int digits) +{ + int max = arr[0]; + for (int i = 1; i < n; i++) + { + if (arr[i] > max) + { + max = arr[i]; + } + } + + for (int exp = 1; max / exp > 0; exp *= 10) + { + countingSort(arr, n, exp); + } +} + +int main() +{ + int n; + printf("Enter the number of elements: "); + scanf("%d", &n); + + int arr[n]; + printf("Enter %d elements:\n", n); + for (int i = 0; i < n; i++) + { + scanf("%d", &arr[i]); + } + + int digits; + printf("Enter the number of digits in the maximum number: "); + scanf("%d", &digits); + + radixSort(arr, n, digits); + + printf("Sorted array: "); + for (int i = 0; i < n; i++) + { + printf("%d ", arr[i]); + } + printf("\n"); + + return 0; +} diff --git a/Assignment18/8/23.exe b/Assignment18/8/23.exe new file mode 100644 index 0000000..360bba7 Binary files /dev/null and b/Assignment18/8/23.exe differ diff --git a/Assignment18/8/pointer20/8.c b/Assignment18/8/pointer20/8.c new file mode 100644 index 0000000..e6780fc --- /dev/null +++ b/Assignment18/8/pointer20/8.c @@ -0,0 +1,9 @@ +#include + +int main() { + int a=56; + int *b = &a;//b is pointer pointing to a + printf("the address of a is %x\n", b); //%x is format specifier that gives hexa decimal + printf("the value of a is %d\n", *b); + +} \ No newline at end of file diff --git a/Assignment18/8/pointer20/8.exe b/Assignment18/8/pointer20/8.exe new file mode 100644 index 0000000..eb7a663 Binary files /dev/null and b/Assignment18/8/pointer20/8.exe differ diff --git a/Assignment18/8/pointer20/Assign3.c b/Assignment18/8/pointer20/Assign3.c new file mode 100644 index 0000000..fcdf9e1 --- /dev/null +++ b/Assignment18/8/pointer20/Assign3.c @@ -0,0 +1,161 @@ +#include +#include +#include +#include +struct node{ + int data; + struct node *next; +}; + +struct node *Create(char c){ + struct node *ptr=malloc(sizeof(struct node)); + ptr->data=c-'0'; + ptr->next=NULL; + return ptr; +} + +void display(struct node *head){ + struct node *temp=head; + while(temp!=NULL){ + printf("%d",temp->data); + temp=temp->next; + } + printf("\n"); +} + +struct node *rev(struct node *head){ + struct node *temp; + struct node *temp1; + struct node *temp2; + struct node *prev; + temp=head; + int count=0; + while(temp!=NULL){ + count++; + temp=temp->next; + } + for(int i=0;inext->next; + temp2=temp->next; + temp->next=temp1; + temp2->next=temp; + head=temp2; + prev=head; + } + else{ + temp1=temp->next; + temp2=temp->next->next; + prev->next=temp1; + temp1->next=temp; + temp->next=temp2; + prev=temp1; + } + } + } + return head; +} + +struct node *sum(struct node *head1,struct node *head2){ + int carry=0; + struct node *temp1,*temp2,*temp; + temp1=head1; + temp2=head2; + int count1=0,count2=0; + while (temp1!=NULL){ + count1++; + temp1=temp1->next; + } + while (temp2!=NULL){ + count2++; + temp2=temp2->next; + } + struct node *head; + head=(struct node*)malloc(sizeof(struct node)); + head->data=0; + head->next=NULL; + temp=head; + if(count1==count2){ + for(int i=0;inext=Create('0'); + temp=temp->next; + } + } + else if(count1>count2){ + for(int i=0;inext=Create('0'); + temp=temp->next; + } + temp2=head2; + while (temp2->next!=NULL) + temp2=temp2->next; + for(int i=0;inext=Create('0'); + temp2=temp2->next; + } + } + else if (count1next=Create('0'); + temp=temp->next; + } + temp1=head1; + while (temp1->next!=NULL) + temp1=temp1->next; + for(int i=0;inext=Create('0'); + temp1=temp1->next; + } + } + temp1=head1; + temp2=head2; + temp=head; + while(temp1!=NULL){ + temp->data=(temp1->data+temp2->data+carry)%10; + carry=(temp1->data+temp2->data+carry)/10; + temp=temp->next; + temp1=temp1->next; + temp2=temp2->next; + } + if(carry) + temp->data=carry; + head=rev(head); + if(head->data==0){ + head=head->next; + return head; + } + else if(head->data!=0) + return head; +} + +int main(){ + char str1[1000]; + printf("Enter first number: "); + scanf("%s",str1); + struct node *head1; + head1=(struct node*)malloc(sizeof(struct node)); + head1->data=str1[0]-'0'; + struct node *temp=head1; + for(int i=1;str1[i]!='\0';i++){ + temp->next=Create(str1[i]); + temp=temp->next; + } + struct node *head3=rev(head1); + char str2[1000]; + printf("Enter second number: "); + scanf("%s",str2); + struct node *head2; + head2=(struct node*)malloc(sizeof(struct node)); + head2->data=str2[0]-'0'; + temp=head2; + for(int i=1;str2[i]!='\0';i++){ + temp->next=Create(str2[i]); + temp=temp->next; + } + struct node *head4=rev(head2); + struct node *result=sum(head3,head4); + display(result); + return 0; +} \ No newline at end of file diff --git a/Assignment18/8/pointer20/Assign3.exe b/Assignment18/8/pointer20/Assign3.exe new file mode 100644 index 0000000..9e0254b Binary files /dev/null and b/Assignment18/8/pointer20/Assign3.exe differ diff --git a/Assignment18/8/pointer20/Assign_3.c b/Assignment18/8/pointer20/Assign_3.c new file mode 100644 index 0000000..bbdb824 --- /dev/null +++ b/Assignment18/8/pointer20/Assign_3.c @@ -0,0 +1,75 @@ +#include +#include + +// Structure to represent a non-zero element in the sparse matrix +struct Node { + int row; + int col; + int value; + struct Node* next; +}; + +// Function to create a new node +struct Node* createNode(int row, int col, int value) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + if (newNode == NULL) { + printf("Memory allocation failed\n"); + exit(1); + } + newNode->row = row; + newNode->col = col; + newNode->value = value; + newNode->next = NULL; + return newNode; +} + +// Function to insert a new node at the end of the linked list +void insertNode(struct Node** head, int row, int col, int value) { + struct Node* newNode = createNode(row, col, value); + if (*head == NULL) { + *head = newNode; + } else { + struct Node* current = *head; + while (current->next != NULL) { + current = current->next; + } + current->next = newNode; + } +} + +// Function to display the linked list representation of the sparse matrix +void displaySparseMatrix(struct Node* head) { + if (head == NULL) { + printf("Sparse matrix is empty.\n"); + return; + } + printf("Row\tColumn\tValue\n"); + while (head != NULL) { + printf("%d\t%d\t%d\n", head->row, head->col, head->value); + head = head->next; + } +} + +int main() { + int rows, cols; + printf("Enter the number of rows and columns in the sparse matrix: "); + scanf("%d %d", &rows, &cols); + + struct Node* sparseMatrix = NULL; + + printf("Enter the elements of the sparse matrix:\n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + int value; + scanf("%d", &value); + if (value != 0) { + insertNode(&sparseMatrix, i, j, value); + } + } + } + + printf("\nLinked list representation of the sparse matrix:\n"); + displaySparseMatrix(sparseMatrix); + + return 0; +} diff --git a/Assignment18/8/pointer20/Assign_3.exe b/Assignment18/8/pointer20/Assign_3.exe new file mode 100644 index 0000000..a21a5d9 Binary files /dev/null and b/Assignment18/8/pointer20/Assign_3.exe differ diff --git a/Assignment18/8/pointer20/Assign_4.c b/Assignment18/8/pointer20/Assign_4.c new file mode 100644 index 0000000..f5f4afa --- /dev/null +++ b/Assignment18/8/pointer20/Assign_4.c @@ -0,0 +1,145 @@ +#include +#include +#include + +// Define a structure for a doubly linked list node +struct Node { + int data; // Info part storing 4 digits + struct Node* prev; // Pointer to the previous node + struct Node* next; // Pointer to the next node +}; + +// Function to create a new node +struct Node* createNode(int data) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = data; + newNode->prev = NULL; + newNode->next = NULL; + return newNode; +} + +// Function to insert a new node at the end of the linked list +void insertEnd(struct Node** head, int data) { + struct Node* newNode = createNode(data); + + if (*head == NULL) { + *head = newNode; + } else { + struct Node* current = *head; + while (current->next != NULL) { + current = current->next; + } + current->next = newNode; + newNode->prev = current; + } +} + +// Function to print the doubly linked list +void printList(struct Node* head) { + while (head != NULL) { + printf("%04d -> ", head->data); // Print 4-digit groups with leading zeros + head = head->next; + } + printf("NULL\n"); +} + +// Function to free memory allocated for the linked list +void freeList(struct Node* head) { + struct Node* current = head; + while (current != NULL) { + struct Node* temp = current; + current = current->next; + free(temp); + } +} + // Function to add two numbers represented by doubly linked lists +struct Node* addNumbers(struct Node* num1, struct Node* num2) { + struct Node* result = NULL; + int carry = 0; + + while (num1 != NULL || num2 != NULL || carry != 0) { + int digit1 = (num1 != NULL) ? num1->data : 0; + int digit2 = (num2 != NULL) ? num2->data : 0; + + int sum = digit1 + digit2 + carry; + carry = sum / 10000; // Calculate carry for the next iteration + + insertEnd(&result, sum % 10000); + + if (num1 != NULL) num1 = num1->prev; + if (num2 != NULL) num2 = num2->prev; + } + + return result; +} + + +int main() { + struct Node* head1 = NULL; + struct Node* head2 = NULL; + char input1[100]; // Assuming a maximum input length of 100 characters + char input2[100]; // Assuming a maximum input length of 100 characters + + printf("Enter the first number: "); + scanf("%s", input1); + + printf("Enter the second number: "); + scanf("%s", input2); + + // Parse the first input number and create the first doubly linked list + int num = 0; + int digitsProcessed = 0; + + for (int i = strlen(input1) - 1; i >= 0; i--) { + int digit = input1[i] - '0'; + num = num * 10 + digit; + digitsProcessed++; + + if (digitsProcessed == 4) { + insertEnd(&head1, num); + num = 0; + digitsProcessed = 0; + } + } + + if (digitsProcessed > 0) { + insertEnd(&head1, num); + } + + // Parse the second input number and create the second doubly linked list + num = 0; + digitsProcessed = 0; + + for (int i = strlen(input2) - 1; i >= 0; i--) { + int digit = input2[i] - '0'; + num = num * 10 + digit; + digitsProcessed++; + + if (digitsProcessed == 4) { + insertEnd(&head2, num); + num = 0; + digitsProcessed = 0; + } + } + + if (digitsProcessed > 0) { + insertEnd(&head2, num); + } + + // Print the input numbers + printf("First number: "); + printList(head1); + printf("Second number: "); + printList(head2); + + // Perform addition and print the result + struct Node* result = addNumbers(head1, head2); + printList(result); + + // Free allocated memory for all lists + freeList(head1); + freeList(head2); + freeList(result); + + return 0; +} diff --git a/Assignment18/8/pointer20/Assign_4.exe b/Assignment18/8/pointer20/Assign_4.exe new file mode 100644 index 0000000..0f33bd2 Binary files /dev/null and b/Assignment18/8/pointer20/Assign_4.exe differ diff --git a/Assignment18/8/pointer20/BTree.c b/Assignment18/8/pointer20/BTree.c new file mode 100644 index 0000000..776c675 --- /dev/null +++ b/Assignment18/8/pointer20/BTree.c @@ -0,0 +1,147 @@ +#include +#include + +#define T 3 // B-tree order + +typedef struct Node { + int keys[2 * T - 1]; + struct Node* children[2 * T]; + int num_keys; + int is_leaf; +} Node; + +Node* createNode() { + Node* new_node = (Node*)malloc(sizeof(Node)); + new_node->num_keys = 0; + new_node->is_leaf = 1; + for (int i = 0; i < 2 * T; i++) { + new_node->children[i] = NULL; + } + return new_node; +} + +Node* search(Node* root, int key) { + int i = 0; + while (i < root->num_keys && key > root->keys[i]) { + i++; + } + if (i < root->num_keys && key == root->keys[i]) { + return root; + } else if (root->is_leaf) { + return NULL; + } else { + return search(root->children[i], key); + } +} + +void splitChild(Node* parent, int index, Node* child) { + Node* new_child = createNode(); + Node* split_child = parent->children[index]; + parent->num_keys++; + + for (int i = parent->num_keys - 1; i > index; i--) { + parent->keys[i] = parent->keys[i - 1]; + parent->children[i + 1] = parent->children[i]; + } + + parent->keys[index] = split_child->keys[T - 1]; + parent->children[index + 1] = new_child; + + for (int i = 0; i < T - 1; i++) { + new_child->keys[i] = split_child->keys[i + T]; + } + + if (!split_child->is_leaf) { + for (int i = 0; i < T; i++) { + new_child->children[i] = split_child->children[i + T]; + } + } + + split_child->num_keys = T - 1; + new_child->num_keys = T - 1; + + for (int i = parent->num_keys; i < 2 * T - 1; i++) { + parent->keys[i] = 0; + } +} + +void insert(Node** root, int key) { + Node* r = *root; + if (r->num_keys == 2 * T - 1) { + Node* new_root = createNode(); + new_root->is_leaf = 0; + new_root->children[0] = r; + *root = new_root; + splitChild(new_root, 0, r); + insertNonFull(new_root, key); + } else { + insertNonFull(r, key); + } +} + +void insertNonFull(Node* x, int key) { + int i = x->num_keys - 1; + if (x->is_leaf) { + while (i >= 0 && key < x->keys[i]) { + x->keys[i + 1] = x->keys[i]; + i--; + } + x->keys[i + 1] = key; + x->num_keys++; + } else { + while (i >= 0 && key < x->keys[i]) { + i--; + } + i++; + if (x->children[i]->num_keys == 2 * T - 1) { + splitChild(x, i, x->children[i]); + if (key > x->keys[i]) { + i++; + } + } + insertNonFull(x->children[i], key); + } +} + +void printTree(Node* root, int level) { + if (root != NULL) { + int i; + for (i = 0; i < root->num_keys; i++) { + printTree(root->children[i], level + 1); + printf("%d ", root->keys[i]); + } + printTree(root->children[i], level + 1); + } +} + +int main() { + Node* root = createNode(); + int num_keys; + + printf("Enter the number of keys to insert: "); + scanf("%d", &num_keys); + + printf("Enter the keys to insert:\n"); + for (int i = 0; i < num_keys; i++) { + int key; + scanf("%d", &key); + insert(&root, key); + } + + int search_key; + printf("Enter a key to search: "); + scanf("%d", &search_key); + Node* result = search(root, search_key); + + if (result != NULL) { + printf("Key %d found in the B-tree.\n", search_key); + } else { + printf("Key %d not found in the B-tree.\n", search_key); + } + + printf("B-tree structure:\n"); + printTree(root, 0); + printf("\n"); + + return 0; +} diff --git a/Assignment18/8/pointer20/BTree.exe b/Assignment18/8/pointer20/BTree.exe new file mode 100644 index 0000000..56397bf Binary files /dev/null and b/Assignment18/8/pointer20/BTree.exe differ diff --git a/Assignment18/8/pointer20/heap.c b/Assignment18/8/pointer20/heap.c new file mode 100644 index 0000000..790fec7 --- /dev/null +++ b/Assignment18/8/pointer20/heap.c @@ -0,0 +1,83 @@ +#include + +int n; +int arr[100]; + +void insert() { + n = n + 1; + printf("Enter the value for insertion: "); + scanf("%d", &arr[n - 1]); +} + +void heapify(int i) { + int smallest = i; + int leftChild = 2 * i + 1; + int rightChild = 2 * i + 2; + + if (leftChild < n && arr[leftChild] < arr[smallest]) { + smallest = leftChild; + } + + if (rightChild < n && arr[rightChild] < arr[smallest]) { + smallest = rightChild; + } + + if (smallest != i) { + int temp = arr[i]; + arr[i] = arr[smallest]; + arr[smallest] = temp; + heapify(smallest); + } +} + +void buildHeap() { + for (int i = n / 2 - 1; i >= 0; i--) { + heapify(i); + } +} + +void display() { + printf("Heap elements after operations:\n"); + for (int i = 0; i < n; i++) { + printf("%d\n", arr[i]); + } +} + +int main() { + int ch; + + printf("Enter the number of elements in the heap: "); + scanf("%d", &n); + + printf("Enter the elements of the heap:\n"); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + while (1) { + printf("Enter a choice\n"); + printf("1: Insertion\n"); + printf("2: Heapify\n"); + printf("3: Display\n"); + printf("4: Exit\n"); + scanf("%d", &ch); + + switch (ch) { + case 1: + insert(); + break; + case 2: + buildHeap(); + break; + case 3: + display(); + break; + case 4: + return 0; + default: + printf("Invalid choice. Please try again.\n"); + } + } + + return 0; +} \ No newline at end of file diff --git a/Assignment18/8/pointer20/heap.exe b/Assignment18/8/pointer20/heap.exe new file mode 100644 index 0000000..608c151 Binary files /dev/null and b/Assignment18/8/pointer20/heap.exe differ diff --git a/Assignment18/8/pointer20/heap_delete.c b/Assignment18/8/pointer20/heap_delete.c new file mode 100644 index 0000000..2544190 --- /dev/null +++ b/Assignment18/8/pointer20/heap_delete.c @@ -0,0 +1,76 @@ +#include + +// Function to swap two elements in an array +void swap(int *x, int *y) { + int temp = *x; + *x = *y; + *y = temp; +} + +// Function to heapify a subtree with the root at index i +// n is the size of the heap +void heapify(int arr[], int n, int i) { + int largest = i; // Initialize largest as root + int left = 2 * i + 1; // Left child + int right = 2 * i + 2; // Right child + + // If left child is larger than root + if (left < n && arr[left] > arr[largest]) + largest = left; + + // If right child is larger than largest so far + if (right < n && arr[right] > arr[largest]) + largest = right; + + // If largest is not root + if (largest != i) { + swap(&arr[i], &arr[largest]); + + // Recursively heapify the affected sub-tree + heapify(arr, n, largest); + } +} + +// Main function to perform heap sort +void heapSort(int arr[], int n) { + // Build a max heap + for (int i = n / 2 - 1; i >= 0; i--) + heapify(arr, n, i); + + // Extract elements one by one from the heap + for (int i = n - 1; i > 0; i--) { + // Move current root to end + swap(&arr[0], &arr[i]); + + // Call heapify on the reduced heap + heapify(arr, i, 0); + } +} + +// Function to print an array +void printArray(int arr[], int size) { + for (int i = 0; i < size; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +int main() { + int arr[] = {12, 11, 13, 5, 6, 7}; + int n = sizeof(arr) / sizeof(arr[0]); + + printf("Unsorted array: \n"); + printArray(arr, n); + + heapSort(arr, n); + + printf("Sorted array: \n"); + printArray(arr, n); + + printf("Elements deleted one by one:\n"); + for (int i = n - 1; i >= 0; i--) { + printf("%d ", arr[i]); // Print the maximum element (last element) + arr[i] = 0; // Mark the element as deleted (set to 0) + } + + return 0; +} diff --git a/Assignment18/8/pointer20/heap_delete.exe b/Assignment18/8/pointer20/heap_delete.exe new file mode 100644 index 0000000..aa9415c Binary files /dev/null and b/Assignment18/8/pointer20/heap_delete.exe differ diff --git a/Assignment18/8/pointer20/heapsort.c b/Assignment18/8/pointer20/heapsort.c new file mode 100644 index 0000000..aefb851 --- /dev/null +++ b/Assignment18/8/pointer20/heapsort.c @@ -0,0 +1,23 @@ +#include + +int main() { + int n; // Size of the array + printf("Enter the size of the array: "); + scanf("%d", &n); + + int arr[n]; // Declare an array of size n + + // Input elements into the array + printf("Enter %d elements:\n", n); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + // Print the elements of the array + printf("Elements in the array: "); + for (int i = 0; i < n; i++) { + printf("%d ", arr[i]); + } + + return 0; +} diff --git a/Assignment18/8/pointer20/heapsort.exe b/Assignment18/8/pointer20/heapsort.exe new file mode 100644 index 0000000..9c5bd67 Binary files /dev/null and b/Assignment18/8/pointer20/heapsort.exe differ diff --git a/Assignment18/8/pointer20/pointer2.c b/Assignment18/8/pointer20/pointer2.c new file mode 100644 index 0000000..3f293cc --- /dev/null +++ b/Assignment18/8/pointer20/pointer2.c @@ -0,0 +1,100 @@ +#include +#include + +// Define a structure for a linked list node +struct Node { + int data; + struct Node* next; +}; + +// Function to insert a node at the end of a linked list +void insert(struct Node** head, int data) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = data; + newNode->next = NULL; + + if (*head == NULL) { + *head = newNode; + } else { + struct Node* temp = *head; + while (temp->next != NULL) { + temp = temp->next; + } + temp->next = newNode; + } +} + +// Function to concatenate two linked lists +struct Node* concatenate(struct Node* list1, struct Node* list2) { + if (list1 == NULL) return list2; + if (list2 == NULL) return list1; + + struct Node* temp = list1; + while (temp->next != NULL) { + temp = temp->next; + } + temp->next = list2; + + return list1; +} + +// Function to perform radix sort on an array of numbers +void radixSort(int arr[], int n) { + // Find the maximum number to determine the number of digits + int max = arr[0]; + for (int i = 1; i < n; i++) { + if (arr[i] > max) { + max = arr[i]; + } + } + + // Perform counting sort for every digit place value + struct Node* buckets[10] = {NULL}; // Create 10 buckets for digits 0-9 + + for (int exp = 1; max / exp > 0; exp *= 10) { + // Place numbers into buckets based on the current digit + for (int i = 0; i < n; i++) { + int index = (arr[i] / exp) % 10; + insert(&buckets[index], arr[i]); + } + + // Concatenate all the buckets to form a new array + int j = 0; + for (int i = 0; i < 10; i++) { + while (buckets[i] != NULL) { + arr[j++] = buckets[i]->data; + struct Node* temp = buckets[i]; + buckets[i] = buckets[i]->next; + free(temp); + } + } + } +} + +int main() { + int n; + printf("Enter the number of elements: "); + scanf("%d", &n); + + int arr[n]; + printf("Enter the elements:\n"); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + printf("Original Array: "); + for (int i = 0; i < n; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + + radixSort(arr, n); + + printf("Sorted Array: "); + for (int i = 0; i < n; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + + return 0; +} diff --git a/Assignment18/8/pointer20/pointer2.exe b/Assignment18/8/pointer20/pointer2.exe new file mode 100644 index 0000000..7f0cf9c Binary files /dev/null and b/Assignment18/8/pointer20/pointer2.exe differ diff --git a/Assignment_1_BT22CSE111.c b/Assignment_1_BT22CSE111.c new file mode 100644 index 0000000..45697af --- /dev/null +++ b/Assignment_1_BT22CSE111.c @@ -0,0 +1,272 @@ +#include +#include +#include + +// Structure to represent a term in the polynomial +typedef struct Term { + float coefficient; + int exponent; + struct Term* next; +} Term; + +// Structure to represent a polynomial (circular linked list with a header node) +typedef struct Polynomial { + Term* header; +} Polynomial; + +// Function to create a new term +Term* createTerm(float coefficient, int exponent) { + Term* term = (Term*)malloc(sizeof(Term)); + term->coefficient = coefficient; + term->exponent = exponent; + term->next = NULL; + return term; +} + +// Function to initialize a polynomial +void initPolynomial(Polynomial* polynomial) { + polynomial->header = createTerm(0.0, -1); // Header node with sentinel values + polynomial->header->next = polynomial->header; // Circular self-reference +} + +// Function to add a term to a polynomial +void addTerm(Polynomial* polynomial, float coefficient, int exponent) { + Term* newTerm = createTerm(coefficient, exponent); + Term* current = polynomial->header; + + while (current->next != polynomial->header && current->next->exponent > exponent) { + current = current->next; + } + + newTerm->next = current->next; + current->next = newTerm; +} + +// Function to read a polynomial and convert it to its circular representation +Polynomial* Pread() { + Polynomial* polynomial = (Polynomial*)malloc(sizeof(Polynomial)); + initPolynomial(polynomial); + + int numTerms; + printf("Enter the number of terms in the polynomial: "); + scanf("%d", &numTerms); + + for (int i = 0; i < numTerms; i++) { + float coefficient; + int exponent; + printf("Enter coefficient and exponent for term %d: ", i + 1); + scanf("%f %d", &coefficient, &exponent); + addTerm(polynomial, coefficient, exponent); + } + + return polynomial; +} + +// Function to output the polynomial in mathematical form +void Pwrite(Polynomial* polynomial) { + Term* current = polynomial->header->next; + int firstTerm = 1; + + while (current != polynomial->header) { + if (current->coefficient != 0) { + if (!firstTerm) { + if (current->coefficient > 0) { + printf(" + "); + } else { + printf(" - "); + } + } + if (fabs(current->coefficient) != 1 || current->exponent == 0) { + printf("%.2f", fabs(current->coefficient)); + } + if (current->exponent > 0) { + printf("x"); + if (current->exponent > 1) { + printf("^%d", current->exponent); + } + } + firstTerm = 0; + } + current = current->next; + } + + if (firstTerm) { + printf("0"); + } + printf("\n"); +} + +// Function to compute c = a + b +Polynomial* Padd(Polynomial* a, Polynomial* b) { + Polynomial* c = (Polynomial*)malloc(sizeof(Polynomial)); + initPolynomial(c); + + Term* termA = a->header->next; + Term* termB = b->header->next; + + while (termA != a->header && termB != b->header) { + if (termA->exponent > termB->exponent) { + addTerm(c, termA->coefficient, termA->exponent); + termA = termA->next; + } else if (termA->exponent < termB->exponent) { + addTerm(c, termB->coefficient, termB->exponent); + termB = termB->next; + } else { + addTerm(c, termA->coefficient + termB->coefficient, termA->exponent); + termA = termA->next; + termB = termB->next; + } + } + + // Add remaining terms from a and b + while (termA != a->header) { + addTerm(c, termA->coefficient, termA->exponent); + termA = termA->next; + } + while (termB != b->header) { + addTerm(c, termB->coefficient, termB->exponent); + termB = termB->next; + } + + return c; +} + +// Function to compute c = a - b +Polynomial* Psub(Polynomial* a, Polynomial* b) { + Polynomial* c = (Polynomial*)malloc(sizeof(Polynomial)); + initPolynomial(c); + + Term* termA = a->header->next; + Term* termB = b->header->next; + + while (termA != a->header && termB != b->header) { + if (termA->exponent > termB->exponent) { + addTerm(c, termA->coefficient, termA->exponent); + termA = termA->next; + } else if (termA->exponent < termB->exponent) { + addTerm(c, -termB->coefficient, termB->exponent); + termB = termB->next; + } else { + addTerm(c, termA->coefficient - termB->coefficient, termA->exponent); + termA = termA->next; + termB = termB->next; + } + } + + // Add remaining terms from a and subtract terms from b + while (termA != a->header) { + addTerm(c, termA->coefficient, termA->exponent); + termA = termA->next; + } + while (termB != b->header) { + addTerm(c, -termB->coefficient, termB->exponent); + termB = termB->next; + } + + return c; +} + + +// Function to compute c = a * b with term reduction +Polynomial* Pmult(Polynomial* a, Polynomial* b) { + Polynomial* c = (Polynomial*)malloc(sizeof(Polynomial)); + initPolynomial(c); + + Term* termA = a->header->next; + + while (termA != a->header) { + Term* termB = b->header->next; + while (termB != b->header) { + addTerm(c, termA->coefficient * termB->coefficient, termA->exponent + termB->exponent); + termB = termB->next; + } + termA = termA->next; + } + + // Combine terms with the same exponent + Term* current = c->header->next; + while (current != c->header && current->next != c->header) { + if (current->exponent == current->next->exponent) { + current->coefficient += current->next->coefficient; + Term* temp = current->next; + current->next = current->next->next; + free(temp); + } else { + current = current->next; + } + } + + return c; +} + + +// Function to evaluate the polynomial at a given point +float Peval(Polynomial* polynomial, float x) { + float result = 0.0; + Term* current = polynomial->header->next; + + while (current != polynomial->header) { + result += current->coefficient * pow(x, current->exponent); + current = current->next; + } + + return result; +} + +// Function to erase a certain term of the polynomial +void Pearse(Polynomial* polynomial, int exponent) { + Term* current = polynomial->header; + while (current->next != polynomial->header) { + if (current->next->exponent == exponent) { + Term* temp = current->next; + current->next = current->next->next; + free(temp); + return; + } + current = current->next; + } +} + +int main() { + printf("Enter polynomial A:\n"); + Polynomial* a = Pread(); + + printf("\nEnter polynomial B:\n"); + Polynomial* b = Pread(); + + printf("\nPolynomial A: "); + Pwrite(a); + + printf("Polynomial B: "); + Pwrite(b); + + Polynomial* c = Padd(a, b); + printf("\nA + B = "); + Pwrite(c); + + Polynomial* d = Psub(a, b); + printf("A - B = "); + Pwrite(d); + + Polynomial* e = Pmult(a, b); + printf("A * B = "); + Pwrite(e); + + float x; + printf("\nEnter the value of x for polynomial evaluation: "); + scanf("%f", &x); + printf("A(when x is: %f) = %.2f\n", x, Peval(a, x)); + printf("B(when x is: %f) = %.2f\n", x, Peval(b, x)); + + int eraseExponent; + printf("\nEnter the exponent of the term to erase from polynomial A: "); + scanf("%d", &eraseExponent); + Pearse(a, eraseExponent); + printf("Polynomial A after erasing term with exponent %d: ", eraseExponent); + Pwrite(a); + + // Free memory + // ... + + return 0; +} diff --git a/Assignment_1_BT22CSE111.exe b/Assignment_1_BT22CSE111.exe new file mode 100644 index 0000000..6644931 Binary files /dev/null and b/Assignment_1_BT22CSE111.exe differ diff --git a/Assignment_3_BT22CSE111.c b/Assignment_3_BT22CSE111.c new file mode 100644 index 0000000..8871fff --- /dev/null +++ b/Assignment_3_BT22CSE111.c @@ -0,0 +1,113 @@ +#include +#include + +// Define a structure for a linked list node +struct Node { + int data; + struct Node* next; +}; + +// Function to insert a node at the end of a linked list +void insert(struct Node** head, int data) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = data; + newNode->next = NULL; + + if (*head == NULL) { + *head = newNode; + } else { + struct Node* current = *head; + while (current->next != NULL) { + current = current->next; + } + current->next = newNode; + } +} + +// Function to find the maximum value in the linked list +int findMax(struct Node* head) { + int max = head->data; + while (head != NULL) { + if (head->data > max) { + max = head->data; + } + head = head->next; + } + return max; +} + +// Function to perform counting sort on the linked list based on a digit's place +void countSort(struct Node** head, int exp) { + if (*head == NULL) { + return; + } + + int count[10] = {0}; + struct Node* output = NULL; + struct Node* current = *head; + + while (current != NULL) { + count[(current->data / exp) % 10]++; + current = current->next; + } + + for (int i = 1; i < 10; i++) { + count[i] += count[i - 1]; + } + + current = *head; + while (current != NULL) { + insert(&output, current->data); + current = current->next; + } + + current = *head; + while (current != NULL) { + int index = (current->data / exp) % 10; + current->data = output->data; + output = output->next; + count[index]--; + current = current->next; + } +} + +// Radix Sort function for linked list +void radixSort(struct Node** head) { + int max = findMax(*head); + + for (int exp = 1; max / exp > 0; exp *= 10) { + countSort(head, exp); + } +} + +// Function to print the linked list +void printList(struct Node* head) { + struct Node* current = head; + while (current != NULL) { + printf("%d ", current->data); + current = current->next; + } + printf("\n"); +} + +int main() { + struct Node* head = NULL; + insert(&head, 170); + insert(&head, 45); + insert(&head, 75); + insert(&head, 90); + insert(&head, 802); + insert(&head, 24); + insert(&head, 2); + insert(&head, 66); + + printf("Original Linked List: "); + printList(head); + + radixSort(&head); + + printf("Sorted Linked List: "); + printList(head); + + return 0; +} diff --git a/Assignment_3_BT22CSE111.exe b/Assignment_3_BT22CSE111.exe new file mode 100644 index 0000000..443e3b4 Binary files /dev/null and b/Assignment_3_BT22CSE111.exe differ diff --git a/Stack_consec.c b/Stack_consec.c new file mode 100644 index 0000000..249960c --- /dev/null +++ b/Stack_consec.c @@ -0,0 +1,95 @@ +#include +#include + +#define MAX_SIZE 100 + +typedef struct +{ + int items[MAX_SIZE]; + int top; +} Stack; + +void initialize(Stack *stack) +{ + stack->top = -1; +} + +int isEmpty(Stack *stack) +{ + return stack->top == -1; +} + +int isFull(Stack *stack) +{ + return stack->top == MAX_SIZE - 1; +} + +void push(Stack *stack, int value) +{ + if (isFull(stack)) + { + printf("Stack overflow\n"); + exit(1); + } + stack->items[++stack->top] = value; +} + +int pop(Stack *stack) +{ + if (isEmpty(stack)) + { + printf("Stack underflow\n"); + exit(1); + } + return stack->items[stack->top--]; +} + +int evaluatePostfix(char *expression) +{ + Stack stack; + initialize(&stack); + int i = 0; + while (expression[i] != '\0') + { + if (isdigit(expression[i])) + { + push(&stack, expression[i] - '0'); // Convert character to integer value and push to stack + } + else + { + int operand2 = pop(&stack); + int operand1 = pop(&stack); + switch (expression[i]) + { + case '+': + push(&stack, operand1 + operand2); + break; + case '-': + push(&stack, operand1 - operand2); + break; + case '*': + push(&stack, operand1 * operand2); + break; + case '/': + push(&stack, operand1 / operand2); + break; + } + } + i++; + } + return pop(&stack); +} + +int main() +{ + char expression[MAX_SIZE]; + + printf("Enter a postfix expression: "); + fgets(expression, sizeof(expression), stdin); + + int result = evaluatePostfix(expression); + + printf("Result: %d\n", result); + + return 0; +} diff --git a/Stack_consec.exe b/Stack_consec.exe new file mode 100644 index 0000000..70e3e7c Binary files /dev/null and b/Stack_consec.exe differ diff --git a/Trees/linklist_binarytree.c b/Trees/linklist_binarytree.c new file mode 100644 index 0000000..e69de29 diff --git a/arrayDeletion.c b/arrayDeletion.c new file mode 100644 index 0000000..2bc0d6f --- /dev/null +++ b/arrayDeletion.c @@ -0,0 +1,23 @@ +#include + +int main() +{ + int pav[]={5,6,4,7,3,2}; + int i, size=5; +printf("initial array is:"); + for(i=0;i<=size;i++) + { + + printf("%d,", pav[i]); + + } + size=size-1;//we want to dlete last element so did it + printf("\n"); + printf("\n"); + printf("after Deletion array is:"); + for(i=0;i<=size;i++) + { + printf(" %d,", pav[i]); + } + +} \ No newline at end of file diff --git a/arrayDeletion.exe b/arrayDeletion.exe new file mode 100644 index 0000000..2e3f8a8 Binary files /dev/null and b/arrayDeletion.exe differ diff --git a/assignment_2.c b/assignment_2.c new file mode 100644 index 0000000..d194bd0 --- /dev/null +++ b/assignment_2.c @@ -0,0 +1,133 @@ +#include +#include +#include + +typedef struct Term { + float coefficient; + int exponent; + struct Term* next; +} Term; + +typedef struct Polynomial { + Term* header; +} Polynomial; + +Term* createTerm(float coefficient, int exponent) { + Term* term = (Term*)malloc(sizeof(Term)); + term->coefficient = coefficient; + term->exponent = exponent; + term->next = NULL; + return term; +} + +void initPolynomial(Polynomial* polynomial) { + polynomial->header = createTerm(0.0, -1); + polynomial->header->next = polynomial->header; +} + +void addTerm(Polynomial* polynomial, float coefficient, int exponent) { + Term* newTerm = createTerm(coefficient, exponent); + Term* current = polynomial->header; + + while (current->next != polynomial->header && current->next->exponent > exponent) { + current = current->next; + } + + newTerm->next = current->next; + current->next = newTerm; +} + +void Pwrite(Polynomial* polynomial) { + Term* current = polynomial->header->next; + int firstTerm = 1; + + while (current != polynomial->header) { + if (current->coefficient != 0) { + if (!firstTerm) { + if (current->coefficient > 0) { + printf(" + "); + } else { + printf(" - "); + } + } + if (fabs(current->coefficient) != 1 || current->exponent == 0) { + printf("%.2f", fabs(current->coefficient)); + } + if (current->exponent > 0) { + printf("x"); + if (current->exponent > 1) { + printf("^%d", current->exponent); + } + } + firstTerm = 0; + } + current = current->next; + } + + if (firstTerm) { + printf("0"); + } + printf("\n"); +} + +void Padd(Polynomial* polynomial, float coefficient, int exponent) { + Term* current = polynomial->header->next; + Term* prev = polynomial->header; + + while (current != polynomial->header && current->exponent > exponent) { + prev = current; + current = current->next; + } + + if (current != polynomial->header && current->exponent == exponent) { + current->coefficient += coefficient; + if (current->coefficient == 0) { + prev->next = current->next; + free(current); + } + } else { + Term* newTerm = createTerm(coefficient, exponent); + newTerm->next = current; + prev->next = newTerm; + } +} + +int main() { + Polynomial result; + initPolynomial(&result); + + printf("Enter polynomial A:\n"); + Polynomial a; + initPolynomial(&a); + int numTermsA; + printf("Enter the number of terms in polynomial A: "); + scanf("%d", &numTermsA); + for (int i = 0; i < numTermsA; i++) { + float coefficient; + int exponent; + printf("Enter coefficient and exponent for term %d: ", i + 1); + scanf("%f %d", &coefficient, &exponent); + addTerm(&a, coefficient, exponent); + } + + printf("Enter polynomial B:\n"); + int numTermsB; + printf("Enter the number of terms in polynomial B: "); + scanf("%d", &numTermsB); + for (int i = 0; i < numTermsB; i++) { + float coefficient; + int exponent; + printf("Enter coefficient for term %d: ", i + 1); + scanf("%f", &coefficient); + exponent = i; // Assigning increasing exponents for the second polynomial + Padd(&result, coefficient, exponent); + } + + printf("\nPolynomial A: "); + Pwrite(&a); + + printf("Polynomial B: "); + Pwrite(&result); + + return 0; +} diff --git a/assignment_2.exe b/assignment_2.exe new file mode 100644 index 0000000..c62f8c0 Binary files /dev/null and b/assignment_2.exe differ diff --git a/bank2.c b/bank2.c new file mode 100644 index 0000000..0d36122 --- /dev/null +++ b/bank2.c @@ -0,0 +1,42 @@ +#include +#include +#include + +// Structure for storing the bank card information +struct BankCard { + char cardNumber[20]; + char cardType[10]; +}; + +int main() { + // Initializing variables + struct BankCard card; + int option; + + // User interface for adding a card + printf("Please select the type of card you want to add:\n"); + printf("1. SBI\n");// see how many cards you can add in your account! + printf("2. Axis\n"); + scanf("%d", &option); + + // Validating the user's choice + if (option == 1) { + strcpy(card.cardType, "SBI"); + } else if (option == 2) { + strcpy(card.cardType, "Axis"); + } else { + printf("Invalid option\n"); + exit(0); + } + + // Getting the card number from the user + printf("Enter the card number: "); + scanf("%s", card.cardNumber); + + // Adding the card to the database (here, we just print the details to the console) + printf("\nCard Added Successfully!\n"); + printf("Card Type: %s\n", card.cardType); + printf("Card Number: %s\n", card.cardNumber); + + return 0; +} diff --git a/bank4.c b/bank4.c new file mode 100644 index 0000000..b34472d --- /dev/null +++ b/bank4.c @@ -0,0 +1,73 @@ +#include +#include + +struct user{ +char username[20]; +char pin[4]; +char security_question[50]; +char security_answer[20]; +}; + +void view_pin(struct user u) +{ +printf("Pin: %s\n", u.pin); +} + +void view_security_question(struct user u) +{ +printf("Security Question: %s\n", u.security_question); +} + +void reset_password(struct user *u) +{ +char new_password[20]; +printf("Enter new password: \n"); +scanf("%s", new_password); +strcpy(u->pin, new_password); +printf("Password reset successful.\n"); +} + +void reset_profile_password(struct user *u) +{ +char new_security_answer[20]; +printf("Enter new security answer: \n"); +scanf("%s", new_security_answer); +strcpy(u->security_answer, new_security_answer); +printf("Security answer reset successful.\n"); +} + +int main() +{ +int choice; +struct user current_user = {"John Doe", "1234", "What is your favorite color?", "Blue"}; +printf("Welcome %s\n", current_user.username); +while(1){ +printf("\n1. View Pin\n"); +printf("2. View Security Question\n"); +printf("3. Reset Password\n"); +printf("4. Reset Profile Password\n"); +printf("5. Exit\n"); +printf("Enter your choice: \n"); +scanf("%d", &choice); +switch(choice){ +case 1: +view_pin(current_user); +break; +case 2: +view_security_question(current_user); +break; +case 3: +reset_password(¤t_user); +break; +case 4: +reset_profile_password(¤t_user); +break; +case 5: +return 0; +default: +printf("Invalid choice. Please try again.\n"); +break; +} +} +return 0; +} \ No newline at end of file diff --git a/bank5.c b/bank5.c new file mode 100644 index 0000000..1ec3484 --- /dev/null +++ b/bank5.c @@ -0,0 +1,33 @@ +#include +#include + +int main() +{ + char card_name[50]; + char card_number[50]; + int expiry_month, expiry_year; + int cvv; + + printf("Enter card name: "); + scanf("%s", card_name); + + printf("Enter card number: "); + scanf("%s", card_number); + + printf("Enter expiry month: "); + scanf("%d", &expiry_month); + + printf("Enter expiry year: "); + scanf("%d", &expiry_year); + + printf("Enter CVV: "); + scanf("%d", &cvv); + + printf("Card details:\n"); + printf("Name: %s\n", card_name); + printf("Number: %s\n", card_number); + printf("Expiry: %d/%d\n", expiry_month, expiry_year); + printf("CVV: %d\n", cvv); + + return 0; +} diff --git a/bubble_sort.c b/bubble_sort.c new file mode 100644 index 0000000..08f7d50 --- /dev/null +++ b/bubble_sort.c @@ -0,0 +1,38 @@ +#include + +void array(int *a, int n) +{ + for (int i = 0; i < n; i++) + { + printf("%d ", a[i]); + } + printf("\n"); +} + +void bubbleSort(int *a, int n) /*since n is 6 + so bubble sort starts with comparison as follows(this is for 1st pass & follows till we get sorted array): + :0,1; 1,2; 2,3; 3,4; 4,5*/ +{ + int temp; + for (int i = 0; i < n - 1; i++) + { /*for noof passes i.e., n-1*/ + for (int j = 0; j < n - 1 - i; j++) /*for no of comparisons for each pass i.e., n-1-i*/ + { + if (a[j] > a[j + 1]) /*more noof swaps seen*/ + { + temp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = temp; + } + } + } +} + +int main() +{ + int a[] = {5, 15, 4, 36, 9, 24}; + int n = 6; + array(a, n); /*initial elements*/ + bubbleSort(a, n); /*function to sort*/ + array(a, n); /*it printd after the bubblesort*/ +} \ No newline at end of file diff --git a/bubble_sort.exe b/bubble_sort.exe new file mode 100644 index 0000000..0731d71 Binary files /dev/null and b/bubble_sort.exe differ diff --git a/dataStructure.c b/dataStructure.c new file mode 100644 index 0000000..8a41c6a --- /dev/null +++ b/dataStructure.c @@ -0,0 +1,10 @@ +#include + +int main() +{ + int pav[][4] = {{1, 5,2, 3},{ 4, 8, 9}, {3}}; + int length = sizeof(pav) / sizeof(pav[0]); + printf("size of array is: %d\n", length); + int col=(sizeof(pav) / sizeof(pav[0][0]))/length; + printf("noof cols= %d", col); +} \ No newline at end of file diff --git a/dataStructure.exe b/dataStructure.exe new file mode 100644 index 0000000..c75384b Binary files /dev/null and b/dataStructure.exe differ diff --git a/ex.c b/ex.c new file mode 100644 index 0000000..bdb9807 --- /dev/null +++ b/ex.c @@ -0,0 +1,151 @@ +#include +#include + +struct node +{ + int data; + struct node *next; +}; + +struct node *head = NULL; + +void addAtBeginning(int data) +{ + struct node *new_node = (struct node *)malloc(sizeof(struct node)); + new_node->data = data; + new_node->next = head; + head = new_node; +} + +void addAtMiddle(int data, int position) +{ + int i; + struct node *new_node = (struct node *)malloc(sizeof(struct node)); + new_node->data = data; + struct node *current = head; + for (i = 1; i < position - 1 && current != NULL; i++) + { + current = current->next; + } + if (current == NULL) + { + printf("Invalid position\n"); + return; + } + new_node->next = current->next; + current->next = new_node; +} + +void addAtEnd(int data) +{ + struct node *new_node = (struct node *)malloc(sizeof(struct node)); + new_node->data = data; + new_node->next = NULL; + if (head == NULL) + { + head = new_node; + return; + } + struct node *current = head; + while (current->next != NULL) + { + current = current->next; + } + current->next = new_node; +} + +void deleteFromBeginning() +{ + if (head == NULL) + { + printf("List is empty\n"); + return; + } + struct node *temp = head; + head = head->next; + free(temp); +} + +void deleteFromMiddle(int position) +{ + int i; + struct node *current = head; + struct node *previous = NULL; + for (i = 1; i < position && current != NULL; i++) + { + previous = current; + current = current->next; + } + if (current == NULL) + { + printf("Invalid position\n"); + return; + } + previous->next = current->next; + free(current); +} + +void deleteFromEnd() +{ + if (head == NULL) + { + printf("List is empty\n"); + return; + } + struct node *current = head; + struct node *previous = NULL; + while (current->next != NULL) + { + previous = current; + current = current->next; + } + if (previous == NULL) + { + head = NULL; + } + else + { + previous->next = NULL; + } + free(current); +} + +void display() +{ + if (head == NULL) + { + printf("List is empty\n"); + return; + } + struct node *current = head; + while (current != NULL) + { + printf("%d ", current->data); + current = current->next; + } + printf("\n"); +} + +void displayReverse(struct node *current) +{ + if (current == NULL) + { + return; + } + displayReverse(current->next); + printf("%d ", current->data); +} + +struct node *search(int data) +{ + struct node *current = head; + while (current != NULL) + { + if (current->data == data) + { + return current; + } + current = current->next; + } + return 0; +} diff --git a/ex2.c b/ex2.c new file mode 100644 index 0000000..00b0288 --- /dev/null +++ b/ex2.c @@ -0,0 +1,25 @@ +#include +int main(){ + int num; + scanf("%d",&num); + int arr[10]={0}; + for (int i = 0; num!=0; i++) + { + int temp=num%10; + arr[temp]++; + num=num/10; + } + for (int i = 0; i < 10; i++) + { + // printf("arr[%d] = %d\n",i,arr[i]); + int digit=arr[i]; + if (digit>1) + { + + printf("the digit %d appears %d\n",i,digit); + } + + } + + +} \ No newline at end of file diff --git a/ex2.exe b/ex2.exe new file mode 100644 index 0000000..1067d59 Binary files /dev/null and b/ex2.exe differ diff --git a/ex3.exe b/ex3.exe new file mode 100644 index 0000000..1aedb5e Binary files /dev/null and b/ex3.exe differ diff --git a/file.c b/file.c new file mode 100644 index 0000000..59e0421 --- /dev/null +++ b/file.c @@ -0,0 +1,16 @@ +# include +int main() +{ + + FILE *ptr = NULL; + char string[50]; + ptr = fopen("file.txt", "w + r"); + fprintf(ptr ,"%s",string); + scanf("%s",string); + + fscanf(ptr,"%s",string); + printf("%s",string); + fclose("file.txt"); + printf("hello"); + return 0; +} diff --git a/file.exe b/file.exe new file mode 100644 index 0000000..5e2047f Binary files /dev/null and b/file.exe differ diff --git a/file.txt b/file.txt new file mode 100644 index 0000000..e69de29 diff --git a/final_bank.c b/final_bank.c new file mode 100644 index 0000000..e69de29 diff --git a/io.txt b/io.txt new file mode 100644 index 0000000..60fe010 --- /dev/null +++ b/io.txt @@ -0,0 +1,6 @@ +the given data is 8 +hello world +the given data is 8 +hello world +the given data is 8 +hello world diff --git a/linklistDeletion.c b/linklistDeletion.c new file mode 100644 index 0000000..6e7e838 --- /dev/null +++ b/linklistDeletion.c @@ -0,0 +1,50 @@ +#include +#include + +struct node +{ + int data; + struct node *next; +}; + +void traverse(struct node *ptr) +{ + while (ptr != NULL) + { + printf("%d->", ptr->data); + ptr = ptr->next; + } +} + +struct node *delete_first(struct node *head) +{ + struct node *ptr = head; + head = head->next; + free(ptr); + return head; +} + +int main() +{ + // allocate memory to the nodes + // first allocate memory and then traverse + struct node *head, *head2, *head3; + head = (struct node *)malloc(sizeof(struct node)); + head2 = (struct node *)malloc(sizeof(struct node)); + head3 = (struct node *)malloc(sizeof(struct node)); + head->data = 8; + head->next = head2; + + head2->data = 6; + head2->next = head3; + + head3->data = 9; + head3->next = NULL; + + printf("linked list: "); + traverse(head); + + printf("\nlist at first deletion: "); + head = delete_first(head); + traverse(head); +} \ No newline at end of file diff --git a/linklistDeletion.exe b/linklistDeletion.exe new file mode 100644 index 0000000..779ef12 Binary files /dev/null and b/linklistDeletion.exe differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..cdf8944 --- /dev/null +++ b/main.c @@ -0,0 +1,9 @@ +#include +int main(){ + FILE *file; + file=fopen("io.txt","a"); + int data =8; + fprintf(file,"the given data is %d\n",data); + fputs("hello world\n",file); + fclose(file); +} \ No newline at end of file diff --git a/main.exe b/main.exe new file mode 100644 index 0000000..a47399e Binary files /dev/null and b/main.exe differ diff --git a/mechanics.c b/mechanics.c new file mode 100644 index 0000000..2aea169 --- /dev/null +++ b/mechanics.c @@ -0,0 +1,45 @@ +#include +#include +int main(){ + printf("Rathod Pavan Kumar Naik\n");printf("BT22CSE111\n"); +float angle, bforce, dforce, eforce, fab, fbc, fde, fda, fdb, fec, feb, sina, cosa,ra, rc; +printf("TRUSS IS SYMMETRIC!");printf(" d e\n"); +printf(" ______\n"); +printf(" d e \n"); +printf(" /\\ /\\\n"); +printf("/ \\/ \\\n"); +printf("--------\n"); +printf("a b c\n"); +printf("Enter force on B in kN:\n"); +scanf("%f", &bforce); +printf("Enter force on D in kN:\n"); +scanf("%f", &dforce); +printf("Enter force on E in kN:\n"); +scanf("%f", &eforce); +printf("Enter angle at A in the trisection DAB:\n"); +scanf("%f", &angle); +sina = sin(3.14*angle / 180); +cosa = cos(3.14*angle / 180); +fdb = bforce / sina; +feb = fdb; +fda = (-1)*fdb; +fec = (-1)*feb; +fda -= dforce*(sina); +fdb += dforce*(sina); +feb += eforce*(sina); +fec -= eforce*(sina); +fde = (-1)*fdb*(cosa); +fab = (-1)*fda*(cosa); +fbc = (-1)*fec*(cosa); +rc = (dforce + 2*bforce+3*eforce) / 4; +ra = dforce + eforce + bforce - rc; +printf("force b/w a&b is:%fkN\n", fab); +printf("force b/w b&c is:%fkN\n", fbc); +printf("force b/w a&d is:%fkN\n", fda); +printf("force b/w d&b is:%fkN\n", fdb); +printf("force b/w d&e is:%fkN\n", fde); +printf("force b/w e&b is:%fkN\n", feb); +printf("force b/w e&c is:%fkN\n", fec); +printf("Ra is:%fkN\n", ra); +printf("Rc is:%fkN\n", rc); +return 0;} \ No newline at end of file diff --git a/mechanics.exe b/mechanics.exe new file mode 100644 index 0000000..c26d160 Binary files /dev/null and b/mechanics.exe differ diff --git a/new.py b/new.py new file mode 100644 index 0000000..93145a8 --- /dev/null +++ b/new.py @@ -0,0 +1,4 @@ +from dash import DASH +from dash import dcc +from dash import html +import pandas as pd \ No newline at end of file diff --git a/practise.c b/practise.c new file mode 100644 index 0000000..77ea6b8 --- /dev/null +++ b/practise.c @@ -0,0 +1,21 @@ +#include +#include + +int main(){ +FILE *fp = NULL; +char str[20]; +char ch; + +fp=fopen("text.txt","r"); + +if(fp == NULL){ + printf("error..."); + exit(1); +} +while(!feof(fp)){ + fgets(str,5,fp);//gettiing input + printf("%s",str); +} + +fclose(fp);} + diff --git a/practise.exe b/practise.exe new file mode 100644 index 0000000..51e1842 Binary files /dev/null and b/practise.exe differ diff --git a/queue_array_implementation.c b/queue_array_implementation.c new file mode 100644 index 0000000..b3f47bf --- /dev/null +++ b/queue_array_implementation.c @@ -0,0 +1,60 @@ +#include +#include + +struct queue{ + int size; + int f;//front element + int r;//rear element + int *arr; +}; + +int isFull(struct queue *q){ + if(q->r==q->size-1){ + return 1; } + else { + return 0; + } + +} + +int isEmpty(struct queue *q){ + if(q->r==q->f){ + return 1; + } + return 0; +} + +void enqueue(struct queue *q, int val){//q is pointer and is accessed through -> + if(isFull(q)) + printf("Queue is full..."); + else { + q->r++; + q->arr[q->r]=val; + } +} + +int dequeue(struct queue *q){ + int a=-1; + if(isEmpty(q)) + printf("queue is empty"); + else { + q->f++; + a=q->arr[q->f]; + } + return a; +} + + +int main() +{ + struct queue q; + q.size =100; + q.f =q.r=-1;//structure is accesses through . + q.arr=(int*)malloc(q.size*sizeof(int)); + + //enque elements + enqueue(&q, 4);//&q bcz its pointer ->taking its address + enqueue(&q, 2); +printf("dequening elements %d", dequeue(&q)); + +} \ No newline at end of file diff --git a/queue_array_implementation.exe b/queue_array_implementation.exe new file mode 100644 index 0000000..a08ff4c Binary files /dev/null and b/queue_array_implementation.exe differ diff --git a/sortAlgorithm.c b/sortAlgorithm.c new file mode 100644 index 0000000..336e614 --- /dev/null +++ b/sortAlgorithm.c @@ -0,0 +1,87 @@ +#include +#include + +struct node +{ + int data; + struct node *next; +}; + +void traverse(struct node *ptr) +{ + while (ptr != NULL) // here ptr is nothing but head itself + { + printf("%d->", ptr->data); + ptr = ptr->next; + } +} + +struct node *insertbegin(struct node *head, int data) +{ + struct node *ptr; + ptr = (struct node *)malloc(sizeof(struct node)); + ptr->next = head; + ptr->data = data; + return ptr; +} + +struct node *insertLast(struct node *head, int data) +{ + struct node *ptr; + ptr = (struct node *)malloc(sizeof(struct node)); + ptr->data = data; + struct node *p = head; + while (p->next != NULL) + { + p->next = ptr; + ptr->next = NULL; + return head; + } +} +int main() +{ + + // allocating the memory to the nodes + struct node *head, *head1, *head2, *head3, *head4, *head5, *head6, *head7; + + head = (struct node *)malloc(sizeof(struct node)); + head1 = (struct node *)malloc(sizeof(struct node)); + head2 = (struct node *)malloc(sizeof(struct node)); + head3 = (struct node *)malloc(sizeof(struct node)); + head4 = (struct node *)malloc(sizeof(struct node)); + head5 = (struct node *)malloc(sizeof(struct node)); + head6 = (struct node *)malloc(sizeof(struct node)); + head7 = (struct node *)malloc(sizeof(struct node)); + + // linking first head to next node + head->data = 8; + head->next = head1; + + head1->data = 14; + head1->next = head2; + + head2->data = 3; + head2->next = head3; + + head3->data = 10; + head3->next = head4; + + head4->data = 9; + head4->next = head5; + + head5->data = 28; + head5->next = head6; + + head6->data = 2; + head6->next = head7; + + head7->data = 4; + head7->next = NULL; + + // traverse(head); // this head is nothing but ptr itself we are ptr so that there would be no confusion + + // head = insertbegin(head, 15); + + head = insertLast(head, 21); + traverse(head); +} \ No newline at end of file diff --git a/sortAlgorithm.exe b/sortAlgorithm.exe new file mode 100644 index 0000000..c90c626 Binary files /dev/null and b/sortAlgorithm.exe differ diff --git a/stack conse.c b/stack conse.c new file mode 100644 index 0000000..a78d9fd --- /dev/null +++ b/stack conse.c @@ -0,0 +1,69 @@ +#include +#include +#include + +#define STACK_SIZE 100 + +int stack[STACK_SIZE]; +int top = -1; + +void push(int data) +{ + if (top >= STACK_SIZE - 1) + { + printf("Stack overflow\n"); + exit(1); + } + stack[++top] = data; +} + +int pop() +{ + if (top < 0) + { + printf("Stack underflow\n"); + exit(1); + } + return stack[top--]; +} + +bool check_consecutive_pairs() +{ + int num_pairs = (top + 1) / 2; // Number of pairs in the stack + int i; + for (i = 0; i < num_pairs; i++) + { + int num1 = pop(); + int num2 = pop(); + if (abs(num1 - num2) != 1) + { // Check if the absolute difference between the two numbers is 1 + return false; + } + } + // If the stack has an odd number of elements, the top element is left out of a pair + if (top >= 0) + { + pop(); + } + return true; +} + +int main() +{ + int arr[] = {4, 5, -2, -3, 11, 10, 5, 6, 20}; + int n = sizeof(arr) / sizeof(arr[0]); + int i; + for (i = 0; i < n; i++) + { + push(arr[i]); + } + if (check_consecutive_pairs()) + { + printf("All successive pairs are consecutive\n"); + } + else + { + printf("Not all successive pairs are consecutive\n"); + } + return 0; +} diff --git a/stack_implementation.c b/stack_implementation.c new file mode 100644 index 0000000..354f737 --- /dev/null +++ b/stack_implementation.c @@ -0,0 +1,101 @@ +#include +#include //for make use of malloc + +struct stack +{ + int size; + int top; + int *arr; +}; + +int isEmpty(struct stack *ptr) +{ // in palce of s we are taking ptr + if (ptr->top == -1) + { + return 1; // true + } + else + { + return 0; + } +} + +int isFull(struct stack *ptr) +{ // in palce of s we are taking ptr + if (ptr->top == ptr->size - 1) + { + return 1; + } + else + { + return 0; + } +} + +// push function i.e., inserting elements in stack +void push(struct stack *ptr, int val) +{ + if (isFull(ptr)) + { //-->using isFull function to check + printf("Stack Overflow\n"); + } + ptr->top++; + ptr->arr[ptr->top] = val; +} + +int pop(struct stack *ptr) +{ + if (isEmpty(ptr)) + { //---.>using isEmpty to check + printf("Stack Underflow cannot pop\n"); + return -1; // since underflow + } + else + { + int val = ptr->arr[ptr->top]; + ptr->top--; + return val; + } +} + +int stackTop(struct stack *ptr) +{ + return ptr->arr[ptr->top]; // returns top element +} + +int stackBottom(struct stack *ptr) +{ + return ptr->arr[0]; // index 0 gives bottom element +} + +int main() /////// we are using --->"s"<--- as the input variable +{ + struct stack *s; // pointer is accesed by -> + s->size = 7; + s->top = -1; // index of arr is -1 mean stack is empty + s->arr = (int *)malloc(s->size * sizeof(int)); + + // pushing an element + s->arr[0] = 4; + s->top++; // oushing valus one by one + + // checking if stack is empty + if (isEmpty(s)) + { + printf("stack is empty\n"); + } + else + { + printf("stack is not empty\n"); + } + push(s, 5); + push(s, 5); + push(s, 5); + push(s, 5); + push(s, 6); + push(s, 5); // ipushed 7 elements as size was taken as=7 + printf("after pushing full:%d\n", isFull(s)); + + pop(s); + printf("after pop : %d\n", pop(s)); // LIFO(last in first out) +} diff --git a/stack_implementation.exe b/stack_implementation.exe new file mode 100644 index 0000000..11a0abc Binary files /dev/null and b/stack_implementation.exe differ diff --git a/start.c b/start.c new file mode 100644 index 0000000..069f778 --- /dev/null +++ b/start.c @@ -0,0 +1,36 @@ +#include +#include + +struct node +{ + int data; + struct node *next; +}; + +void traversing_list(struct node *ptr) +{ + + while (ptr != NULL) + { + printf("%d->", ptr->data); + ptr = ptr->next; + } +} +int main() +{ // allocating the memory to 3 nodes i.e., head with the pointer + struct node *head, *head2, *head3; + head = (struct node *)malloc(sizeof(struct node)); + head2 = (struct node *)malloc(sizeof(struct node)); + head3 = (struct node *)malloc(sizeof(struct node)); + + head->data = 5; // -->LINKING head to head1 + head->next = head2; // pointing the next space in the head to head1 + + head2->data = 8; // LINKING head1 to head2 + head2->next = head3; + + head3->data = 15; // LINKING head2 to head3 + head3->next = NULL; // head3 stops pointing to next element when it reaches to NULL + + traversing_list(head); // calling traversing_list function by kepping "head" as first node element to "ptr" +} \ No newline at end of file diff --git a/start.exe b/start.exe new file mode 100644 index 0000000..ac1303c Binary files /dev/null and b/start.exe differ diff --git a/tempCodeRunnerFile.c b/tempCodeRunnerFile.c new file mode 100644 index 0000000..e831a43 --- /dev/null +++ b/tempCodeRunnerFile.c @@ -0,0 +1,6 @@ +void swap(int *a, int *b) +{ + int temp = *a; + *a = *b; + *b = temp; +} \ No newline at end of file diff --git a/test1.c b/test1.c new file mode 100644 index 0000000..e7ead0f --- /dev/null +++ b/test1.c @@ -0,0 +1,120 @@ +#include +#include +#include +#define MAX_SIZE 100 + +// Structure to represent a queue +struct Queue { + int items[MAX_SIZE]; + int front; + int rear; +}; + +// Function to create an empty queue +void createQueue(struct Queue *q) { + q->front = -1; + q->rear = -1; +} + +// Function to check if the queue is full +int isFull(struct Queue *q) { + if (q->rear == MAX_SIZE - 1) + return 1; + else + return 0; +} + +// Function to check if the queue is empty +int isEmpty(struct Queue *q) { + if (q->front == -1) + return 1; + else + return 0; +} + +// Function to add an element to the queue +void enqueue(struct Queue *q, int value) { + if (isFull(q)) { + printf("Queue is full. Cannot enqueue element.\n"); + } else { + if (q->front == -1) + q->front = 0; + q->rear++; + q->items[q->rear] = value; + } +} + +// Function to remove an element from the queue +int dequeue(struct Queue *q) { + int item; + if (isEmpty(q)) { + printf("Queue is empty. Cannot dequeue element.\n"); + return -1; + } else { + item = q->items[q->front]; + q->front++; + if (q->front > q->rear) { + q->front = -1; + q->rear = -1; + } + return item; + } +} + +// Function to count duplicates in the queue +int countDuplicates(struct Queue *q) { + int count = 0; + int visited[MAX_SIZE] = {0}; // Array to keep track of visited elements + + // Iterate through the queue + for (int i = q->front; i <= q->rear; i++) { + visited[q->items[i]]++; + if (visited[q->items[i]] > 1) { + count++; + } + } + return count; +} + +// Function to find the maximum element in the queue +int findMax(struct Queue *q) { + int max = q->items[q->front]; + for (int i = q->front + 1; i <= q->rear; i++) { + if (q->items[i] > max) { + max = q->items[i]; + } + } + return max; +} + +// Function to count the occurrences of the maximum element in the queue +int countMaxOccurrences(struct Queue *q) { + int max = findMax(q); + int count = 0; + for (int i = q->front; i <= q->rear; i++) { + if (q->items[i] == max) { + count++; + } + } + return count; +} + +int main() { + struct Queue q; + createQueue(&q); + + int size, element; + printf("Enter the number of elements: "); + scanf("%d", &size); + + printf("Enter the elements:\n"); + for (int i = 0; i < size; i++) { + scanf("%d", &element); + enqueue(&q, element); + } + + int duplicates = countDuplicates(&q); + int maxOccurrences = countMaxOccurrences(&q); + + printf("Number of duplicates in the queueis: %d\n", duplicates); +printf("Number of occurrences of the maximum element: %d\n", maxOccurrences);} diff --git a/test1.exe b/test1.exe new file mode 100644 index 0000000..5806109 Binary files /dev/null and b/test1.exe differ diff --git a/test2.c b/test2.c new file mode 100644 index 0000000..25f2307 --- /dev/null +++ b/test2.c @@ -0,0 +1,95 @@ +#include +#include +#include + +#define MAX_SIZE 100//maximum size is 100 + +struct Stack { + char items[MAX_SIZE];//took char bcz input for palindrome is string + int top; +}; + +// Function to create an empty stack +struct Stack* createStack() +{ + struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); + stack->top = -1; + return stack; +} + +// Function to check if the stack is empty +int isEmpty(struct Stack* stack) +{ + return (stack->top == -1); +} + +// Function to push an element onto the stack +void push(struct Stack* stack, char item) +{ + if (stack->top == MAX_SIZE - 1) { + printf("\nStack Overflow"); + } + else { + stack->top++; + stack->items[stack->top] = item; + } +} + +// Function to pop an element from the stack +char pop(struct Stack* stack) +{ + if (isEmpty(stack)) { + printf("\nStack Underflow"); + return '\0'; + } + else { + char item = stack->items[stack->top]; + stack->top--; + return item; + } +} + +// Function to check if a string is a palindrome +int isPalindrome(char* str) +{ + int len = strlen(str); + struct Stack* stack = createStack(); + + // Push each character onto the stack + for (int i = 0; i < len; i++) { + push(stack, str[i]); + } + + // Pop each character and build the reversed string + char reversed[MAX_SIZE]; + int index = 0; + while (!isEmpty(stack)) { + reversed[index++] = pop(stack); + } + reversed[index] = '\0'; + + // Compare the original string with the reversed string + if (strcmp(str, reversed) == 0) { + return 1; // Palindrome + } + else { + return 0; // Not a palindrome + } +} + +int main() +{ + char str[MAX_SIZE]; + + printf("Enter a string: "); + scanf("%s", str); + + if (isPalindrome(str)) { + printf("%s is a palindrome.\n", str); + } + else { + printf("%s is not a palindrome.\n", str); + } + + return 0; +} diff --git a/test2.exe b/test2.exe new file mode 100644 index 0000000..0be8b60 Binary files /dev/null and b/test2.exe differ diff --git a/test_1.c b/test_1.c new file mode 100644 index 0000000..3fb9d43 --- /dev/null +++ b/test_1.c @@ -0,0 +1,142 @@ +#include +#include + +#define MAX_SIZE 100 + +struct Queue { + struct Node* f;//f is front element + struct Node* r;//r is rear element +}; +struct Node { + int data; + struct Node* next; +}; +void startQueue(struct Queue* q) { + q->f = NULL; + q->r = NULL; +} + +int isEmpty(struct Queue* q) { + return (q->f == NULL); +} +//adding element in queue +void enqueue(struct Queue* q, int data) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = data; + newNode->next = NULL; + + if (isEmpty(q)) { + q->f = newNode; + q->r = newNode; + } + else { + q->r->next = newNode; + q->r = newNode; + } +} +//removing element in queue +int dequeue(struct Queue* q) { + if (isEmpty(q)) { + printf("Queue is empty.\n"); + return -1; + } + + struct Node* temp = q->f; + int data = temp->data; + + q->f = q->f->next; + + if (q->f == NULL) + q->r = NULL; + + free(temp);//freeing the memeory after removing elements + return data; +} + +int countDuplicates(struct Queue* q) {//function to count the duplicates in queue + int count = 0; + + if (isEmpty(q)) { + printf("Queue is empty.\n"); + return count; + } + + struct Node* current = q->f; + + while (current != NULL) { + int data = current->data; + int duplicateCount = 0; + struct Node* temp = current->next; + + while (temp != NULL) { + if (temp->data == data) + duplicateCount++; + + temp = temp->next; + } + + if (duplicateCount > 0) + count += duplicateCount; + + current = current->next; + } + + return count; +} + +int repeatedFrequency(struct Queue* q) {//function to find the maximum frequency + if (isEmpty(q)) { + printf("Queue is empty.\n"); + return -1; + } + + int maxCount = 0; + int maxFreq = q->f->data; + + struct Node* current = q->f; + + while (current != NULL) { + int data = current->data; + int count = 0; + struct Node* temp = current->next; + + while (temp != NULL) { + if (temp->data == data) + count++; + + temp = temp->next; + } + + if (count > maxCount) { + maxCount = count; + maxFreq = data; + } + + current = current->next; + } + + return maxFreq; +} + +int main() {//main function call + struct Queue q; + startQueue(&q); + + int n, element; + printf("Enter no:of elements you require in the Queue: "); + scanf("%d", &n); + + printf("Enter the elements:\n"); + for (int i = 0; i < n; i++) { + scanf("%d", &element); + enqueue(&q, element);//took &q bcz we gave q as a pointer + } + + int duplicateCount = countDuplicates(&q); + printf("Number of duplicates are: %d\n", duplicateCount); + + int max_freq = repeatedFrequency(&q); + printf("Element with maximum frequency: %d\n", max_freq); + + return 0; +} diff --git a/test_1.exe b/test_1.exe new file mode 100644 index 0000000..96b9bb4 Binary files /dev/null and b/test_1.exe differ diff --git a/text.txt b/text.txt new file mode 100644 index 0000000..9a6886d --- /dev/null +++ b/text.txt @@ -0,0 +1,3 @@ +pavan +practise +3 days remain \ No newline at end of file