From 4bd67aeb2279857fc780ccf91b5ed92b5d866694 Mon Sep 17 00:00:00 2001 From: Shivraj Bhattacharya Date: Tue, 28 Jul 2026 21:54:34 +0530 Subject: [PATCH] [REFACTOR] : Added C codes for all corresponding cpp codes --- 01_Essential_c_and_cpp/01_basic_array.c | 12 + 01_Essential_c_and_cpp/02_structure.c | 30 +++ 01_Essential_c_and_cpp/03_pointer.c | 17 ++ 01_Essential_c_and_cpp/04_reference.c | 10 + .../05_pointer_to_Structure.c | 31 +++ 01_Essential_c_and_cpp/06_functions.c | 20 ++ .../07_array_as_parameter.c | 46 ++++ .../08_structure_as_parameter.c | 49 ++++ .../09_01_monolythic_program.c | 12 + ...odeular_with_structure_programming_style.c | 33 +++ 01_Essential_c_and_cpp/09_03_oop_style.c | 35 +++ .../10_rectangle_class_with_oop.c | 66 +++++ .../11_above_program_as_template_class.c | 129 ++++++++++ 03_recursion/01_first_recursion_program.c | 16 ++ .../02_static_variable_in_recursion.c | 20 ++ 03_recursion/03_tree_recursion.c | 17 ++ 03_recursion/04_indirect_recursion.c | 25 ++ 03_recursion/05_nested_recursion.c | 17 ++ .../06_sum_of_first_n_natural_number.c | 33 +++ 03_recursion/07_factorial.c | 24 ++ 03_recursion/08_exponent.c | 33 +++ 03_recursion/09_taylor_series.c | 21 ++ .../10_taylor_series_using_horners_rule.c | 26 ++ 03_recursion/11_fibonacci.c | 57 +++++ 03_recursion/12_ncr.c | 28 +++ 03_recursion/13_toh.c | 27 ++ .../01_static_array_and_dynamic_array.c | 15 ++ .../02_increasing_array_size.c | 17 ++ 04_Array_representation/03_2d_array.c | 51 ++++ .../01_creating_array_of_desired_size.c | 65 +++++ 05_Array_ADT/02_arrray_deletion.c | 59 +++++ 05_Array_ADT/03_01_linear_search.c | 36 +++ 05_Array_ADT/03_02_linerar_search.c | 63 +++++ 05_Array_ADT/04_binary_search.c | 56 +++++ 05_Array_ADT/05_get_set_max_sum.c | 76 ++++++ .../05_reverse_and_shift_of_an_array.c | 72 ++++++ .../06_01_inserting_in_sorted_array.c | 36 +++ 05_Array_ADT/06_02_check_if_soted.c | 35 +++ 05_Array_ADT/06_03_-ve.c | 35 +++ 05_Array_ADT/07_merging_array.c | 43 ++++ 05_Array_ADT/08_set.c | 155 ++++++++++++ 05_Array_ADT/09_menu.c | 177 +++++++++++++ 05_Array_ADT/10_01_finding_mising_element.c | 44 ++++ 05_Array_ADT/10_02_Duplicate.c | 88 +++++++ 05_Array_ADT/11_Sum_of_k.c | 56 +++++ 05_Array_ADT/12_Max_and_min.c | 27 ++ 06_string/01_string.c | 13 + 06_string/02_changing_case.c | 16 ++ 06_string/03_vowels_no_of_words.c | 28 +++ 06_string/04_valid.c | 18 ++ 06_string/05_reverse_string.c | 41 ++++ 06_string/06_comparing_string.c | 68 +++++ 06_string/07_duplicates.c | 41 ++++ .../08_duplicates_with_bitwise_operator.c | 17 ++ 06_string/09_anagram.c | 73 ++++++ 06_string/10_01_permutation.c | 32 +++ 06_string/10_02_permutattion.c | 31 +++ 07_Matrix/02_Diagonal_matrix_with_c++_class.c | 64 +++++ 07_Matrix/04_c++_lower_triangular_matrix.c | 150 +++++++++++ 08_sparse_matrix/03_sparse_matrix_using_c++.c | 124 ++++++++++ .../04_polynomial_representation.c | 78 ++++++ 09_Linked_list/01_display_linked_list.c | 47 ++++ .../02_recursively_display_linked_list.c | 55 +++++ 09_Linked_list/03_count_and_sum.c | 78 ++++++ 09_Linked_list/04_Max_Linked_list.c | 67 +++++ 09_Linked_list/05_linear_search.c | 100 ++++++++ 09_Linked_list/06_Inserting_in_Linked_list.c | 94 +++++++ .../07_Inserting_in_sorted_linked_list.c | 79 ++++++ 09_Linked_list/08_Deleting_in_linked_list.c | 87 +++++++ 09_Linked_list/09_Sorted_checking.c | 63 +++++ 09_Linked_list/10_REmove_duplicate_data.c | 73 ++++++ 09_Linked_list/11_Reverse_Linked_list.c | 117 +++++++++ .../12_Concatenate_and_merge_linked_list.c | 119 +++++++++ 09_Linked_list/13_isloop.c | 71 ++++++ 09_Linked_list/14_Linked_list_in_class.c | 114 +++++++++ 09_Linked_list/15_circular_LinkedList.c | 61 +++++ .../16_Insert_circular_linked_lidt.c | 103 ++++++++ .../17_deleting_from_circular_linked_list.c | 133 ++++++++++ 09_Linked_list/18_doubly_LL.c | 130 ++++++++++ 09_Linked_list/19_CDoubleLL.c | 143 +++++++++++ ...20_MiddleNode_and_intersection_of_two_LL.c | 4 + .../01_sparse_matrix.c | 141 +++++++++++ .../02_polynomial_linked_list.c | 53 ++++ 11_Stack/01_Stack_using_Arry.c | 88 +++++++ 11_Stack/02_Stack_using_LL.c | 72 ++++++ 11_Stack/03_parenthesis_is_balanced.c | 77 ++++++ .../04_parenthesis_is_balanced_extended.c | 82 +++++++ 11_Stack/05_infix_to_postfix.c | 110 +++++++++ 11_Stack/06_infix_to_posstfix.c | 131 ++++++++++ 11_Stack/07_postfix_eval.c | 99 ++++++++ 12_Queue/01_enqueue_and_dequeue.c | 56 +++++ 12_Queue/02_circular_queue.c | 60 +++++ 12_Queue/03_queue_using_ll.c | 75 ++++++ 13_Trees/01_creating_binary_tree.c | 60 +++++ 13_Trees/02_Traversing.c | 127 ++++++++++ 13_Trees/03_LevelOrder.c | 89 +++++++ 13_Trees/04_Height_and_count.c | 135 ++++++++++ 13_Trees/Node.h | 17 +- 13_Trees/Queue.h | 68 +++-- 13_Trees/Stack.h | 67 +++-- 14_BST/01_bst.c | 169 +++++++++++++ 15_AVL_Tree/01_LL_Rotation.c | 232 ++++++++++++++++++ 17_Heap/create_heap.c | 55 +++++ 103 files changed, 6558 insertions(+), 77 deletions(-) create mode 100644 01_Essential_c_and_cpp/01_basic_array.c create mode 100644 01_Essential_c_and_cpp/02_structure.c create mode 100644 01_Essential_c_and_cpp/03_pointer.c create mode 100644 01_Essential_c_and_cpp/04_reference.c create mode 100644 01_Essential_c_and_cpp/05_pointer_to_Structure.c create mode 100644 01_Essential_c_and_cpp/06_functions.c create mode 100644 01_Essential_c_and_cpp/07_array_as_parameter.c create mode 100644 01_Essential_c_and_cpp/08_structure_as_parameter.c create mode 100644 01_Essential_c_and_cpp/09_01_monolythic_program.c create mode 100644 01_Essential_c_and_cpp/09_02_modeular_with_structure_programming_style.c create mode 100644 01_Essential_c_and_cpp/09_03_oop_style.c create mode 100644 01_Essential_c_and_cpp/10_rectangle_class_with_oop.c create mode 100644 01_Essential_c_and_cpp/11_above_program_as_template_class.c create mode 100644 03_recursion/01_first_recursion_program.c create mode 100644 03_recursion/02_static_variable_in_recursion.c create mode 100644 03_recursion/03_tree_recursion.c create mode 100644 03_recursion/04_indirect_recursion.c create mode 100644 03_recursion/05_nested_recursion.c create mode 100644 03_recursion/06_sum_of_first_n_natural_number.c create mode 100644 03_recursion/07_factorial.c create mode 100644 03_recursion/08_exponent.c create mode 100644 03_recursion/09_taylor_series.c create mode 100644 03_recursion/10_taylor_series_using_horners_rule.c create mode 100644 03_recursion/11_fibonacci.c create mode 100644 03_recursion/12_ncr.c create mode 100644 03_recursion/13_toh.c create mode 100644 04_Array_representation/01_static_array_and_dynamic_array.c create mode 100644 04_Array_representation/02_increasing_array_size.c create mode 100644 04_Array_representation/03_2d_array.c create mode 100644 05_Array_ADT/01_creating_array_of_desired_size.c create mode 100644 05_Array_ADT/02_arrray_deletion.c create mode 100644 05_Array_ADT/03_01_linear_search.c create mode 100644 05_Array_ADT/03_02_linerar_search.c create mode 100644 05_Array_ADT/04_binary_search.c create mode 100644 05_Array_ADT/05_get_set_max_sum.c create mode 100644 05_Array_ADT/05_reverse_and_shift_of_an_array.c create mode 100644 05_Array_ADT/06_01_inserting_in_sorted_array.c create mode 100644 05_Array_ADT/06_02_check_if_soted.c create mode 100644 05_Array_ADT/06_03_-ve.c create mode 100644 05_Array_ADT/07_merging_array.c create mode 100644 05_Array_ADT/08_set.c create mode 100644 05_Array_ADT/09_menu.c create mode 100644 05_Array_ADT/10_01_finding_mising_element.c create mode 100644 05_Array_ADT/10_02_Duplicate.c create mode 100644 05_Array_ADT/11_Sum_of_k.c create mode 100644 05_Array_ADT/12_Max_and_min.c create mode 100644 06_string/01_string.c create mode 100644 06_string/02_changing_case.c create mode 100644 06_string/03_vowels_no_of_words.c create mode 100644 06_string/04_valid.c create mode 100644 06_string/05_reverse_string.c create mode 100644 06_string/06_comparing_string.c create mode 100644 06_string/07_duplicates.c create mode 100644 06_string/08_duplicates_with_bitwise_operator.c create mode 100644 06_string/09_anagram.c create mode 100644 06_string/10_01_permutation.c create mode 100644 06_string/10_02_permutattion.c create mode 100644 07_Matrix/02_Diagonal_matrix_with_c++_class.c create mode 100644 07_Matrix/04_c++_lower_triangular_matrix.c create mode 100644 08_sparse_matrix/03_sparse_matrix_using_c++.c create mode 100644 08_sparse_matrix/04_polynomial_representation.c create mode 100644 09_Linked_list/01_display_linked_list.c create mode 100644 09_Linked_list/02_recursively_display_linked_list.c create mode 100644 09_Linked_list/03_count_and_sum.c create mode 100644 09_Linked_list/04_Max_Linked_list.c create mode 100644 09_Linked_list/05_linear_search.c create mode 100644 09_Linked_list/06_Inserting_in_Linked_list.c create mode 100644 09_Linked_list/07_Inserting_in_sorted_linked_list.c create mode 100644 09_Linked_list/08_Deleting_in_linked_list.c create mode 100644 09_Linked_list/09_Sorted_checking.c create mode 100644 09_Linked_list/10_REmove_duplicate_data.c create mode 100644 09_Linked_list/11_Reverse_Linked_list.c create mode 100644 09_Linked_list/12_Concatenate_and_merge_linked_list.c create mode 100644 09_Linked_list/13_isloop.c create mode 100644 09_Linked_list/14_Linked_list_in_class.c create mode 100644 09_Linked_list/15_circular_LinkedList.c create mode 100644 09_Linked_list/16_Insert_circular_linked_lidt.c create mode 100644 09_Linked_list/17_deleting_from_circular_linked_list.c create mode 100644 09_Linked_list/18_doubly_LL.c create mode 100644 09_Linked_list/19_CDoubleLL.c create mode 100644 09_Linked_list/20_MiddleNode_and_intersection_of_two_LL.c create mode 100644 10_Sparse_matrix_and_polynimial_representation/01_sparse_matrix.c create mode 100644 10_Sparse_matrix_and_polynimial_representation/02_polynomial_linked_list.c create mode 100644 11_Stack/01_Stack_using_Arry.c create mode 100644 11_Stack/02_Stack_using_LL.c create mode 100644 11_Stack/03_parenthesis_is_balanced.c create mode 100644 11_Stack/04_parenthesis_is_balanced_extended.c create mode 100644 11_Stack/05_infix_to_postfix.c create mode 100644 11_Stack/06_infix_to_posstfix.c create mode 100644 11_Stack/07_postfix_eval.c create mode 100644 12_Queue/01_enqueue_and_dequeue.c create mode 100644 12_Queue/02_circular_queue.c create mode 100644 12_Queue/03_queue_using_ll.c create mode 100644 13_Trees/01_creating_binary_tree.c create mode 100644 13_Trees/02_Traversing.c create mode 100644 13_Trees/03_LevelOrder.c create mode 100644 13_Trees/04_Height_and_count.c create mode 100644 14_BST/01_bst.c create mode 100644 15_AVL_Tree/01_LL_Rotation.c create mode 100644 17_Heap/create_heap.c diff --git a/01_Essential_c_and_cpp/01_basic_array.c b/01_Essential_c_and_cpp/01_basic_array.c new file mode 100644 index 0000000..7173b81 --- /dev/null +++ b/01_Essential_c_and_cpp/01_basic_array.c @@ -0,0 +1,12 @@ +#include +int main() +{ + int a[5]={1,2,3,2,1}; + for(int i = 0; i < 5; i++) + { + int x = a[i]; + x=x*2; + printf("%d\n", x);//Length of array as each int is of 4bit therefore 4*5=20bit lenth or size + } + return 0; +} diff --git a/01_Essential_c_and_cpp/02_structure.c b/01_Essential_c_and_cpp/02_structure.c new file mode 100644 index 0000000..b0c9849 --- /dev/null +++ b/01_Essential_c_and_cpp/02_structure.c @@ -0,0 +1,30 @@ +#include +struct reportcard +{ + char name[20]; + int roll; + int tot_marks; + +}; + +int main() +{ + int i; + struct reportcard r[3];//declartion + for(i=0;i<3;i++)//initialization + { + printf("Name "); + scanf("%s", r[i].name); + printf("roll no "); + scanf("%d", &r[i].roll); + printf("Total Marks "); + scanf("%d", &r[i].tot_marks); + printf("\n"); + } + for(i=0;i<3;i++) + { + printf("%s %d %d\n", r[i].name, r[i].roll, r[i].tot_marks); + } + + return 0; +} diff --git a/01_Essential_c_and_cpp/03_pointer.c b/01_Essential_c_and_cpp/03_pointer.c new file mode 100644 index 0000000..d0b983b --- /dev/null +++ b/01_Essential_c_and_cpp/03_pointer.c @@ -0,0 +1,17 @@ +#include +#include +int main() +{ + int a[5]={1,3,5,7,9}; + int *p=&a[0],*p2,*p3;//(declaration) + printf("Stack Memory array %d\n",p[2]);//using pointer p as array a + p2=(int *)malloc(5*sizeof(int));//accessing heap memory using pointer in c (Initialization) + p3=(int *)malloc(5 * sizeof(int));//accessing heap memory using pointer in c++ + p2[0]=4; + p3[0]=5; + printf("Heap Memory array %d %d \n",p2[0],p3[0]);//(dereferencing) + free(p2);//dealocating heap memory in c(deallocation) + free(p3);//dealocating heap memory in c++ + printf("Heap Memory array %d %d ",p2[0],p3[0]); + return 0; +} diff --git a/01_Essential_c_and_cpp/04_reference.c b/01_Essential_c_and_cpp/04_reference.c new file mode 100644 index 0000000..bc5c0ea --- /dev/null +++ b/01_Essential_c_and_cpp/04_reference.c @@ -0,0 +1,10 @@ +//reference only for c++ +#include +int main() +{ + int a=10; + int *ref=&a;//declaring and initializing reference variable + printf("%d\n",a); + printf("%d", *ref); + return 0; +} diff --git a/01_Essential_c_and_cpp/05_pointer_to_Structure.c b/01_Essential_c_and_cpp/05_pointer_to_Structure.c new file mode 100644 index 0000000..3ce3d11 --- /dev/null +++ b/01_Essential_c_and_cpp/05_pointer_to_Structure.c @@ -0,0 +1,31 @@ +#include +#include + +struct RECTANGLE +{ + int length; + int breath; + /* data */ +}; + +int main() +{ + struct RECTANGLE r={5,5}; + r.length=10;//editing rectangle value + printf("Structure Rectangle in stack and accessed using variable %d %d\n", r.length, r.breath); + + //Now Using Pointer + struct RECTANGLE *p1=&r; + p1->length=2;//could even be (*p).length=2 + printf("Structure Rectangle in stack and accessed using pointer %d %d\n", p1->length, p1->breath); + + //Now creating object structure rectangle in heap memory using pointer + struct RECTANGLE *p2;//struct can be skipped in c++ + p2=(struct RECTANGLE *)malloc(sizeof(struct RECTANGLE));//this is for c++ for c it would be p2=(return datatype *)malloc(size of object need to be created) + //here it would be p2=(struct RECTANGLE *)malloc(sizeof(struct RECTANGLE)) + p2->length=14; + p2->breath=15; + printf("Structure Rectangle in heap and accessed using pointer %d %d\n", p2->length, p2->breath); + free(p2); + return 0; +} diff --git a/01_Essential_c_and_cpp/06_functions.c b/01_Essential_c_and_cpp/06_functions.c new file mode 100644 index 0000000..e9ac19d --- /dev/null +++ b/01_Essential_c_and_cpp/06_functions.c @@ -0,0 +1,20 @@ +#include + +void func(int x, int *y, int *z) +{ + x++; + (*y)++; + (*z)++; +} + +int main() +{ + int a=5,b=10,c=15; + + printf("\nBefore calling function \na = %d\nb = %d\nc = %d\n", a, b, c); + + func(a, &b, &c);//here a is call by value ,b is call by address//c is call by refence + + printf("\nAfter calling function \na = %d\nb = %d\nc = %d\n", a, b, c);//here a is call by value ,b is call by address//c is call by refence + return 0; +} diff --git a/01_Essential_c_and_cpp/07_array_as_parameter.c b/01_Essential_c_and_cpp/07_array_as_parameter.c new file mode 100644 index 0000000..28bbef8 --- /dev/null +++ b/01_Essential_c_and_cpp/07_array_as_parameter.c @@ -0,0 +1,46 @@ +#include +#include + +void funct1(int *a) +{ + int i; + for(i=0;i<5;i++) + { + printf("\nfunt1 %d", a[i]); + } +} +void funct2(int a[]) +{ + int i; + for(i=0;i<5;i++) + { + printf("\nfunt2 %d", a[i]); + } +} +int * funct3(int size)//returning address of an array , created and stored in heap by function +{ + int *a,i; + a=(int *)malloc(size * sizeof(int));//or a=(int *)malloc(size of array*size of data type) // creating an array in heap memory + for(i=0;i +#include + +struct RECTANGLE +{ + int length; + int breath; +}; + +void fun1(struct RECTANGLE r1) +{ + (r1.length)++; + (r1.breath)++; +} +void fun2(struct RECTANGLE *r1) +{ + (r1->length)++; + (r1->breath)++; +} +void fun3(struct RECTANGLE *r1) +{ + r1->length++; + r1->breath++; +} +struct RECTANGLE * fun4() +{ + struct RECTANGLE *r2; + r2=(struct RECTANGLE *)malloc(sizeof(struct RECTANGLE));// or r2=(struct RECTANGLE *)malloc(sizeof(struct RECTANGLE)); + r2->length=1; + r2->breath=2; + return r2; +} +int main() +{ + struct RECTANGLE r={10,5}; + printf("Before any call %d %d\n", r.length, r.breath); + fun1(r); + printf("After call by value %d %d\n", r.length, r.breath); + fun2(&r); + printf("After call by referece %d %d\n", r.length, r.breath); + fun3(&r); + printf("After call by address %d %d\n", r.length, r.breath); + + // creating structure in heap memory and accesing them using function + struct RECTANGLE *ptr=fun4(); + printf("After creating structure in heap memory %d %d\n", ptr->length, ptr->breath);// -> is to access struct elements using pointer + free(ptr); + return 0; +} diff --git a/01_Essential_c_and_cpp/09_01_monolythic_program.c b/01_Essential_c_and_cpp/09_01_monolythic_program.c new file mode 100644 index 0000000..6db57d9 --- /dev/null +++ b/01_Essential_c_and_cpp/09_01_monolythic_program.c @@ -0,0 +1,12 @@ +//monolytic style of programing +#include +int main() +{ + int l,b; + printf("Enter lenth and breath of rectangle "); + scanf("%d %d", &l, &b); + int area=l*b; + int peri=2*(l+b); + printf("%d is area\n%d is perimeter",area,peri); + return 0; +} diff --git a/01_Essential_c_and_cpp/09_02_modeular_with_structure_programming_style.c b/01_Essential_c_and_cpp/09_02_modeular_with_structure_programming_style.c new file mode 100644 index 0000000..69d52ad --- /dev/null +++ b/01_Essential_c_and_cpp/09_02_modeular_with_structure_programming_style.c @@ -0,0 +1,33 @@ +//modular style of programing along with structure +#include + +struct Rectangle +{ + int length; + int breath; +}; +void initialization(struct Rectangle *r,int l,int b) +{ + r->length=l; + r->breath=b; +} +int area(struct Rectangle r1) +{ + return r1.length*r1.breath; +} +int peri(struct Rectangle r1) +{ + return 2*(r1.length+r1.breath); +} +int main() +{ + struct Rectangle r={0,0}; + int l,b; + printf("Enter lenth and breath of rectangle "); + scanf("%d %d", &l, &b); + initialization(&r,l,b); + int a=area(r); + int p=peri(r); + printf("%d is area\n%d is perimeter",a,p); + return 0; +} diff --git a/01_Essential_c_and_cpp/09_03_oop_style.c b/01_Essential_c_and_cpp/09_03_oop_style.c new file mode 100644 index 0000000..dddca58 --- /dev/null +++ b/01_Essential_c_and_cpp/09_03_oop_style.c @@ -0,0 +1,35 @@ +//object oriented style of programing +#include + +struct Rectangle +{ + int length; + int breath; +}; + +void init_Rectangle(struct Rectangle* self, int l, int b) +{ + self->length=l; + self->breath=b; +} +int Rectangle_area(struct Rectangle* self) +{ + return self->length * self->breath; +} +int Rectangle_peri(struct Rectangle* self) +{ + return 2*(self->length + self->breath); +} + +int main() +{ + int l,b; + printf("Enter lenth and breath of rectangle "); + scanf("%d %d", &l, &b); + struct Rectangle r; + init_Rectangle(&r, l, b); + int a = Rectangle_area(&r); + int p = Rectangle_peri(&r); + printf("%d is area\n%d is perimeter",a,p); + return 0; +} diff --git a/01_Essential_c_and_cpp/10_rectangle_class_with_oop.c b/01_Essential_c_and_cpp/10_rectangle_class_with_oop.c new file mode 100644 index 0000000..a15f33f --- /dev/null +++ b/01_Essential_c_and_cpp/10_rectangle_class_with_oop.c @@ -0,0 +1,66 @@ +#include + +struct Rectangle +{ + int length; + int breadth; +}; + +void init_Rectangle_default(struct Rectangle* self) +{ + self->length=0; + self->breadth=0; +} +void init_Rectangle(struct Rectangle* self, int l, int b) +{ + self->length=l; + self->breadth=b; +} +int Rectangle_area(struct Rectangle* self) +{ + return self->length * self->breadth; +} +int Rectangle_perimeter(struct Rectangle* self) +{ + return 2*(self->length + self->breadth); +} +void Rectangle_setLength(struct Rectangle* self, int l) +{ + self->length=l; +} +void Rectangle_setbreadth(struct Rectangle* self, int b) +{ + self->breadth=b; +} +int Rectangle_getLength(struct Rectangle* self) +{ + return self->length; +} +int Rectangle_getbreadth(struct Rectangle* self) +{ + return self->breadth; +} +void destroy_Rectangle(struct Rectangle* self) +{ + printf("Destructor\n"); +} + +int main() +{ + struct Rectangle r; + init_Rectangle(&r, 10, 4); + int a=Rectangle_area(&r); + int p=Rectangle_perimeter(&r); + printf("area = %d perimerter = %d\n",a,p); + Rectangle_setLength(&r, 5); + Rectangle_setbreadth(&r, 4); + a=Rectangle_area(&r); + p=Rectangle_perimeter(&r); + printf("area = %d perimerter = %d\n",a,p); + int l=Rectangle_getLength(&r); + int b=Rectangle_getbreadth(&r); + printf("Length = %d breadth = %d\n",l,b); + + destroy_Rectangle(&r); + return 0; +} diff --git a/01_Essential_c_and_cpp/11_above_program_as_template_class.c b/01_Essential_c_and_cpp/11_above_program_as_template_class.c new file mode 100644 index 0000000..2dcf68e --- /dev/null +++ b/01_Essential_c_and_cpp/11_above_program_as_template_class.c @@ -0,0 +1,129 @@ +#include + +// int specialization +struct Rectangle_int +{ + int length; + int breadth; +}; + +void init_Rectangle_int(struct Rectangle_int* self, int l, int b) +{ + self->length = l; + self->breadth = b; +} +int Rectangle_int_area(struct Rectangle_int* self) +{ + return self->length * self->breadth; +} +int Rectangle_int_perimeter(struct Rectangle_int* self) +{ + return 2*(self->length + self->breadth); +} +void Rectangle_int_setLength(struct Rectangle_int* self, int l) +{ + self->length = l; +} +void Rectangle_int_setbreadth(struct Rectangle_int* self, int b) +{ + self->breadth = b; +} +int Rectangle_int_getLength(struct Rectangle_int* self) +{ + return self->length; +} +int Rectangle_int_getbreadth(struct Rectangle_int* self) +{ + return self->breadth; +} +void destroy_Rectangle_int(struct Rectangle_int* self) +{ + printf("Destructor\n"); +} + + +// float specialization +struct Rectangle_float +{ + float length; + float breadth; +}; + +void init_Rectangle_float(struct Rectangle_float* self, float l, float b) +{ + self->length = l; + self->breadth = b; +} +float Rectangle_float_area(struct Rectangle_float* self) +{ + return self->length * self->breadth; +} +float Rectangle_float_perimeter(struct Rectangle_float* self) +{ + return 2.0f * (self->length + self->breadth); +} +void Rectangle_float_setLength(struct Rectangle_float* self, float l) +{ + self->length = l; +} +void Rectangle_float_setbreadth(struct Rectangle_float* self, float b) +{ + self->breadth = b; +} +float Rectangle_float_getLength(struct Rectangle_float* self) +{ + return self->length; +} +float Rectangle_float_getbreadth(struct Rectangle_float* self) +{ + return self->breadth; +} +void destroy_Rectangle_float(struct Rectangle_float* self) +{ + printf("Destructor\n"); +} + + +int main() +{ + printf("INTEGER\n"); + struct Rectangle_int r; + init_Rectangle_int(&r, 10, 4); + int a = Rectangle_int_area(&r); + int p = Rectangle_int_perimeter(&r); + printf("area = %d perimerter = %d\n", a, p); + + Rectangle_int_setLength(&r, 5); + Rectangle_int_setbreadth(&r, 4); + a = Rectangle_int_area(&r); + p = Rectangle_int_perimeter(&r); + printf("area = %d perimerter = %d\n", a, p); + + int l = Rectangle_int_getLength(&r); + int b = Rectangle_int_getbreadth(&r); + printf("Length = %d breadth = %d\n", l, b); + + destroy_Rectangle_int(&r); + + + printf("\n\nFLOAT\n"); + struct Rectangle_float r1; + init_Rectangle_float(&r1, 10.0, 4.0); + float a1 = Rectangle_float_area(&r1); + float p1 = Rectangle_float_perimeter(&r1); + printf("area = %f perimerter = %f\n", a1, p1); + + Rectangle_float_setLength(&r1, 5.3); + Rectangle_float_setbreadth(&r1, 4.1); + a1 = Rectangle_float_area(&r1); + p1 = Rectangle_float_perimeter(&r1); + printf("area = %f perimerter = %f\n", a1, p1); + + float l1 = Rectangle_float_getLength(&r1); + float b1 = Rectangle_float_getbreadth(&r1); + printf("Length = %f breadth = %f\n", l1, b1); + + destroy_Rectangle_float(&r1); + + return 0; +} diff --git a/03_recursion/01_first_recursion_program.c b/03_recursion/01_first_recursion_program.c new file mode 100644 index 0000000..ed136d7 --- /dev/null +++ b/03_recursion/01_first_recursion_program.c @@ -0,0 +1,16 @@ +#include + +void funct1(int n) +{ + if(n>0) + { + printf("%d", n); + funct1(n-1); + } +} +int main() +{ + int i=3; + funct1(i); + return 0; +} diff --git a/03_recursion/02_static_variable_in_recursion.c b/03_recursion/02_static_variable_in_recursion.c new file mode 100644 index 0000000..1315ce1 --- /dev/null +++ b/03_recursion/02_static_variable_in_recursion.c @@ -0,0 +1,20 @@ +#include +int y=0;//global variable + +int static_fun(int n) +{ + static int x=0;//static variable + if(n>0) + { + x++; + return static_fun(n-1)+x; + } + return 0; +} +int main() +{ + int a,b=5; + a=static_fun(b); + printf("%d", a); + return 0; +} diff --git a/03_recursion/03_tree_recursion.c b/03_recursion/03_tree_recursion.c new file mode 100644 index 0000000..0fdc0a2 --- /dev/null +++ b/03_recursion/03_tree_recursion.c @@ -0,0 +1,17 @@ +#include + +void fun(int n) +{ + if(n>0) + { + printf("%d ", n); + fun(n-1); + fun(n-1); + } + +} +int main() +{ + fun(3); + return 0; +} diff --git a/03_recursion/04_indirect_recursion.c b/03_recursion/04_indirect_recursion.c new file mode 100644 index 0000000..e2a6e39 --- /dev/null +++ b/03_recursion/04_indirect_recursion.c @@ -0,0 +1,25 @@ +#include + +void fun2(int); +void fun1(int n) +{ + if(n>0) + { + printf("%d ", n); + fun2(n-1); + } + +} +void fun2(int n) +{ + if(n>0) + { + printf("%d ", n); + fun1(n/2); + } +} +int main() +{ + fun1(10); + return 0; +} diff --git a/03_recursion/05_nested_recursion.c b/03_recursion/05_nested_recursion.c new file mode 100644 index 0000000..d3693ed --- /dev/null +++ b/03_recursion/05_nested_recursion.c @@ -0,0 +1,17 @@ +#include + +int fun(int n) +{ + if(n>100) + return n-10; + else + return fun(fun(n+11)); +} + +int main() +{ + + int i=fun(97); + printf("%d", i); + return 0; +} diff --git a/03_recursion/06_sum_of_first_n_natural_number.c b/03_recursion/06_sum_of_first_n_natural_number.c new file mode 100644 index 0000000..3e6514d --- /dev/null +++ b/03_recursion/06_sum_of_first_n_natural_number.c @@ -0,0 +1,33 @@ +#include + +int sum(int n) +{ + if(n<=0) + return 0; + return n+sum(n-1); + +} +int Isum(int n) +{ + int i; + int sum=0; + for (i=0;i<=n;i++) + { + sum=sum+i; + } + return sum; +} +int Isum_factorial(int n) +{ + int i,factorial=1; + for(i=1;i<=n;i++) + factorial=factorial*i; + return factorial; + +} +int main() +{ + int s=Isum(5); + printf("%d", s); + return 0; +} diff --git a/03_recursion/07_factorial.c b/03_recursion/07_factorial.c new file mode 100644 index 0000000..5c8291a --- /dev/null +++ b/03_recursion/07_factorial.c @@ -0,0 +1,24 @@ +#include + +int fac(int n) +{ + if(n<=0) + return 1; + else + return n*fac(n-1); +} +int Ifac(int n) +{ + int i,factorial=1; + for(i=1;i<=n;i++) + factorial=factorial*i; + return factorial; + +} + +int main() +{ + int factorial=Ifac(5); + printf("%d", factorial); + return 0; +} diff --git a/03_recursion/08_exponent.c b/03_recursion/08_exponent.c new file mode 100644 index 0000000..041d454 --- /dev/null +++ b/03_recursion/08_exponent.c @@ -0,0 +1,33 @@ +#include + +int expo(int m,int n) +{ + if(n<=0) + return 1; + return m*expo(m,n-1); +} +int expo1(int m,int n) +{ + if(n==0) + return 1; + if(n%2==0) + return expo1(m*m,n/2); + else + return m*expo1(m*m,(n-1)/2); +} +int iexpo(int m,int n) +{ + int e=1,i; + for(i=1;i<=n;i++) + { + e=e*m; + } + return e; +} + +int main() +{ + int e=expo1(2,3); + printf("%d", e); + return 0; +} diff --git a/03_recursion/09_taylor_series.c b/03_recursion/09_taylor_series.c new file mode 100644 index 0000000..cd77b4e --- /dev/null +++ b/03_recursion/09_taylor_series.c @@ -0,0 +1,21 @@ +#include + +double tay(int x,int n) +{ + static float p=1,f=1; + if(n==0) + return 1; + else + { + double r=tay(x,n-1); + p=p*x; + f=f*n; + return r+(p/f); + } + +} +int main() +{ + printf("%f\n", tay(1,10)); + return 0; +} diff --git a/03_recursion/10_taylor_series_using_horners_rule.c b/03_recursion/10_taylor_series_using_horners_rule.c new file mode 100644 index 0000000..eb46cdf --- /dev/null +++ b/03_recursion/10_taylor_series_using_horners_rule.c @@ -0,0 +1,26 @@ +#include + +float e1(float x,int n)//using loop +{ + float s=1; + for(;n>0;n--) + { + s=1+x/n*s; + } + return s; +} + +float e(float x,int n)//using recursion +{ + static float s=1; + if(n==0) + return s; + s=1+x/n*s; + return e(x,n-1); +} +int main() +{ + printf("%f\n", e(1,10)); + printf("%f", e(1,10)); + return 0; +} diff --git a/03_recursion/11_fibonacci.c b/03_recursion/11_fibonacci.c new file mode 100644 index 0000000..c6d5fc8 --- /dev/null +++ b/03_recursion/11_fibonacci.c @@ -0,0 +1,57 @@ +#include + +int ifib1(int n)//fibonacci series using loops/iteration +{ + int t0=0; + int t1=1,s; + for(int i=2;i<=n;i++) + { + s=t1+t0; + t0=t1; + t1=s; + } + return s; +} +int ifib2(int n)//fibonacci series using loops/iteration +{ + int f[n+1]; + f[0]=0; + f[1]=1; + for(int i=2;i<=n;i++) + { + f[i]=f[i-2]+f[i-1]; + } + return f[n]; +} +int rfib(int n)//fibonacci series using recursion +{ + if(n<=1) + return n; + return rfib(n-1)+rfib(n-2); +} +int F[10]; +int mfib(int n)//fibonacci series using recursion using memoization +{ + if(n<=1) + return n; + else + { + if(F[n-1]==-1)//it means no value entered till yet as it is -1 + F[n-1]=mfib(n-1); + if(F[n-2]==-1) + F[n-2]=mfib(n-2); + F[n]=F[n-1]+F[n-2]; + return F[n-1]+F[n-2]; + } +} + +int main() +{ + for(int i=0;i<=10;i++) + F[i]=-1; + printf("%d\n", ifib1(7)); + printf("%d\n", ifib2(7)); + printf("%d\n", rfib(7)); + printf("%d\n", mfib(5)); + return 0; +} diff --git a/03_recursion/12_ncr.c b/03_recursion/12_ncr.c new file mode 100644 index 0000000..7c5d5d8 --- /dev/null +++ b/03_recursion/12_ncr.c @@ -0,0 +1,28 @@ +#include + +int fact(int n) +{ + if(n<=1) + return 1; + else + return fact(n-1)*n; +} +int ncr(int n,int r)//ncr combination using factorial function +{ + int num=fact(n); + int den=fact(r)*fact(n-r); + return num/den; +} + +int pncr(int n,int r)//using pascal triangle +{ + if((n==1) || (n==r)) + return 1; + return pncr(n-1,r-1)+pncr(n-1,r); +} +int main() +{ + printf("%d\n", ncr(3,2)); + printf("%d\n", pncr(3,2)); + return 0; +} diff --git a/03_recursion/13_toh.c b/03_recursion/13_toh.c new file mode 100644 index 0000000..6d5306f --- /dev/null +++ b/03_recursion/13_toh.c @@ -0,0 +1,27 @@ +#include + +void TOH(int n,int A,int B,int C) +{ + if(n>0) + { + TOH(n-1,A,C,B); + printf("Moving from %d to %d\n\n", A, C); + TOH(n-1,B,A,C); + } +} +void TOH1(int n,int A,int B,int C,int D)//for four towes +{ + if(n>0) + { + TOH1(n-1,A,B,D,C); + printf("(%d,%d)\n", A, D); + TOH1(n-1,C,A,B,D); + } +} + +int main() +{ + TOH(3,1,2,3); + TOH1(5,1,2,3,4); + return 0; +} diff --git a/04_Array_representation/01_static_array_and_dynamic_array.c b/04_Array_representation/01_static_array_and_dynamic_array.c new file mode 100644 index 0000000..8beb931 --- /dev/null +++ b/04_Array_representation/01_static_array_and_dynamic_array.c @@ -0,0 +1,15 @@ +#include +#include +int main() +{ + int A[5]={1,2,3,4,5};//Static array in stack + int *B=(int*)malloc(5 * sizeof(int));// dynamic array in heap + for(int i=0;i<5;i++) + B[i]=i+6; + for(int i=0;i<5;i++) + printf("%d\n", A[i]); + for(int i=0;i<5;i++) + printf("%d\n", B[i]); + free(B); + return 0; +} diff --git a/04_Array_representation/02_increasing_array_size.c b/04_Array_representation/02_increasing_array_size.c new file mode 100644 index 0000000..945dc4b --- /dev/null +++ b/04_Array_representation/02_increasing_array_size.c @@ -0,0 +1,17 @@ +#include +#include +int main() +{ + int *p=(int*)malloc(5 * sizeof(int)); + for(int i=0;i<5;i++) + p[i]=i; + int *q=(int*)malloc(10 * sizeof(int)); + for(int i=0;i<5;i++) + q[i]=p[i]; + q[6]=456; + p=q;//changing size of array + q=NULL; + for(int i=0;i<10;i++) + printf("%d\n", p[i]); + return 0; +} diff --git a/04_Array_representation/03_2d_array.c b/04_Array_representation/03_2d_array.c new file mode 100644 index 0000000..464f754 --- /dev/null +++ b/04_Array_representation/03_2d_array.c @@ -0,0 +1,51 @@ +#include +#include +int main() +{ + int A[2][2];//Method 1:Whole 2D array in stack + A[0][0]=2; + A[0][1]=3; + A[1][0]=4; + A[1][1]=5; + for(int i=0;i<2;i++) + { + for(int j=0;j<2;j++) + printf("%d ", A[i][j]); + printf("\n"); + } + printf("\n"); + + int *B[2];//Method 2:Row in stack and column in heap + B[0]=(int*)malloc(2 * sizeof(int));//Return int pointer + B[1]=(int*)malloc(2 * sizeof(int));//Return int pointer + B[0][0]=12; + B[0][1]=13; + B[1][0]=14; + B[1][1]=15; + for(int i=0;i<2;i++) + { + for(int j=0;j<2;j++) + printf("%d ", B[i][j]); + printf("\n"); + } + printf("\n"); + + int **C;//Method 3: complete 2D array in heap + C=(int**)malloc(2 * sizeof(int*));//Return int double pointer + C[0]=(int*)malloc(2 * sizeof(int));//Return int pointer + C[1]=(int*)malloc(2 * sizeof(int));//Return int pointer + C[0][0]=112; + C[0][1]=113; + C[1][0]=114; + C[1][1]=115; + for(int i=0;i<2;i++) + { + for(int j=0;j<2;j++) + printf("%d ", C[i][j]); + printf("\n"); + } + printf("\n"); + + + return 0; +} diff --git a/05_Array_ADT/01_creating_array_of_desired_size.c b/05_Array_ADT/01_creating_array_of_desired_size.c new file mode 100644 index 0000000..8204370 --- /dev/null +++ b/05_Array_ADT/01_creating_array_of_desired_size.c @@ -0,0 +1,65 @@ +#include +#include + +struct Array { + int* A; + int size; + int length; +}; + +void init_Array(struct Array* self, int size) { + self->size = size; + self->length = 0; + self->A = (int*)malloc(size * sizeof(int)); +} + +void create(struct Array* self) { + printf("Enter number of elements: "); + fflush(stdout); + scanf("%d", &self->length); + printf("Enter the array elements: \n"); + for (int i = 0; i < self->length; i++){ + printf("Array element: %d = ", i); + fflush(stdout); + scanf("%d", &self->A[i]); + } +} + +void append(struct Array* self, int a) { + if(self->length < self->size) { + self->A[self->length] = a; + self->length++; + } +} + +void display(struct Array* self) { + for (int i = 0; i < self->length; i++){ + printf("%d ", self->A[i]); + } +} + +void insert(struct Array* self, int index, int value) { + if(index >= 0 && index <= self->size){ + for(int i = self->length; i > index; i--) { + self->A[i] = self->A[i-1]; + } + self->A[index] = value; + self->length++; + } +} + +void destroy_Array(struct Array* self) { + free(self->A); + printf("Array destroyed\n"); +} + +int main() { + struct Array arr; + init_Array(&arr, 10); + create(&arr); + append(&arr, 2); + insert(&arr, 3, 78); + display(&arr); + destroy_Array(&arr); + return 0; +} diff --git a/05_Array_ADT/02_arrray_deletion.c b/05_Array_ADT/02_arrray_deletion.c new file mode 100644 index 0000000..3bc41c1 --- /dev/null +++ b/05_Array_ADT/02_arrray_deletion.c @@ -0,0 +1,59 @@ +#include +#include + +struct Array { + int* A; + int size; + int length; +}; + +void init_Array(struct Array* self, int size) { + self->size = size; + self->length = 0; + self->A = (int*)malloc(size * sizeof(int)); +} + +void create(struct Array* self) { + printf("Enter number of elements: "); + fflush(stdout); + scanf("%d", &self->length); + printf("Enter the array elements: \n"); + for (int i = 0; i < self->length; i++){ + printf("Array element: %d = ", i); + fflush(stdout); + scanf("%d", &self->A[i]); + } +} + +void display(struct Array* self) { + for (int i = 0; i < self->length; i++){ + printf("%d ", self->A[i]); + } + printf("\n"); +} + +void del(struct Array* self, int index) { + if(index >= 0 && index <= self->length) { + for(int i = index; i < self->length - 1; i++) { + self->A[i] = self->A[i+1]; + } + self->length--; + printf("deleted index %d element in array\n", index); + } +} + +void destroy_Array(struct Array* self) { + free(self->A); + printf("Array destroyed\n"); +} + +int main() { + struct Array arr; + init_Array(&arr, 10); + create(&arr); + display(&arr); + del(&arr, 3); + display(&arr); + destroy_Array(&arr); + return 0; +} diff --git a/05_Array_ADT/03_01_linear_search.c b/05_Array_ADT/03_01_linear_search.c new file mode 100644 index 0000000..e58273a --- /dev/null +++ b/05_Array_ADT/03_01_linear_search.c @@ -0,0 +1,36 @@ +#include + +struct Array { + int A[10]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +void swap(int *x, int *y) { + int t = *x; + *x = *y; + *y = t; +} + +int LinearSearch(struct Array *arr, int value) { + for(int i = 0; i < arr->length; i++) { + if(arr->A[i] == value) { + swap(&arr->A[i-1], &arr->A[i]); + return i; + } + } + return -1; +} + +int main() { + struct Array arr = {{1,2,3,4,5,6,7}, 10, 7}; + printf("%d\n", LinearSearch(&arr, 4)); + Display(arr); + return 0; +} diff --git a/05_Array_ADT/03_02_linerar_search.c b/05_Array_ADT/03_02_linerar_search.c new file mode 100644 index 0000000..abdf6eb --- /dev/null +++ b/05_Array_ADT/03_02_linerar_search.c @@ -0,0 +1,63 @@ +#include +#include + +struct Array { + int* A; + int size; + int length; +}; + +void init_Array(struct Array* self, int size) { + self->size = size; + self->length = 0; + self->A = (int*)malloc(size * sizeof(int)); +} + +void create(struct Array* self) { + printf("Enter number of elements: "); + fflush(stdout); + scanf("%d", &self->length); + printf("Enter the array elements: \n"); + for (int i = 0; i < self->length; i++){ + printf("Array element: %d = ", i); + fflush(stdout); + scanf("%d", &self->A[i]); + } +} + +void display(struct Array* self) { + for (int i = 0; i < self->length; i++){ + printf("%d ", self->A[i]); + } + printf("\n"); +} + +int l_search(struct Array* self, int value) { + for(int i = 0; i < self->length; i++) { + if(self->A[i] == value) { + if(i > 0) { + int t = self->A[i-1]; //Transpose method to improve linear search + self->A[i-1] = self->A[i]; + self->A[i] = t; + } + return i; + } + } + return -1; +} + +void destroy_Array(struct Array* self) { + free(self->A); + printf("Array destroyed\n"); +} + +int main() { + struct Array arr; + init_Array(&arr, 10); + create(&arr); + display(&arr); + printf("element found at index %d\n", l_search(&arr, 3)); + display(&arr); + destroy_Array(&arr); + return 0; +} diff --git a/05_Array_ADT/04_binary_search.c b/05_Array_ADT/04_binary_search.c new file mode 100644 index 0000000..327d31a --- /dev/null +++ b/05_Array_ADT/04_binary_search.c @@ -0,0 +1,56 @@ +#include + +struct Array { + int A[10]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +void swap(int *x, int *y) { + int t = *x; + *x = *y; + *y = t; +} + +int BinarySearch(struct Array arr, int value) { + if(arr.length > 0) { + int l = 0; + int h = arr.length - 1; + while(l <= h) { + int mid = (l + h) / 2; + if(value == arr.A[mid]) + return mid; + else if(arr.A[mid] < value) + l = mid + 1; + else + h = mid - 1; + } + } + return -1; +} + +int RBinarySearch(struct Array arr, int value, int l, int h) { + if(l <= h) { + int mid = (l + h) / 2; + if(value == arr.A[mid]) + return mid; + else if(value < arr.A[mid]) + return RBinarySearch(arr, value, l, mid - 1); + else + return RBinarySearch(arr, value, mid + 1, h); + } + return -1; +} + +int main() { + struct Array arr = {{1,2,3,4,5,6,7}, 10, 7}; + printf("%d\n", RBinarySearch(arr, 6, 0, arr.length - 1)); + Display(arr); + return 0; +} diff --git a/05_Array_ADT/05_get_set_max_sum.c b/05_Array_ADT/05_get_set_max_sum.c new file mode 100644 index 0000000..7bcd2d1 --- /dev/null +++ b/05_Array_ADT/05_get_set_max_sum.c @@ -0,0 +1,76 @@ +#include + +struct Array { + int A[10]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +void swap(int *x, int *y) { + int t = *x; + *x = *y; + *y = t; +} + +int Get(struct Array arr, int index) { + if(index >= 0 && index <= arr.length) + return arr.A[index]; + return -1; +} + +void Set(struct Array *arr, int index, int value) { + if(index >= 0 && index <= arr->length) + arr->A[index] = value; +} + +int Max(struct Array arr) { + int max = arr.A[0]; + for(int i = 1; i < arr.length; i++) { + if(arr.A[i] > max) + max = arr.A[i]; + } + return max; +} + +int Min(struct Array arr) { + int min = arr.A[0]; + for(int i = 1; i < arr.length; i++) { + if(arr.A[i] < min) + min = arr.A[i]; + } + return min; +} + +int Sum(struct Array arr) { + int sum = 0; + for(int i = 0; i < arr.length; i++) { + sum = sum + arr.A[i]; + } + return sum; +} + +float Avg(struct Array arr) { //u can call function sum in avg and type cast it as float-> (float)Sum(arr)/arr.length + float sum = 0; + for(int i = 0; i < arr.length; i++) { + sum = sum + arr.A[i]; + } + return sum / arr.length; +} + +int RSum(struct Array arr, int n) { //by recursion + if(n >= 0) + return RSum(arr, n - 1) + arr.A[n]; + return 0; +} + +int main() { + struct Array arr = {{1,2,3,4,5,6,8}, 10, 7}; + printf("%f\n", Avg(arr)); + return 0; +} diff --git a/05_Array_ADT/05_reverse_and_shift_of_an_array.c b/05_Array_ADT/05_reverse_and_shift_of_an_array.c new file mode 100644 index 0000000..4746963 --- /dev/null +++ b/05_Array_ADT/05_reverse_and_shift_of_an_array.c @@ -0,0 +1,72 @@ +#include + +struct Array { + int A[10]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +void swap(int *x, int *y) { + int t = *x; + *x = *y; + *y = t; +} + +void RUA(struct Array arr) { //reversing array using auxilary array B + int arr2[10]; + for(int i = (arr.length - 1), j = 0; i >= 0; i--, j++) { + arr2[j] = arr.A[i]; + } + for(int i = 0; i < (arr.length); i++) { + arr.A[i] = arr2[i]; + } + Display(arr); +} + +void INT(struct Array *arr) { //reversing array using interchanging + for(int i = 0, j = (arr->length - 1); i < j; i++, j--) + swap(&arr->A[i], &arr->A[j]); +} + +void r_shift(struct Array *arr) { //right shift + for(int j = (arr->length - 1); j > 0; j--) + arr->A[j] = arr->A[j - 1]; + arr->A[0] = 0; +} + +void r_shift_r(struct Array *arr) { //right shift roatation + for(int i = 0; i < arr->length; i++) { + int temp = arr->A[arr->length - 1]; + for(int j = (arr->length - 1); j > 0; j--) + arr->A[j] = arr->A[j - 1]; + arr->A[0] = temp; + } +} + +void l_shift(struct Array *arr) { //left shift + for(int j = 1; j < (arr->length); j++) + arr->A[j - 1] = arr->A[j]; + arr->A[arr->length - 1] = 0; +} + +void l_shift_r(struct Array *arr) { //left shift roatation + for(int i = 1; i <= (arr->length); i++) { + int temp = arr->A[0]; + for(int j = 1; j < (arr->length); j++) + arr->A[j - 1] = arr->A[j]; + arr->A[(arr->length) - 1] = temp; + } +} + +int main() { + struct Array arr = {{1,2,3,4,5,6,8}, 10, 7}; + r_shift_r(&arr); + Display(arr); + return 0; +} diff --git a/05_Array_ADT/06_01_inserting_in_sorted_array.c b/05_Array_ADT/06_01_inserting_in_sorted_array.c new file mode 100644 index 0000000..96f6a7d --- /dev/null +++ b/05_Array_ADT/06_01_inserting_in_sorted_array.c @@ -0,0 +1,36 @@ +#include + +struct Array { + int A[10]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +void swap(int *x, int *y) { + int t = *x; + *x = *y; + *y = t; +} + +void insert(struct Array *arr, int x) { + int i = arr->length - 1; + while(i >= 0 && arr->A[i] > x) { + arr->A[i + 1] = arr->A[i]; + i--; + } + arr->A[i + 1] = x; + arr->length++; +} + +int main() { + struct Array arr = {{1,2,3,4,6,8,10}, 10, 7}; + insert(&arr, 7); + Display(arr); + return 0; +} diff --git a/05_Array_ADT/06_02_check_if_soted.c b/05_Array_ADT/06_02_check_if_soted.c new file mode 100644 index 0000000..f9972d8 --- /dev/null +++ b/05_Array_ADT/06_02_check_if_soted.c @@ -0,0 +1,35 @@ +#include + +struct Array { + int A[10]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +void swap(int *x, int *y) { + int t = *x; + *x = *y; + *y = t; +} + +int sort(struct Array arr) { + int i = 0; + while(i < arr.length - 1) { + if(arr.A[i] > arr.A[i + 1]) + return 0; // false + i++; + } + return 1; // true +} + +int main() { + struct Array arr = {{1,2,3,4,5,8,10}, 10, 7}; + printf("%d\n", sort(arr)); + return 0; +} diff --git a/05_Array_ADT/06_03_-ve.c b/05_Array_ADT/06_03_-ve.c new file mode 100644 index 0000000..447a700 --- /dev/null +++ b/05_Array_ADT/06_03_-ve.c @@ -0,0 +1,35 @@ +#include + +struct Array { + int A[10]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +void swap(int *x, int *y) { + int t = *x; + *x = *y; + *y = t; +} + +void neg(struct Array *arr) { + int i = 0, j = arr->length - 1; + while(i < j) { + while (arr->A[i] < 0) i++; + while(arr->A[j] > 0) j--; + if(i < j) swap(&arr->A[i], &arr->A[j]); + } +} + +int main() { + struct Array arr = {{-1,2,-3,4,-5,8,-10}, 10, 7}; + neg(&arr); + Display(arr); + return 0; +} diff --git a/05_Array_ADT/07_merging_array.c b/05_Array_ADT/07_merging_array.c new file mode 100644 index 0000000..1c0de4d --- /dev/null +++ b/05_Array_ADT/07_merging_array.c @@ -0,0 +1,43 @@ +#include + +struct Array { + int A[100]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +void swap(int *x, int *y) { + int t = *x; + *x = *y; + *y = t; +} + +void Merge(struct Array arr1, struct Array arr2, struct Array *arr3) { + int i = 0, j = 0, k = 0; + while(i < arr1.length && j < arr2.length) { + if(arr1.A[i] < arr2.A[j]) + arr3->A[k++] = arr1.A[i++]; + else + arr3->A[k++] = arr2.A[j++]; + } + for(; i < arr1.length; i++) + arr3->A[k++] = arr1.A[i++]; + for(; j < arr2.length; j++) + arr3->A[k++] = arr2.A[j++]; +} + +int main() { + struct Array arr1 = {{1,4,7,13,17,20,36}, 100, 7}; + struct Array arr2 = {{6,8,9,10,11,24}, 100, 6}; + struct Array arr3 = {{}, arr1.size + arr2.size, arr1.length + arr2.length}; + Merge(arr1, arr2, &arr3); + printf("%lu\n", (unsigned long)sizeof(arr3)); // Size in bytes + Display(arr3); + return 0; +} diff --git a/05_Array_ADT/08_set.c b/05_Array_ADT/08_set.c new file mode 100644 index 0000000..b17a504 --- /dev/null +++ b/05_Array_ADT/08_set.c @@ -0,0 +1,155 @@ +#include + +struct Array { + int A[100]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +void swap(int *x, int *y) { + int t = *x; + *x = *y; + *y = t; +} + +int sort(struct Array arr) { + int i = 0; + while(i < arr.length - 1) { + if(arr.A[i] > arr.A[i + 1]) + return 0; + i++; + } + return 1; +} + +struct Array Union(struct Array arr1, struct Array arr2) { + struct Array arr3; + if(sort(arr1) && sort(arr2)) { //if arr1 and arr2 are sorted + int i = 0, j = 0, k = 0; + while(i < arr1.length && j < arr2.length) { + if(arr1.A[i] < arr2.A[j]) + arr3.A[k++] = arr1.A[i++]; + else if(arr1.A[i] == arr2.A[j]) { + arr3.A[k++] = arr1.A[i++]; + j++; + } + else + arr3.A[k++] = arr2.A[j++]; + } + for(; i < arr1.length; i++) + arr3.A[k++] = arr1.A[i]; + for(; j < arr2.length; j++) + arr3.A[k++] = arr2.A[j]; + arr3.length = k; + arr3.size = 100; + return arr3; + } + else { + int i = 0, j = 0, k = 0, m = 0; + for(i = 0; i < arr1.length; i++, k++) + arr3.A[k] = arr1.A[i]; + for(i = 0; i < arr2.length; i++) { + int l = 0, j = 0; + while(j < arr1.length) { + if(arr2.A[i] == arr3.A[j]) { //checking for every element of array 1 if it exist in array 2 + l = 1; + break; + } + j++; + } + if(l == 0) + arr3.A[k++] = arr2.A[i]; + } + arr3.length = k; + arr3.size = 100; + return arr3; + } +} + +struct Array Intersection(struct Array arr1, struct Array arr2) { + struct Array arr3; + if(sort(arr1) && sort(arr2)) { //if arr1 and arr2 are sorted + int i = 0, j = 0, k = 0; + while(i < arr1.length && j < arr2.length) { + if(arr1.A[i] < arr2.A[j]) + i++; + else if(arr1.A[i] == arr2.A[j]) { + arr3.A[k++] = arr1.A[i]; + i++; + j++; + } + else + j++; + } + arr3.length = k; + arr3.size = 100; + return arr3; + } + else { + int i = 0, j = 0, k = 0; + for(i = 0; i < arr1.length; i++) { + for(j = 0; j < arr2.length; j++) { + if(arr1.A[i] == arr2.A[j]) { + arr3.A[k++] = arr1.A[i]; + break; + } + } + } + arr3.length = k; + arr3.size = 100; + return arr3; + } +} + +struct Array Difference(struct Array arr1, struct Array arr2) { + struct Array arr3; + if(sort(arr1) && sort(arr2)) { //if arr1 and arr2 are sorted + int i = 0, j = 0, k = 0; + while(i < arr1.length && j < arr2.length) { + if(arr1.A[i] < arr2.A[j]) + arr3.A[k++] = arr1.A[i++]; + else if(arr2.A[j] < arr1.A[i]) + j++; + else if(arr1.A[i] == arr2.A[j]) { + i++; + j++; + } + } + for(; i < arr1.length; i++) + arr3.A[k++] = arr1.A[i]; + arr3.length = k; + arr3.size = 100; + return arr3; + } + else { + int i = 0, j = 0, k = 0, m = 0; + for(i = 0; i < arr1.length; i++) { + m = 0; + for(j = 0; j < arr2.length; j++) { + if(arr1.A[i] == arr2.A[j]) { + m = 1; + break; + } + } + if(m == 0) + arr3.A[k++] = arr1.A[i]; + } + arr3.length = k; + arr3.size = 100; + return arr3; + } +} + +int main() { + struct Array arr1 = {{2,6,10,15,12,25}, 100, 6}; + struct Array arr2 = {{3,6,7,15,20}, 100, 5}; + struct Array d = Difference(arr1, arr2); + Display(d); + return 0; +} diff --git a/05_Array_ADT/09_menu.c b/05_Array_ADT/09_menu.c new file mode 100644 index 0000000..4fe1d3b --- /dev/null +++ b/05_Array_ADT/09_menu.c @@ -0,0 +1,177 @@ +#include +#include + +struct Array_int { + int * A; + int size; + int length; +}; + +void init_Array_int_default(struct Array_int* self) { + self->size = 10; + self->length = 0; + self->A = (int*)malloc(self->size * sizeof(int)); +} + +void init_Array_int(struct Array_int* self, int size) { + self->size = size; + self->length = 0; + self->A = (int*)malloc(self->size * sizeof(int)); +} + +void insert(struct Array_int* self, int index, int value) { + if(index >= 0 && index <= self->size){ + for(int i = self->length; i > index; i--) { + self->A[i] = self->A[i - 1]; + } + self->A[index] = value; + self->length++; + } +} + +void append(struct Array_int* self, int x) { + if(self->length < self->size) { + self->A[self->length] = x; + self->length++; + } +} + +void display(struct Array_int* self) { + for (int i = 0; i < self->length; i++){ + printf("%d ", self->A[i]); + } +} + +int l_search(struct Array_int* self, int value) { + for(int i = 0; i < self->length; i++) { + if(self->A[i] == value) { + return i; + } + } + return -1; +} + +void deletion(struct Array_int* self, int index) { + if(index >= 0 && index <= self->length) { + for(int i = index; i < self->length - 1; i++) { + self->A[i] = self->A[i + 1]; + } + self->length--; + printf("deleted index %d element in array\n", index); + } +} + +int Sum(struct Array_int* self) { + int sum = 0; + for(int i = 0; i < self->length; i++) + sum = sum + self->A[i]; + return sum; +} + +struct Array_int* Union(struct Array_int* self, struct Array_int arr2) { + struct Array_int* arr3 = (struct Array_int*)malloc(sizeof(struct Array_int)); + init_Array_int(arr3, 100); + int i = 0, j = 0, k = 0, m = 0; + for(i = 0; i < self->length; i++, k++) + arr3->A[k] = self->A[i]; + for(i = 0; i < arr2.length; i++) { + int l = 0, j = 0; + while(j < self->length) { + if(arr2.A[i] == self->A[j]) { + l = 1; + break; + } + j++; + } + if(l == 0) + arr3->A[k++] = arr2.A[i]; + } + arr3->length = k; + return arr3; +} + +void destroy_Array_int(struct Array_int* self) { + free(self->A); +} + +int main() { + int i, sz; + int x; + printf("Enter size of array "); + fflush(stdout); + scanf("%d", &sz); + struct Array_int arr; + init_Array_int(&arr, sz); + int ch; + do { + printf("\nMenu\n"); + printf("1.Append\n"); + printf("2.Insert\n"); + printf("3.Search\n"); + printf("4.Sum\n"); + printf("5.Display\n"); + printf("6.Delete\n"); + printf("7.Exit\n"); + + scanf("%d", &ch); + switch(ch) { + case 1: + printf("Enter element to appended\n"); + fflush(stdout); + scanf("%d", &x); + append(&arr, x); + break; + + case 2: + printf("Enter element to be inserted\n"); + fflush(stdout); + scanf("%d", &x); + printf("Enter position of element\n"); + fflush(stdout); + scanf("%d", &i); + insert(&arr, i, x); + break; + + case 3: + printf("Enter element to be searched\n"); + fflush(stdout); + scanf("%d", &x); + i = l_search(&arr, x); + if(i == -1) + printf("Element not found\n"); + else + printf("Element found at %d position ", i); + break; + + case 4: + printf("Sum of array elements is %d", Sum(&arr)); + break; + + case 5: + display(&arr); + break; + + case 6: + printf("Enter index at which elements will be deleted\n"); + fflush(stdout); + scanf("%d", &i); + deletion(&arr, i); + break; + } + } while(ch < 7); + + struct Array_int a2; + init_Array_int(&a2, 10); + append(&a2, 4); + append(&a2, 5); + append(&a2, 6); + struct Array_int* a3 = Union(&arr, a2); + display(a3); + + destroy_Array_int(&arr); + destroy_Array_int(&a2); + destroy_Array_int(a3); + free(a3); + + return 0; +} diff --git a/05_Array_ADT/10_01_finding_mising_element.c b/05_Array_ADT/10_01_finding_mising_element.c new file mode 100644 index 0000000..d7678ce --- /dev/null +++ b/05_Array_ADT/10_01_finding_mising_element.c @@ -0,0 +1,44 @@ +#include + +struct Array { + int A[100]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +int main() { + struct Array arr = {{1,2,3,4,5,7,8}, 100, 7}; + int sum = 0, s = 0; + for(int i = 0; i < arr.length; i++) //method 1 + sum = sum + arr.A[i]; + + s = arr.A[arr.length - 1] * (arr.A[arr.length - 1] - 1) / 2; + + struct Array arr2 = {{6,7,10,11,13,14,15}, 100, 7}; //Menthod 2 for multiple missing element + int diff = arr2.A[0] - 0; + for(int i = 0; i < arr2.length; i++) { + if(arr2.A[i] - i != diff) { + while(diff < (arr2.A[i] - i)) { + printf("%d\n", diff + i); + diff++; + } + } + } + + struct Array arr3 = {{1,7,5,2,3}, 100, 5}; //Menthod 3 for multiple missing element using hashing + //here l=1 h=7 + int A1[8] = {0}; + for(int i = 0; i < arr3.length; i++) + A1[arr3.A[i]]++; + for(int i = 1; i < 8; i++) { + if(A1[i] == 0) + printf("element %d is missing\n", i); + } + return 0; +} diff --git a/05_Array_ADT/10_02_Duplicate.c b/05_Array_ADT/10_02_Duplicate.c new file mode 100644 index 0000000..e238c3a --- /dev/null +++ b/05_Array_ADT/10_02_Duplicate.c @@ -0,0 +1,88 @@ +#include + +struct Array { + int A[100]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +int main() { + // Array a1={{1,2,3,3,4,4,4,5},100,8};//Method 1 for sorted + // int dup=0; + // for(int i=0;i1) + // cout<1) + // cout< 1) + printf("%d is repeated %d times\n", i, A1[i]); + } + + return 0; +} diff --git a/05_Array_ADT/11_Sum_of_k.c b/05_Array_ADT/11_Sum_of_k.c new file mode 100644 index 0000000..f60d8af --- /dev/null +++ b/05_Array_ADT/11_Sum_of_k.c @@ -0,0 +1,56 @@ +#include + +struct Array { + int A[100]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +int main() { + // Array a1={{2,5,7,8,3,4,9,6},100,8};//here k=10 find pair of numbers in array whose sum is 10 + // int k=10; + // //Method 1 + // for(int i=0;i + +struct Array { + int A[100]; + int size; + int length; +}; + +void Display(struct Array arr) { + for(int i = 0; i < arr.length; i++) { + printf("%d ", arr.A[i]); + } +} + +int main() { + struct Array a1 = {{1,2,3,4,6,8,9,12,14,17}, 100, 10}; + int max = a1.A[0], min = a1.A[0]; + + for(int i = 1; i < a1.length; i++) { + if(a1.A[i] < min) + min = a1.A[i]; + else if(a1.A[i] > max) + max = a1.A[i]; + } + printf("%d %d\n", max, min); + return 0; +} diff --git a/06_string/01_string.c b/06_string/01_string.c new file mode 100644 index 0000000..0a91d2f --- /dev/null +++ b/06_string/01_string.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + char name1[]="Anurag Maurya"; + char name2[20]; + printf("Enter your name "); + gets(name2); + printf("ok %s\n",name2); + printf("%s and %s are friends", name1, name2); + + return 0; +} diff --git a/06_string/02_changing_case.c b/06_string/02_changing_case.c new file mode 100644 index 0000000..7db3923 --- /dev/null +++ b/06_string/02_changing_case.c @@ -0,0 +1,16 @@ +#include + +int main() +{ + char name[]="AnuRAG maURyA"; + for(int i=0;name[i]!='\0';i++) + { + if(name[i]>=65 &&name[i]<=90)//if chararcter is uppercase + name[i]=name[i]+32; + else if(name[i]>=97 && name[i]<=122)//if chararcter is lowercase + name[i]=name[i]-32; + } + printf("%s", name); + + return 0; +} diff --git a/06_string/03_vowels_no_of_words.c b/06_string/03_vowels_no_of_words.c new file mode 100644 index 0000000..95740a1 --- /dev/null +++ b/06_string/03_vowels_no_of_words.c @@ -0,0 +1,28 @@ +#include + +int main() +{ + char sample_word[11]={'T','a','j',' ',' ','m','a','h','a','l','\0'}; + int v_count=0,c_count=0; + for(int i=0;sample_word[i]!='\0';i++) + { + if(sample_word[i]=='a'||sample_word[i]=='e'|| sample_word[i]=='i'|| sample_word[i]=='o' || sample_word[i]=='u' || + sample_word[i]=='A'|| sample_word[i]=='E' || sample_word[i]=='I'|| sample_word[i]=='O' || sample_word[i]=='U') + v_count++;//counting as vowel + + else if((sample_word[i]>=65 && sample_word[i]<=90) || (sample_word[i]>=97 && sample_word[i]<122)) + c_count++;//counting consonent + } + printf("number of vowels = %d\nnumber of consonent = %d\n", v_count, c_count); + + //count words + int space=0; + for(int i=0;sample_word[i]!='\0';i++) + { + if(sample_word[i]==' '&& sample_word[i-1]!=' ')//checking if its a space and not white space + space++;//counting space + } + printf("number of words = %d", space+1); + + return 0; +} diff --git a/06_string/04_valid.c b/06_string/04_valid.c new file mode 100644 index 0000000..3b465e3 --- /dev/null +++ b/06_string/04_valid.c @@ -0,0 +1,18 @@ +#include +#include + +bool valid(char name[]) +{ + for(int i=0;name[i]!='\0';i++) + { + if(!(name[i]>=65 && name[i]<=90) && !(name[i]>=97 && name[i]<=122) && !(name[i]>=48 && name[i]<=57)) + return false; + } + return true; +} +int main() +{ + char name[] ="An3ura835)6HHHg"; + printf("%d", valid(name)); + return 0; +} diff --git a/06_string/05_reverse_string.c b/06_string/05_reverse_string.c new file mode 100644 index 0000000..61d99c9 --- /dev/null +++ b/06_string/05_reverse_string.c @@ -0,0 +1,41 @@ +#include + +int main() +{ + char word[]="Anurag"; + //method 1 + char helper[10]; + int i,j; + for(i=0;word[i]!='\0';i++) + {} + i--; + for(j=0;i>=0;j++,i--) + { + helper[j]=word[i]; + } + helper[j]='\0'; + for(i=0;helper[i]!='\0';i++) + { + word[i]=helper[i]; + } + printf("%s\n", word); + + //Method 2 + char word2[]="Anurag"; + for(j=0;word2[j]!='\0';j++) + {} + j--; + i=0; + while(i + +int main() +{ + //comparing two strings + char A[]="Painter"; + char B[]="painter"; + int i,j; + for(i=0,j=0;A[i]!='\0' && B[j]!='\0';j++,i++)//checking condition if string ends + { + if(A[i]>=65 && A[i]<=90)//checking if its uppercase then converting to lower case + A[i]=A[i]+32; + else if(B[i]>=65 && B[i]<=90)//checking if its uppercase then converting to lower case + B[i]=B[i]+32; + if(A[i]!=B[j])//checking if they are not equal + break; + } + if(A[i]==B[j]) + printf("both are equal\n"); + else if(A[i]>B[j]) + printf("A is greater\n"); + else + printf("B is greater\n"); + + + //checking if Palindrone + + char word[]="madami"; + //method 1 + char helper[10];//helper string/array + for(i=0;word[i]!='\0';i++)//incrementing i value till it reacher the last index + {} + i--; + for(j=0;i>=0;j++,i--)//copying reverse of word in helper + { + helper[j]=word[i]; + } + helper[j]='\0';//assigining last value with null term + for(i=0;helper[i]!='\0';i++) + { + if(word[i]!=helper[i])//if not equal break out of loop + break; + } + if(word[i]=='\0')//checking if it reached last till last null term + printf("String is pallindrone\n"); + else + printf("string is not palindrone\n"); + + //Method 2 + char word2[]="MADoM"; + for(j=0;word2[j]!='\0';j++) + {} + j--; + i=0; + while(i + +int main() +{ + char a4[10]="Hesllsos";//method 1 using for loop + for(int i=0;a4[i]!='\0';i++) + { + if(a4[i]!=-1) + { + int count=1; + for(int j=i+1;a4[j]!='\0';j++) + { + if(a4[i]==a4[j]) + { + a4[j]=-1; + count++; + } + } + if(count>1) + printf("%c is duplicated %d times\n", a4[i], count-1); + } + } + printf("\n\n\n\n"); + + //method 2 using hash table i.e hashing + char word[]="helloehl"; + int a[26]={0}; + for(int i=0;word[i]!='\0';i++) + a[word[i]-97]++;//writing program for lowercase word only + + for(int i=0;i<=25;i++) + { + if(a[i]>1) + { + char k=i+97; + printf("%c is repeated %d times\n", k, a[i]-1); + } + } + + return 0; +} diff --git a/06_string/08_duplicates_with_bitwise_operator.c b/06_string/08_duplicates_with_bitwise_operator.c new file mode 100644 index 0000000..7885f3f --- /dev/null +++ b/06_string/08_duplicates_with_bitwise_operator.c @@ -0,0 +1,17 @@ +#include + +int main() +{ + char word[]="Hellohedppd"; + int a=0;//assigning all bits as zero + for(int i=0;word[i]!='\0';i++) + { + int b=1;//assigning 0 bit as 1 i.e turning is on + b=b<<(word[i]-97);//turning on the bit for that particular char + if(a&b) + printf("Dublicated letter %c\n", word[i]); + else + a=a|b; + } + return 0; +} diff --git a/06_string/09_anagram.c b/06_string/09_anagram.c new file mode 100644 index 0000000..729f9f8 --- /dev/null +++ b/06_string/09_anagram.c @@ -0,0 +1,73 @@ +#include + +int main() +{ + //Anagram using method 1 i.e for loop + char word1[]="elbow"; + char word2[]="below"; + int i,j; + for(i=0;word1[i]!='\0';i++) + { + for(j=0;word2[j]!='\0';j++) + { + if(word1[i]==word2[j]) + break; + } + if(word2[j]=='\0') + break; + } + if(word1[i]=='\0') + printf("Its an anagram \n"); + else + printf("Its not an angram\n"); + + //Anagram using method 2 using hasing + + char word3[]="cat"; + char word4[]="tac"; + + int h[25]={0}; + for(i=0;word3[i]!='\0';i++) + h[word3[i]-97]++; + + for(i=0;word4[i]!='\0';i++) + { if((h[word4[i]-97]-1)<0) + { + printf("Its not a anagram\n"); + break; + } + } + if(word4[i]=='\0') + printf("Its an anagram\n"); + + //method 3 using betwise operator + + char word5[]="tac"; + char word6[]="cat"; + int a=0; + for(i=0;word5[i]!='\0';i++)//assigning word6 character as per bit + { + int b=1;//assigning 0 bit as 1 i.e turning is on + b=b<<(word5[i]-97);//turning on the bit for that particular char + if(a&b) + continue; + else + a=a|b; + } + for(i=0;word6[i]!='\0';i++) + { + int b=1;//assigning 0 bit as 1 i.e turning is on + b=b<<(word6[i]-97);//turning on the bit for that particular char + if(a&b) + continue; + else + { + printf("Its not a anagram\n"); + break; + } + } + if(word6[i]=='\0') + printf("Its an anagram"); + + return 0; +} diff --git a/06_string/10_01_permutation.c b/06_string/10_01_permutation.c new file mode 100644 index 0000000..3261e31 --- /dev/null +++ b/06_string/10_01_permutation.c @@ -0,0 +1,32 @@ +#include + +void perm(char s[],int k) +{ + static int A[10]={0}; + static char Res[10]; + int i; + if(s[k]=='\0') + { + Res[k]='\0'; + printf("%s\n", Res); + } + else + { + for(int i=0;s[i]!='\0';i++) + { + if(A[i]==0) + { + Res[k]=s[i]; + A[i]=1; + perm(s,k+1); + A[i]=0; + } + } + } +} +int main() +{ + char s[]="ABC"; + perm(s,0); + return 0; +} diff --git a/06_string/10_02_permutattion.c b/06_string/10_02_permutattion.c new file mode 100644 index 0000000..90dd804 --- /dev/null +++ b/06_string/10_02_permutattion.c @@ -0,0 +1,31 @@ +#include + +void swap(char *c1,char *c2) +{ + char t=*c1; + *c1=*c2; + *c2=t; +} +void perm(char s[],int l,int h) +{ + if(l==h) + { + printf("%s\n", s); + } + else + { + for(int i=l;i<=h;i++) + { + swap(&s[i],&s[l]); + perm(s,l+1,h); + swap(&s[i],&s[l]); + } + } +} +int main() +{ + char s[]="ABC"; + perm(s,0,2); + + return 0; +} diff --git a/07_Matrix/02_Diagonal_matrix_with_c++_class.c b/07_Matrix/02_Diagonal_matrix_with_c++_class.c new file mode 100644 index 0000000..f3dcfba --- /dev/null +++ b/07_Matrix/02_Diagonal_matrix_with_c++_class.c @@ -0,0 +1,64 @@ +#include +#include + +typedef struct { + int n; + int *A; +} Matrix; + +void init_Matrix(Matrix* self, int n) +{ + self->n=n; + self->A=(int*)malloc(n * sizeof(int)); +} + +void destroy_Matrix(Matrix* self) +{ + free(self->A); +} + +void Matrix_set(Matrix* self, int i,int j,int x) +{ + if(i==j) + { + self->A[i-1]=x; + } +} + +int Matrix_get(Matrix* self, int i,int j) +{ + if(i==j) + return self->A[i-1]; + else + return 0; +} + +void Matrix_Display(Matrix* self) +{ + for(int i=0;i<(self->n);i++) + { + for(int j=0;j<(self->n);j++) + { + if(i==j) + printf("%d ", self->A[i]); + else + printf("0 "); + } + printf("\n"); + } +} + +int main() +{ + Matrix m; + init_Matrix(&m, 3); + Matrix_set(&m, 1,1,4); + Matrix_set(&m, 2,2,5); + Matrix_set(&m, 3,3,6); + Matrix_Display(&m); + printf("%d\n", Matrix_get(&m, 3,3)); + + destroy_Matrix(&m); + + return 0; +} diff --git a/07_Matrix/04_c++_lower_triangular_matrix.c b/07_Matrix/04_c++_lower_triangular_matrix.c new file mode 100644 index 0000000..7471cf3 --- /dev/null +++ b/07_Matrix/04_c++_lower_triangular_matrix.c @@ -0,0 +1,150 @@ +#include +#include + +typedef struct { + int n; + int *A; +} rMatrix; + +void init_rMatrix(rMatrix* self, int n) +{ + self->n=n; + self->A=(int*)malloc(n * sizeof(int)); // In original size was n, but it should technically be n*(n+1)/2. The original code says `new int[n]`. I'll copy the original logic. +} + +void destroy_rMatrix(rMatrix* self) +{ + free(self->A); +} + +void rMatrix_set(rMatrix* self, int i,int j,int x) +{ + if(i>=j) + { + int t=((i*(i-1)/2)+(j-1)); + self->A[t]=x; + } +} +int rMatrix_get(rMatrix* self, int i,int j) +{ + if(i>=j) + { + int t=((i*(i-1)/2)+(j-1)); + return self->A[t]; + } + else + return 0; +} +void rMatrix_Display(rMatrix* self) +{ + for(int i=1;i<=(self->n);i++)//here i is considered as matrix index so is started from 1 + { + for(int j=1;j<=(self->n);j++) + { + if(i>=j) + { + int t=((i*(i-1)/2)+(j-1)); + printf("%d ",self->A[t]); + } + else + printf("%d ",0); + } + printf("\n"); + } +} + +typedef struct { + int n; + int *A; +} cMatrix; + +void init_cMatrix(cMatrix* self, int n) +{ + self->n=n; + self->A=(int*)malloc(n * sizeof(int)); // Again, `new int[n]` in original. +} + +void destroy_cMatrix(cMatrix* self) +{ + free(self->A); +} + +void cMatrix_cset(cMatrix* self, int i,int j,int x) +{ + if(i>=j) + { + if(i>=j) + { + self->A[self->n*(j-1)-(j-1)*(j-2)/2+(i-j)]=x; + } + + } +} +int cMatrix_cget(cMatrix* self, int i,int j) +{ + if(i>=j) + { + return self->A[self->n*(j-1)-(j-1)*(j-2)/2+(i-j)]; + } + else + return 0; +} +void cMatrix_cDisplay(cMatrix* self) +{ + for(int i=1;i<=self->n;i++)//here i is considered as matrix index so is started from 1 + { + for(int j=1;j<=self->n;j++) + { + if(i>=j) + { + printf("%d ",self->A[self->n*(j-1)-(j-1)*(j-2)/2+(i-j)]); + } + else + printf("%d ",0); + } + printf("\n"); + } +} +int main() +{ + int num,x; + //using row major formula + + printf("Enter number of elements "); + scanf("%d",&num); + rMatrix m; + init_rMatrix(&m, num); + printf("Enter the elements "); + for(int i=1;i<=num;i++) + { + for(int j=1;j<=num;j++) + { + scanf("%d",&x); + rMatrix_set(&m, i,j,x); + } + } + + rMatrix_Display(&m); + printf("\n\n"); + int num2; + //using column major formula + printf("Enter number of elements "); + scanf("%d",&num2); + cMatrix m2; + init_cMatrix(&m2, num2); + printf("Enter the elements "); + for(int i=1;i<=num2;i++) + { + for(int j=1;j<=num2;j++) + { + scanf("%d",&x); + cMatrix_cset(&m2, i,j,x); + } + } + cMatrix_cDisplay(&m2); + + destroy_rMatrix(&m); + destroy_cMatrix(&m2); + + return 0; +} diff --git a/08_sparse_matrix/03_sparse_matrix_using_c++.c b/08_sparse_matrix/03_sparse_matrix_using_c++.c new file mode 100644 index 0000000..e4c1ecf --- /dev/null +++ b/08_sparse_matrix/03_sparse_matrix_using_c++.c @@ -0,0 +1,124 @@ +#include +#include + +typedef struct { + int i; + int j; + int x; +} Element; + +typedef struct { + int m; + int n; + int num; + Element *elm; +} Sparse; + +void init_Sparse(Sparse* self, int m,int n,int num) +{ + self->m=m; + self->n=n; + self->num=num; + self->elm=(Element*)malloc(self->num * sizeof(Element)); +} + +void destroy_Sparse(Sparse* self) +{ + free(self->elm); +} + +Sparse Sparse_add(Sparse* self, Sparse *s) +{ + int i=0,j=0,k=0;//i for referrring elements of array of struct Element pointed by elm in sparse structure pointed by s1 + // j for s2 and k for sum + Sparse sum; + init_Sparse(&sum, self->m, self->n, self->num + s->num); + while (inum && jnum) + { + if(self->elm[i].ielm[j].i)// row of s1 is less than s2 + sum.elm[k++]=self->elm[i++]; + + else if(self->elm[i].i>s->elm[j].i)//row of s2 is less than s1 + sum.elm[k++]=s->elm[j++]; + + else + { + if(self->elm[i].jelm[j].j)//row of s1 and s2 is same but column of s1 is less than s2 + sum.elm[k++]=self->elm[i++]; + + else if(self->elm[i].j>s->elm[j].j)//row of s1 and s2 is same but column of s2 is less than s1 + sum.elm[k++]=s->elm[j++]; + + else//row and column both are same + { + sum.elm[k]=self->elm[i];//copy comple s1 struct elm elements + sum.elm[k++].x=s->elm[j++].x+self->elm[i++].x;//add s1 and s2 values + } + } + + } + //remaining elements + for(;inum;i++) + sum.elm[k++]=self->elm[i]; + + for(;jnum;j++) + sum.elm[k++]=s->elm[j]; + + sum.num=k; + return sum; +} + +void Sparse_read(Sparse* self) +{ + printf("Enter non-zero element\n"); + int i; + for(i=0;inum;i++) + { + scanf("%d %d %d", &self->elm[i].i, &self->elm[i].j, &self->elm[i].x); + } +} + +void Sparse_print(Sparse* self) +{ + int i,j; + int a; + a=0; + printf("\n"); + for(i=0;im;i++) + { + for(j=0;jn;j++) + { + if(i==self->elm[a].i && j==self->elm[a].j) + { + printf("%d ", self->elm[a++].x); + } + else{ + printf("0 "); + } + } + printf("\n"); + } + printf("---------------------------------------------------------------\n"); +} + +int main() +{ + Sparse s1; + init_Sparse(&s1, 3, 3, 3); + Sparse_read(&s1); + Sparse_print(&s1); + + Sparse s2; + init_Sparse(&s2, 3, 3, 3); + Sparse_read(&s2); + Sparse_print(&s2); + + Sparse s3 = Sparse_add(&s1, &s2); + Sparse_print(&s3); + + destroy_Sparse(&s1); + destroy_Sparse(&s2); + destroy_Sparse(&s3); + + return 0; +} diff --git a/08_sparse_matrix/04_polynomial_representation.c b/08_sparse_matrix/04_polynomial_representation.c new file mode 100644 index 0000000..e8e93d8 --- /dev/null +++ b/08_sparse_matrix/04_polynomial_representation.c @@ -0,0 +1,78 @@ +#include +#include + +struct Term +{ + int coeff; + int expo; +}; +struct polynomial +{ + int n; + struct Term *t; +}; +void create(struct polynomial * p) +{ + printf("Enter number of elements "); + scanf("%d", &p->n); + printf("\n"); + p->t = (struct Term *)malloc(p->n * sizeof(struct Term)); + for(int i=0;in;i++) + { + printf("Enter %dth term coeff ", i); + scanf("%d", &p->t[i].coeff); + printf("Enter %dth term expo ", i); + scanf("%d", &p->t[i].expo); + printf("\n"); + } +} +void display(struct polynomial *p) +{ + for(int i=0;in;i++) + { + printf("%dx%d", p->t[i].coeff, p->t[i].expo); + if(i!=(p->n)-1) + printf(" + "); + } + printf("\n"); +} +struct polynomial addition(struct polynomial *p1,struct polynomial *p2) +{ + int i=0,j=0,k=0; + struct polynomial sum; + sum.t = (struct Term *)malloc((p1->n + p2->n) * sizeof(struct Term)); + while(in && jn) + { + if(p1->t[i].expo>p2->t[j].expo) + sum.t[k++]=p1->t[i++]; + else if(p2->t[j].expo>p1->t[i].expo) + sum.t[k++]=p2->t[j++]; + else + { + sum.t[k].expo=p1->t[i].expo; + sum.t[k++].coeff=p1->t[i++].coeff+p2->t[j++].coeff; + } + } + for(;in;i++) + sum.t[k++]=p1->t[i]; + for(;jn;j++) + sum.t[k++]=p2->t[j]; + sum.n=k; + return sum; +} +int main() +{ + struct polynomial p1,p2,sum; + create(&p1); + display(&p1); + create(&p2); + display(&p2); + sum=addition(&p1,&p2); + display(&sum); + + free(p1.t); + free(p2.t); + free(sum.t); + + return 0; +} diff --git a/09_Linked_list/01_display_linked_list.c b/09_Linked_list/01_display_linked_list.c new file mode 100644 index 0000000..3492945 --- /dev/null +++ b/09_Linked_list/01_display_linked_list.c @@ -0,0 +1,47 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t;//putting address of current node in the next of last node + last=t;//assigning current node as last node + } +} + +void Display() +{ + struct Node *p; + p=first; + while(p!=NULL) + { + printf("%d\n", p->data); + p=p->next; + } +} + +int main() +{ + int A[]={4,5,8,9,7}; + create(A,5); + Display(); + + return 0; +} diff --git a/09_Linked_list/02_recursively_display_linked_list.c b/09_Linked_list/02_recursively_display_linked_list.c new file mode 100644 index 0000000..afbb372 --- /dev/null +++ b/09_Linked_list/02_recursively_display_linked_list.c @@ -0,0 +1,55 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t; //putting address of current node in the next of last node + last=t; //assigning current node as last node + } +} + +void Display(struct Node *p) //display using recurssion +{ + if(p!=NULL) + { + printf("%d\n", p->data); + Display(p->next); + } +} + +void RDisplay(struct Node *p)// reverse display using recurssion +{ + if(p!=NULL) + { + RDisplay(p->next); + printf("%d\n", p->data); + } + +} + +int main() +{ + int A[]={4,5,8,9,7}; + create(A,5); + RDisplay(first); + + return 0; +} diff --git a/09_Linked_list/03_count_and_sum.c b/09_Linked_list/03_count_and_sum.c new file mode 100644 index 0000000..6033a4d --- /dev/null +++ b/09_Linked_list/03_count_and_sum.c @@ -0,0 +1,78 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t; //putting address of current node in the next of last node + last=t; //assigning current node as last node + } +} + +void count()//counting of nodes +{ + int c=0; + struct Node *p; + p=first; + while(p) + { + c++; + p=p->next; + } + printf(" Count is %d\n", c); +} + +int Rcount(struct Node *p)//recursive counting of nodes +{ + if(p) + return Rcount(p->next)+1; + return 0; +} + +void sum()// sum of data of nodes +{ + int s=0; + struct Node *p; + p=first; + while(p) + { + s=s+p->data; + p=p->next; + } + printf(" sum is %d\n", s); +} + +int Rsum(struct Node *p)// recursive sum of data of nodes +{ + if(p) + return p->data+Rsum(p->next); + return 0; +} + +int main() +{ + int A[]={4,5,8,9,7,1,9,4,7}; + create(A,9); + count(); + printf("count is : %d\n", Rcount(first)); + sum(); + printf("sum is : %d\n", Rsum(first)); + return 0; +} diff --git a/09_Linked_list/04_Max_Linked_list.c b/09_Linked_list/04_Max_Linked_list.c new file mode 100644 index 0000000..e01539b --- /dev/null +++ b/09_Linked_list/04_Max_Linked_list.c @@ -0,0 +1,67 @@ +#include +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t; //putting address of current node in the next of last node + last=t; //assigning current node as last node + } +} + +int Max(struct Node * p) +{ + int max=-2147483648; // INT32_MIN equivalent + while (p!=NULL) + { + if(maxdata) + max=p->data; + p=p->next; + } + return max; + +} + +int RMax(struct Node * p) +{ + int x=0; + if(p==0) + return -2147483648; + else + { + x=RMax(p->next); + if(x>p->data) + return x; + else + return p->data; + } + + +} + +int main() +{ + int A[]={4,5,78,8,94,7222}; + create(A,6); + printf("%d\n", RMax(first)); + + return 0; +} diff --git a/09_Linked_list/05_linear_search.c b/09_Linked_list/05_linear_search.c new file mode 100644 index 0000000..38c6b14 --- /dev/null +++ b/09_Linked_list/05_linear_search.c @@ -0,0 +1,100 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t;//putting address of current node in the next of last node + last=t;//assigning current node as last node + } +} + +void Display() +{ + struct Node *p; + p=first; + while(p!=NULL) + { + printf("%d\n", p->data); + p=p->next; + } +} + +struct Node *Linear_Search(struct Node *p,int key) +{ + while(p!=NULL) + { + if(p->data == key) + return p; + p=p->next; + + } + return NULL; +} + +struct Node *RLinear_Search(struct Node *p,int key) +{ + if(p==NULL) + return NULL; + else if(p->data == key) + return p; + else + return RLinear_Search(p->next,key); + +} + +struct Node *ILinear_Search(struct Node *p,int key) +{ + struct Node *q = NULL; + while(p!=NULL) + { + if(p->data == key) + { + if(p==first)// very important as in case if the first element is same to key there + // wont be any value to q + return p; + else + { + q->next=p->next; + p->next=first; + first=p; + } + return p; + } + q=p; + p=p->next; + } + return NULL; +} + + +int main() +{ + int A[]={4,5,8,9,7}; + create(A,5); + struct Node *temp; + temp=ILinear_Search(first,8); + if(temp) + printf("Key founded %d\n", temp->data); + else + printf("Key not found\n"); + Display(); + return 0; +} diff --git a/09_Linked_list/06_Inserting_in_Linked_list.c b/09_Linked_list/06_Inserting_in_Linked_list.c new file mode 100644 index 0000000..f3c8c3e --- /dev/null +++ b/09_Linked_list/06_Inserting_in_Linked_list.c @@ -0,0 +1,94 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +int count(struct Node *p)//counting of nodes +{ + int c=0; + + p=first; + while(p) + { + c++; + p=p->next; + } + return c; +} + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t;//putting address of current node in the next of last node + last=t;//assigning current node as last node + } +} + +void Display() +{ + struct Node *p; + p=first; + while(p!=NULL) + { + printf("%d\n", p->data); + p=p->next; + } +} + + +void insert(int pos ,int value) +{ + int i; + struct Node * p; + struct Node *t; + if(pos<0 || pos>count(first)) + return ; + + p=first; + t=(struct Node*)malloc(sizeof(struct Node)); + if(pos==0) + { + t->data=value; + t->next=first; + first=t; + } + else if(pos>0) + { + for(i=0;inext; + } + t->data=value; + t->next=p->next; + p->next=t; + } +} +int main() +{ + int A[]={4,5,8,9,7}; + create(A,5); + printf(" Before insert\n"); + Display(); + insert(2,78); + + printf(" After insert\n"); + Display(); + + return 0; +} diff --git a/09_Linked_list/07_Inserting_in_sorted_linked_list.c b/09_Linked_list/07_Inserting_in_sorted_linked_list.c new file mode 100644 index 0000000..974d764 --- /dev/null +++ b/09_Linked_list/07_Inserting_in_sorted_linked_list.c @@ -0,0 +1,79 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t;//putting address of current node in the next of last node + last=t;//assigning current node as last node + } +} + +void Display() +{ + struct Node *p; + p=first; + while(p!=NULL) + { + printf("%d\n", p->data); + p=p->next; + } +} + +void S_Insert(struct Node *p,int value) +{ + struct Node *q = NULL; + struct Node *t; + t=(struct Node*)malloc(sizeof(struct Node)); + t->data=value; + t->next=NULL; + + if(first==NULL) + first=t; + else + { + while (p && p->datanext; + } + if(p==first) + { + t->next=first; + first=t; + } + else{ + q->next=t; + t->next=p; + } + + } + +} + +int main() +{ + int A[]={4,7,9,12,18}; + create(A,5); + S_Insert(first,8); + Display(); + + return 0; +} diff --git a/09_Linked_list/08_Deleting_in_linked_list.c b/09_Linked_list/08_Deleting_in_linked_list.c new file mode 100644 index 0000000..8556515 --- /dev/null +++ b/09_Linked_list/08_Deleting_in_linked_list.c @@ -0,0 +1,87 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t;//putting address of current node in the next of last node + last=t;//assigning current node as last node + } +} + +int count(struct Node *p)//counting of nodes +{ + int c=0; + + p=first; + while(p) + { + c++; + p=p->next; + } + return c; +} + +void Display() +{ + struct Node *p; + p=first; + while(p!=NULL) + { + printf("%d\n", p->data); + p=p->next; + } +} + +int Delete(struct Node *p,int index) +{ + struct Node *q=NULL; + int i; + int x=-1;//to store data of deleted node + if(index<0 || index>count(p)) + return -1; + if(index==1) + { + x=first->data; + first=first->next; + } + else + { + for(i=0;inext; + } + x=p->data; + q->next=p->next; + } + + free(p); + return x; +} +int main() +{ + int A[]={4,7,9,12,18}; + create(A,5); + printf("Deleted : %d\n", Delete(first,3)); + Display(); + + return 0; +} diff --git a/09_Linked_list/09_Sorted_checking.c b/09_Linked_list/09_Sorted_checking.c new file mode 100644 index 0000000..68bad32 --- /dev/null +++ b/09_Linked_list/09_Sorted_checking.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t;//putting address of current node in the next of last node + last=t;//assigning current node as last node + } +} + +void Display() +{ + struct Node *p; + p=first; + while(p!=NULL) + { + printf("%d\n", p->data); + p=p->next; + } +} + +bool isSorted(struct Node *p) +{ + int n=-2147483648; // INT32_MIN equivalent + while(p!=NULL) + { + if(p->datadata; + p=p->next; + } + return true; +} + +int main() +{ + int A[]={4,7,19,12,18}; + create(A,5); + printf("Sorted : %d\n", isSorted(first)); + + + return 0; +} diff --git a/09_Linked_list/10_REmove_duplicate_data.c b/09_Linked_list/10_REmove_duplicate_data.c new file mode 100644 index 0000000..a4c4b4d --- /dev/null +++ b/09_Linked_list/10_REmove_duplicate_data.c @@ -0,0 +1,73 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t;//putting address of current node in the next of last node + last=t;//assigning current node as last node + } +} + +void Display() +{ + struct Node *p; + p=first; + while(p!=NULL) + { + printf("%d\n", p->data); + p=p->next; + } +} + +void Remove_Duplicate(struct Node *p) +{ + struct Node *q; + // q=new Node; // removed memory leak from original + q=p->next; + while(q!=NULL) + { + if(p->data!=q->data) + { + p=q; + q=q->next; + } + else + { + p->next=q->next; + free(q); + q=p->next; + } + } + +} + +int main() +{ + int A[]={4,7,7,7,12,12,15,18}; + create(A,8); + printf("before\n"); + Display(); + printf("After\n"); + Remove_Duplicate(first); + Display(); + + return 0; +} diff --git a/09_Linked_list/11_Reverse_Linked_list.c b/09_Linked_list/11_Reverse_Linked_list.c new file mode 100644 index 0000000..0601ba2 --- /dev/null +++ b/09_Linked_list/11_Reverse_Linked_list.c @@ -0,0 +1,117 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t;//putting address of current node in the next of last node + last=t;//assigning current node as last node + } +} + +void Display() +{ + struct Node *p; + p=first; + while(p!=NULL) + { + printf("%d\n", p->data); + p=p->next; + } +} + +int count(struct Node *p)//counting of nodes +{ + int c=0; + + p=first; + while(p) + { + c++; + p=p->next; + } + return c; +} + +void reverse1(struct Node *p)//Reverse using (Auxiliary)array +{ + int *A,i=0; + struct Node *q; + A=(int*)malloc(count(p)*sizeof(int)); + q=p; + while(q!=NULL) + { + A[i]=q->data; + q=q->next; + i++; + } + q=p; + i--; + while(q!=NULL) + { + q->data=A[i]; + q=q->next; + i--; + } + free(A); +} + +void reverse2(struct Node *p)//reversing by reversing the links +{ + struct Node *q=NULL,*r=NULL; + while(p!=NULL) + { + r=q; + q=p; + p=p->next; + q->next=r; + } + first=q;//make first the q as links are reveresed at q is at the last node after loop +} + +void reverse3(struct Node *q,struct Node *p) +{ + if(p!=NULL) + { + reverse3(p,p->next); + p->next=q; + } + else + { + first=q; + } +} + + + +int main() +{ + int A[]={4,7,19,12,15,18}; + create(A,6); + printf("Before\n"); + Display(); + reverse3(NULL,first); + printf("After\n"); + Display(); + + + + return 0; +} diff --git a/09_Linked_list/12_Concatenate_and_merge_linked_list.c b/09_Linked_list/12_Concatenate_and_merge_linked_list.c new file mode 100644 index 0000000..f445d10 --- /dev/null +++ b/09_Linked_list/12_Concatenate_and_merge_linked_list.c @@ -0,0 +1,119 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL,*second=NULL,*third=NULL; + +void create1(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t;//putting address of current node in the next of last node + last=t;//assigning current node as last node + } +} +void create2(int A[],int n) +{ + int i; + struct Node *last, *t; + second=(struct Node*)malloc(sizeof(struct Node)); + second->data=A[0]; + second->next=NULL; + last=second;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t;//putting address of current node in the next of last node + last=t;//assigning current node as last node + } +} + +void Display(struct Node *p) +{ + + while(p!=NULL) + { + printf("%d\n", p->data); + p=p->next; + } +} + + +void Concatenate(struct Node *p,struct Node *q) +{ + third=p; + while(p->next) + p=p->next; + p->next=q; +} + +void Merge(struct Node *p,struct Node *q) +{ + struct Node *last; + if(p->data< q->data) + { + third=last=p; + p=p->next; + third->next=NULL; + } + else{ + third=last=q; + q=q->next; + third->next=NULL; + } + while (p && q) + { + if(p->data < q->data) + { + last->next=p; + last=p; + p=p->next; + last->next=NULL; + } + else + { + last->next=q; + last=q; + q=q->next; + last->next=NULL; + } + } + if(p) + last->next=p; + else + last->next=q; + +} +int main() +{ + int A[]={4,7,17,21,25,30}; + int B[]={5,15,19,23,27,34,37}; + create1(A,6); + printf("First\n"); + Display(first); + create2(B,7); + printf("second \n"); + Display(second); + printf("third\n"); + Merge(first,second); + Display(third); + + + return 0; +} diff --git a/09_Linked_list/13_isloop.c b/09_Linked_list/13_isloop.c new file mode 100644 index 0000000..e32f49d --- /dev/null +++ b/09_Linked_list/13_isloop.c @@ -0,0 +1,71 @@ +#include +#include +#include + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *last, *t; + first=(struct Node*)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first;//addreess is passed and now first is refferd as last + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t;//putting address of current node in the next of last node + last=t;//assigning current node as last node + } +} + +void Display() +{ + struct Node *p; + p=first; + while(p!=NULL) + { + printf("%d\n", p->data); + p=p->next; + } +} +bool isloop(struct Node *f) +{ + struct Node *p,*q; + p=q=f; + do + { + p=p->next; + q=q->next; + q=q->next!=NULL?q->next:NULL; + } while (p && q && p!=q); + + return p==q?true:false; +} + +int main() +{ + struct Node *p,*q; + int A[]={4,7,19,12,15,18}; + create(A,6); + p=first->next->next; + q=first->next->next->next->next->next; + q->next=p; + if(isloop(first)) + { + printf("there\'s a loop"); + } + else + printf("Theres not a loop"); + + + return 0; +} diff --git a/09_Linked_list/14_Linked_list_in_class.c b/09_Linked_list/14_Linked_list_in_class.c new file mode 100644 index 0000000..72fa756 --- /dev/null +++ b/09_Linked_list/14_Linked_list_in_class.c @@ -0,0 +1,114 @@ +#include +#include + +struct Node { + int data; + struct Node *next; +}; + +typedef struct { + struct Node *first; +} LinkedList; + +void init_LinkedList(LinkedList *self) { + self->first = NULL; +} + +void init_LinkedList_Array(LinkedList *self, int A[], int n) { + int i; + struct Node *last, *t; + self->first = (struct Node*)malloc(sizeof(struct Node)); + self->first->data = A[0]; + self->first->next = NULL; + last = self->first; + for(i = 1; i < n; i++) { + t = (struct Node*)malloc(sizeof(struct Node)); + t->data = A[i]; + t->next = NULL; + last->next = t; + last = t; + } +} + +int LinkedList_length(LinkedList *self) { + int c = 0; + struct Node *p = self->first; + while(p) { + c++; + p = p->next; + } + return c; +} + +void LinkedList_display(LinkedList *self) { + struct Node *p = self->first; + while(p != NULL) { + printf("%d\n", p->data); + p = p->next; + } +} + +void LinkedList_insert(LinkedList *self, int pos, int value) { + if(pos < 0 || pos > LinkedList_length(self)) return; + struct Node *p = self->first; + struct Node *t = (struct Node*)malloc(sizeof(struct Node)); + if(pos == 0) { + t->data = value; + t->next = self->first; + self->first = t; + } else if (pos > 0) { + for(int i = 0; i < pos - 1 && p; i++) { + p = p->next; + } + t->data = value; + t->next = p->next; + p->next = t; + } +} + +int LinkedList_del(LinkedList *self, int index) { + struct Node *q = NULL, *p = self->first; + int x = -1; + if(index < 0 || index > LinkedList_length(self)) return -1; + if(index == 0) { + x = self->first->data; + self->first = self->first->next; + } else { + for(int i = 0; i < index; i++) { + q = p; + p = p->next; + } + x = p->data; + q->next = p->next; + } + free(p); + return x; +} + +void destroy_LinkedList(LinkedList *self) { + struct Node *p = self->first; + while(self->first) { + self->first = self->first->next; + free(p); + p = self->first; + } +} + +int main() { + int A[] = {1, 6, 8, 12, 56}; + LinkedList l; + init_LinkedList_Array(&l, A, 5); + + LinkedList_insert(&l, 2, 67); + + LinkedList_display(&l); + + printf("lenght %d\n", LinkedList_length(&l)); + + LinkedList_del(&l, 1); + printf("After deletion\n"); + LinkedList_display(&l); + + destroy_LinkedList(&l); + return 0; +} diff --git a/09_Linked_list/15_circular_LinkedList.c b/09_Linked_list/15_circular_LinkedList.c new file mode 100644 index 0000000..e1545fe --- /dev/null +++ b/09_Linked_list/15_circular_LinkedList.c @@ -0,0 +1,61 @@ +#include +#include + +struct Node { + int data; + struct Node *next; +}; + +typedef struct { + struct Node *Head; +} CircularLinkedList; + +void init_CircularLinkedList_Array(CircularLinkedList *self, int A[], int n) { + struct Node *last, *t; + int i; + self->Head = (struct Node*)malloc(sizeof(struct Node)); + self->Head->data = A[0]; + self->Head->next = self->Head; + last = self->Head; + for(i = 1; i < n; i++) { + t = (struct Node*)malloc(sizeof(struct Node)); + t->data = A[i]; + t->next = last->next; + last->next = t; + last = t; + } +} + +struct Node* CircularLinkedList_getHead(CircularLinkedList *self) { + return self->Head; +} + +void CircularLinkedList_Rdisplay(CircularLinkedList *self, struct Node *p) { + static int flag = 0; + if(p != self->Head || flag == 0) { + flag = 1; + printf("%d->", p->data); + CircularLinkedList_Rdisplay(self, p->next); + } + flag = 0; +} + +void CircularLinkedList_display(CircularLinkedList *self) { + struct Node *p = self->Head; + do { + printf("%d->", p->data); + p = p->next; + } while(p != self->Head); + printf("\n"); +} + +int main() { + int A[] = {1, 3, 5, 8, 9}; + + CircularLinkedList l; + init_CircularLinkedList_Array(&l, A, 5); + CircularLinkedList_display(&l); + struct Node *h = CircularLinkedList_getHead(&l); + CircularLinkedList_Rdisplay(&l, h); + return 0; +} diff --git a/09_Linked_list/16_Insert_circular_linked_lidt.c b/09_Linked_list/16_Insert_circular_linked_lidt.c new file mode 100644 index 0000000..ebf0c9f --- /dev/null +++ b/09_Linked_list/16_Insert_circular_linked_lidt.c @@ -0,0 +1,103 @@ +#include +#include + +struct Node { + int data; + struct Node *next; +}; + +typedef struct { + struct Node *Head; +} CircularLinkedList; + +void init_CircularLinkedList(CircularLinkedList *self) { + self->Head = NULL; +} + +void init_CircularLinkedList_Array(CircularLinkedList *self, int A[], int n) { + struct Node *last, *t; + int i; + self->Head = (struct Node*)malloc(sizeof(struct Node)); + self->Head->data = A[0]; + self->Head->next = self->Head; + last = self->Head; + for(i = 1; i < n; i++) { + t = (struct Node*)malloc(sizeof(struct Node)); + t->data = A[i]; + t->next = last->next; + last->next = t; + last = t; + } +} + +struct Node* CircularLinkedList_getHead(CircularLinkedList *self) { + return self->Head; +} + +int CircularLinkedList_length(CircularLinkedList *self) { + struct Node *p = self->Head; + int l = 0; + if(self->Head == NULL) return 0; + do { + l++; + p = p->next; + } while(p != self->Head); + return l; +} + +void CircularLinkedList_insert(CircularLinkedList *self, int index, int value) { + if(index < 0 || index > CircularLinkedList_length(self)) + return; + struct Node *p = self->Head, *t; + int i; + t = (struct Node*)malloc(sizeof(struct Node)); + t->data = value; + if(index == 0) { + if(self->Head == NULL) { + t->next = t; + self->Head = t; + } else { + while(p->next != self->Head) + p = p->next; + t->next = p->next; + p->next = t; + } + } else { + for(i = 0; i < index - 1; i++) + p = p->next; + t->next = p->next; + p->next = t; + } +} + +void CircularLinkedList_Rdisplay(CircularLinkedList *self, struct Node *p) { + static int flag = 0; + if(p != self->Head || flag == 0) { + flag = 1; + printf("%d->", p->data); + CircularLinkedList_Rdisplay(self, p->next); + } + flag = 0; +} + +void CircularLinkedList_display(CircularLinkedList *self) { + struct Node *p = self->Head; + if(self->Head == NULL) return; + do { + printf("%d->", p->data); + p = p->next; + } while(p != self->Head); + printf("\n"); +} + +int main() { + int A[] = {1, 3, 5, 8, 9}; + CircularLinkedList l; + init_CircularLinkedList_Array(&l, A, 5); + printf("Before :"); + CircularLinkedList_display(&l); + CircularLinkedList_insert(&l, 1, 2); + printf("After : "); + CircularLinkedList_display(&l); + return 0; +} diff --git a/09_Linked_list/17_deleting_from_circular_linked_list.c b/09_Linked_list/17_deleting_from_circular_linked_list.c new file mode 100644 index 0000000..bcced32 --- /dev/null +++ b/09_Linked_list/17_deleting_from_circular_linked_list.c @@ -0,0 +1,133 @@ +#include +#include + +struct Node { + int data; + struct Node *next; +}; + +typedef struct { + struct Node *Head; +} CircularLinkedList; + +void init_CircularLinkedList(CircularLinkedList *self) { + self->Head = NULL; +} + +void init_CircularLinkedList_Array(CircularLinkedList *self, int A[], int n) { + struct Node *last, *t; + int i; + self->Head = (struct Node*)malloc(sizeof(struct Node)); + self->Head->data = A[0]; + self->Head->next = self->Head; + last = self->Head; + for(i = 1; i < n; i++) { + t = (struct Node*)malloc(sizeof(struct Node)); + t->data = A[i]; + t->next = last->next; + last->next = t; + last = t; + } +} + +struct Node* CircularLinkedList_getHead(CircularLinkedList *self) { + return self->Head; +} + +int CircularLinkedList_length(CircularLinkedList *self) { + struct Node *p = self->Head; + int l = 0; + if(self->Head == NULL) return 0; + do { + l++; + p = p->next; + } while(p != self->Head); + return l; +} + +void CircularLinkedList_insert(CircularLinkedList *self, int index, int value) { + if(index < 0 || index > CircularLinkedList_length(self)) + return; + struct Node *p = self->Head, *t; + int i; + t = (struct Node*)malloc(sizeof(struct Node)); + t->data = value; + if(index == 0) { + if(self->Head == NULL) { + t->next = t; + self->Head = t; + } else { + while(p->next != self->Head) + p = p->next; + t->next = p->next; + p->next = t; + } + } else { + for(i = 0; i < index - 1; i++) + p = p->next; + t->next = p->next; + p->next = t; + } +} + +int CircularLinkedList_Delete(CircularLinkedList *self, int index) { + if(index < 0 || index > CircularLinkedList_length(self)) + return -1; + struct Node *p = self->Head, *q; + int x, i; + if(index == 1) { + while(p->next != self->Head) + p = p->next; + if(p == self->Head) { + x = p->data; + free(p); + self->Head = NULL; + return x; + } + x = self->Head->data; + p->next = self->Head->next; + free(self->Head); + self->Head = p->next; + return x; + } else { + for(i = 0; i < index - 2; i++) + p = p->next; + q = p->next; + x = q->data; + p->next = q->next; + free(q); + return x; + } +} + +void CircularLinkedList_Rdisplay(CircularLinkedList *self, struct Node *p) { + static int flag = 0; + if(p != self->Head || flag == 0) { + flag = 1; + printf("%d->", p->data); + CircularLinkedList_Rdisplay(self, p->next); + } + flag = 0; +} + +void CircularLinkedList_display(CircularLinkedList *self) { + struct Node *p = self->Head; + if(self->Head == NULL) return; + do { + printf("%d->", p->data); + p = p->next; + } while(p != self->Head); + printf("\n"); +} + +int main() { + int A[] = {1, 3, 5, 8, 9}; + CircularLinkedList l; + init_CircularLinkedList_Array(&l, A, 5); + printf("Before :"); + CircularLinkedList_display(&l); + printf("Deleted : %d\n", CircularLinkedList_Delete(&l, 1)); + printf("After : "); + CircularLinkedList_display(&l); + return 0; +} diff --git a/09_Linked_list/18_doubly_LL.c b/09_Linked_list/18_doubly_LL.c new file mode 100644 index 0000000..9e92b80 --- /dev/null +++ b/09_Linked_list/18_doubly_LL.c @@ -0,0 +1,130 @@ +#include +#include + +struct Node { + struct Node *prev; + int data; + struct Node *next; +}; + +typedef struct { + struct Node *first; +} DoublyLL; + +void init_DoublyLL_Array(DoublyLL *self, int A[], int n) { + struct Node *last, *t; + int i; + self->first = (struct Node*)malloc(sizeof(struct Node)); + self->first->data = A[0]; + self->first->prev = self->first->next = NULL; + last = self->first; + for(i = 1; i < n; i++) { + t = (struct Node*)malloc(sizeof(struct Node)); + t->next = last->next; + t->data = A[i]; + t->prev = last; + last->next = t; + last = t; + } +} + +void DoublyLL_Display(DoublyLL *self) { + struct Node *p = self->first; + while(p) { + printf("%d->", p->data); + p = p->next; + } + printf("\n"); +} + +int DoublyLL_Length(DoublyLL *self) { + struct Node *p = self->first; + int x = 0; + while(p) { + ++x; + p = p->next; + } + return x; +} + +void DoublyLL_Insert(DoublyLL *self, int index, int x) { + if(index < 0 || index > DoublyLL_Length(self)) + return; + struct Node *t, *p = self->first; + int i; + t = (struct Node*)malloc(sizeof(struct Node)); + t->data = x; + if(index == 0) { + t->next = self->first; + t->prev = NULL; + self->first = t; + } else { + for(i = 0; i < index - 1; i++) + p = p->next; + t->next = p->next; + if(p->next) + p->next->prev = t; + p->next = t; + t->prev = p; + } +} + +int DoublyLL_Delete(DoublyLL *self, int index) { + if(index < 0 || index > DoublyLL_Length(self)) + return -1; + struct Node *p = self->first; + int x, i; + if(index == 1) { + self->first = self->first->next; + x = p->data; + free(p); + if(self->first) + self->first->prev = NULL; + return x; + } else { + for(i = 0; i < index - 1; i++) + p = p->next; + p->prev->next = p->next; + if(p->next) + p->next->prev = p->prev; + x = p->data; + free(p); + return x; + } +} + +void DoublyLL_Reverse(DoublyLL *self) { + struct Node *temp, *p = self->first; + while(p) { + temp = p->prev; + p->prev = p->next; + p->next = temp; + p = p->prev; + if(p && p->next == NULL) + self->first = p; + } +} + +void destroy_DoublyLL(DoublyLL *self) { + struct Node *p = self->first, *q; + while(p) { + q = p; + p = p->next; + printf("\nDeleted :%d", q->data); + free(q); + } +} + +int main() { + int A[] = {1, 4, 6, 7, 9}; + DoublyLL a; + init_DoublyLL_Array(&a, A, 5); + printf("Before \n"); + DoublyLL_Display(&a); + DoublyLL_Reverse(&a); + printf("After \n"); + DoublyLL_Display(&a); + printf("\nLength : %d\n", DoublyLL_Length(&a)); + destroy_DoublyLL(&a); + return 0; +} diff --git a/09_Linked_list/19_CDoubleLL.c b/09_Linked_list/19_CDoubleLL.c new file mode 100644 index 0000000..6d6051a --- /dev/null +++ b/09_Linked_list/19_CDoubleLL.c @@ -0,0 +1,143 @@ +#include +#include + +struct Node { + struct Node *prev; + int data; + struct Node *next; +}; + +typedef struct { + struct Node *first; +} CDoublyLL; + +void init_CDoublyLL(CDoublyLL *self) { + self->first = NULL; +} + +void init_CDoublyLL_Array(CDoublyLL *self, int A[], int n) { + struct Node *last, *t; + int i; + self->first = (struct Node*)malloc(sizeof(struct Node)); + self->first->data = A[0]; + self->first->next = self->first; + last = self->first; + for(i = 1; i < n; i++) { + t = (struct Node*)malloc(sizeof(struct Node)); + t->next = last->next; + t->data = A[i]; + t->prev = last; + last->next = t; + last = t; + } + self->first->prev = last; +} + +void CDoublyLL_Display(CDoublyLL *self) { + struct Node *p = self->first; + if(self->first == NULL) return; + do { + printf("%d->", p->data); + p = p->next; + } while(p != self->first); + printf("\n"); +} + +int CDoublyLL_Length(CDoublyLL *self) { + struct Node *p = self->first; + if(self->first == NULL) + return 0; + int x = 0; + do { + ++x; + p = p->next; + } while(p != self->first); + return x; +} + +int CDoublyLL_Delete(CDoublyLL *self, int index) { + if(index < 0 || index > CDoublyLL_Length(self)) + return -1; + struct Node *p = self->first; + int x, i; + if(index == 1) { + if(self->first->next) { + self->first->prev->next = self->first->next; + self->first = self->first->next; + self->first->prev = p->prev; + x = p->data; + } else { + self->first = NULL; + } + free(p); + return x; + } else { + for(i = 0; i < index - 1; i++) + p = p->next; + p->prev->next = p->next; + p->next->prev = p->prev; + x = p->data; + free(p); + return x; + } +} + +void CDoublyLL_Insert(CDoublyLL *self, int index, int x) { + if(index < 0 || index > CDoublyLL_Length(self)) + return; + struct Node *t, *p = self->first; + int i; + t = (struct Node*)malloc(sizeof(struct Node)); + t->data = x; + if(index == 0) { + if(self->first) { + t->next = self->first; + t->prev = self->first->prev; + t->prev->next = t; + self->first->prev = t; + } else { + t->next = t->prev = t; + } + self->first = t; + } else { + for(i = 0; i < index - 1; i++) + p = p->next; + t->next = p->next; + if(p->next) + p->next->prev = t; + p->next = t; + t->prev = p; + } +} + +void destroy_CDoublyLL(CDoublyLL *self) { + struct Node *p = self->first, *q; + int len = CDoublyLL_Length(self); + while(len > 0) { + q = p->next; + printf("Deleted :%d\n", p->data); + free(p); + p = q; + --len; + } +} + +int main() { + CDoublyLL a; + init_CDoublyLL(&a); + CDoublyLL_Insert(&a, 0, 3); + CDoublyLL_Insert(&a, 1, 7); + CDoublyLL_Insert(&a, 2, 4); + printf("Before\n"); + CDoublyLL_Display(&a); + printf("Before Length : %d\n", CDoublyLL_Length(&a)); + + printf("Deleted : %d\n", CDoublyLL_Delete(&a, 1)); + + printf("After\n"); + CDoublyLL_Display(&a); + printf("AFter Length : %d\n", CDoublyLL_Length(&a)); + + destroy_CDoublyLL(&a); + return 0; +} diff --git a/09_Linked_list/20_MiddleNode_and_intersection_of_two_LL.c b/09_Linked_list/20_MiddleNode_and_intersection_of_two_LL.c new file mode 100644 index 0000000..83381f1 --- /dev/null +++ b/09_Linked_list/20_MiddleNode_and_intersection_of_two_LL.c @@ -0,0 +1,4 @@ +//will be doing it later +int main() { + return 0; +} diff --git a/10_Sparse_matrix_and_polynimial_representation/01_sparse_matrix.c b/10_Sparse_matrix_and_polynimial_representation/01_sparse_matrix.c new file mode 100644 index 0000000..01cbae6 --- /dev/null +++ b/10_Sparse_matrix_and_polynimial_representation/01_sparse_matrix.c @@ -0,0 +1,141 @@ +#include +#include + +typedef struct Node { + int row; + int column; + int value; + struct Node *next; +} Node; + +typedef struct Sparse_Matrix { + Node *first; + int m, n; +} Sparse_Matrix; + +void init_Sparse_Matrix(Sparse_Matrix* self) { + self->first = NULL; + int i, j, data; + printf("Enter the dimension of Matrix "); + scanf("%d%d", &self->m, &self->n); + Node *p = NULL, *q = NULL; + printf("Enter the Matrix: \n"); + + for(i = 0; i < self->m; i++) { + for(j = 0; j < self->n; j++) { + scanf("%d", &data); + if(data != 0) { + p = (Node*)malloc(sizeof(Node)); + if(q != NULL) + q->next = p; + else + self->first = p; + p->value = data; + p->row = i; + p->column = j; + p->next = NULL; + q = p; + } + } + } +} + +Node* Sparse_Matrix_GetHead(Sparse_Matrix* self) { + return self->first; +} + +void Sparse_Matrix_Merge(Sparse_Matrix* self, Sparse_Matrix b) { + Node *first1 = self->first, *first2 = Sparse_Matrix_GetHead(&b), *first3 = NULL, *p = NULL, *q = NULL; + while (first1 && first2) { + if(first1->row < first2->row) { + p = first1; + if(q) + q->next = p; + else + first3 = p; + q = p; + first1 = first1->next; + p->next = NULL; + } + else if(first2->row < first1->row) { + p = first2; + if(q) + q->next = p; + else + first3 = p; + q = p; + first2 = first2->next; + p->next = NULL; + } + else { + if(first1->column < first2->column) { + p = first1; + if(q) + q->next = p; + else + first3 = p; + q = p; + first1 = first1->next; + p->next = NULL; + } + else if(first2->column < first1->column) { + p = first2; + if(q) + q->next = p; + else + first3 = p; + q = p; + first2 = first2->next; + p->next = NULL; + } + else { + p = first1; + if(q) + q->next = p; + else + first3 = p; + q = p; + p->value = first1->value + first2->value; + first1 = first1->next; + Node *temp = first2; + first2 = first2->next; + temp->next = NULL; + p->next = NULL; + } + } + } + if(first1) + p->next = first1; + else + p->next = first2; + self->first = first3; +} + +void Sparse_Matrix_Display(Sparse_Matrix* self) { + Node *p = NULL; + p = self->first; + for(int i = 0; i < self->m; i++) { + for(int j = 0; j < self->n; j++) { + if(p && p->row == i && p->column == j) { + printf("%d ", p->value); + p = p->next; + } + else { + printf("0 "); + } + } + printf("\n"); + } +} + +int main() { + Sparse_Matrix a; + Sparse_Matrix b; + + init_Sparse_Matrix(&a); + init_Sparse_Matrix(&b); + + Sparse_Matrix_Merge(&a, b); + Sparse_Matrix_Display(&a); + return 0; +} diff --git a/10_Sparse_matrix_and_polynimial_representation/02_polynomial_linked_list.c b/10_Sparse_matrix_and_polynimial_representation/02_polynomial_linked_list.c new file mode 100644 index 0000000..18e2545 --- /dev/null +++ b/10_Sparse_matrix_and_polynimial_representation/02_polynomial_linked_list.c @@ -0,0 +1,53 @@ +#include +#include +#include + +struct Node { + int coeff; + int exp; + struct Node *next; +} *poly = NULL; + +void create() { + struct Node *t = NULL, *last = NULL; + int num; + printf("Enter the number of polynomials "); + scanf("%d", &num); + + printf("Enter the coefficient and exponent of each polynomial "); + for(int i = 0; i < num; i++) { + t = (struct Node*)malloc(sizeof(struct Node)); + scanf("%d%d", &t->coeff, &t->exp); + if(poly == NULL) { + poly = last = t; + } + else { + last->next = t; + last = t; + } + last->next = NULL; + } +} + +void display(struct Node *p) { + while(p) { + printf("%dx**%d + ", p->coeff, p->exp); + p = p->next; + } +} + +long eva(struct Node *p, int x) { + long val = 0; + while(p) { + val += p->coeff * pow(x, p->exp); + p = p->next; + } + return val; +} + +int main() { + create(); + display(poly); + printf("\nAnswer of Evaluation%ld", eva(poly, 1)); + return 0; +} diff --git a/11_Stack/01_Stack_using_Arry.c b/11_Stack/01_Stack_using_Arry.c new file mode 100644 index 0000000..f37bc1e --- /dev/null +++ b/11_Stack/01_Stack_using_Arry.c @@ -0,0 +1,88 @@ +#include +#include + +struct Stack { + int size; + int top; + int *S; +}; + +void create(struct Stack *st) { + printf("Enter the size of stack : "); + scanf("%d", &st->size); + st->top = -1; + st->S = (int*)malloc(st->size * sizeof(int)); +} + +void display(struct Stack st) { + if(st.top == -1) + printf("stack is Empty"); + else{ + int i; + for(i = st.top; i >= 0; i--) { + printf("%d ", st.S[i]); + } + printf("\n"); + } +} + +void push(struct Stack *st, int x) { + if(st->size - st->top == 1) { + printf("stack is overflow\n"); + } + else{ + st->top++; + st->S[st->top] = x; + } +} + +int pop(struct Stack *st) { + int x = -1; + if(st->top == -1) { + printf("Stack is underflow\n"); + } + else{ + x = st->S[st->top]; + st->top--; + } + return x; +} + +int peek(struct Stack st, int index) { + int x = -1; + if(st.top - index + 1 < 0) + printf("Invalid Index\n"); + else{ + x = st.S[st.top - index + 1]; + } + return x; +} + +int StackTop(struct Stack st) { + int x = -1; + if(st.top != -1){ + x = st.S[st.top]; + } + return x; +} + +int isEmpty(struct Stack st) { + if(st.top == -1) + return 1; + else + return 0; +} + +int isFull(struct Stack st) { + if(st.size - st.top == 1) + return 1; + else + return 0; +} + +int main() { + struct Stack st; + create(&st); + display(st); + return 0; +} diff --git a/11_Stack/02_Stack_using_LL.c b/11_Stack/02_Stack_using_LL.c new file mode 100644 index 0000000..537b881 --- /dev/null +++ b/11_Stack/02_Stack_using_LL.c @@ -0,0 +1,72 @@ +#include +#include + +typedef struct Node { + int data; + struct Node *next; +} Node; + +typedef struct Stack { + Node *top; +} Stack; + +void init_Stack(Stack* self) { + self->top = NULL; +} + +void Stack_push(Stack* self, int x) { + Node *t = (Node*)malloc(sizeof(Node)); + if(t == NULL) + printf("Stack overflow\n"); + else{ + t->data = x; + if(self->top) + { + t->next = self->top; + self->top = t; + } + else{ + self->top = t; + t->next = NULL; + } + } +} + +int Stack_pop(Stack* self) { + int x = -1; + if(self->top == NULL) + printf("Stack underflow\n"); + else{ + Node *t = self->top; + self->top = self->top->next; + x = t->data; + free(t); + } + return x; +} + +void Stack_Display(Stack* self) { + if(self->top == NULL) + printf("Stack is Empty\n"); + else{ + Node *t = self->top; + while(t) + { + printf("%d ", t->data); + t = t->next; + } + printf("\n"); + } +} + +int main() { + Stack st; + init_Stack(&st); + Stack_pop(&st); + Stack_push(&st, 5); + Stack_push(&st, 3); + Stack_push(&st, 8); + printf("popped %d\n", Stack_pop(&st)); + Stack_Display(&st); + return 0; +} diff --git a/11_Stack/03_parenthesis_is_balanced.c b/11_Stack/03_parenthesis_is_balanced.c new file mode 100644 index 0000000..8cbe885 --- /dev/null +++ b/11_Stack/03_parenthesis_is_balanced.c @@ -0,0 +1,77 @@ +#include +#include +#include + +struct Stack { + int size; + int top; + char *S; +}; + +void create(struct Stack *st, char *s) { + st->size = strlen(s); + st->top = -1; + st->S = (char*)malloc(st->size * sizeof(char)); +} + +void push(struct Stack *st, char x) { + if(st->size - st->top == 1) { + printf("stack is overflow\n"); + } + else { + st->top++; + st->S[st->top] = x; + } +} + +char pop(struct Stack *st) { + char x = -1; + if(st->top == -1) { + printf("Stack is underflow\n"); + } + else { + x = st->S[st->top]; + st->top--; + } + return x; +} + +int isEmpty(struct Stack st) { + if(st.top == -1) + return 1; + else + return 0; +} + +int isFull(struct Stack st) { + if(st.size - st.top == 1) + return 1; + else + return 0; +} + +int isBalanced(char *s) { + int i; + struct Stack st; + create(&st, s); + + for(i = 0; s[i] != '\0'; i++) { + if(s[i] == '(') + push(&st, '('); + else if(s[i] == ')') { + if(isEmpty(st)) + return 0; + pop(&st); + } + } + if(isEmpty(st)) + return 1; + else + return 0; +} + +int main() { + char s[] = "((Hello))+(World))"; + printf("%d\n", isBalanced(s)); + return 0; +} diff --git a/11_Stack/04_parenthesis_is_balanced_extended.c b/11_Stack/04_parenthesis_is_balanced_extended.c new file mode 100644 index 0000000..c79e9fb --- /dev/null +++ b/11_Stack/04_parenthesis_is_balanced_extended.c @@ -0,0 +1,82 @@ +#include +#include +#include + +struct Stack { + int size; + int top; + char *S; +}; + +void create(struct Stack *st, char *s) { + int x = strlen(s); + st->size = x; + st->top = -1; + st->S = (char*)malloc(st->size * sizeof(char)); +} + +void push(struct Stack *st, char x) { + if(st->size - st->top == 1) { + printf("stack is overflow\n"); + } + else{ + st->top++; + st->S[st->top] = x; + } +} + +char pop(struct Stack *st) { + char x = -1; + if(st->top == -1) { + printf("Stack is underflow\n"); + } + else{ + x = st->S[st->top]; + st->top--; + } + return x; +} + +int isEmpty(struct Stack st) { + if(st.top == -1) + return 1; + else + return 0; +} + +int isFull(struct Stack st) { + if(st.size - st.top == 1) + return 1; + else + return 0; +} + +int isBalanced(char *s) { + int i; + struct Stack st; + create(&st, s); + + for(i = 0; s[i] != '\0'; i++) { + if(s[i] == '(' || s[i] == '{' || s[i] == '[') + push(&st, s[i]); + else if(s[i] == ')' || s[i] == ']' || s[i] == '}') { + if(isEmpty(st)) + return 0; + char x = pop(&st); + if((s[i] - x) <= 2) //working with ascii code + continue; + else + return 0; + } + } + if(isEmpty(st)) + return 1; + else + return 0; +} + +int main() { + char s[] = "{([hedfsdfllo])}"; + printf("%d\n", isBalanced(s)); + return 0; +} diff --git a/11_Stack/05_infix_to_postfix.c b/11_Stack/05_infix_to_postfix.c new file mode 100644 index 0000000..f06f2d0 --- /dev/null +++ b/11_Stack/05_infix_to_postfix.c @@ -0,0 +1,110 @@ +#include +#include +#include + +struct Stack { + int size; + int top; + char *S; +}; + +void create(struct Stack *st, char *s) { + int x = strlen(s); + st->size = x; + st->top = -1; + st->S = (char*)malloc(st->size * sizeof(char)); +} + +void push(struct Stack *st, char x) { + if(st->size - st->top == 1) { + printf("stack is overflow\n"); + } + else{ + st->top++; + st->S[st->top] = x; + } +} + +char TopValue(struct Stack st) { + if(st.top == -1) + return -1; + else + return st.S[st.top]; +} + +char pop(struct Stack *st) { + char x = -1; + if(st->top == -1) { + printf("Stack is underflow\n"); + } + else{ + x = st->S[st->top]; + st->top--; + } + return x; +} + +int isEmpty(struct Stack st) { + if(st.top == -1) + return 1; + else + return 0; +} + +int isFull(struct Stack st) { + if(st.size - st.top == 1) + return 1; + else + return 0; +} + +int isOperand(char s) { + if(s == '*' || s == '/' || s == '+' || s == '-') + return 0; + else + return 1; +} + +int pre(char s) { + if(s == '*' || s == '/') + return 2; + else if(s == '+' || s == '-') + return 1; + return 0; +} + +char* ToPost(char *s) { + char *postfix; + int i = 0, j = 0; + postfix = (char*)malloc((strlen(s) + 2) * sizeof(char)); + struct Stack st; + create(&st, s); + push(&st, '#'); + while(s[i] != '\0') { + if(isOperand(s[i])) + postfix[j++] = s[i++]; + else { + if(pre(s[i]) > pre(TopValue(st))) + push(&st, s[i++]); + else { + postfix[j++] = pop(&st); + } + } + } + while(!isEmpty(st)) { + char popped = pop(&st); + if (popped != '#') { + postfix[j++] = popped; + } + } + postfix[j] = '\0'; + return postfix; +} + +int main() { + char s[] = "a+b*c/d+e"; + char *postfix = ToPost(s); + printf("%s\n", postfix); + free(postfix); + return 0; +} diff --git a/11_Stack/06_infix_to_posstfix.c b/11_Stack/06_infix_to_posstfix.c new file mode 100644 index 0000000..c23fd7d --- /dev/null +++ b/11_Stack/06_infix_to_posstfix.c @@ -0,0 +1,131 @@ +#include +#include +#include + +struct Stack { + int size; + int top; + char *S; +}; + +void create(struct Stack *st, char *s) { + int x = strlen(s); + st->size = x; + st->top = -1; + st->S = (char*)malloc(st->size * sizeof(char)); +} + +void push(struct Stack *st, char x) { + if(st->size - st->top == 1) { + printf("stack is overflow\n"); + } + else{ + st->top++; + st->S[st->top] = x; + } +} + +char TopValue(struct Stack st) { + if(st.top == -1) + return -1; + else + return st.S[st.top]; +} + +char pop(struct Stack *st) { + char x = -1; + if(st->top == -1) { + printf("Stack is underflow\n"); + } + else{ + x = st->S[st->top]; + st->top--; + } + return x; +} + +int isEmpty(struct Stack st) { + if(st.top == -1) + return 1; + else + return 0; +} + +int isFull(struct Stack st) { + if(st.size - st.top == 1) + return 1; + else + return 0; +} + +int isOperand(char s) { + if(s == '*' || s == '/' || s == '+' || s == '-' || s == '^' || s == '(' || s == ')') + return 0; + else + return 1; +} + +int out_pre(char s) { + if(s == '+' || s == '-') + return 1; + else if(s == '*' || s == '/') + return 3; + else if(s == '^') + return 6; + else if(s == '(') + return 7; + else if(s == ')') + return 0; + return 0; +} + +int ins_pre(char s) { + if(s == '+' || s == '-') + return 2; + else if(s == '*' || s == '/') + return 4; + else if(s == '^') + return 5; + else if(s == '(') + return 0; + return -1; +} + +char* ToPost(char *s) { + char *postfix; + int i = 0, j = 0; + postfix = (char*)malloc((strlen(s) + 2) * sizeof(char)); + struct Stack st; + create(&st, s); + push(&st, '#'); + while(s[i] != '\0') { + if(isOperand(s[i])) + postfix[j++] = s[i++]; + else { + if(out_pre(s[i]) > ins_pre(TopValue(st))) + push(&st, s[i++]); + else if(out_pre(s[i]) < ins_pre(TopValue(st))) { + postfix[j++] = pop(&st); + } + else{ + pop(&st); + i++; + } + } + } + while(!isEmpty(st)) { + char popped = pop(&st); + if(popped != '#') + postfix[j++] = popped; + } + postfix[j] = '\0'; + return postfix; +} + +int main() { + char s[] = "((a+b)*c)-d^e^f"; + char *postfix = ToPost(s); + printf("%s\n", postfix); + free(postfix); + return 0; +} diff --git a/11_Stack/07_postfix_eval.c b/11_Stack/07_postfix_eval.c new file mode 100644 index 0000000..1bd3a74 --- /dev/null +++ b/11_Stack/07_postfix_eval.c @@ -0,0 +1,99 @@ +#include +#include +#include + +struct Stack { + int size; + int top; + int *S; +}; + +void create(struct Stack *st, char *s) { + int x = strlen(s); + st->size = x; + st->top = -1; + st->S = (int*)malloc(st->size * sizeof(int)); +} + +void push(struct Stack *st, int x) { + if(st->size - st->top == 1) { + printf("stack is overflow\n"); + } + else { + st->top++; + st->S[st->top] = x; + } +} + +int pop(struct Stack *st) { + int x = -1; + if(st->top == -1) { + printf("Stack is underflow\n"); + } + else { + x = st->S[st->top]; + st->top--; + } + return x; +} + +int isEmpty(struct Stack st) { + if(st.top == -1) + return 1; + else + return 0; +} + +int isFull(struct Stack st) { + if(st.size - st.top == 1) + return 1; + else + return 0; +} + +int isOperand(char s) { + if(s == '*' || s == '/' || s == '+' || s == '-' || s == '^' || s == '(' || s == ')') + return 0; + else + return 1; +} + +int eval(char *postfix) { + struct Stack st; + create(&st, postfix); + int i, x1, x2, r; + for(i = 0; postfix[i] != '\0'; i++) { + if(isOperand(postfix[i])) { + push(&st, (postfix[i] - '0')); + } + else{ + x2 = pop(&st); + x1 = pop(&st); + switch(postfix[i]) { + case '+': + r = x1 + x2; + break; + case '-': + r = x1 - x2; + break; + case '*': + r = x1 * x2; + break; + case '/': + r = x1 / x2; + break; + default: + break; + } + push(&st, r); + } + } + return pop(&st); +} + +int main() { + char s[] = "2362/*+"; + int answer = eval(s); + printf("%d\n", answer); + return 0; +} diff --git a/12_Queue/01_enqueue_and_dequeue.c b/12_Queue/01_enqueue_and_dequeue.c new file mode 100644 index 0000000..09570eb --- /dev/null +++ b/12_Queue/01_enqueue_and_dequeue.c @@ -0,0 +1,56 @@ +#include +#include + +typedef struct Queue { + int size; + int front; + int rear; + int *Q; +} Queue; + +void init_Queue(Queue* self) { + printf("Enter the size of queue\n"); + scanf("%d", &self->size); + self->Q = (int*)malloc(self->size * sizeof(int)); + self->front = self->rear = -1; +} + +void Queue_enqueue(Queue* self, int x) { + if(self->rear == self->size - 1) + printf("Queue is full\n"); + else{ + ++self->rear; + self->Q[self->rear] = x; + } +} + +int Queue_dequeue(Queue* self) { + int x = -1; + if(self->front == self->rear) + printf("Queue is Empty\n"); + else{ + ++self->front; + x = self->Q[self->front]; + } + return x; +} + +void Queue_display(Queue* self) { + int i = self->front + 1; + for(; i <= self->rear; i++) + printf("%d ", self->Q[i]); + printf("\n"); +} + +int main() { + Queue q; + init_Queue(&q); + Queue_enqueue(&q, 1); + Queue_enqueue(&q, 2); + Queue_enqueue(&q, 3); + Queue_enqueue(&q, 4); + Queue_enqueue(&q, 5); + printf("Deleted %d\n", Queue_dequeue(&q)); + Queue_display(&q); + return 0; +} diff --git a/12_Queue/02_circular_queue.c b/12_Queue/02_circular_queue.c new file mode 100644 index 0000000..28c9ab1 --- /dev/null +++ b/12_Queue/02_circular_queue.c @@ -0,0 +1,60 @@ +#include +#include + +typedef struct Queue { + int size; + int front; + int rear; + int *Q; +} Queue; + +void init_Queue(Queue* self) { + printf("Enter the size of queue\n"); + scanf("%d", &self->size); + self->size++; + self->Q = (int*)malloc(self->size * sizeof(int)); + self->front = self->rear = 0; +} + +void Queue_enqueue(Queue* self, int x) { + if((self->rear + 1) % self->size == self->front) + printf("Queue is full\n"); + else{ + self->rear = (self->rear + 1) % self->size; + self->Q[self->rear] = x; + } +} + +int Queue_dequeue(Queue* self) { + int x = -1; + if(self->front == self->rear) + printf("Queue is full\n"); + else{ + self->front = (self->front + 1) % self->size; + x = self->Q[self->front]; + } + return x; +} + +void Queue_display(Queue* self) { + int i = self->front + 1; + do{ + printf("%d ", self->Q[i]); + i = (i + 1) % self->size; + } while(i != (self->rear + 1) % self->size); + printf("\n"); +} + +int main() { + Queue q; + init_Queue(&q); + Queue_enqueue(&q, 1); + Queue_enqueue(&q, 2); + Queue_enqueue(&q, 3); + Queue_enqueue(&q, 4); + Queue_enqueue(&q, 5); + printf("Deleted %d\n", Queue_dequeue(&q)); + Queue_enqueue(&q, 67); + Queue_display(&q); + return 0; +} diff --git a/12_Queue/03_queue_using_ll.c b/12_Queue/03_queue_using_ll.c new file mode 100644 index 0000000..bee9049 --- /dev/null +++ b/12_Queue/03_queue_using_ll.c @@ -0,0 +1,75 @@ +#include +#include + +typedef struct Node { + int data; + struct Node *next; +} Node; + +typedef struct Queue { + Node *front; + Node *rear; +} Queue; + +void init_Queue(Queue* self) { + self->front = self->rear = NULL; +} + +void Queue_enqueue(Queue* self, int x) { + Node *t; + t = (Node*)malloc(sizeof(Node)); + if(t == NULL) + printf("Queue is full\n"); + else{ + t->data = x; + t->next = NULL; + if(self->front == NULL) + self->front = self->rear = t; + else{ + self->rear->next = t; + self->rear = t; + } + } +} + +int Queue_dequeue(Queue* self) { + int x = -1; + Node *t; + if(self->front == NULL) + printf("Queue is Empty\n"); + else{ + t = self->front; + if(self->front->next) + self->front = self->front->next; + else + self->front = NULL; + x = t->data; + free(t); + } + return x; +} + +void Queue_display(Queue* self) { + Node *i = self->front; + while(i) { + printf("%d ", i->data); + i = i->next; + } + printf("\n"); +} + +int main() { + Queue q; + init_Queue(&q); + Queue_enqueue(&q, 1); + Queue_enqueue(&q, 2); + Queue_enqueue(&q, 3); + Queue_enqueue(&q, 4); + Queue_enqueue(&q, 5); + printf("Deleted %d\n", Queue_dequeue(&q)); + Queue_enqueue(&q, 14); + Queue_enqueue(&q, 15); + + Queue_display(&q); + return 0; +} diff --git a/13_Trees/01_creating_binary_tree.c b/13_Trees/01_creating_binary_tree.c new file mode 100644 index 0000000..a96148c --- /dev/null +++ b/13_Trees/01_creating_binary_tree.c @@ -0,0 +1,60 @@ +#include +#include +#include "Node.h" +#include "Queue.h" + +struct createtree { + struct Node *p, *t; + int x; + struct Queue q; +}; + +void init_createtree(struct createtree* self) { + printf("Enter the root Node data "); + scanf("%d", &root->data); + root->lchild = root->rchild = NULL; + // Assuming create is not explicitly required based on original or is initialized elsewhere + enqueue(&self->q, root); + while(!isEmpty(&self->q)) { + self->p = dequeue(&self->q); + printf("Enter the value of left child of %d ", self->p->data); + scanf("%d", &self->x); + if(self->x != -1) { + self->t = (struct Node*)malloc(sizeof(struct Node)); + self->t->data = self->x; + self->t->lchild = self->t->rchild = NULL; + self->p->lchild = self->t; + enqueue(&self->q, self->t); + } + printf("Enter the value of right child of %d ", self->p->data); + scanf("%d", &self->x); + if(self->x != -1) { + self->t = (struct Node*)malloc(sizeof(struct Node)); + self->t->data = self->x; + self->t->lchild = self->t->rchild = NULL; + self->p->rchild = self->t; + enqueue(&self->q, self->t); + } + } +} + +void createtree_display(struct createtree* self) { + struct Node *t; + struct Queue q; + enqueue(&q, root); + while(!isEmpty(&q)) { + t = dequeue(&q); + printf("%d ", t->data); + if(t->lchild) + enqueue(&q, t->lchild); + if(t->rchild) + enqueue(&q, t->rchild); + } +} + +int main() { + struct createtree r; + init_createtree(&r); + createtree_display(&r); + return 0; +} diff --git a/13_Trees/02_Traversing.c b/13_Trees/02_Traversing.c new file mode 100644 index 0000000..fa34150 --- /dev/null +++ b/13_Trees/02_Traversing.c @@ -0,0 +1,127 @@ +//Mine not working becz of some fault +#include +#include +#include "Node.h" +#include "Queue.h" +#include "Stack.h" + +struct createtree { + struct Node *p, *t; + int x; + struct Queue q; +}; + +void createtree_rinorder(struct createtree* self, struct Node *t) { + if(t) { + createtree_rinorder(self, t->lchild); + printf("%d ", t->data); + createtree_rinorder(self, t->rchild); + } +} + +void createtree_inorder(struct createtree* self) { + struct Stack st; + create(&st); + long int num; + struct Node *t = root; + while(t || !isEmpty(st)) { + if(t != NULL) { + push(&st, (long int)t); // storing address in long int + t = t->lchild; + } else { + num = pop(&st); + t = (struct Node *)num; // long int to address + printf("%d ", t->data); + t = t->rchild; + } + } +} + +void createtree_preorder(struct createtree* self) { + struct Stack st; + create(&st); + long int num; + struct Node *t = root; + while(t || !isEmpty(st)) { + if(t != NULL) { + push(&st, (long int)t); + printf("%d ", t->data); + t = t->lchild; + } else { + num = pop(&st); + t = (struct Node *)num; + t = t->rchild; + } + } +} + +void createtree_postorder(struct createtree* self) { + struct Stack st; + create(&st); + long int num; + struct Node *t = root; + while(t || !isEmpty(st)) { + if(t != NULL) { + push(&st, (long int)t); + t = t->lchild; + } else { + num = pop(&st); + if(num > 0) { + push(&st, -1 * num); + t = ((struct Node *)num)->rchild; + } else { + printf("%d ", ((struct Node *)(-1 * num))->data); + t = NULL; + } + } + } +} + +void createtree_display(struct createtree* self) { + struct Node *t; + struct Queue q; + enqueue(&q, root); + while(!isEmpty(q)) { // Using what matched the original Stack isEmpty parameter signature + t = dequeue(&q); + printf("%d ", t->data); + if(t->lchild) + enqueue(&q, t->lchild); + if(t->rchild) + enqueue(&q, t->rchild); + } +} + +void init_createtree(struct createtree* self) { + printf("Enter the root Node data "); + scanf("%d", &root->data); + root->lchild = root->rchild = NULL; + enqueue(&self->q, root); + while(!isEmpty(self->q)) { + self->p = dequeue(&self->q); + printf("Enter the value of left child of %d ", self->p->data); + scanf("%d", &self->x); + if(self->x != -1) { + self->t = (struct Node*)malloc(sizeof(struct Node)); + self->t->data = self->x; + self->t->lchild = self->t->rchild = NULL; + self->p->lchild = self->t; + enqueue(&self->q, self->t); + } + printf("Enter the value of right child of %d ", self->p->data); + scanf("%d", &self->x); + if(self->x != -1) { + self->t = (struct Node*)malloc(sizeof(struct Node)); + self->t->data = self->x; + self->t->lchild = self->t->rchild = NULL; + self->p->rchild = self->t; + enqueue(&self->q, self->t); + } + } +} + +int main() { + struct createtree r; + init_createtree(&r); + createtree_inorder(&r); + return 0; +} diff --git a/13_Trees/03_LevelOrder.c b/13_Trees/03_LevelOrder.c new file mode 100644 index 0000000..a3cbcf2 --- /dev/null +++ b/13_Trees/03_LevelOrder.c @@ -0,0 +1,89 @@ +#include +#include +#include "Node.h" + +struct NodeQueue { + struct Node** data; + int front; + int rear; + int capacity; +}; +void init_NodeQueue(struct NodeQueue* q, int cap) { + q->capacity = cap; + q->front = 0; + q->rear = 0; + q->data = (struct Node**)malloc(cap * sizeof(struct Node*)); +} +void NodeQueue_push(struct NodeQueue* q, struct Node* n) { + q->data[q->rear++] = n; +} +struct Node* NodeQueue_pop(struct NodeQueue* q) { + return q->data[q->front++]; +} +struct Node* NodeQueue_front(struct NodeQueue* q) { + return q->data[q->front]; +} +int NodeQueue_empty(struct NodeQueue* q) { + return q->front == q->rear; +} + +struct createtree { + struct Node *p, *t; + int x; + struct NodeQueue q; +}; + +void createtree_Levelorder(struct createtree* self) { + struct NodeQueue q; + init_NodeQueue(&q, 1000); + struct Node *t = root; + NodeQueue_push(&q, t); + while(!NodeQueue_empty(&q)) { + t = NodeQueue_front(&q); + NodeQueue_pop(&q); + printf("%d ", t->data); + if(t->lchild) { + NodeQueue_push(&q, t->lchild); + } + if(t->rchild) { + NodeQueue_push(&q, t->rchild); + } + } +} + +void init_createtree(struct createtree* self) { + printf("Enter the root Node data "); + scanf("%d", &root->data); + root->lchild = root->rchild = NULL; + init_NodeQueue(&self->q, 1000); + NodeQueue_push(&self->q, root); + while(!NodeQueue_empty(&self->q)) { + self->p = NodeQueue_front(&self->q); + NodeQueue_pop(&self->q); + printf("Enter the value of left child of %d ", self->p->data); + scanf("%d", &self->x); + if(self->x != -1) { + self->t = (struct Node*)malloc(sizeof(struct Node)); + self->t->data = self->x; + self->t->lchild = self->t->rchild = NULL; + self->p->lchild = self->t; + NodeQueue_push(&self->q, self->t); + } + printf("Enter the value of right child of %d ", self->p->data); + scanf("%d", &self->x); + if(self->x != -1) { + self->t = (struct Node*)malloc(sizeof(struct Node)); + self->t->data = self->x; + self->t->lchild = self->t->rchild = NULL; + self->p->rchild = self->t; + NodeQueue_push(&self->q, self->t); + } + } +} + +int main() { + struct createtree t; + init_createtree(&t); + createtree_Levelorder(&t); + return 0; +} diff --git a/13_Trees/04_Height_and_count.c b/13_Trees/04_Height_and_count.c new file mode 100644 index 0000000..7d07ccc --- /dev/null +++ b/13_Trees/04_Height_and_count.c @@ -0,0 +1,135 @@ +#include +#include +#include "Node.h" + +struct NodeQueue { + struct Node** data; + int front; + int rear; + int capacity; +}; +void init_NodeQueue(struct NodeQueue* q, int cap) { + q->capacity = cap; + q->front = 0; + q->rear = 0; + q->data = (struct Node**)malloc(cap * sizeof(struct Node*)); +} +void NodeQueue_push(struct NodeQueue* q, struct Node* n) { + q->data[q->rear++] = n; +} +struct Node* NodeQueue_pop(struct NodeQueue* q) { + return q->data[q->front++]; +} +struct Node* NodeQueue_front(struct NodeQueue* q) { + return q->data[q->front]; +} +int NodeQueue_empty(struct NodeQueue* q) { + return q->front == q->rear; +} + +struct createtree { + struct Node *p, *t; + int x; + struct NodeQueue q; +}; + +int createtree_leafNodeCount_node(struct createtree* self, struct Node *p) { + int x; + int y; + if (p != NULL){ + x = createtree_leafNodeCount_node(self, p->lchild); + y = createtree_leafNodeCount_node(self, p->rchild); + if (p->lchild == NULL && p->rchild == NULL){ + return x + y + 1; + } else { + return x + y; + } + } + return 0; +} + +int createtree_leafNodeCount(struct createtree* self) { + return createtree_leafNodeCount_node(self, root); +} + +int createtree_count_node(struct createtree* self, struct Node *p) { + if(p != NULL) + return createtree_count_node(self, p->lchild) + createtree_count_node(self, p->rchild) + 1; + return 0; +} + +int createtree_count(struct createtree* self) { + return createtree_count_node(self, root); +} + +int createtree_height_node(struct createtree* self, struct Node *p) { + int x, y; + if(p) { + x = createtree_height_node(self, p->lchild); + y = createtree_height_node(self, p->rchild); + if(x > y) + return x + 1; + else + return y + 1; + } + return 0; +} + +int createtree_height(struct createtree* self) { + return createtree_height_node(self, root); +} + +void createtree_Levelorder(struct createtree* self) { + struct NodeQueue q; + init_NodeQueue(&q, 1000); + struct Node *t = root; + NodeQueue_push(&q, t); + while(!NodeQueue_empty(&q)) { + t = NodeQueue_front(&q); + NodeQueue_pop(&q); + printf("%d ", t->data); + if(t->lchild) { + NodeQueue_push(&q, t->lchild); + } + if(t->rchild) { + NodeQueue_push(&q, t->rchild); + } + } +} + +void init_createtree(struct createtree* self) { + printf("Enter the root Node data "); + scanf("%d", &root->data); + root->lchild = root->rchild = NULL; + init_NodeQueue(&self->q, 1000); + NodeQueue_push(&self->q, root); + while(!NodeQueue_empty(&self->q)) { + self->p = NodeQueue_front(&self->q); + NodeQueue_pop(&self->q); + printf("Enter the value of left child of %d ", self->p->data); + scanf("%d", &self->x); + if(self->x != -1) { + self->t = (struct Node*)malloc(sizeof(struct Node)); + self->t->data = self->x; + self->t->lchild = self->t->rchild = NULL; + self->p->lchild = self->t; + NodeQueue_push(&self->q, self->t); + } + printf("Enter the value of right child of %d ", self->p->data); + scanf("%d", &self->x); + if(self->x != -1) { + self->t = (struct Node*)malloc(sizeof(struct Node)); + self->t->data = self->x; + self->t->lchild = self->t->rchild = NULL; + self->p->rchild = self->t; + NodeQueue_push(&self->q, self->t); + } + } +} + +int main() { + struct createtree t; + init_createtree(&t); + printf("\nleaf Node %d", createtree_leafNodeCount(&t)); + return 0; +} diff --git a/13_Trees/Node.h b/13_Trees/Node.h index e04c943..db98866 100644 --- a/13_Trees/Node.h +++ b/13_Trees/Node.h @@ -1,7 +1,12 @@ -class Node -{ - public: - Node *lchild; +#ifndef NODE_H +#define NODE_H + +typedef struct Node { + struct Node *lchild; int data; - Node *rchild; -}*root=new Node; \ No newline at end of file + struct Node *rchild; +} Node; + +extern Node *root; + +#endif \ No newline at end of file diff --git a/13_Trees/Queue.h b/13_Trees/Queue.h index e71e399..9ce47ad 100644 --- a/13_Trees/Queue.h +++ b/13_Trees/Queue.h @@ -1,47 +1,45 @@ -class Queue -{ - private: - int size; - int front=-1; - int rear=-1; - Node **Q; - public: - Queue(); - void enqueue(Node* x); - Node* dequeue(); - int isEmpty(); -}; -Queue::Queue() -{ - size=100; - Q=new Node*[size]; - front=rear=-1; +#ifndef QUEUE_H +#define QUEUE_H + +#include +#include +#include "Node.h" + +typedef struct Queue { + int size; + int front; + int rear; + Node **Q; +} Queue; + +static void init_Queue(Queue* self) { + self->size = 100; + self->Q = (Node**)malloc(self->size * sizeof(Node*)); + self->front = self->rear = -1; } -void Queue::enqueue(Node *x) -{ - if(rear==size-1) - cout<<"Queue is full\n"; +static void enqueue(Queue* self, Node *x) { + if(self->rear == self->size - 1) + printf("Queue is full\n"); else { - ++rear; - Q[rear]=x; - + ++self->rear; + self->Q[self->rear] = x; } } -Node *Queue::dequeue() -{ - Node *x=NULL; - if(front==rear) - cout<<"Queue is Empty\n"; +static Node* dequeue(Queue* self) { + Node *x = NULL; + if(self->front == self->rear) + printf("Queue is Empty\n"); else{ - ++front; - x=Q[front]; + ++self->front; + x = self->Q[self->front]; } return x; } -int Queue::isEmpty() -{ - return front==rear; +static int isEmptyQueue(Queue* self) { + return self->front == self->rear; } + +#endif diff --git a/13_Trees/Stack.h b/13_Trees/Stack.h index 60494e4..85b63ad 100644 --- a/13_Trees/Stack.h +++ b/13_Trees/Stack.h @@ -1,69 +1,64 @@ +#ifndef STACK_H +#define STACK_H -struct Stack{ +#include +#include + +typedef struct Stack { int size; int top; long int *S; -}; +} Stack; -void create(Stack *st) -{ - cout<<"Enter the size of stack : "; - cin>>st->size; - st->top=-1; - st->S=new long int[st->size]; +static void createStack(Stack *st) { + printf("Enter the size of stack : "); + scanf("%d", &st->size); + st->top = -1; + st->S = (long int*)malloc(st->size * sizeof(long int)); } - - -void push(Stack *st,long int x) -{ - if(st->size-st->top==1) - { - cout<<"stack is overflow\n"; +static void push(Stack *st, long int x) { + if(st->size - st->top == 1) { + printf("stack is overflow\n"); } else{ st->top++; - st->S[st->top]=x; + st->S[st->top] = x; } } -long int pop(Stack *st) -{ - long int x=-1; - if(st->top==-1) - { - cout<<"Stack is underflow\n"; +static long int pop(Stack *st) { + long int x = -1; + if(st->top == -1) { + printf("Stack is underflow\n"); } else{ - x=st->S[st->top]; + x = st->S[st->top]; st->top--; } return x; } - - -long int StackTop(Stack st) -{ - long int x=-1; - if(st.top!=-1){ - x=st.S[st.top]; +static long int StackTop(Stack st) { + long int x = -1; + if(st.top != -1){ + x = st.S[st.top]; } return x; } -int isEmpty(Stack st) -{ - if(st.top==-1) +static int isEmptyStack(Stack st) { + if(st.top == -1) return 1; else return 0; } -int isFull(Stack st) -{ - if(st.size-st.top==1) +static int isFullStack(Stack st) { + if(st.size - st.top == 1) return 1; else return 0; } + +#endif diff --git a/14_BST/01_bst.c b/14_BST/01_bst.c new file mode 100644 index 0000000..8043b97 --- /dev/null +++ b/14_BST/01_bst.c @@ -0,0 +1,169 @@ +#include +#include + +typedef struct Node { + struct Node* lchild; + int data; + struct Node* rchild; +} Node; + +Node* root = NULL; + +typedef struct NodeStack { + Node** data; + int top; + int capacity; +} NodeStack; + +void init_NodeStack(NodeStack* s, int cap) { + s->capacity = cap; + s->top = -1; + s->data = (Node**)malloc(cap * sizeof(Node*)); +} +void push(NodeStack* s, Node* x) { s->data[++s->top] = x; } +Node* pop(NodeStack* s) { return s->data[s->top--]; } +Node* top(NodeStack* s) { return s->data[s->top]; } +int isEmpty(NodeStack* s) { return s->top == -1; } + +Node* Rinsert(Node *p, int key) { + Node *t; + if(p == NULL) { + t = (Node*)malloc(sizeof(Node)); + t->data = key; + t->lchild = t->rchild = NULL; + return t; + } + if(p->data > key) + p->lchild = Rinsert(p->lchild, key); + else if(p->data < key) + p->rchild = Rinsert(p->rchild, key); + return p; +} + +void insert(int key) { + Node *p = root, *r; + if(p == NULL) { + p = (Node*)malloc(sizeof(Node)); + p->lchild = p->rchild = NULL; + p->data = key; + root = p; + return; + } + else { + while(p != NULL) { + r = p; + if(p->data < key) + p = p->rchild; + else if(p->data > key) + p = p->lchild; + else + return; + } + p = (Node*)malloc(sizeof(Node)); + p->lchild = p->rchild = NULL; + p->data = key; + if(r->data < key) + r->rchild = p; + else + r->lchild = p; + } +} + +void inorder(Node *p) { + if(p) { + inorder(p->lchild); + printf("%d ", p->data); + fflush(stdout); + inorder(p->rchild); + } +} + +int Height(Node *t) { + if(t == NULL) + return 0; + int lh = Height(t->lchild); + int rh = Height(t->rchild); + return lh < rh ? rh + 1 : lh + 1; +} + +Node *Inprec(Node *p) { + while(p && p->rchild != NULL) + p = p->rchild; + return p; +} + +Node *Insucc(Node *p) { + while(p && p->lchild != NULL) + p = p->lchild; + return p; +} + +Node *Delete(Node *p, int key) { + Node *t = NULL; + if (p == NULL) + return NULL; + if(p->lchild == NULL && p->rchild == NULL) { + if (p == root) { + root = NULL; + } + free(p); + return NULL; + } + if(p->data > key) + p->lchild = Delete(p->lchild, key); + else if(p->data < key) + p->rchild = Delete(p->rchild, key); + else { + if(Height(p->lchild) > Height(p->rchild)) { + t = Inprec(p->lchild); + p->data = t->data; + p->lchild = Delete(p->lchild, t->data); + } + else { + t = Insucc(p->rchild); + p->data = t->data; + p->rchild = Delete(p->rchild, t->data); + } + } + return p; +} + +void CreatePre() { + int pre[] = {30, 20, 10, 15, 25, 40, 50, 45}; + int n = 8; + NodeStack st; + init_NodeStack(&st, 100); + Node *p, *t; + root = (Node*)malloc(sizeof(Node)); + int i = 0; + root->data = pre[i++]; + root->lchild = root->rchild = NULL; + p = root; + while (i < n) { + if(p->data > pre[i]) { + t = (Node*)malloc(sizeof(Node)); + t->data = pre[i++]; + t->lchild = t->rchild = NULL; + p->lchild = t; + push(&st, p); + p = t; + } + else if(p->data < pre[i] && (isEmpty(&st) ? 1 : (top(&st)->data > pre[i] ? 1 : 0))) { + t = (Node*)malloc(sizeof(Node)); + t->data = pre[i++]; + t->lchild = t->rchild = NULL; + p->rchild = t; + p = t; + } + else { + p = top(&st); + pop(&st); + } + } +} + +int main() { + CreatePre(); + inorder(root); + return 0; +} diff --git a/15_AVL_Tree/01_LL_Rotation.c b/15_AVL_Tree/01_LL_Rotation.c new file mode 100644 index 0000000..2641971 --- /dev/null +++ b/15_AVL_Tree/01_LL_Rotation.c @@ -0,0 +1,232 @@ +#include +#include + +struct Node { + struct Node* lchild; + int data; + int height; + struct Node* rchild; +} *root = NULL; + +struct NodeStack { + struct Node** data; + int top; + int capacity; +}; +void init_NodeStack(struct NodeStack* s, int cap) { + s->capacity = cap; + s->top = -1; + s->data = (struct Node**)malloc(cap * sizeof(struct Node*)); +} +void NodeStack_push(struct NodeStack* s, struct Node* val) { + s->data[++(s->top)] = val; +} +struct Node* NodeStack_pop(struct NodeStack* s) { + return s->data[(s->top)--]; +} +struct Node* NodeStack_top(struct NodeStack* s) { + return s->data[s->top]; +} +int NodeStack_isEmpty(struct NodeStack* s) { + return s->top == -1; +} + +int NodeHeight(struct Node *p) { + if(p == NULL) + return 0; + int hl, hr; + hl = p && p->lchild ? p->lchild->height : 0; + hr = p && p->rchild ? p->rchild->height : 0; + return hl > hr ? hl + 1 : hr + 1; +} + +int BalanceFactor(struct Node *p) { + int hl, hr; + hl = p && p->lchild ? p->lchild->height : 0; + hr = p && p->rchild ? p->rchild->height : 0; + return hl - hr; +} + +struct Node* LLRotation(struct Node *p) { + struct Node *pl = p->lchild; + struct Node *plr = pl->rchild; + + p->lchild = plr; + pl->rchild = p; + + p->height = NodeHeight(p); + pl->height = NodeHeight(pl); + + if(p == root) { + root = pl; + return pl; + } + return p; +} + +struct Node* RRRotation(struct Node *p) { + struct Node *pr = p->rchild; + struct Node *prl = pr->lchild; + + p->rchild = prl; + pr->lchild = p; + + p->height = NodeHeight(p); + pr->height = NodeHeight(pr); + + if(p == root) { + root = pr; + return pr; + } + return p; +} + +struct Node* LRRotation(struct Node *p) { + struct Node *pl = p->lchild; + struct Node *plr = pl->rchild; + + pl->rchild = plr->lchild; + p->lchild = plr->rchild; + plr->lchild = pl; + plr->rchild = p; + + p->height = NodeHeight(p); + pl->height = NodeHeight(pl); + plr->height = NodeHeight(plr); + + if(p == root) + root = plr; + + return plr; +} + +struct Node* RLRotation(struct Node *p) { + struct Node *pr = p->rchild; + struct Node *prl = pr->lchild; + + p->rchild = prl->lchild; + pr->lchild = prl->rchild; + prl->lchild = p; + prl->rchild = pr; + + p->height = NodeHeight(p); + pr->height = NodeHeight(pr); + prl->height = NodeHeight(prl); + + if(p == root) + root = prl; + + return prl; +} + +struct Node* Rinsert(struct Node *p, int key) { + struct Node *t; + if(p == NULL) { + t = (struct Node*)malloc(sizeof(struct Node)); + t->data = key; + t->height = 1; + t->lchild = t->rchild = NULL; + return t; + } + if(p->data > key) + p->lchild = Rinsert(p->lchild, key); + else if(p->data < key) + p->rchild = Rinsert(p->rchild, key); + p->height = NodeHeight(p); + + if(BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == 1) + return LLRotation(p); + else if(BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == -1) + return LRRotation(p); + else if(BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == 1) + return RLRotation(p); + else if(BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == -1) + return RRRotation(p); + return p; +} + +struct Node* Inprec(struct Node *p) { + while(p && p->rchild != NULL) + p = p->rchild; + return p; +} + +struct Node* Insucc(struct Node *p) { + while(p && p->lchild != NULL) + p = p->lchild; + return p; +} + +struct Node* Delete(struct Node *p, int key) { + struct Node *t = NULL; + if (p == NULL) + return NULL; + if(p->lchild == NULL && p->rchild == NULL) { + if (p == root){ + root = NULL; + } + free(p); + return NULL; + } + if(p->data > key) { + p->lchild = Delete(p->lchild, key); + } + else if(p->data < key) + p->rchild = Delete(p->rchild, key); + else { + if(NodeHeight(p->lchild) > NodeHeight(p->rchild)) { + t = Inprec(p->lchild); + p->data = t->data; + p->lchild = Delete(p->lchild, t->data); + } else { + t = Insucc(p->rchild); + p->data = t->data; + p->rchild = Delete(p->rchild, t->data); + } + } + + p->height = NodeHeight(p); + + if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == 1) { + return LLRotation(p); + } else if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == -1){ + return LRRotation(p); + } else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == -1){ + return RRRotation(p); + } else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == 1){ + return RLRotation(p); + } else if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == 0){ + return LLRotation(p); + } else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == 0){ + return RRRotation(p); + } + + return p; +} + +void Display(struct Node *p) { + printf("%d\n", p->data); + if(p->lchild) { + printf("left child of %d is ", p->data); + Display(p->lchild); + } + + if(p->rchild) { + printf("right child of %d is ", p->data); + Display(p->rchild); + } +} + +int main() { + root = Rinsert(root, 50); + Rinsert(root, 60); + Rinsert(root, 40); + Rinsert(root, 30); + Rinsert(root, 55); + Rinsert(root, 45); + Rinsert(root, 35); + + Delete(root, 60); + Display(root); + return 0; +} diff --git a/17_Heap/create_heap.c b/17_Heap/create_heap.c new file mode 100644 index 0000000..e4cce2a --- /dev/null +++ b/17_Heap/create_heap.c @@ -0,0 +1,55 @@ +#include +#include + +void create_heap(int A[], int n) { + int i, temp = A[n]; + i = n; + while(i > 1 && temp > A[i/2]) { + A[i] = A[i/2]; + i = i/2; + } + A[i] = temp; +} + +int delete_heap(int A[], int n) { + int i, temp, j; + int val = A[1]; + A[1] = A[n]; + i = 1; + j = i * 2; + while(j <= n - 1) { + if(A[j+1] > A[j] && j+1 <= n - 1) + j = j + 1; + if(A[i] < A[j]) { + temp = A[i]; + A[i] = A[j]; + A[j] = temp; + i = j; + j = i * 2; + } + else break; + } + A[n] = val; + return val; +} + +int main() { + int A[] = {0, 10, 20, 30, 25, 5, 40, 35}; + create_heap(A, 1); + create_heap(A, 2); + create_heap(A, 3); + create_heap(A, 4); + create_heap(A, 5); + create_heap(A, 6); + create_heap(A, 7); + delete_heap(A, 6); + delete_heap(A, 5); + delete_heap(A, 4); + delete_heap(A, 3); + delete_heap(A, 2); + delete_heap(A, 1); + for(int i = 0; i < 8; i++) { + printf("%d ", A[i]); + } + return 0; +}