├── 01_Essential_c_and_cpp ├── 00_c_and_c++_essential_hand_written_notes.pdf ├── 01_basic_array.cpp ├── 02_structure.cpp ├── 03_pointer.cpp ├── 04_reference.cpp ├── 05_pointer_to_Structure.cpp ├── 06_functions.cpp ├── 07_array_as_parameter.cpp ├── 08_structure_as_parameter.cpp ├── 09_01_monolythic_program.cpp ├── 09_02_modeular_with_structure_programming_style.cpp ├── 09_03_oop_style.cpp ├── 10_rectangle_class_with_oop.cpp └── 11_above_program_as_template_class.cpp ├── 02_Introduction_hand_written_notes.pdf ├── 03_recursion ├── 00_recursion_hand_written_notes.pdf ├── 01_first_recursion_program.cpp ├── 02_static_variable_in_recursion.cpp ├── 03_tree_recursion.cpp ├── 04_indirect_recursion.cpp ├── 05_nested_recursion.cpp ├── 06_sum_of_first_n_natural_number.cpp ├── 07_factorial.cpp ├── 08_exponent.cpp ├── 09_taylor_series.cpp ├── 10_taylor_series_using_horners_rule.cpp ├── 11_fibonacci.cpp ├── 12_ncr.cpp └── 13_toh.cpp ├── 04_Array_representation ├── 00_Array_representation_hand_written_notes.pdf ├── 01_static_array_and_dynamic_array.cpp ├── 02_increasing_array_size.cpp └── 03_2d_array.cpp ├── 05_Array_ADT ├── 00_Array_ADT__Hand_written_notes.pdf ├── 01_creating_array_of_desired_size.cpp ├── 02_arrray_deletion.cpp ├── 03_01_linear_search.cpp ├── 03_02_linerar_search.cpp ├── 04_binary_search.cpp ├── 05_get_set_max_sum.cpp ├── 05_reverse_and_shift_of_an_array.cpp ├── 06_01_inserting_in_sorted_array.cpp ├── 06_02_check_if_soted.cpp ├── 06_03_-ve.cpp ├── 07_merging_array.cpp ├── 08_set.cpp ├── 09_menu.cpp ├── 10_01_finding_mising_element.cpp ├── 10_02_Duplicate.cpp ├── 11_Sum_of_k.cpp └── 12_Max_and_min.cpp ├── 06_string ├── 00_String_handwritten_notes.pdf ├── 01_string.cpp ├── 02_changing_case.cpp ├── 03_vowels_no_of_words.cpp ├── 04_valid.cpp ├── 05_reverse_string.cpp ├── 06_comparing_string.cpp ├── 07_duplicates.cpp ├── 08_duplicates_with_bitwise_operator.cpp ├── 09_anagram.cpp ├── 10_00_Method 1.jpg ├── 10_00_Method 2 using swap function.jpg ├── 10_01_permutation.cpp └── 10_02_permutattion.cpp ├── 07_Matrix ├── 00_Matrix_handwritten_notes.pdf ├── 01_Diagonal_matrix.c ├── 02_Diagonal_matrix_with_c++_class.cpp ├── 03_lower_triangular_matrix.c └── 04_c++_lower_triangular_matrix.cpp ├── 08_sparse_matrix ├── 00_sparse_matrix_handwritten_notes.pdf ├── 01_creation_and_display_of_sparse_matrix.c ├── 02_addition.c ├── 03_sparse_matrix_using_c++.cpp └── 04_polynomial_representation.cpp ├── 09_Linked_list ├── 00_Linked_List_handwritten_notes.pdf ├── 01_display_linked_list.cpp ├── 02_recursively_display_linked_list.cpp ├── 03_count_and_sum.cpp ├── 04_Max_Linked_list.cpp ├── 05_linear_search.cpp ├── 06_Inserting_in_Linked_list.cpp ├── 07_Inserting_in_sorted_linked_list.cpp ├── 08_Deleting_in_linked_list.cpp ├── 09_Sorted_checking.cpp ├── 10_REmove_duplicate_data.cpp ├── 11_Reverse_Linked_list.cpp ├── 12_Concatenate_and_merge_linked_list.cpp ├── 13_isloop.cpp ├── 14_Linked_list_in_class.cpp ├── 15_circular_LinkedList.cpp ├── 16_Insert_circular_linked_lidt.cpp ├── 17_deleting_from_circular_linked_list.cpp ├── 18_doubly_LL.cpp ├── 19_CDoubleLL.cpp └── 20_MiddleNode_and_intersection_of_two_LL.cpp ├── 10_Sparse_matrix_and_polynimial_representation ├── 00_Sparse_matrix_and_polynomial_representation_handwritten_notes.pdf ├── 01_sparse_matrix.cpp └── 02_polynomial_linked_list.cpp ├── 11_Stack ├── 00_stack_handwritten_notes.pdf ├── 01_Stack_using_Arry.cpp ├── 02_Stack_using_LL.cpp ├── 03_parenthesis_is_balanced.cpp ├── 04_parenthesis_is_balanced_extended.cpp ├── 05_infix_to_postfix.cpp ├── 06_infix_to_posstfix.cpp └── 07_postfix_eval.cpp ├── 12_Queue ├── 00_queue_handwritten_notes.pdf ├── 01_enqueue_and_dequeue.cpp ├── 02_circular_queue.cpp └── 03_queue_using_ll.cpp ├── 13_Trees ├── 01_creating_binary_tree.cpp ├── 02_Traversing.cpp ├── 03_LevelOrder.cpp ├── 04_Height_and_count.cpp ├── Node.h ├── Queue.h └── Stack.h ├── 14_BST └── 01_bst.cpp ├── 15_AVL_Tree └── 01_LL_Rotation.cpp ├── 17_Heap └── create_heap.cpp └── README.md /01_Essential_c_and_cpp/00_c_and_c++_essential_hand_written_notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anuragmaurya-code/Data-Structures/8503e041ef5d1d11f411f1ffd277b0972d262c2b/01_Essential_c_and_cpp/00_c_and_c++_essential_hand_written_notes.pdf -------------------------------------------------------------------------------- /01_Essential_c_and_cpp/01_basic_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int a[5]={1,2,3,2,1}; 6 | for(int x:a) 7 | { 8 | x=x*2; 9 | cout< 2 | using namespace std; 3 | struct reportcard 4 | { 5 | char name[20]; 6 | int roll; 7 | int tot_marks; 8 | 9 | }; 10 | 11 | int main() 12 | { 13 | int i; 14 | struct reportcard r[3];//declartion 15 | for(i=0;i<3;i++)//initialization 16 | { 17 | cout<<"Name "; 18 | cin>>r[i].name; 19 | cout<<"roll no "; 20 | cin>>r[i].roll; 21 | cout<<"Total Marks "; 22 | cin>>r[i].tot_marks; 23 | cout< 2 | #include 3 | #include 4 | int main() 5 | { 6 | int a[5]={1,3,5,7,9}; 7 | int *p=&a[0],*p2,*p3;//(declaration) 8 | printf("Stack Memory array %d\n",p[2]);//using pointer p as array a 9 | p2=(int *)malloc(5*sizeof(int));//accessing heap memory using pointer in c (Initialization) 10 | p3=new int(5);//accessing heap memory using pointer in c++ 11 | p2[0]=4; 12 | p3[0]=5; 13 | printf("Heap Memory array %d %d \n",p2[0],p3[0]);//(dereferencing) 14 | free(p2);//dealocating heap memory in c(deallocation) 15 | delete [ ] p3;//dealocating heap memory in c++ 16 | printf("Heap Memory array %d %d ",p2[0],p3[0]); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /01_Essential_c_and_cpp/04_reference.cpp: -------------------------------------------------------------------------------- 1 | //reference only for c++ 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int a=10; 7 | int &ref=a;//declaring and initializing reference variable 8 | printf("%d\n",a); 9 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | struct RECTANGLE 6 | { 7 | int length; 8 | int breath; 9 | /* data */ 10 | }; 11 | int main() 12 | { 13 | struct RECTANGLE r={5,5}; 14 | r.length=10;//editing rectangle value 15 | cout<<"Structure Rectangle in stack and accessed using variable "<length=2;//could even be (*p).length=2 21 | cout<<"Structure Rectangle in stack and accessed using pointer "<length<<" "<breath<length=14; 29 | p2->breath=15; 30 | cout<<"Structure Rectangle in heap and accessed using pointer "<length<<" "<breath< 2 | #include 3 | using namespace std; 4 | void func(int x,int *y,int &z) 5 | { 6 | x++; 7 | (*y)++; 8 | z++; 9 | } 10 | int main() 11 | { 12 | int a=5,b=10,c=15; 13 | 14 | cout<<"\nBefore calling function "< 2 | #include 3 | using namespace std; 4 | struct RECTANGLE 5 | { 6 | int length; 7 | int breath; 8 | }; 9 | void fun1(struct RECTANGLE r1) 10 | { 11 | (r1.length)++; 12 | (r1.breath)++; 13 | } 14 | void fun2(struct RECTANGLE &r1) 15 | { 16 | (r1.length)++; 17 | (r1.breath)++; 18 | } 19 | void fun3(struct RECTANGLE *r1) 20 | { 21 | r1->length++; 22 | r1->breath++; 23 | } 24 | struct RECTANGLE * fun4() 25 | { 26 | struct RECTANGLE *r2; 27 | r2=new RECTANGLE;// or r2=(struct RECTANGLE *)malloc(sizeof(struct RECTANGLE)); 28 | r2->length=1; 29 | r2->breath=2; 30 | return r2; 31 | 32 | } 33 | int main() 34 | { 35 | struct RECTANGLE r={10,5}; 36 | cout<<"Before any call "<length<<" "<< ptr->breath< is to access struct elements using pointer 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /01_Essential_c_and_cpp/09_01_monolythic_program.cpp: -------------------------------------------------------------------------------- 1 | //monolytic style of programing 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int l,b; 8 | cout<<"Enter lenth and breath of rectangle "; 9 | cin>>l>>b; 10 | int area=l*b; 11 | int peri=2*(l+b); 12 | printf("%d is area\n%d is perimeter",area,peri); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /01_Essential_c_and_cpp/09_02_modeular_with_structure_programming_style.cpp: -------------------------------------------------------------------------------- 1 | //modular style of programing along with structure 2 | #include 3 | #include 4 | using namespace std; 5 | struct Rectangle 6 | { 7 | int length; 8 | int breath; 9 | }; 10 | int initialization(struct Rectangle *r,int l,int b) 11 | { 12 | r->length=l; 13 | r->breath=b; 14 | } 15 | int area(struct Rectangle r1) 16 | { 17 | return r1.length*r1.breath; 18 | } 19 | int peri(Rectangle r1) 20 | { 21 | return 2*(r1.length+r1.breath); 22 | } 23 | int main() 24 | { 25 | struct Rectangle r={0,0}; 26 | int l,b; 27 | cout<<"Enter lenth and breath of rectangle "; 28 | cin>>l>>b; 29 | initialization(&r,l,b); 30 | int a=area(r); 31 | int p=peri(r); 32 | printf("%d is area\n%d is perimeter",a,p); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /01_Essential_c_and_cpp/09_03_oop_style.cpp: -------------------------------------------------------------------------------- 1 | //object oriented style of programing 2 | #include 3 | #include 4 | using namespace std; 5 | class Rectangle//class rectangle 6 | { 7 | private: 8 | int length; 9 | int breath; 10 | public: 11 | Rectangle (int l,int b)//constructor of rectangle class ,it gets initalize as soon as class rectangle is declared in main 12 | { 13 | length=l; 14 | breath=b; 15 | } 16 | int area() 17 | { 18 | return length*breath; 19 | } 20 | int peri() 21 | { 22 | return 2*(length+breath); 23 | } 24 | }; 25 | int main() 26 | { 27 | int l,b; 28 | cout<<"Enter lenth and breath of rectangle "; 29 | cin>>l>>b; 30 | Rectangle r(l,b); 31 | int a=r.area(); 32 | int p=r.peri(); 33 | printf("%d is area\n%d is perimeter",a,p); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /01_Essential_c_and_cpp/10_rectangle_class_with_oop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Rectangle 4 | { 5 | private: 6 | int length; 7 | int breadth; 8 | public: 9 | Rectangle()//default constructor if no parameter is passed 10 | { 11 | length=0; 12 | breadth=0; 13 | } 14 | Rectangle(int l,int b); 15 | int area(); 16 | int perimeter();//perimeter ,area,rectangle code could even be written inside class like getlength and others have 17 | void setLength(int l) 18 | { 19 | length=l; 20 | } 21 | void setbreadth(int b) 22 | { 23 | breadth=b; 24 | } 25 | int getLength() 26 | { 27 | return length; 28 | } 29 | int getbreadth() 30 | { 31 | return breadth; 32 | } 33 | ~Rectangle() 34 | { 35 | cout<<"Destructor"; 36 | } 37 | }; 38 | Rectangle::Rectangle(int l,int b) 39 | { 40 | length=l; 41 | breadth=b; 42 | } 43 | int Rectangle::area() 44 | { 45 | return length*breadth; 46 | } 47 | int Rectangle::perimeter() 48 | { 49 | return 2*(length+breadth); 50 | } 51 | int main() 52 | { 53 | Rectangle r(10,4); 54 | int a=r.area(); 55 | int p=r.perimeter(); 56 | printf("area = %d perimerter = %d\n",a,p); 57 | r.setLength(5); 58 | r.setbreadth(4); 59 | a=r.area(); 60 | p=r.perimeter(); 61 | printf("area = %d perimerter = %d\n",a,p); 62 | int l=r.getLength(); 63 | int b=r.getbreadth(); 64 | printf("Length = %d breadth = %d\n",l,b); 65 | return 0; 66 | } -------------------------------------------------------------------------------- /01_Essential_c_and_cpp/11_above_program_as_template_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | class Rectangle 5 | { 6 | private: 7 | T length; 8 | T breadth; 9 | public: 10 | Rectangle(T l,T b); 11 | T area(); 12 | T perimeter();//perimeter ,area,rectangle code could even be written inside class like getlength and others have 13 | void setLength(T l) 14 | { 15 | length=l; 16 | } 17 | void setbreadth(T b) 18 | { 19 | breadth=b; 20 | } 21 | T getLength() 22 | { 23 | return length; 24 | } 25 | T getbreadth() 26 | { 27 | return breadth; 28 | } 29 | ~Rectangle() 30 | { 31 | cout<<"Destructor"; 32 | } 33 | }; 34 | 35 | template 36 | Rectangle::Rectangle(T l,T b) 37 | { 38 | length=l; 39 | breadth=b; 40 | } 41 | 42 | template 43 | T Rectangle::area() 44 | { 45 | return length*breadth; 46 | } 47 | 48 | template 49 | T Rectangle::perimeter() 50 | { 51 | return 2*(length+breadth); 52 | } 53 | 54 | int main() 55 | { 56 | cout<<"INTEGER"< r(10,4); 58 | int a=r.area(); 59 | int p=r.perimeter(); 60 | printf("area = %d perimerter = %d\n",a,p); 61 | r.setLength(5); 62 | r.setbreadth(4); 63 | a=r.area(); 64 | p=r.perimeter(); 65 | printf("area = %d perimerter = %d\n",a,p); 66 | int l=r.getLength(); 67 | int b=r.getbreadth(); 68 | printf("Length = %d breadth = %d\n",l,b); 69 | 70 | 71 | cout< r1(10.0,4.0); 73 | float a1=r1.area(); 74 | float p1=r1.perimeter(); 75 | printf("area = %f perimerter = %f\n",a1,p1); 76 | r1.setLength(5.3); 77 | r1.setbreadth(4.1 ); 78 | a1=r1.area(); 79 | p1=r1.perimeter(); 80 | printf("area = %f perimerter = %f\n",a1,p1); 81 | float l1=r1.getLength(); 82 | float b1=r1.getbreadth(); 83 | printf("Length = %f breadth = %f\n",l1,b1); 84 | return 0; 85 | } -------------------------------------------------------------------------------- /02_Introduction_hand_written_notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anuragmaurya-code/Data-Structures/8503e041ef5d1d11f411f1ffd277b0972d262c2b/02_Introduction_hand_written_notes.pdf -------------------------------------------------------------------------------- /03_recursion/00_recursion_hand_written_notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anuragmaurya-code/Data-Structures/8503e041ef5d1d11f411f1ffd277b0972d262c2b/03_recursion/00_recursion_hand_written_notes.pdf -------------------------------------------------------------------------------- /03_recursion/01_first_recursion_program.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | void funct1(int n) 5 | { 6 | if(n>0) 7 | { 8 | cout< 2 | int y=0;//global variable 3 | using namespace std; 4 | int static_fun(int n) 5 | { 6 | static int x=0;//static variable 7 | if(n>0) 8 | { 9 | x++; 10 | return static_fun(n-1)+x; 11 | } 12 | return 0; 13 | } 14 | int main() 15 | { 16 | int a,b=5; 17 | a=static_fun(b); 18 | cout< 2 | using namespace std; 3 | void fun(int n) 4 | { 5 | if(n>0) 6 | { 7 | cout< 2 | using namespace std; 3 | void fun2(int); 4 | void fun1(int n) 5 | { 6 | if(n>0) 7 | { 8 | cout<0) 16 | { 17 | cout< 2 | using namespace std; 3 | int fun(int n) 4 | { 5 | if(n>100) 6 | return n-10; 7 | else 8 | return fun(fun(n+11)); 9 | } 10 | 11 | int main() 12 | { 13 | 14 | int i=fun(97); 15 | cout< 2 | using namespace std; 3 | int sum(int n) 4 | { 5 | if(n<=0) 6 | return 0; 7 | return n+sum(n-1); 8 | 9 | } 10 | int Isum(int n) 11 | { 12 | int i; 13 | int sum=0; 14 | for (i=0;i<=n;i++) 15 | { 16 | sum=sum+i; 17 | } 18 | return sum; 19 | } 20 | int Isum(int n) 21 | { 22 | int i,factorial=1; 23 | for(i=1;i<=n;i++) 24 | factorial=factorial*i; 25 | return factorial; 26 | 27 | } 28 | int main() 29 | { 30 | int sum=Isum(5); 31 | cout< 2 | using namespace std; 3 | int fac(int n) 4 | { 5 | if(n<=0) 6 | return 1; 7 | else 8 | return n*fac(n-1); 9 | } 10 | int Ifac(int n) 11 | { 12 | int i,factorial=1; 13 | for(i=1;i<=n;i++) 14 | factorial=factorial*i; 15 | return factorial; 16 | 17 | } 18 | 19 | int main() 20 | { 21 | int factorial=Ifac(5); 22 | cout< 2 | using namespace std; 3 | int expo(int m,int n) 4 | { 5 | if(n<=0) 6 | return 1; 7 | return m*expo(m,n-1); 8 | } 9 | int expo1(int m,int n) 10 | { 11 | if(n==0) 12 | return 1; 13 | if(n%2==0) 14 | return expo1(m*m,n/2); 15 | else 16 | return m*expo1(m*m,(n-1)/2); 17 | } 18 | int iexpo(int m,int n) 19 | { 20 | int e=1,i; 21 | for(i=1;i<=n;i++) 22 | { 23 | e=e*m; 24 | } 25 | return e; 26 | } 27 | 28 | int main() 29 | { 30 | int e=expo1(2,3); 31 | cout< 2 | #include 3 | using namespace std; 4 | double tay(int x,int n) 5 | { 6 | static float p=1,f=1; 7 | if(n==0) 8 | return 1; 9 | else 10 | { 11 | double r=tay(x,n-1); 12 | p=p*x; 13 | f=f*n; 14 | return r+(p/f); 15 | } 16 | 17 | } 18 | int main() 19 | { 20 | cout< 2 | 3 | float e1(float x,int n)//using loop 4 | { 5 | float s=1; 6 | for(;n>0;n--) 7 | { 8 | s=1+x/n*s; 9 | } 10 | return s; 11 | } 12 | 13 | float e(float x,int n)//using recursion 14 | { 15 | static float s=1; 16 | if(n==0) 17 | return s; 18 | s=1+x/n*s; 19 | return e(x,n-1); 20 | } 21 | int main() 22 | { 23 | std::cout< 2 | using namespace std; 3 | int ifib1(int n)//fibonacci series using loops/iteration 4 | { 5 | 6 | int t0=0; 7 | int t1=1,s; 8 | for(int i=2;i<=n;i++) 9 | { 10 | s=t1+t0; 11 | t0=t1; 12 | t1=s; 13 | } 14 | return s; 15 | } 16 | int ifib2(int n)//fibonacci series using loops/iteration 17 | { 18 | 19 | int f[n]; 20 | f[0]=0; 21 | f[1]=1; 22 | for(int i=2;i<=n;i++) 23 | { 24 | f[i]=f[i-2]+f[i-1]; 25 | } 26 | return f[n]; 27 | } 28 | int rfib(int n)//fibonacci series using recursion 29 | { 30 | if(n<=1) 31 | return n; 32 | return rfib(n-1)+rfib(n-2); 33 | } 34 | int F[10]; 35 | int mfib(int n)//fibonacci series using recursion using memoization 36 | { 37 | if(n<=1) 38 | return n; 39 | else 40 | { 41 | if(F[n-1]==-1)//it means no value entered till yet as it is -1 42 | F[n-1]=mfib(n-1); 43 | if(F[n-2]==-1) 44 | F[n-2]=mfib(n-2); 45 | F[n]=F[n-1]+F[n-2]; 46 | return F[n-1]+F[n-2]; 47 | } 48 | } 49 | 50 | int main() 51 | { 52 | for(int i=0;i<=10;i++) 53 | F[i]=-1; 54 | cout< 2 | int fact(int n) 3 | { 4 | if(n<=1) 5 | return 1; 6 | else 7 | return fact(n-1)*n; 8 | } 9 | int ncr(int n,int r)//ncr combination using factorial function 10 | { 11 | int num=fact(n); 12 | int den=fact(r)*fact(n-r); 13 | return num/den; 14 | } 15 | 16 | int pncr(int n,int r)//using pascal triangle 17 | { 18 | if((n==1) || (n==r)) 19 | return 1; 20 | return pncr(n-1,r-1)+pncr(n-1,r); 21 | } 22 | int main() 23 | { 24 | std::cout< 2 | void TOH(int n,int A,int B,int C) 3 | { 4 | if(n>0) 5 | { 6 | TOH(n-1,A,C,B); 7 | std::cout<<"Moving from "<0) 14 | { 15 | TOH1(n-1,A,B,D,C); 16 | std::cout<<"("< 2 | #include 3 | int main() 4 | { 5 | int A[5]={1,2,3,4,5};//Static array in stack 6 | int *B=new int[5];// dynamic array in heap 7 | for(int i=0;i<5;i++) 8 | B[i]=i+6; 9 | for(int i=0;i<5;i++) 10 | std::cout< 2 | int main() 3 | { 4 | int *p=new int[5]; 5 | for(int i=0;i<5;i++) 6 | p[i]=i; 7 | int *q=new int [10]; 8 | for(int i=0;i<5;i++) 9 | q[i]=p[i]; 10 | q[6]=456; 11 | p=q;//changing size of array 12 | q=NULL; 13 | for(int i=0;i<10;i++) 14 | std::cout< 2 | #include 3 | int main() 4 | { 5 | int A[2][2];//Method 1:Whole 2D array in stack 6 | A[0][0]=2; 7 | A[0][1]=3; 8 | A[1][0]=4; 9 | A[1][1]=5; 10 | for(int i=0;i<2;i++) 11 | { 12 | for(int j=0;j<2;j++) 13 | std::cout< 2 | using namespace std; 3 | 4 | class Array{ 5 | 6 | private: 7 | int* A; 8 | int size; 9 | int length; 10 | 11 | public: 12 | Array(int size){ 13 | this->size = size; 14 | A = new int [size]; 15 | } 16 | 17 | void create(){ 18 | cout << "Enter number of elements: " << flush; 19 | cin >> length; 20 | cout << "Enter the array elements: " << endl; 21 | for (int i = 0; i < length; i++){ 22 | cout << "Array element: " << i << " = " << flush; 23 | cin >> A[i]; 24 | } 25 | } 26 | void append(int a) 27 | { 28 | if(length=0 && index<=size){ 42 | for(int i=length;i>index;i--) 43 | { 44 | A[i]=A[i-1]; 45 | } 46 | A[index]=value; 47 | length++; 48 | } 49 | } 50 | 51 | ~Array(){//destructor 52 | delete[] A; 53 | cout << "Array destroyed" << endl; 54 | } 55 | }; 56 | 57 | int main() { 58 | 59 | Array arr(10); 60 | arr.create(); 61 | arr.append(2); 62 | arr.insert(3,78); 63 | arr.display(); 64 | 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /05_Array_ADT/02_arrray_deletion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Array{ 5 | 6 | private: 7 | int* A; 8 | int size; 9 | int length; 10 | 11 | public: 12 | Array(int size){ 13 | this->size = size; 14 | A = new int [size]; 15 | } 16 | 17 | void create(){ 18 | cout << "Enter number of elements: " << flush; 19 | cin >> length; 20 | cout << "Enter the array elements: " << endl; 21 | for (int i = 0; i < length; i++){ 22 | cout << "Array element: " << i << " = " << flush; 23 | cin >> A[i]; 24 | } 25 | } 26 | 27 | void display(){ 28 | for (int i = 0; i < length; i++){ 29 | cout << A[i] << " "; 30 | } 31 | std::cout<=0 && index<=length) 36 | { 37 | for(int i=index;i 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[10]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;ilength;i++) 25 | { 26 | if(arr->A[i]==value) 27 | { 28 | swap(&arr->A[i-1],&arr->A[i]); 29 | return i; 30 | } 31 | } 32 | return -1; 33 | } 34 | 35 | int main() 36 | { 37 | struct Array arr={{1,2,3,4,5,6,7},10,7}; 38 | cout< 2 | using namespace std; 3 | 4 | class Array{ 5 | 6 | private: 7 | int* A; 8 | int size; 9 | int length; 10 | 11 | public: 12 | Array(int size){ 13 | this->size = size; 14 | A = new int [size]; 15 | } 16 | 17 | void create(){ 18 | cout << "Enter number of elements: " << flush; 19 | cin >> length; 20 | cout << "Enter the array elements: " << endl; 21 | for (int i = 0; i < length; i++){ 22 | cout << "Array element: " << i << " = " << flush; 23 | cin >> A[i]; 24 | } 25 | } 26 | 27 | void display(){ 28 | for (int i = 0; i < length; i++){ 29 | cout << A[i] << " "; 30 | } 31 | std::cout<0) 40 | { 41 | int t=A[i-1];//Transpose method to improve linear search 42 | A[i-1]=A[i]; 43 | A[i]=t; 44 | } 45 | return i; 46 | } 47 | } 48 | return -1; 49 | } 50 | ~Array(){//destructor 51 | delete[] A; 52 | cout << "Array destroyed" << endl; 53 | } 54 | }; 55 | 56 | int main() { 57 | 58 | Array arr(10); 59 | arr.create(); 60 | arr.display(); 61 | cout<<"element found at index "< 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[10]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;i0) 25 | { 26 | int l=0; 27 | int h=arr.length-1; 28 | while(l<=h) 29 | { 30 | int mid=(l+h)/2; 31 | if(value==arr.A[mid]) 32 | return mid; 33 | else if(arr.A[mid] 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[10]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;i=0 && index<=arr.length ) 25 | return arr.A[index]; 26 | return -1; 27 | } 28 | void Set(struct Array *arr,int index,int value) 29 | { 30 | if(index>=0 && index<=arr->length ) 31 | arr->A[index]=value; 32 | } 33 | int Max(struct Array arr) 34 | { 35 | int max=arr.A[0]; 36 | for(int i=1;imax) 39 | max=arr.A[i]; 40 | } 41 | return max; 42 | } 43 | int Min(struct Array arr) 44 | { 45 | int min=arr.A[0]; 46 | for(int i=1;i (float)Sum(arr)/arr.length 63 | { 64 | float sum=0; 65 | for(int i=0;i=0) 74 | return RSum(arr,n-1)+arr.A[n]; 75 | return 0; 76 | } 77 | int main() 78 | { 79 | struct Array arr={{1,2,3,4,5,6,8},10,7}; 80 | cout< 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[10]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;i=0;i--,j++) 26 | { 27 | arr2[j]=arr.A[i]; 28 | } 29 | for(int i=0;i<(arr.length);i++) 30 | { 31 | arr.A[i]=arr2[i]; 32 | } 33 | Display(arr); 34 | } 35 | 36 | void INT(struct Array *arr)//reversing array using interchanging 37 | { 38 | for(int i=0,j=(arr->length-1);iA[i],&arr->A[j]); 40 | } 41 | void r_shift(struct Array *arr)//right shift 42 | { 43 | for(int j=(arr->length-1);j>0;j--) 44 | arr->A[j]=arr->A[j-1]; 45 | arr->A[0]=0; 46 | 47 | } 48 | 49 | void r_shift_r(struct Array *arr)//right shift roatation 50 | { 51 | for(int i=0;ilength;i++) 52 | { 53 | int temp=arr->A[arr->length-1]; 54 | for(int j=(arr->length-1);j>0;j--) 55 | arr->A[j]=arr->A[j-1]; 56 | arr->A[0]=temp; 57 | } 58 | } 59 | void l_shift(struct Array *arr)//left shift 60 | { 61 | for(int j=1;j<(arr->length);j++) 62 | arr->A[j-1]=arr->A[j]; 63 | arr->A[arr->length-1]=0; 64 | 65 | } 66 | 67 | void l_shift_r(struct Array *arr)//left shift roatation 68 | { 69 | for(int i=1;i<=(arr->length);i++) 70 | { 71 | int temp=arr->A[0]; 72 | for(int j=1;j<(arr->length);j++) 73 | arr->A[j-1]=arr->A[j]; 74 | arr->A[(arr->length)-1]=temp; 75 | } 76 | } 77 | int main() 78 | { 79 | struct Array arr={{1,2,3,4,5,6,8},10,7}; 80 | r_shift_r(&arr); 81 | Display(arr); 82 | return 0; 83 | } -------------------------------------------------------------------------------- /05_Array_ADT/06_01_inserting_in_sorted_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[10]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;ilength-1; 25 | while(i>=0 && arr->A[i]>x) 26 | { 27 | arr->A[i+1]=arr->A[i]; 28 | i--; 29 | } 30 | arr->A[i+1]=x; 31 | arr->length++; 32 | } 33 | 34 | int main() 35 | { 36 | struct Array arr={{1,2,3,4,6,8,10},10,7}; 37 | insert(&arr,7); 38 | Display(arr); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /05_Array_ADT/06_02_check_if_soted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[10]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;iarr.A[i+1]) 28 | return false; 29 | i++; 30 | 31 | } 32 | return true; 33 | } 34 | 35 | int main() 36 | { 37 | struct Array arr={{1,2,3,4,5,8,10},10,7}; 38 | std::cout< 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[10]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;ilength-1; 25 | while(iA[i]<0)i++; 28 | while(arr->A[j]>0)j--; 29 | if(iA[i],&arr->A[j]); 30 | } 31 | } 32 | 33 | int main() 34 | { 35 | struct Array arr={{-1,2,-3,4,-5,8,-10},10,7}; 36 | neg(&arr); 37 | Display(arr); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /05_Array_ADT/07_merging_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[100]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;iA[k++]=arr1.A[i++]; 29 | else 30 | arr3->A[k++]=arr2.A[j++]; 31 | } 32 | for(;iA[k++]=arr1.A[i++]; 34 | for(;jA[k++]=arr2.A[j++]; 36 | } 37 | 38 | int main() 39 | { 40 | struct Array arr1={{1,4,7,13,17,20,36},100,7}; 41 | struct Array arr2={{6,8,9,10,11,24},100,6}; 42 | struct Array arr3={{},arr1.size+arr2.size,arr1.length+arr2.length}; 43 | Merge(arr1,arr2,&arr3); 44 | std::cout< 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[100]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;iarr.A[i+1]) 28 | return false; 29 | i++; 30 | 31 | } 32 | return true; 33 | } 34 | struct Array Union(struct Array arr1,struct Array arr2) 35 | { 36 | struct Array arr3; 37 | if(sort(arr1) && sort(arr2))//if arr1 and arr2 are sorted 38 | { 39 | int i=0,j=0,k=0; 40 | while(i 2 | using namespace std; 3 | 4 | template 5 | class Array{ 6 | private: 7 | T * A; 8 | int size; 9 | int length; 10 | public: 11 | Array() 12 | { 13 | size=10; 14 | length=0; 15 | A=new T[size]; 16 | 17 | } 18 | Array(int size) 19 | { 20 | this->size=size; 21 | length=0; 22 | A=new T[size]; 23 | 24 | } 25 | void insert(int index,T value){ 26 | if(index>=0 && index<=size){ 27 | for(int i=length;i>index;i--) 28 | { 29 | A[i]=A[i-1]; 30 | } 31 | A[index]=value; 32 | length++; 33 | } 34 | } 35 | void append(T x){ 36 | if(length=0 && index<=length) 62 | { 63 | for(int i=index;i 88 | Array* Array::Union(Array arr2) 89 | { 90 | Array* arr3=new Array[10]; 91 | int i=0,j=0,k=0,m=0; 92 | for(i=0;iA[k]=A[i]; 94 | for(i=0;iA[k++]=arr2.A[i]; 109 | } 110 | arr3->length=k; 111 | arr3->size=100; 112 | return arr3; 113 | } 114 | 115 | 116 | 117 | int main() { 118 | int i,sz; 119 | int x; 120 | cout<<"Enter size of array "; 121 | cin>>sz; 122 | Array arr(sz); 123 | int ch; 124 | do{ 125 | cout<<"\nMenu\n"; 126 | cout<<"1.Append\n"; 127 | cout<<"2.Insert\n"; 128 | cout<<"3.Search\n"; 129 | cout<<"4.Sum\n"; 130 | cout<<"5.Display\n"; 131 | cout<<"6.Delete\n"; 132 | cout<<"7.Exit\n"; 133 | 134 | cin>>ch; 135 | switch(ch) 136 | { 137 | case 1: 138 | cout<<"Enter element to appended\n"; 139 | cin>>x; 140 | arr.append(x); 141 | break; 142 | 143 | case 2: 144 | cout<<"Enter element to be inserted\n"; 145 | cin>>x; 146 | cout<<"Enter position of element\n"; 147 | cin>>i; 148 | arr.insert(i,x); 149 | break; 150 | 151 | case 3: 152 | cout<<"Enter element to be searched\n"; 153 | cin>>x; 154 | i=arr.l_search(x); 155 | if(i==-1) 156 | cout<<"Element not found\n"; 157 | else 158 | cout<<"Element found at "<>i; 172 | arr.deletion(i); 173 | break; 174 | 175 | } 176 | }while(ch<7); 177 | 178 | Array a2(10); 179 | a2.append(4); 180 | a2.append(5); 181 | a2.append(6); 182 | Array *a3=arr.Union(a2); 183 | a3->display(); 184 | 185 | return 0; 186 | } -------------------------------------------------------------------------------- /05_Array_ADT/10_01_finding_mising_element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[100]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;i 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[100]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;i1) 54 | // cout<1) 70 | // cout<1) 88 | cout< 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[100]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;i 2 | using namespace std; 3 | struct Array 4 | { 5 | int A[100]; 6 | int size; 7 | int length; 8 | }; 9 | void Display(struct Array arr) 10 | { 11 | for(int i=0;imax) 26 | max=a1.A[i]; 27 | } 28 | cout< 2 | #include 3 | int main() 4 | { 5 | char name1[]="Anurag Maurya"; 6 | char name2[20]; 7 | std::cout<<"Enter your name "; 8 | gets(name2); 9 | printf("ok %s\n",name2); 10 | std::cout< 2 | int main() 3 | { 4 | char name[]="AnuRAG maURyA"; 5 | for(int i=0;name[i]!='\0';i++) 6 | { 7 | if(name[i]>=65 &&name[i]<=90)//if chararcter is uppercase 8 | name[i]=name[i]+32; 9 | else if(name[i]>=97 && name[i]<=122)//if chararcter is lowercase 10 | name[i]=name[i]-32; 11 | } 12 | std::cout< 2 | int main() 3 | { 4 | char sample_word[11]={'T','a','j',' ',' ','m','a','h','a','l','\0'}; 5 | int v_count=0,c_count=0; 6 | for(int i=0;sample_word[i]!='\0';i++) 7 | { 8 | if(sample_word[i]=='a'||sample_word[i]=='e'|| sample_word[i]=='i'|| sample_word[i]=='o' || sample_word[i]=='u' || 9 | sample_word[i]=='A'|| sample_word[i]=='E' || sample_word[i]=='I'|| sample_word[i]=='O' || sample_word[i]=='U') 10 | v_count++;//counting as vowel 11 | 12 | else if(sample_word[i]>=65 && sample_word[i]<=90||sample_word[i]>=97 && sample_word[i]<122) 13 | c_count++;//counting consonent 14 | } 15 | std::cout<<"number of vowels = "< 2 | int main() 3 | { 4 | char word[]="Anurag"; 5 | //method 1 6 | char helper[10]; 7 | int i,j; 8 | for(i=0;word[i]!='\0';i++) 9 | {} 10 | i--; 11 | for(j=0;i>=0;j++,i--) 12 | { 13 | helper[j]=word[i]; 14 | } 15 | helper[j]='\0'; 16 | for(i=0;helper[i]!='\0';i++) 17 | { 18 | word[i]=helper[i]; 19 | } 20 | std::cout< 2 | int main() 3 | { 4 | //comparing two strings 5 | char A[]="Painter"; 6 | char B[]="painter"; 7 | int i,j; 8 | for(i=0,j=0;A[i]!='\0',B[j]!='\0';j++,i++)//checking condition if string ends 9 | { 10 | if(A[i]>=65 && A[i]<=90)//checking if its uppercase then converting to lower case 11 | A[i]=A[i]+32; 12 | else if(B[i]>=65 && B[i]<=90)//checking if its uppercase then converting to lower case 13 | B[i]=B[i]+32; 14 | if(A[i]!=B[j])//checking if they are not equal 15 | break; 16 | } 17 | if(A[i]==B[j]) 18 | std::cout<<"both are equal"<B[j]) 20 | std::cout<<"A is greater"<=0;j++,i--)//copying reverse of word in helper 34 | { 35 | helper[j]=word[i]; 36 | } 37 | helper[j]='\0';//assigining last value with null term 38 | for(i=0;helper[i]!='\0';i++) 39 | { 40 | if(word[i]!=helper[i])//if not equal break out of loop 41 | break; 42 | } 43 | if(word[i]=='\0')//checking if it reached last till last null term 44 | std::cout<<"String is pallindrone"< 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | char a4[10]="Hesllsos";//method 1 using for loop 7 | for(int i=0;a4[i]!='\0';i++) 8 | { 9 | if(a4[i]!=-1) 10 | { 11 | int count=1; 12 | for(int j=i+1;a4[j]!='\0';j++) 13 | { 14 | if(a4[i]==a4[j]) 15 | { 16 | a4[j]=-1; 17 | count++; 18 | } 19 | } 20 | if(count>1) 21 | cout<1) 37 | { 38 | char k=i+97; 39 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | char word[]="Hellohedppd"; 6 | int a=0;//assigning all bits as zero 7 | for(int i=0;word[i]!='\0';i++) 8 | { 9 | int b=1;//assigning 0 bit as 1 i.e turning is on 10 | b=b< 2 | using namespace std; 3 | int main() 4 | { 5 | //Anagram using method 1 i.e for loop 6 | char word1[]="elbow"; 7 | char word2[]="below"; 8 | int i,j; 9 | for(i=0;word1[i]!='\0';i++) 10 | { 11 | for(j=0;word2[j]!='\0';j++) 12 | { 13 | if(word1[i]==word2[j]) 14 | break; 15 | } 16 | if(word2[j]=='\0') 17 | break; 18 | } 19 | if(word1[i]=='\0') 20 | cout<<"Its an anagram "< 2 | #include 3 | using namespace std; 4 | void perm(char s[],int k) 5 | { 6 | static int A[10]={0}; 7 | static char Res[10]; 8 | int i; 9 | if(s[k]=='\0') 10 | { 11 | Res[k]='\0'; 12 | cout< 2 | using namespace std; 3 | void swap(char *c1,char *c2) 4 | { 5 | char t=*c1; 6 | *c1=*c2; 7 | *c2=t; 8 | } 9 | void perm(char s[],int l,int h) 10 | { 11 | if(l==h) 12 | { 13 | cout< 2 | struct matrix 3 | { 4 | int n; 5 | int A[10]; 6 | }; 7 | void set(struct matrix *a,int i,int j,int x) 8 | { 9 | if(i==j) 10 | { 11 | a->A[i-1]=x; 12 | } 13 | 14 | } 15 | int get(struct matrix *a,int i,int j) 16 | { 17 | if(i==j) 18 | a->A[i-1]; 19 | else 20 | return 0; 21 | } 22 | void Display(struct matrix *a) 23 | { 24 | for(int i=0;i<(a->n);i++) 25 | { 26 | for(int j=0;j<(a->n);j++) 27 | { 28 | if(i==j) 29 | printf("%d ",a->A[i]); 30 | else 31 | printf("%d ",0); 32 | } 33 | printf("\n"); 34 | } 35 | } 36 | int main() 37 | { 38 | struct matrix m; 39 | m.n=4; 40 | set(&m,1,1,7); 41 | set(&m,2,2,5); 42 | set(&m,3,3,6); 43 | set(&m,4,4,3); 44 | Display(&m); 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /07_Matrix/02_Diagonal_matrix_with_c++_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Matrix 4 | { 5 | private: 6 | int n; 7 | int *A; 8 | public: 9 | Matrix(int n)//constructor of matrix 10 | { 11 | this->n=n; 12 | A=new int[n]; 13 | } 14 | 15 | void set(int i,int j,int x); 16 | 17 | int get(int i,int j); 18 | 19 | void Display(); 20 | 21 | ~ Matrix(){//destructor destructs/clear heap memory allocated 22 | delete []A; 23 | 24 | } 25 | 26 | }; 27 | void Matrix::set(int i,int j,int x)// :: scope resolution operator to indicate function belong to class matrix 28 | { 29 | if(i==j) 30 | { 31 | A[i-1]=x; 32 | } 33 | 34 | } 35 | int Matrix::get(int i,int j) 36 | { 37 | if(i==j) 38 | return A[i-1]; 39 | else 40 | return 0; 41 | } 42 | void Matrix::Display() 43 | { 44 | for(int i=0;i<(n);i++) 45 | { 46 | for(int j=0;j<(n);j++) 47 | { 48 | if(i==j) 49 | cout< 2 | #include 3 | struct matrix{ //structure of matrix 4 | int n; 5 | int *A; 6 | }; 7 | void set(struct matrix *a,int i,int j,int x)//set for row major mapping 8 | { 9 | 10 | if(i>=j) 11 | { 12 | int t=((i*(i-1)/2)+(j-1));// it means sum of i-1 rows then the jth element in matrix 13 | a->A[t]=x; 14 | } 15 | 16 | } 17 | 18 | int get(struct matrix *a,int i,int j)//get for row major mapping 19 | { 20 | 21 | if(i>=j) 22 | { 23 | int t=((i*(i-1)/2)+(j-1)); 24 | return a->A[t]; 25 | } 26 | else 27 | return 0; 28 | } 29 | void Display(struct matrix *a)//Display for row major mapping 30 | { 31 | for(int i=1;i<=(a->n);i++)//here i is considered as matrix index so is started from 1 32 | { 33 | for(int j=1;j<=(a->n);j++) 34 | { 35 | if(i>=j) 36 | { 37 | int t=((i*(i-1)/2)+(j-1)); 38 | printf("%d ",a->A[t]); 39 | } 40 | else 41 | printf("%d ",0); 42 | } 43 | printf("\n"); 44 | } 45 | } 46 | void cset(struct matrix *a,int i,int j,int x)//set for column major mapping 47 | { 48 | if(i>=j) 49 | { 50 | if(i>=j) 51 | { 52 | a->A[a->n*(j-1)-(j-1)*(j-2)/2+(i-j)]=x; 53 | } 54 | 55 | } 56 | 57 | } 58 | 59 | int cget(struct matrix *a,int i,int j)//get for column major mapping 60 | { 61 | 62 | if(i>=j) 63 | { 64 | return a->A[a->n*(j-1)-(j-1)*(j-2)/2+(i-j)]; 65 | } 66 | else 67 | return 0; 68 | } 69 | void cDisplay(struct matrix *a)//Display column major mapping 70 | { 71 | for(int i=1;i<=(a->n);i++)//here i is considered as matrix index so is started from 1 72 | { 73 | for(int j=1;j<=(a->n);j++) 74 | { 75 | if(i>=j) 76 | { 77 | printf("%d ",a->A[a->n*(j-1)-(j-1)*(j-2)/2+(i-j)]); 78 | } 79 | else 80 | printf("%d ",0); 81 | } 82 | printf("\n"); 83 | } 84 | } 85 | int main() 86 | { 87 | int num,x; 88 | //using row major formula 89 | struct matrix m; 90 | printf("Enter number dimension of matrix "); 91 | scanf("%d",&num); 92 | m.n=num; 93 | m.A=(int *)malloc((m.n*(m.n+1)/2)*sizeof(int));//as it contains this many number of elements 94 | printf("Enter the elements "); 95 | for(int i=1;i<=num;i++) 96 | { 97 | for(int j=1;j<=num;j++) 98 | { 99 | scanf("%d",&x); 100 | set(&m,i,j,x); 101 | } 102 | } 103 | 104 | Display(&m); 105 | printf("\n\n"); 106 | 107 | 108 | //using column major formula 109 | printf("Enter number of elements "); 110 | scanf("%d",&num); 111 | m.n=num; 112 | m.A=(int *)malloc((m.n*(m.n+1)/2)*sizeof(int)); 113 | printf("Enter the elements "); 114 | for(int i=1;i<=num;i++) 115 | { 116 | for(int j=1;j<=num;j++) 117 | { 118 | scanf("%d",&x); 119 | cset(&m,i,j,x); 120 | } 121 | } 122 | 123 | cDisplay(&m); 124 | 125 | return 0; 126 | } -------------------------------------------------------------------------------- /07_Matrix/04_c++_lower_triangular_matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class rMatrix//row major representaion 4 | { 5 | private: 6 | int n; 7 | int *A; 8 | public: 9 | rMatrix(int n)//constructor of matrix 10 | { 11 | this->n=n; 12 | A=new int[n]; 13 | } 14 | 15 | void set(int i,int j,int x); 16 | 17 | int get(int i,int j); 18 | 19 | void Display(); 20 | 21 | ~ rMatrix(){//destructor destructs/clear heap memory allocated 22 | delete []A; 23 | 24 | } 25 | 26 | }; 27 | void rMatrix::set(int i,int j,int x)// :: scope resolution operator to indicate function belong to class matrix 28 | { 29 | if(i>=j) 30 | { 31 | int t=((i*(i-1)/2)+(j-1)); 32 | A[t]=x; 33 | } 34 | } 35 | int rMatrix::get(int i,int j) 36 | { 37 | if(i>=j) 38 | { 39 | int t=((i*(i-1)/2)+(j-1)); 40 | return A[t]; 41 | } 42 | else 43 | return 0; 44 | } 45 | void rMatrix::Display() 46 | { 47 | for(int i=1;i<=(n);i++)//here i is considered as matrix index so is started from 1 48 | { 49 | for(int j=1;j<=(n);j++) 50 | { 51 | if(i>=j) 52 | { 53 | int t=((i*(i-1)/2)+(j-1)); 54 | printf("%d ",A[t]); 55 | } 56 | else 57 | printf("%d ",0); 58 | } 59 | printf("\n"); 60 | } 61 | } 62 | 63 | class cMatrix//row major representaion 64 | { 65 | private: 66 | int n; 67 | int *A; 68 | public: 69 | cMatrix(int n)//constructor of matrix 70 | { 71 | this->n=n; 72 | A=new int[n]; 73 | } 74 | 75 | void cset(int i,int j,int x); 76 | 77 | int cget(int i,int j); 78 | 79 | void cDisplay(); 80 | 81 | ~ cMatrix(){//destructor destructs/clear heap memory allocated 82 | delete []A; 83 | 84 | } 85 | 86 | }; 87 | void cMatrix::cset(int i,int j,int x)// :: scope resolution operator to indicate function belong to class matrix 88 | { 89 | if(i>=j) 90 | { 91 | if(i>=j) 92 | { 93 | A[n*(j-1)-(j-1)*(j-2)/2+(i-j)]=x; 94 | } 95 | 96 | } 97 | } 98 | int cMatrix::cget(int i,int j) 99 | { 100 | if(i>=j) 101 | { 102 | return A[n*(j-1)-(j-1)*(j-2)/2+(i-j)]; 103 | } 104 | else 105 | return 0; 106 | } 107 | void cMatrix::cDisplay() 108 | { 109 | for(int i=1;i<=n;i++)//here i is considered as matrix index so is started from 1 110 | { 111 | for(int j=1;j<=n;j++) 112 | { 113 | if(i>=j) 114 | { 115 | printf("%d ",A[n*(j-1)-(j-1)*(j-2)/2+(i-j)]); 116 | } 117 | else 118 | printf("%d ",0); 119 | } 120 | printf("\n"); 121 | } 122 | } 123 | int main() 124 | { 125 | int num,x; 126 | //using row major formula 127 | 128 | printf("Enter number of elements "); 129 | scanf("%d",&num); 130 | class rMatrix m(num); 131 | printf("Enter the elements "); 132 | for(int i=1;i<=num;i++) 133 | { 134 | for(int j=1;j<=num;j++) 135 | { 136 | scanf("%d",&x); 137 | m.set(i,j,x); 138 | } 139 | } 140 | 141 | m.Display(); 142 | printf("\n\n"); 143 | int num2; 144 | //using column major formula 145 | printf("Enter number of elements "); 146 | scanf("%d",&num2); 147 | class cMatrix m2(num2); 148 | printf("Enter the elements "); 149 | for(int i=1;i<=num2;i++) 150 | { 151 | for(int j=1;j<=num2;j++) 152 | { 153 | scanf("%d",&x); 154 | m2.cset(i,j,x); 155 | } 156 | } 157 | m2.cDisplay(); 158 | 159 | return 0; 160 | } -------------------------------------------------------------------------------- /08_sparse_matrix/00_sparse_matrix_handwritten_notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anuragmaurya-code/Data-Structures/8503e041ef5d1d11f411f1ffd277b0972d262c2b/08_sparse_matrix/00_sparse_matrix_handwritten_notes.pdf -------------------------------------------------------------------------------- /08_sparse_matrix/01_creation_and_display_of_sparse_matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Element{ 4 | int i; 5 | int j; 6 | int x; 7 | }; 8 | struct sparse 9 | { 10 | int m; 11 | int n; 12 | int num; 13 | struct Element *elm; 14 | }; 15 | void create(struct sparse *s) 16 | { 17 | printf("Enter Dimension of sparse natrix "); 18 | scanf("%d%d",&s->m,&s->n); 19 | printf("\nEnter number of non-zero element "); 20 | scanf("%d",&s->num); 21 | s->elm=(struct Element *)malloc(s->num*sizeof(struct Element)); 22 | for(int i=0;inum;i++) 23 | { 24 | printf("\nEnter %d non-zero elememt ",i+1); 25 | scanf("%d%d%d",&s->elm[i].i,&s->elm[i].j,&s->elm[i].x); 26 | } 27 | } 28 | void display(struct sparse s) 29 | { 30 | int k=0; 31 | for(int i=0;i 2 | #include 3 | struct Element{ 4 | int i; 5 | int j; 6 | int x; 7 | }; 8 | struct sparse 9 | { 10 | int m; 11 | int n; 12 | int num; 13 | struct Element *elm; 14 | }; 15 | void create(struct sparse *s) 16 | { 17 | printf("Enter Dimension of sparse matrix "); 18 | scanf("%d%d",&s->m,&s->n); 19 | printf("\nEnter number of non-zero element "); 20 | scanf("%d",&s->num); 21 | s->elm=(struct Element *)malloc(s->num*sizeof(struct Element)); 22 | for(int i=0;inum;i++) 23 | { 24 | printf("\nEnter %d non-zero elememt ",i+1); 25 | scanf("%d%d%d",&s->elm[i].i,&s->elm[i].j,&s->elm[i].x); 26 | } 27 | } 28 | void display(struct sparse s) 29 | { 30 | int k=0; 31 | for(int i=0;im!=s2->m || s1->n!=s2->n) 48 | return 0; 49 | struct sparse *sum=(struct sparse *)malloc(sizeof(struct sparse)); 50 | sum->m=s1->m; 51 | sum->n=s1->n; 52 | sum->elm=(struct Element *)malloc(sizeof(struct Element)*s1->num+s2->num); 53 | while (inum && jnum) 54 | { 55 | if(s1->elm[i].ielm[j].i)// row of s1 is less than s2 56 | sum->elm[k++]=s1->elm[i++]; 57 | 58 | else if(s1->elm[i].i>s2->elm[j].i)//row of s2 is less than s1 59 | sum->elm[k++]=s2->elm[j++]; 60 | 61 | else 62 | if(s1->elm[i].jelm[j].j)//row of s1 and s2 is same but column of s1 is less than s2 63 | sum->elm[k++]=s1->elm[i++]; 64 | 65 | else if(s1->elm[i].j>s2->elm[j].j)//row of s1 and s2 is same but column of s2 is less than s1 66 | sum->elm[k++]=s2->elm[j++]; 67 | 68 | else//row and column both are same 69 | { 70 | sum->elm[k]=s1->elm[i++];//copy comple s1 struct elm elements 71 | sum->elm[k++].x=sum->elm[k].x+s2->elm[j++].x;//add s1 and s2 values 72 | } 73 | } 74 | //remaining elements 75 | for(;inum;i++) 76 | sum->elm[k++]=s1->elm[i]; 77 | 78 | for(;jnum;j++) 79 | sum->elm[k++]=s2->elm[j]; 80 | 81 | sum->num=k; 82 | return sum; 83 | 84 | } 85 | int main() 86 | { 87 | struct sparse s1; 88 | create(&s1); 89 | display(s1); 90 | struct sparse s2; 91 | create(&s2); 92 | display(s2); 93 | struct sparse *sum=add(&s1,&s2); 94 | printf("\n\n"); 95 | display(*sum); 96 | 97 | return 0; 98 | } -------------------------------------------------------------------------------- /08_sparse_matrix/03_sparse_matrix_using_c++.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Element 4 | { 5 | public: 6 | int i; 7 | int j; 8 | int x; 9 | }; 10 | class Sparse 11 | { 12 | private: 13 | int m; 14 | int n; 15 | int num; 16 | Element *elm; 17 | public: 18 | Sparse(int m,int n,int num) 19 | { 20 | this->m=m; 21 | this->n=n; 22 | this->num=num; 23 | elm=new Element[this->num]; 24 | } 25 | friend istream & operator >>(istream &is,Sparse &s);// friend is a global function 26 | friend ostream & operator << (ostream &os,Sparse &s); 27 | Sparse operator+(Sparse &s); 28 | }; 29 | Sparse Sparse::operator+(Sparse &s) 30 | { 31 | 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 32 | // j for s2 and k for sum 33 | Sparse *sum=new Sparse(m,n,num+s.num); 34 | sum->elm=new Element[num+s.num]; 35 | while (ielm[k++]=elm[i++]; 39 | 40 | else if(elm[i].i>s.elm[j].i)//row of s2 is less than s1 41 | sum->elm[k++]=s.elm[j++]; 42 | 43 | else 44 | { 45 | if(elm[i].jelm[k++]=elm[i++]; 47 | 48 | else if(elm[i].j>s.elm[j].j)//row of s1 and s2 is same but column of s2 is less than s1 49 | sum->elm[k++]=s.elm[j++]; 50 | 51 | else//row and column both are same 52 | { 53 | sum->elm[k]=elm[i];//copy comple s1 struct elm elements 54 | sum->elm[k++].x=s.elm[j++].x+elm[i++].x;//add s1 and s2 values 55 | } 56 | } 57 | 58 | } 59 | //remaining elements 60 | for(;ielm[k++]=elm[i]; 62 | 63 | for(;jnum;j++) 64 | sum->elm[k++]=s.elm[j]; 65 | 66 | sum->num=k; 67 | return *sum; 68 | } 69 | istream & operator >> (istream &is,Sparse &s) 70 | { 71 | cout<<"Enter non-zero element\n"; 72 | int i; 73 | for(i=0;i>s.elm[i].i>>s.elm[i].j>>s.elm[i].x; 76 | } 77 | return is; 78 | } 79 | ostream & operator << (ostream &os,Sparse &s) 80 | { 81 | int i,j; 82 | int a; 83 | a=0; 84 | cout<>s1; 105 | cout<>s2; 108 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | struct Term 6 | { 7 | int coeff; 8 | int expo; 9 | }; 10 | struct polynomial 11 | { 12 | int n; 13 | struct Term *t; 14 | }; 15 | void create(struct polynomial * p) 16 | { 17 | cout<<"Enter number of elements "; 18 | cin>>p->n; 19 | cout<t=new struct Term [p->n]; 21 | for(int i=0;in;i++) 22 | { 23 | cout<<"Enter "<>p->t[i].coeff; 25 | cout<<"Enter "<>p->t[i].expo; 27 | cout<n;i++) 33 | { 34 | cout<t[i].coeff<<"x"<t[i].expo; 35 | if(i!=(p->n)-1) 36 | cout<<" + "; 37 | } 38 | cout<n + p2->n]; 45 | while(in && jn) 46 | { 47 | if(p1->t[i].expo>p2->t[j].expo) 48 | sum.t[k++]=p1->t[i++]; 49 | else if(p2->t[j].expo>p1->t[i].expo) 50 | sum.t[k++]=p2->t[j++]; 51 | else 52 | { 53 | sum.t[k].expo=p1->t[i].expo; 54 | sum.t[k++].coeff=p1->t[i++].coeff+p2->t[j++].coeff; 55 | } 56 | } 57 | for(;in;i++) 58 | sum.t[k++]=p1->t[i]; 59 | for(;jn;j++) 60 | sum.t[k++]=p2->t[j]; 61 | sum.n=k; 62 | return sum; 63 | } 64 | int main() 65 | { 66 | struct polynomial p1,p2,sum; 67 | create(&p1); 68 | display(&p1); 69 | create(&p2); 70 | display(&p2); 71 | sum=addition(&p1,&p2); 72 | display(&sum); 73 | return 0; 74 | } -------------------------------------------------------------------------------- /09_Linked_list/00_Linked_List_handwritten_notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anuragmaurya-code/Data-Structures/8503e041ef5d1d11f411f1ffd277b0972d262c2b/09_Linked_list/00_Linked_List_handwritten_notes.pdf -------------------------------------------------------------------------------- /09_Linked_list/01_display_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t;//putting address of current node in the next of last node 26 | last=t;//assigning current node as last node 27 | } 28 | } 29 | 30 | void Display() 31 | { 32 | struct Node *p; 33 | p=first; 34 | while(p!=NULL) 35 | { 36 | cout<data<next; 38 | } 39 | } 40 | 41 | int main() 42 | { 43 | int A[]={4,5,8,9,7}; 44 | create(A,5); 45 | Display(); 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /09_Linked_list/02_recursively_display_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t; //putting address of current node in the next of last node 26 | last=t; //assigning current node as last node 27 | } 28 | } 29 | 30 | void Display(struct Node *p) //display using recurssion 31 | { 32 | if(p!=NULL) 33 | { 34 | cout<data<next); 37 | } 38 | void RDisplay(struct Node *p)// reverse display using recurssion 39 | { 40 | if(p!=NULL) 41 | { 42 | RDisplay(p->next); 43 | cout<data< 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t; //putting address of current node in the next of last node 26 | last=t; //assigning current node as last node 27 | } 28 | } 29 | void count()//counting of nodes 30 | { 31 | int c=0; 32 | struct Node *p; 33 | p=first; 34 | while(p) 35 | { 36 | c++; 37 | p=p->next; 38 | } 39 | cout<<" Count is "<next)+1; 45 | return 0; 46 | } 47 | void sum()// sum of data of nodes 48 | { 49 | int s=0; 50 | struct Node *p; 51 | p=first; 52 | while(p) 53 | { 54 | s=s+p->data; 55 | p=p->next; 56 | } 57 | cout<<" sum is "<data+Rsum(p->next); 63 | return 0; 64 | } 65 | 66 | int main() 67 | { 68 | int A[]={4,5,8,9,7,1,9,4,7}; 69 | create(A,9); 70 | count(); 71 | cout<<"count is : "< 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t; //putting address of current node in the next of last node 26 | last=t; //assigning current node as last node 27 | } 28 | } 29 | 30 | int Max(struct Node * p) 31 | { 32 | int max=INT32_MIN; 33 | while (p!=NULL) 34 | { 35 | if(maxdata) 36 | max=p->data; 37 | p=p->next; 38 | } 39 | return max; 40 | 41 | } 42 | 43 | int RMax(struct Node * p) 44 | { 45 | int x=0; 46 | if(p==0) 47 | return INT32_MIN; 48 | else 49 | { 50 | x=RMax(p->next); 51 | if(x>p->data) 52 | return x; 53 | else 54 | return p->data; 55 | } 56 | 57 | 58 | } 59 | 60 | int main() 61 | { 62 | int A[]={4,5,78,8,94,7222}; 63 | create(A,6); 64 | cout< 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t;//putting address of current node in the next of last node 26 | last=t;//assigning current node as last node 27 | } 28 | } 29 | 30 | void Display() 31 | { 32 | struct Node *p; 33 | p=first; 34 | while(p!=NULL) 35 | { 36 | cout<data<next; 38 | } 39 | } 40 | 41 | struct Node *Linear_Search(struct Node *p,int key) 42 | { 43 | while(p!=NULL) 44 | { 45 | if(p->data == key) 46 | return p; 47 | p=p->next; 48 | 49 | } 50 | return NULL; 51 | } 52 | 53 | struct Node *RLinear_Search(struct Node *p,int key) 54 | { 55 | if(p==NULL) 56 | return NULL; 57 | else if(p->data == key) 58 | return p; 59 | else 60 | return RLinear_Search(p->next,key); 61 | 62 | } 63 | 64 | struct Node *ILinear_Search(struct Node *p,int key) 65 | { 66 | struct Node *q; 67 | while(p!=NULL) 68 | { 69 | if(p->data == key) 70 | { 71 | if(p==first)// very important as in case if the first element is same to key there 72 | // wont be any value to q 73 | return p; 74 | else 75 | { 76 | q->next=p->next; 77 | p->next=first; 78 | first=p; 79 | } 80 | return p; 81 | } 82 | q=p; 83 | p=p->next; 84 | } 85 | return NULL; 86 | } 87 | 88 | 89 | int main() 90 | { 91 | int A[]={4,5,8,9,7}; 92 | create(A,5); 93 | struct Node *temp; 94 | temp=ILinear_Search(first,8); 95 | if(temp) 96 | cout<<"Key founded "<data< 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | int count(struct Node *p)//counting of nodes 12 | { 13 | int c=0; 14 | 15 | p=first; 16 | while(p) 17 | { 18 | c++; 19 | p=p->next; 20 | } 21 | return c; 22 | } 23 | 24 | void create(int A[],int n) 25 | { 26 | int i; 27 | struct Node *last, *t; 28 | first=new Node; 29 | first->data=A[0]; 30 | first->next=NULL; 31 | last=first;//addreess is passed and now first is refferd as last 32 | 33 | for(int i=1;idata=A[i]; 37 | t->next=NULL; 38 | last->next=t;//putting address of current node in the next of last node 39 | last=t;//assigning current node as last node 40 | } 41 | } 42 | 43 | void Display() 44 | { 45 | struct Node *p; 46 | p=first; 47 | while(p!=NULL) 48 | { 49 | cout<data<next; 51 | } 52 | } 53 | 54 | 55 | void insert(int pos ,int value) 56 | { 57 | if(pos<0 || pos>count(first)) 58 | return ; 59 | 60 | struct Node * p; 61 | struct Node *t; 62 | p=first; 63 | t=new Node; 64 | if(pos==0) 65 | { 66 | t->data=value; 67 | t->next=first; 68 | first=t; 69 | } 70 | else if(pos>0) 71 | { 72 | for(int i=0;inext; 76 | } 77 | t->data=value; 78 | t->next=p->next; 79 | p->next=t; 80 | } 81 | } 82 | int main() 83 | { 84 | int A[]={4,5,8,9,7}; 85 | create(A,5); 86 | cout<< " Before insert"< 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t;//putting address of current node in the next of last node 26 | last=t;//assigning current node as last node 27 | } 28 | } 29 | 30 | void Display() 31 | { 32 | struct Node *p; 33 | p=first; 34 | while(p!=NULL) 35 | { 36 | cout<data<next; 38 | } 39 | } 40 | 41 | void S_Insert(struct Node *p,int value) 42 | { 43 | struct Node *q; 44 | struct Node *t; 45 | t=new Node; 46 | t->data=value; 47 | t->next=NULL; 48 | 49 | if(first==NULL) 50 | first=t; 51 | else 52 | { 53 | while (p && p->datanext; 57 | } 58 | if(p==first) 59 | { 60 | t->next=first; 61 | first=t; 62 | } 63 | else{ 64 | q->next=t; 65 | t->next=p; 66 | } 67 | 68 | } 69 | 70 | } 71 | 72 | int main() 73 | { 74 | int A[]={4,7,9,12,18}; 75 | create(A,5); 76 | S_Insert(first,8); 77 | Display(); 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /09_Linked_list/08_Deleting_in_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t;//putting address of current node in the next of last node 26 | last=t;//assigning current node as last node 27 | } 28 | } 29 | 30 | int count(struct Node *p)//counting of nodes 31 | { 32 | int c=0; 33 | 34 | p=first; 35 | while(p) 36 | { 37 | c++; 38 | p=p->next; 39 | } 40 | return c; 41 | } 42 | 43 | void Display() 44 | { 45 | struct Node *p; 46 | p=first; 47 | while(p!=NULL) 48 | { 49 | cout<data<next; 51 | } 52 | } 53 | 54 | int Delete(struct Node *p,int index) 55 | { 56 | struct Node *q=NULL; 57 | int x=-1;//to store data of deleted node 58 | if(index<0 || index>count(p)) 59 | return -1; 60 | if(index==1) 61 | { 62 | x=first->data; 63 | first=first->next; 64 | } 65 | else 66 | { 67 | for(int i=0;inext; 71 | } 72 | x=p->data; 73 | q->next=p->next; 74 | } 75 | 76 | delete p; 77 | return x; 78 | } 79 | int main() 80 | { 81 | int A[]={4,7,9,12,18}; 82 | create(A,5); 83 | cout<<"Deleted : "< 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t;//putting address of current node in the next of last node 26 | last=t;//assigning current node as last node 27 | } 28 | } 29 | 30 | void Display() 31 | { 32 | struct Node *p; 33 | p=first; 34 | while(p!=NULL) 35 | { 36 | cout<data<next; 38 | } 39 | } 40 | 41 | bool isSorted(struct Node *p) 42 | { 43 | int n=INT32_MIN; 44 | while(p!=NULL) 45 | { 46 | if(p->datadata; 49 | p=p->next; 50 | } 51 | return true; 52 | } 53 | 54 | int main() 55 | { 56 | int A[]={4,7,19,12,18}; 57 | create(A,5); 58 | cout<<"Sorted : "< 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t;//putting address of current node in the next of last node 26 | last=t;//assigning current node as last node 27 | } 28 | } 29 | 30 | void Display() 31 | { 32 | struct Node *p; 33 | p=first; 34 | while(p!=NULL) 35 | { 36 | cout<data<next; 38 | } 39 | } 40 | 41 | void Remove_Duplicate(struct Node *p) 42 | { 43 | struct Node *q; 44 | q=new Node; 45 | q=p->next; 46 | while(q!=NULL) 47 | { 48 | if(p->data!=q->data) 49 | { 50 | p=q; 51 | q=q->next; 52 | } 53 | else 54 | { 55 | p->next=q->next; 56 | delete q; 57 | q=p->next; 58 | } 59 | } 60 | 61 | } 62 | 63 | int main() 64 | { 65 | int A[]={4,7,7,7,12,12,15,18}; 66 | create(A,8); 67 | cout<<"before\n"; 68 | Display(); 69 | cout<<"After\n"; 70 | Remove_Duplicate(first); 71 | Display(); 72 | 73 | 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /09_Linked_list/11_Reverse_Linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t;//putting address of current node in the next of last node 26 | last=t;//assigning current node as last node 27 | } 28 | } 29 | 30 | void Display() 31 | { 32 | struct Node *p; 33 | p=first; 34 | while(p!=NULL) 35 | { 36 | cout<data<next; 38 | } 39 | } 40 | 41 | int count(struct Node *p)//counting of nodes 42 | { 43 | int c=0; 44 | 45 | p=first; 46 | while(p) 47 | { 48 | c++; 49 | p=p->next; 50 | } 51 | return c; 52 | } 53 | 54 | void reverse1(struct Node *p)//Reverse using (Auxiliary)array 55 | { 56 | int *A,i=0; 57 | struct Node *q; 58 | A=new int[count(p)]; 59 | q=p; 60 | while(q!=NULL) 61 | { 62 | A[i]=q->data; 63 | q=q->next; 64 | i++; 65 | } 66 | q=p; 67 | i--; 68 | while(q!=NULL) 69 | { 70 | q->data=A[i]; 71 | q=q->next; 72 | i--; 73 | } 74 | 75 | } 76 | 77 | void reverse2(struct Node *p)//reversing by reversing the links 78 | { 79 | struct Node *q=NULL,*r=NULL; 80 | while(p!=NULL) 81 | { 82 | r=q; 83 | q=p; 84 | p=p->next; 85 | q->next=r; 86 | } 87 | first=q;//make first the q as links are reveresed at q is at the last node after loop 88 | } 89 | 90 | void reverse3(struct Node *q,struct Node *p) 91 | { 92 | if(p!=NULL) 93 | { 94 | reverse3(p,p->next); 95 | p->next=q; 96 | } 97 | else 98 | { 99 | first=q; 100 | } 101 | } 102 | 103 | 104 | 105 | int main() 106 | { 107 | int A[]={4,7,19,12,15,18}; 108 | create(A,6); 109 | cout<<"Before"< 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL,*second=NULL,*third=NULL; 10 | 11 | void create1(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t;//putting address of current node in the next of last node 26 | last=t;//assigning current node as last node 27 | } 28 | } 29 | void create2(int A[],int n) 30 | { 31 | int i; 32 | struct Node *last, *t; 33 | second=new Node; 34 | second->data=A[0]; 35 | second->next=NULL; 36 | last=second;//addreess is passed and now first is refferd as last 37 | 38 | for(int i=1;idata=A[i]; 42 | t->next=NULL; 43 | last->next=t;//putting address of current node in the next of last node 44 | last=t;//assigning current node as last node 45 | } 46 | } 47 | 48 | void Display(struct Node *p) 49 | { 50 | 51 | while(p!=NULL) 52 | { 53 | cout<data<next; 55 | } 56 | } 57 | 58 | 59 | void Concatenate(struct Node *p,struct Node *q) 60 | { 61 | third=p; 62 | while(p->next) 63 | p=p->next; 64 | p->next=q; 65 | } 66 | 67 | void Merge(struct Node *p,struct Node *q) 68 | { 69 | struct Node *last; 70 | if(p->data< q->data) 71 | { 72 | third=last=p; 73 | p=p->next; 74 | third->next=NULL; 75 | } 76 | else{ 77 | third=last=q; 78 | q=q->next; 79 | third->next=NULL; 80 | } 81 | while (p && q) 82 | { 83 | if(p->data < q->data) 84 | { 85 | last->next=p; 86 | last=p; 87 | p=p->next; 88 | last->next=NULL; 89 | } 90 | else 91 | { 92 | last->next=q; 93 | last=q; 94 | q=q->next; 95 | last->next=NULL; 96 | } 97 | } 98 | if(p) 99 | last->next=p; 100 | else 101 | last->next=q; 102 | 103 | } 104 | int main() 105 | { 106 | int A[]={4,7,17,21,25,30}; 107 | int B[]={5,15,19,23,27,34,37}; 108 | create1(A,6); 109 | cout<<"First"< 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }*first=NULL; 10 | 11 | void create(int A[],int n) 12 | { 13 | int i; 14 | struct Node *last, *t; 15 | first=new Node; 16 | first->data=A[0]; 17 | first->next=NULL; 18 | last=first;//addreess is passed and now first is refferd as last 19 | 20 | for(int i=1;idata=A[i]; 24 | t->next=NULL; 25 | last->next=t;//putting address of current node in the next of last node 26 | last=t;//assigning current node as last node 27 | } 28 | } 29 | 30 | void Display() 31 | { 32 | struct Node *p; 33 | p=first; 34 | while(p!=NULL) 35 | { 36 | cout<data<next; 38 | } 39 | } 40 | bool isloop(struct Node *f) 41 | { 42 | struct Node *p,*q; 43 | p=q=f; 44 | do 45 | { 46 | p=p->next; 47 | q=q->next; 48 | q=q->next!=NULL?q->next:NULL; 49 | } while (p && q && p!=q); 50 | 51 | return p==q?true:false; 52 | } 53 | 54 | int main() 55 | { 56 | struct Node *p,*q; 57 | int A[]={4,7,19,12,15,18}; 58 | create(A,6); 59 | p=first->next->next; 60 | q=first->next->next->next->next->next; 61 | q->next=p; 62 | if(isloop(first)) 63 | { 64 | cout<<"there\'s a loop"; 65 | } 66 | else 67 | cout<<"Theres not a loop"; 68 | 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /09_Linked_list/14_Linked_list_in_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | class Node{ 5 | public: 6 | int data; 7 | Node *next; 8 | }; 9 | 10 | class LinkedList 11 | { 12 | private: 13 | Node *first; 14 | public: 15 | LinkedList() 16 | { 17 | first=NULL; 18 | } 19 | LinkedList(int A[],int n); 20 | void display(); 21 | int length(); 22 | void insert(int pos ,int value); 23 | int del(int index); 24 | ~LinkedList(); 25 | 26 | }; 27 | 28 | LinkedList::~LinkedList() 29 | { 30 | Node *p=first; 31 | while(first) 32 | { 33 | first=first->next; 34 | delete p; 35 | p=first; 36 | } 37 | } 38 | int LinkedList::del(int index) 39 | { 40 | Node *q=NULL,*p=first; 41 | int x=-1;//to store data of deleted node 42 | if(index<0 || index>length()) 43 | return -1; 44 | if(index==0) 45 | { 46 | x=first->data; 47 | first=first->next; 48 | } 49 | else 50 | { 51 | for(int i=0;inext; 55 | } 56 | x=p->data; 57 | q->next=p->next; 58 | } 59 | 60 | delete p; 61 | return x; 62 | } 63 | LinkedList::LinkedList(int A[],int n) 64 | { 65 | int i; 66 | Node *last, *t; 67 | first=new Node; 68 | first->data=A[0]; 69 | first->next=NULL; 70 | last=first;//addreess is passed and now first is refferd as last 71 | 72 | for(int i=1;idata=A[i]; 76 | t->next=NULL; 77 | last->next=t;//putting address of current node in the next of last node 78 | last=t;//assigning current node as last node 79 | } 80 | } 81 | int LinkedList::length()//counting of nodes 82 | { 83 | int c=0; 84 | Node *p; 85 | p=first; 86 | while(p) 87 | { 88 | c++; 89 | p=p->next; 90 | } 91 | return c; 92 | } 93 | void LinkedList::display() 94 | { 95 | Node *p; 96 | p=first; 97 | while(p!=NULL) 98 | { 99 | cout<data<next; 101 | } 102 | } 103 | void LinkedList::insert(int pos ,int value) 104 | { 105 | if(pos<0 || pos>length()) 106 | return ; 107 | 108 | Node * p; 109 | Node *t; 110 | p=first; 111 | t=new Node; 112 | if(pos==0) 113 | { 114 | t->data=value; 115 | t->next=first; 116 | first=t; 117 | } 118 | else if(pos>0) 119 | { 120 | for(int i=0;inext; 124 | } 125 | t->data=value; 126 | t->next=p->next; 127 | p->next=t; 128 | } 129 | } 130 | int main() 131 | { 132 | int A[]={1,6,8,12,56}; 133 | LinkedList l(A,5); 134 | 135 | l.insert(2,67); 136 | 137 | l.display(); 138 | 139 | cout<<"lenght "< 2 | #include 3 | using namespace std; 4 | class Node{ 5 | public: 6 | int data; 7 | Node *next; 8 | }; 9 | 10 | class CircularLinkedList{ 11 | private: 12 | Node *Head; 13 | public: 14 | CircularLinkedList(int A[],int n); 15 | Node * getHead(){return Head;} 16 | void display(); 17 | void Rdisplay(Node *p); 18 | }; 19 | void CircularLinkedList::Rdisplay(Node *p) 20 | { 21 | static int flag=0; 22 | if(p!=Head || flag==0) 23 | { 24 | flag=1; 25 | cout<data<<"->"; 26 | Rdisplay(p->next); 27 | } 28 | flag=0; 29 | } 30 | void CircularLinkedList::display() 31 | { 32 | Node *p=Head; 33 | do{ 34 | cout<data<<"->"; 35 | p=p->next; 36 | 37 | }while(p!=Head); 38 | cout<data=A[0]; 46 | Head->next=Head; 47 | last=Head; 48 | for(int i=1;idata=A[i]; 52 | t->next=last->next; 53 | last->next=t; 54 | last=t; 55 | } 56 | } 57 | 58 | int main() 59 | { 60 | 61 | int A[]={1,3,5,8,9}; 62 | 63 | CircularLinkedList l(A,5); 64 | l.display(); 65 | Node *h=l.getHead(); 66 | l.Rdisplay(h); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /09_Linked_list/16_Insert_circular_linked_lidt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Node{ 5 | public: 6 | int data; 7 | Node *next; 8 | }; 9 | 10 | class CircularLinkedList{ 11 | private: 12 | Node *Head; 13 | public: 14 | CircularLinkedList(){Head=NULL;} 15 | CircularLinkedList(int A[],int n); 16 | Node * getHead(){return Head;} 17 | void display(); 18 | void Rdisplay(Node *p); 19 | int length(); 20 | void insert(int index,int value); 21 | }; 22 | void CircularLinkedList::insert(int index,int value) 23 | { 24 | if(index<0 || index>length()) 25 | return ; 26 | Node *p=Head,*t; 27 | t=new Node; 28 | t->data=value; 29 | if(index==0) 30 | { 31 | 32 | if(Head==NULL) 33 | { 34 | t->next=t; 35 | Head=t; 36 | } 37 | else 38 | { 39 | while(p->next!=Head) 40 | p=p->next; 41 | t->next=p->next; 42 | p->next=t; 43 | 44 | } 45 | } 46 | else{ 47 | for(int i=0;inext; 49 | t->next=p->next; 50 | p->next=t; 51 | } 52 | } 53 | int CircularLinkedList::length() 54 | { 55 | Node *p=Head; 56 | int l=0; 57 | do 58 | { 59 | l++; 60 | p=p->next; 61 | }while(p!=Head); 62 | return l; 63 | 64 | } 65 | void CircularLinkedList::Rdisplay(Node *p) 66 | { 67 | static int flag=0; 68 | if(p!=Head || flag==0) 69 | { 70 | flag=1; 71 | cout<data<<"->"; 72 | Rdisplay(p->next); 73 | } 74 | flag=0; 75 | } 76 | void CircularLinkedList::display() 77 | { 78 | Node *p=Head; 79 | do{ 80 | cout<data<<"->"; 81 | p=p->next; 82 | 83 | }while(p!=Head); 84 | cout<data=A[0]; 92 | Head->next=Head; 93 | last=Head; 94 | for(int i=1;idata=A[i]; 98 | t->next=last->next; 99 | last->next=t; 100 | last=t; 101 | } 102 | } 103 | 104 | int main() 105 | { 106 | 107 | int A[]={1,3,5,8,9}; 108 | CircularLinkedList l(A,5); 109 | cout<<"Before :"; 110 | l.display(); 111 | l.insert(1,2); 112 | cout<<"After : "; 113 | l.display(); 114 | return 0; 115 | } 116 | -------------------------------------------------------------------------------- /09_Linked_list/17_deleting_from_circular_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Node{ 5 | public: 6 | int data; 7 | Node *next; 8 | }; 9 | 10 | class CircularLinkedList{ 11 | private: 12 | Node *Head; 13 | public: 14 | CircularLinkedList(){Head=NULL;} 15 | CircularLinkedList(int A[],int n); 16 | Node * getHead(){return Head;} 17 | void display(); 18 | void Rdisplay(Node *p); 19 | int length(); 20 | void insert(int index,int value); 21 | int Delete(int index); 22 | }; 23 | int CircularLinkedList::Delete(int index) 24 | { 25 | if(index<0 || index>length()) 26 | return -1; 27 | Node *p=Head,*q; 28 | int x,i; 29 | if(index==1) 30 | { 31 | while(p->next!=Head) 32 | p=p->next; 33 | if(p==Head) 34 | { 35 | x=p->data; 36 | delete p; 37 | Head=NULL; 38 | return x; 39 | } 40 | x=Head->data; 41 | p->next=Head->next; 42 | delete Head; 43 | Head=p->next; 44 | return x; 45 | } 46 | else{ 47 | for(i=0;inext; 49 | q=p->next; 50 | x=q->data; 51 | p->next=q->next; 52 | delete q; 53 | return x; 54 | } 55 | } 56 | void CircularLinkedList::insert(int index,int value) 57 | { 58 | if(index<0 || index>length()) 59 | return ; 60 | Node *p=Head,*t; 61 | t=new Node; 62 | t->data=value; 63 | if(index==0) 64 | { 65 | 66 | if(Head==NULL) 67 | { 68 | t->next=t; 69 | Head=t; 70 | } 71 | else 72 | { 73 | while(p->next!=Head) 74 | p=p->next; 75 | t->next=p->next; 76 | p->next=t; 77 | 78 | } 79 | } 80 | else{ 81 | for(int i=0;inext; 83 | t->next=p->next; 84 | p->next=t; 85 | } 86 | } 87 | int CircularLinkedList::length() 88 | { 89 | Node *p=Head; 90 | int l=0; 91 | do 92 | { 93 | l++; 94 | p=p->next; 95 | }while(p!=Head); 96 | return l; 97 | 98 | } 99 | void CircularLinkedList::Rdisplay(Node *p) 100 | { 101 | static int flag=0; 102 | if(p!=Head || flag==0) 103 | { 104 | flag=1; 105 | cout<data<<"->"; 106 | Rdisplay(p->next); 107 | } 108 | flag=0; 109 | } 110 | void CircularLinkedList::display() 111 | { 112 | Node *p=Head; 113 | do{ 114 | cout<data<<"->"; 115 | p=p->next; 116 | 117 | }while(p!=Head); 118 | cout<data=A[0]; 126 | Head->next=Head; 127 | last=Head; 128 | for(int i=1;idata=A[i]; 132 | t->next=last->next; 133 | last->next=t; 134 | last=t; 135 | } 136 | } 137 | 138 | int main() 139 | { 140 | 141 | int A[]={1,3,5,8,9}; 142 | CircularLinkedList l(A,5); 143 | cout<<"Before :"; 144 | l.display(); 145 | cout<<"Deleted : "< 2 | #include 3 | using namespace std; 4 | class Node { 5 | public: 6 | Node *prev; 7 | int data; 8 | Node *next; 9 | }; 10 | class DoublyLL 11 | { 12 | private: 13 | Node *first=NULL; 14 | public: 15 | DoublyLL(int A[],int n); 16 | void Display(); 17 | int Length(); 18 | void Insert(int index,int x); 19 | int Delete(int index); 20 | void Reverse(); 21 | ~DoublyLL(); 22 | 23 | }; 24 | void DoublyLL::Reverse() 25 | { 26 | Node *temp,*p=first; 27 | while(p) 28 | { 29 | temp=p->prev; 30 | p->prev=p->next; 31 | p->next=temp; 32 | p=p->prev; 33 | if(p && p->next==NULL)// 34 | first=p; 35 | } 36 | } 37 | int DoublyLL::Delete(int index) 38 | { 39 | if(index<0 || index>Length()) 40 | return -1; 41 | 42 | Node *p=first; 43 | int x; 44 | 45 | if(index==1) 46 | { 47 | first=first->next; 48 | x=p->data; 49 | delete p; 50 | if(first) 51 | first->prev=NULL; 52 | return x; 53 | } 54 | else{ 55 | for(int i=0;inext; 57 | p->prev->next=p->next; 58 | if(p->next) 59 | p->next->prev=p->prev; 60 | x=p->data; 61 | delete p; 62 | return x; 63 | } 64 | } 65 | void DoublyLL::Insert(int index,int x) 66 | { 67 | if(index<0 || index>Length()) 68 | return; 69 | Node *t,*p=first; 70 | t=new Node; 71 | t->data=x; 72 | if(index==0)//before first node 73 | { 74 | t->next=first; 75 | t->prev=NULL; 76 | first=t; 77 | } 78 | else{ 79 | for(int i=0;inext; 81 | t->next=p->next; 82 | if(p->next) 83 | p->next->prev=t; 84 | p->next=t; 85 | t->prev=p; 86 | } 87 | } 88 | int DoublyLL::Length() 89 | { 90 | Node *p=first; 91 | int x=0; 92 | while(p) 93 | { 94 | ++x; 95 | p=p->next; 96 | } 97 | return x; 98 | } 99 | void DoublyLL::Display() 100 | { 101 | Node *p=first; 102 | while(p) 103 | { 104 | cout<data<<"->"; 105 | p=p->next; 106 | } 107 | cout<<"\n"; 108 | } 109 | DoublyLL::DoublyLL(int A[],int n) 110 | { 111 | Node *last,*t; 112 | first=new Node; 113 | first->data=A[0]; 114 | first->prev=first->next=NULL; 115 | last=first; 116 | for(int i=1;inext=last->next; 120 | t->data=A[i]; 121 | t->prev=last; 122 | last->next=t; 123 | last=t; 124 | } 125 | 126 | } 127 | 128 | DoublyLL::~DoublyLL() 129 | { 130 | Node *p=first,*q; 131 | while(p) 132 | { 133 | q=p; 134 | p=p->next; 135 | cout<<"\n"<<"Deleted :"<data; 136 | delete q; 137 | } 138 | } 139 | 140 | int main() 141 | { 142 | int A[]={1,4,6,7,9}; 143 | DoublyLL a(A,5); 144 | cout<<"Before "< 2 | #include 3 | using namespace std; 4 | class Node { 5 | public: 6 | Node *prev; 7 | int data; 8 | Node *next; 9 | }; 10 | class CDoublyLL 11 | { 12 | private: 13 | Node *first=NULL; 14 | public: 15 | CDoublyLL() 16 | {first=NULL;} 17 | CDoublyLL(int A[],int n); 18 | void Display(); 19 | int Length(); 20 | void Insert(int index,int x); 21 | int Delete(int index); 22 | ~CDoublyLL(); 23 | 24 | }; 25 | CDoublyLL::CDoublyLL(int A[],int n) 26 | { 27 | Node *last,*t; 28 | first=new Node; 29 | first->data=A[0]; 30 | first->next=first; 31 | last=first; 32 | for(int i=1;inext=last->next; 36 | t->data=A[i]; 37 | t->prev=last; 38 | last->next=t; 39 | last=t; 40 | } 41 | first->prev=last; 42 | 43 | } 44 | 45 | void CDoublyLL::Display() 46 | { 47 | Node *p=first; 48 | do 49 | { 50 | cout<data<<"->"; 51 | p=p->next; 52 | }while(p!=first); 53 | cout<<"\n"; 54 | } 55 | 56 | int CDoublyLL::Length() 57 | { 58 | Node *p=first; 59 | if(first==NULL) 60 | return 0; 61 | int x=0; 62 | do 63 | { 64 | ++x; 65 | p=p->next; 66 | }while(p!=first); 67 | return x; 68 | } 69 | 70 | int CDoublyLL::Delete(int index) 71 | { 72 | if(index<0 || index>Length()) 73 | return -1; 74 | 75 | Node *p=first; 76 | int x; 77 | 78 | if(index==1) 79 | { 80 | if(first->next) 81 | { 82 | first->prev->next=first->next; 83 | first=first->next; 84 | first->prev=p->prev; 85 | x=p->data; 86 | } 87 | else{ 88 | first=NULL; 89 | } 90 | delete p; 91 | return x; 92 | } 93 | else{ 94 | for(int i=0;inext; 96 | p->prev->next=p->next; 97 | p->next->prev=p->prev; 98 | x=p->data; 99 | delete p; 100 | return x; 101 | } 102 | } 103 | void CDoublyLL::Insert(int index,int x) 104 | { 105 | if(index<0 || index>Length()) 106 | return; 107 | Node *t,*p=first; 108 | t=new Node; 109 | t->data=x; 110 | if(index==0)//before first node 111 | { 112 | if(first) 113 | { 114 | t->next=first; 115 | t->prev=first->prev; 116 | t->prev->next=t; 117 | first->prev=t; 118 | } 119 | else{ 120 | t->next=t->prev=t; 121 | } 122 | first=t; 123 | } 124 | else{ 125 | for(int i=0;inext; 127 | t->next=p->next; 128 | if(p->next) 129 | p->next->prev=t; 130 | p->next=t; 131 | t->prev=p; 132 | } 133 | } 134 | 135 | 136 | 137 | 138 | CDoublyLL::~CDoublyLL() 139 | { 140 | Node *p=first,*q; 141 | int len=Length(); 142 | while(len>0) 143 | { 144 | q=p->next; 145 | cout<<"Deleted :"<data< 2 | #include 3 | using namespace std; 4 | class Node{ 5 | public: 6 | int row; 7 | int column; 8 | int value; 9 | Node *next; 10 | }; 11 | class Sparse_Matrix{ 12 | private: 13 | Node *first=NULL; 14 | int m,n; 15 | public: 16 | Sparse_Matrix(); 17 | void Display(); 18 | void Merge(Sparse_Matrix); 19 | Node *GetHead(){ 20 | return first; 21 | } 22 | }; 23 | void Sparse_Matrix::Merge(Sparse_Matrix b) 24 | { 25 | Node *first1=first,*first2=b.GetHead(),*first3=NULL,*p=NULL,*q=NULL; 26 | while (first1 && first2) 27 | { 28 | if(first1->rowrow) 29 | { 30 | p=first1; 31 | if(q) 32 | q->next=p; 33 | else 34 | first3=p; 35 | q=p; 36 | first1=first1->next; 37 | p->next=NULL; 38 | } 39 | else if(first2->rowrow) 40 | { 41 | p=first2; 42 | if(q) 43 | q->next=p; 44 | else 45 | first3=p; 46 | q=p; 47 | first2=first2->next; 48 | p->next=NULL; 49 | } 50 | else 51 | { 52 | if(first1->columncolumn) 53 | { 54 | p=first1; 55 | if(q) 56 | q->next=p; 57 | else 58 | first3=p; 59 | q=p; 60 | first1=first1->next; 61 | p->next=NULL; 62 | } 63 | else if(first2->columncolumn) 64 | { 65 | p=first2; 66 | if(q) 67 | q->next=p; 68 | else 69 | first3=p; 70 | q=p; 71 | first2=first2->next; 72 | p->next=NULL; 73 | } 74 | else{ 75 | p=first1; 76 | if(q) 77 | q->next=p; 78 | else 79 | first3=p; 80 | q=p; 81 | p->value=first1->value+first2->value; 82 | first1=first1->next; 83 | Node *temp=first2; 84 | first2=first2->next; 85 | temp->next=NULL; 86 | p->next=NULL; 87 | } 88 | } 89 | } 90 | if(first1) 91 | p->next=first1; 92 | else 93 | p->next=first2; 94 | first=first3; 95 | 96 | } 97 | Sparse_Matrix::Sparse_Matrix() 98 | { 99 | 100 | int i,j,data; 101 | cout<<"Enter the dimension of Matrix "; 102 | cin>>m>>n; 103 | Node *p=NULL,*q=NULL; 104 | cout<<"Enter the Matrix: \n"; 105 | 106 | for(int i=0;i>data; 111 | if(data!=0) 112 | { 113 | 114 | p=new Node; 115 | if(q!=NULL) 116 | q->next=p; 117 | else 118 | first=p; 119 | p->value=data; 120 | p->row=i; 121 | p->column=j; 122 | p->next=NULL; 123 | q=p; 124 | 125 | } 126 | } 127 | } 128 | 129 | } 130 | void Sparse_Matrix::Display() 131 | { 132 | Node *p=NULL; 133 | p=first; 134 | for(int i=0;irow==i && p->column==j) 139 | { 140 | cout<value<< " "; 141 | p=p->next; 142 | } 143 | else{ 144 | cout<<"0 "; 145 | } 146 | } 147 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | struct Node{ 6 | int coeff; 7 | int exp; 8 | struct Node *next; 9 | }*poly=NULL; 10 | 11 | void create() 12 | { 13 | struct Node *t=NULL,*last=NULL; 14 | int num; 15 | cout<<"Enter the number of polynomials "; 16 | cin>>num; 17 | 18 | cout<<"Enter the coefficient and exponent of each polynomial "; 19 | for(int i=0;i>t->coeff>>t->exp; 23 | if(poly==NULL) 24 | { 25 | poly=last=t; 26 | } 27 | else{ 28 | last->next=t; 29 | last=t; 30 | } 31 | last->next=NULL; 32 | } 33 | } 34 | 35 | void display(struct Node *p) 36 | { 37 | while(p) 38 | { 39 | cout<coeff<<"x**"<exp<<" + "; 40 | p=p->next; 41 | } 42 | } 43 | 44 | long eva(struct Node *p,int x) 45 | { 46 | long val=0; 47 | while(p) 48 | { 49 | val+=p->coeff*pow(x,p->exp); 50 | p=p->next; 51 | } 52 | return val; 53 | } 54 | int main() 55 | { 56 | create(); 57 | display(poly); 58 | cout<<"\nAnswer of Evaluation"< 2 | #include 3 | using namespace std; 4 | struct Stack{ 5 | int size; 6 | int top; 7 | int *S; 8 | }; 9 | 10 | void create(Stack *st) 11 | { 12 | cout<<"Enter the size of stack : "; 13 | cin>>st->size; 14 | st->top=-1; 15 | st->S=new int[st->size]; 16 | } 17 | 18 | void display(Stack st) 19 | { 20 | if(st.top==-1) 21 | cout<<"stack is Empty"; 22 | else{ 23 | int i; 24 | for(i=st.top;i>=0;i--) 25 | { 26 | cout<size-st->top==1) 35 | { 36 | cout<<"stack is overflow\n"; 37 | } 38 | else{ 39 | st->top++; 40 | st->S[st->top]=x; 41 | } 42 | } 43 | 44 | int pop(Stack *st) 45 | { 46 | int x=-1; 47 | if(st->top==-1) 48 | { 49 | cout<<"Stack is underflow\n"; 50 | } 51 | else{ 52 | x=st->S[st->top]; 53 | st->top--; 54 | } 55 | return x; 56 | } 57 | 58 | int peek(Stack st,int index) 59 | { 60 | int x=-1; 61 | if(st.top-index+1<0) 62 | cout<<"Invalid Index\n"; 63 | else{ 64 | x=st.S[st.top-index+1]; 65 | } 66 | return x; 67 | } 68 | 69 | int StackTop(Stack st) 70 | { 71 | int x=-1; 72 | if(st.top!=-1){ 73 | x=st.S[st.top]; 74 | } 75 | return x; 76 | } 77 | 78 | int isEmpty(Stack st) 79 | { 80 | if(st.top==-1) 81 | return 1; 82 | else 83 | return 0; 84 | } 85 | 86 | int isFull(Stack st) 87 | { 88 | if(st.size-st.top==1) 89 | return 1; 90 | else 91 | return 0; 92 | } 93 | int main() 94 | { 95 | struct Stack st; 96 | create(&st); 97 | display(st); 98 | return 0; 99 | } -------------------------------------------------------------------------------- /11_Stack/02_Stack_using_LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Node{ 6 | public: 7 | int data; 8 | Node *next; 9 | }; 10 | 11 | class Stack{ 12 | private: 13 | Node *top; 14 | public: 15 | Stack(){top=NULL;} 16 | void push(int x); 17 | int pop(); 18 | void Display(); 19 | }; 20 | 21 | void Stack::push(int x) 22 | { 23 | Node *t=new Node; 24 | if(t==NULL) 25 | cout<<"Stack overflow\n"; 26 | else{ 27 | t->data=x; 28 | if(top) 29 | { 30 | t->next=top; 31 | top=t; 32 | } 33 | else{ 34 | top=t; 35 | t->next=NULL; 36 | } 37 | } 38 | } 39 | 40 | int Stack::pop() 41 | { 42 | int x=-1; 43 | if(top==NULL) 44 | cout<<"Stack underflow\n"; 45 | else{ 46 | Node *t=top; 47 | top=top->next; 48 | x=t->data; 49 | delete t; 50 | } 51 | return x; 52 | } 53 | 54 | void Stack::Display() 55 | { 56 | 57 | if(top==NULL) 58 | cout<<"Stack is Empty\n"; 59 | else{ 60 | Node *t=top; 61 | while(t) 62 | { 63 | cout<data<<" "; 64 | t=t->next; 65 | } 66 | } 67 | } 68 | 69 | int main() 70 | { 71 | Stack st; 72 | st.pop(); 73 | st.push(5); 74 | st.push(3); 75 | st.push(8); 76 | cout<<"popped "< 2 | #include 3 | #include 4 | using namespace std; 5 | struct Stack{ 6 | int size; 7 | int top; 8 | char *S; 9 | }; 10 | 11 | 12 | void create(Stack *st,char *s) 13 | { 14 | st->size=strlen(s); 15 | st->top=-1; 16 | st->S=new char[st->size]; 17 | } 18 | 19 | void push(Stack *st,char x) 20 | { 21 | if(st->size-st->top==1) 22 | { 23 | cout<<"stack is overflow\n"; 24 | } 25 | else{ 26 | st->top++; 27 | st->S[st->top]=x; 28 | } 29 | } 30 | 31 | char pop(Stack *st) 32 | { 33 | char x=-1; 34 | if(st->top==-1) 35 | { 36 | cout<<"Stack is underflow\n"; 37 | } 38 | else{ 39 | x=st->S[st->top]; 40 | st->top--; 41 | } 42 | return x; 43 | } 44 | 45 | 46 | int isEmpty(Stack st) 47 | { 48 | if(st.top==-1) 49 | return 1; 50 | else 51 | return 0; 52 | } 53 | 54 | int isFull(Stack st) 55 | { 56 | if(st.size-st.top==1) 57 | return 1; 58 | else 59 | return 0; 60 | } 61 | 62 | int isBalanced(char *s) 63 | { 64 | int i; 65 | struct Stack st; 66 | create(&st,s); 67 | 68 | for(i=0;s[i]!='\0';i++) 69 | { 70 | if(s[i]=='(') 71 | push(&st,'('); 72 | else if(s[i]==')') 73 | { 74 | if(isEmpty(st)) 75 | return 0; 76 | pop(&st); 77 | } 78 | } 79 | if(isEmpty(st)) 80 | return 1; 81 | else 82 | return 0; 83 | 84 | } 85 | int main() 86 | { 87 | char s[]="((Hello))+(World))"; 88 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | struct Stack{ 6 | int size; 7 | int top; 8 | char *S; 9 | }; 10 | 11 | 12 | void create(Stack *st,char *s) 13 | { 14 | int x=strlen(s); 15 | st->size=x; 16 | st->top=-1; 17 | st->S=new char[st->size]; 18 | } 19 | 20 | void push(Stack *st,char x) 21 | { 22 | if(st->size-st->top==1) 23 | { 24 | cout<<"stack is overflow\n"; 25 | } 26 | else{ 27 | st->top++; 28 | st->S[st->top]=x; 29 | } 30 | } 31 | 32 | char pop(Stack *st) 33 | { 34 | char x=-1; 35 | if(st->top==-1) 36 | { 37 | cout<<"Stack is underflow\n"; 38 | } 39 | else{ 40 | x=st->S[st->top]; 41 | st->top--; 42 | } 43 | return x; 44 | } 45 | 46 | 47 | int isEmpty(Stack st) 48 | { 49 | if(st.top==-1) 50 | return 1; 51 | else 52 | return 0; 53 | } 54 | 55 | int isFull(Stack st) 56 | { 57 | if(st.size-st.top==1) 58 | return 1; 59 | else 60 | return 0; 61 | } 62 | 63 | int isBalanced(char *s) 64 | { 65 | int i; 66 | struct Stack st; 67 | create(&st,s); 68 | 69 | for(i=0;s[i]!='\0';i++) 70 | { 71 | if(s[i]=='(' || s[i]=='{' || s[i]=='[') 72 | push(&st,s[i]); 73 | else if(s[i]==')' || s[i]==']' || s[i]=='}') 74 | { 75 | if(isEmpty(st)) 76 | return 0; 77 | char x=pop(&st); 78 | if((s[i]-x) <=2)//working with ascii code 79 | continue; 80 | else 81 | return 0; 82 | 83 | } 84 | } 85 | if(isEmpty(st)) 86 | return 1; 87 | else 88 | return 0; 89 | 90 | } 91 | int main() 92 | { 93 | char s[]="{([hedfsdfllo])}"; 94 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | struct Stack{ 6 | int size; 7 | int top; 8 | char *S; 9 | }; 10 | 11 | 12 | void create(Stack *st,char *s) 13 | { 14 | int x=strlen(s); 15 | st->size=x; 16 | st->top=-1; 17 | st->S=new char[st->size]; 18 | } 19 | 20 | void push(Stack *st,char x) 21 | { 22 | if(st->size-st->top==1) 23 | { 24 | cout<<"stack is overflow\n"; 25 | } 26 | else{ 27 | st->top++; 28 | st->S[st->top]=x; 29 | } 30 | } 31 | 32 | char TopValue(Stack st) 33 | { 34 | if(st.top==-1) 35 | return -1; 36 | else 37 | return st.S[st.top]; 38 | } 39 | 40 | char pop(Stack *st) 41 | { 42 | char x=-1; 43 | if(st->top==-1) 44 | { 45 | cout<<"Stack is underflow\n"; 46 | } 47 | else{ 48 | x=st->S[st->top]; 49 | st->top--; 50 | } 51 | return x; 52 | } 53 | 54 | 55 | int isEmpty(Stack st) 56 | { 57 | if(st.top==-1) 58 | return 1; 59 | else 60 | return 0; 61 | } 62 | 63 | int isFull(Stack st) 64 | { 65 | if(st.size-st.top==1) 66 | return 1; 67 | else 68 | return 0; 69 | } 70 | 71 | int isOperand(char s) 72 | { 73 | if(s=='*' || s=='/' || s=='+' || s=='-') 74 | return 0; 75 | else 76 | return 1; 77 | } 78 | 79 | int pre(char s) 80 | { 81 | if(s=='*' || s=='/') 82 | return 2; 83 | else if(s=='+' || s=='-') 84 | return 1; 85 | return 0; 86 | } 87 | 88 | char* ToPost(char *s) 89 | { 90 | char *postfix; 91 | int i=0,j=0; 92 | postfix=new char[strlen(s+2)]; 93 | Stack st; 94 | create(&st,s); 95 | push(&st,'#');//because of comaprison of top vale with string 1st value # is pushed 96 | //so there wont be any error 97 | while(s[i]!='\0') 98 | { 99 | if(isOperand(s[i])) 100 | postfix[j++]=s[i++]; 101 | else 102 | { 103 | if(pre(s[i])>pre(TopValue(st)))//because of this comparison stack cant be expty so # is pushed 104 | push(&st,s[i++]); 105 | else 106 | { 107 | postfix[j++]=pop(&st); 108 | } 109 | } 110 | } 111 | while(!isEmpty(st)) 112 | { 113 | postfix[j++]=pop(&st); 114 | } 115 | postfix[j]='\0'; 116 | return postfix; 117 | } 118 | 119 | int main() 120 | { 121 | char s[]="a+b*c/d+e"; 122 | char *postfix=ToPost(s); 123 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | struct Stack{ 6 | int size; 7 | int top; 8 | char *S; 9 | }; 10 | 11 | 12 | void create(Stack *st,char *s) 13 | { 14 | int x=strlen(s); 15 | st->size=x; 16 | st->top=-1; 17 | st->S=new char[st->size]; 18 | } 19 | 20 | void push(Stack *st,char x) 21 | { 22 | if(st->size-st->top==1) 23 | { 24 | cout<<"stack is overflow\n"; 25 | } 26 | else{ 27 | st->top++; 28 | st->S[st->top]=x; 29 | } 30 | } 31 | 32 | char TopValue(Stack st) 33 | { 34 | if(st.top==-1) 35 | return -1; 36 | else 37 | return st.S[st.top]; 38 | } 39 | 40 | char pop(Stack *st) 41 | { 42 | char x=-1; 43 | if(st->top==-1) 44 | { 45 | cout<<"Stack is underflow\n"; 46 | } 47 | else{ 48 | x=st->S[st->top]; 49 | st->top--; 50 | } 51 | return x; 52 | } 53 | 54 | 55 | int isEmpty(Stack st) 56 | { 57 | if(st.top==-1) 58 | return 1; 59 | else 60 | return 0; 61 | } 62 | 63 | int isFull(Stack st) 64 | { 65 | if(st.size-st.top==1) 66 | return 1; 67 | else 68 | return 0; 69 | } 70 | 71 | int isOperand(char s) 72 | { 73 | if(s=='*' || s=='/' || s=='+' || s=='-' || s=='^' || s=='(' || s==')') 74 | return 0; 75 | else 76 | return 1; 77 | } 78 | 79 | int out_pre(char s) 80 | { 81 | if(s=='+' || s=='-') 82 | return 1; 83 | else if(s=='*' || s=='/') 84 | return 3; 85 | else if(s=='^') 86 | return 6; 87 | else if(s=='(') 88 | return 7; 89 | else if(s==')') 90 | return 0; 91 | return 0; 92 | } 93 | 94 | int ins_pre(char s) 95 | { 96 | if(s=='+' || s=='-') 97 | return 2; 98 | else if(s=='*' || s=='/') 99 | return 4; 100 | else if(s=='^') 101 | return 5; 102 | else if(s=='(') 103 | return 0; 104 | return -1; 105 | } 106 | 107 | char* ToPost(char *s) 108 | { 109 | char *postfix; 110 | int i=0,j=0; 111 | postfix=new char[strlen(s+2)]; 112 | Stack st; 113 | create(&st,s); 114 | push(&st,'#');//because of comaprison of top vale with string 1st value # is pushed 115 | //so there wont be any error 116 | while(s[i]!='\0') 117 | { 118 | if(isOperand(s[i])) 119 | postfix[j++]=s[i++]; 120 | else 121 | { 122 | if(out_pre(s[i])>ins_pre(TopValue(st)))//because of this comparison stack cant be expty so # is pushed 123 | push(&st,s[i++]); 124 | else if(out_pre(s[i]) 2 | #include 3 | #include 4 | using namespace std; 5 | struct Stack{ 6 | int size; 7 | int top; 8 | int *S; 9 | }; 10 | 11 | 12 | void create(Stack *st,char *s) 13 | { 14 | int x=strlen(s); 15 | st->size=x; 16 | st->top=-1; 17 | st->S=new int[st->size]; 18 | } 19 | 20 | void push(Stack *st,int x) 21 | { 22 | if(st->size-st->top==1) 23 | { 24 | cout<<"stack is overflow\n"; 25 | } 26 | else{ 27 | st->top++; 28 | st->S[st->top]=x; 29 | } 30 | } 31 | 32 | 33 | 34 | int pop(Stack *st) 35 | { 36 | int x=-1; 37 | if(st->top==-1) 38 | { 39 | cout<<"Stack is underflow\n"; 40 | } 41 | else{ 42 | x=st->S[st->top]; 43 | st->top--; 44 | } 45 | return x; 46 | } 47 | 48 | 49 | int isEmpty(Stack st) 50 | { 51 | if(st.top==-1) 52 | return 1; 53 | else 54 | return 0; 55 | } 56 | 57 | int isFull(Stack st) 58 | { 59 | if(st.size-st.top==1) 60 | return 1; 61 | else 62 | return 0; 63 | } 64 | 65 | int isOperand(char s) 66 | { 67 | if(s=='*' || s=='/' || s=='+' || s=='-' || s=='^' || s=='(' || s==')') 68 | return 0; 69 | else 70 | return 1; 71 | } 72 | 73 | 74 | int eval(char *postfix) 75 | { 76 | Stack st; 77 | create(&st,postfix); 78 | int i,x1,x2,r; 79 | for(i=0;postfix[i]!='\0';i++) 80 | { 81 | if(isOperand(postfix[i])) 82 | { 83 | push(&st,(postfix[i]-'0')); 84 | } 85 | else{ 86 | x2=pop(&st); 87 | x1=pop(&st); 88 | switch(postfix[i]) 89 | { 90 | case '+': 91 | r=x1+x2; 92 | break; 93 | case '-': 94 | r=x1-x2; 95 | break; 96 | case '*': 97 | r=x1*x2; 98 | break; 99 | case '/': 100 | r=x1/x2; 101 | break; 102 | default: 103 | break; 104 | 105 | } push(&st,r); 106 | } 107 | } 108 | return pop(&st); 109 | } 110 | int main() 111 | { 112 | char s[]="2362/*+"; 113 | int answer=eval(s); 114 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class Queue 6 | { 7 | private: 8 | int size; 9 | int front; 10 | int rear; 11 | int *Q; 12 | public: 13 | Queue(); 14 | void enqueue(int x); 15 | int dequeue(); 16 | void display(); 17 | }; 18 | Queue::Queue() 19 | { 20 | cout<<"Enter the size of queue\n"; 21 | cin>>size; 22 | Q=new int[size]; 23 | front=rear=-1; 24 | } 25 | 26 | void Queue::enqueue(int x) 27 | { 28 | if(rear==size-1) 29 | cout<<"Queue is full\n"; 30 | else{ 31 | ++rear; 32 | Q[rear]=x; 33 | 34 | } 35 | } 36 | 37 | int Queue::dequeue() 38 | { 39 | int x=-1; 40 | if(front==rear) 41 | cout<<"Queue is Empty\n"; 42 | else{ 43 | ++front; 44 | x=Q[front]; 45 | } 46 | return x; 47 | } 48 | 49 | void Queue::display() 50 | { 51 | int i=front+1; 52 | for(;i<=rear;i++) 53 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class Queue 6 | { 7 | private: 8 | int size; 9 | int front; 10 | int rear; 11 | int *Q; 12 | public: 13 | Queue(); 14 | void enqueue(int x); 15 | int dequeue(); 16 | void display(); 17 | }; 18 | Queue::Queue() 19 | { 20 | cout<<"Enter the size of queue\n"; 21 | cin>>size; 22 | size++; 23 | Q=new int[size]; 24 | front=rear=0; 25 | } 26 | 27 | void Queue::enqueue(int x) 28 | { 29 | if((rear+1)%size==front) 30 | cout<<"Queue is full\n"; 31 | else{ 32 | rear=(rear+1)%size; 33 | Q[rear]=x; 34 | 35 | } 36 | } 37 | 38 | int Queue::dequeue() 39 | { 40 | int x=-1; 41 | if(front==rear) 42 | cout<<"Queue is full\n"; 43 | else{ 44 | front=(front+1)%size; 45 | x=Q[front]; 46 | } 47 | return x; 48 | } 49 | 50 | void Queue::display() 51 | { 52 | int i=front+1; 53 | do{ 54 | cout< 2 | #include 3 | using namespace std; 4 | class Node{ 5 | public: 6 | int data; 7 | Node *next; 8 | }; 9 | class Queue 10 | { 11 | private: 12 | Node *front; 13 | Node *rear; 14 | public: 15 | Queue(){front=rear=NULL;} 16 | void enqueue(int x); 17 | int dequeue(); 18 | void display(); 19 | }; 20 | 21 | void Queue::enqueue(int x) 22 | { 23 | Node *t; 24 | t=new Node; 25 | if(t==NULL) 26 | cout<<"Queue is full\n"; 27 | else{ 28 | t->data=x; 29 | t->next=NULL; 30 | if(front==NULL) 31 | front=rear=t; 32 | else{ 33 | rear->next=t; 34 | rear=t; 35 | } 36 | } 37 | } 38 | 39 | int Queue::dequeue() 40 | { 41 | int x=-1; 42 | Node *t; 43 | if(front==NULL) 44 | cout<<"Queue is Empty\n"; 45 | else{ 46 | t=front; 47 | if(front->next) 48 | front=front->next; 49 | else 50 | front=NULL; 51 | x=t->data; 52 | delete t; 53 | } 54 | return x; 55 | } 56 | 57 | void Queue::display() 58 | { 59 | Node *i=front; 60 | while(i) 61 | { 62 | cout<data<<" "; 63 | i=i->next; 64 | } 65 | cout<<"\n"; 66 | } 67 | int main() 68 | { 69 | Queue q; 70 | q.enqueue(1); 71 | q.enqueue(2); 72 | q.enqueue(3); 73 | q.enqueue(4); 74 | q.enqueue(5); 75 | cout<<"Deleted "< 2 | #include 3 | #include"Node.h" 4 | #include"Queue.h" 5 | using namespace std; 6 | 7 | class createtree 8 | { 9 | public: 10 | Node *p,*t; 11 | int x; 12 | Queue q; 13 | 14 | createtree() 15 | { 16 | cout<<"Enter the root Node data "; 17 | cin>>root->data; 18 | root->lchild=root->rchild=NULL; 19 | q.enqueue(root); 20 | while(!q.isEmpty()) 21 | { 22 | p=q.dequeue(); 23 | cout<<"Enter the value of left child of "<data<<" "; 24 | cin>>x; 25 | if(x!=-1) 26 | { 27 | t=new Node; 28 | t->data=x; 29 | t->lchild=t->rchild=nullptr; 30 | p->lchild=t; 31 | q.enqueue(t); 32 | } 33 | cout<<"Enter the value of right child of "<data<<" "; 34 | cin>>x; 35 | if(x!=-1) 36 | { 37 | t=new Node; 38 | t->data=x; 39 | t->lchild=t->rchild=nullptr; 40 | p->rchild=t; 41 | q.enqueue(t); 42 | } 43 | 44 | } 45 | } 46 | 47 | void display() 48 | { 49 | Node *t; 50 | Queue q; 51 | q.enqueue(root); 52 | while(!q.isEmpty()) 53 | { 54 | t=q.dequeue(); 55 | cout<data<<" "; 56 | if(t->lchild) 57 | q.enqueue(t->lchild); 58 | if(t->rchild) 59 | q.enqueue(t->rchild); 60 | 61 | } 62 | 63 | } 64 | }; 65 | 66 | int main() 67 | { 68 | createtree r; 69 | r.display(); 70 | return 0; 71 | 72 | } -------------------------------------------------------------------------------- /13_Trees/02_Traversing.cpp: -------------------------------------------------------------------------------- 1 | //Mine not working becz of some fault 2 | #include 3 | #include 4 | using namespace std; 5 | #include"Node.h" 6 | #include"Queue.h" 7 | #include"Stack.h" 8 | 9 | 10 | class createtree 11 | { 12 | public: 13 | Node *p,*t; 14 | int x; 15 | Queue q; 16 | createtree(); 17 | void display(); 18 | void postorder(); 19 | void preorder(); 20 | void inorder(); 21 | void rinorder(Node *);//recursive inorder 22 | 23 | }; 24 | void createtree::rinorder(Node *t) 25 | { 26 | if(t) 27 | { 28 | rinorder(t->lchild); 29 | cout<data<<" "; 30 | rinorder(t->rchild); 31 | } 32 | 33 | } 34 | void createtree::inorder() 35 | { 36 | struct Stack st; 37 | create(&st); 38 | long int num; 39 | Node *t=root; 40 | while(t || !isEmpty(st)) 41 | { 42 | if(t!=NULL) 43 | { 44 | push(&st,(long int)t);//storing address in long int 45 | t=t->lchild; 46 | } 47 | else 48 | { 49 | num=pop(&st); 50 | t=(Node *)num;// long int to address 51 | cout<data<<" "; 52 | t=t->rchild; 53 | } 54 | } 55 | } 56 | void createtree::preorder() 57 | { 58 | struct Stack st; 59 | create(&st); 60 | long int num; 61 | Node *t=root; 62 | while(t || !isEmpty(st)) 63 | { 64 | if(t!=NULL) 65 | { 66 | push(&st,(long int)t); 67 | cout<data<<" "; 68 | t=t->lchild; 69 | } 70 | else 71 | { 72 | num=pop(&st); 73 | t=(Node *)num; 74 | t=t->rchild; 75 | } 76 | } 77 | } 78 | void createtree::postorder() 79 | { 80 | struct Stack st; 81 | create(&st); 82 | long int num; 83 | Node *t=root; 84 | while(t || !isEmpty(st)) 85 | { 86 | if(t!=NULL) 87 | { 88 | push(&st,(long int)t); 89 | t=t->lchild; 90 | } 91 | else 92 | { 93 | num=pop(&st); 94 | if(num>0) 95 | { 96 | push(&st,-1*num); 97 | t=((Node *)num)->rchild; 98 | } 99 | else{ 100 | cout<<((Node *)(-1*num))->data<<" "; 101 | t=NULL; 102 | } 103 | } 104 | } 105 | } 106 | void createtree::display() 107 | { 108 | Node *t; 109 | Queue q; 110 | q.enqueue(root); 111 | while(!q.isEmpty()) 112 | { 113 | t=q.dequeue(); 114 | cout<data<<" "; 115 | if(t->lchild) 116 | q.enqueue(t->lchild); 117 | if(t->rchild) 118 | q.enqueue(t->rchild); 119 | 120 | } 121 | 122 | } 123 | createtree::createtree() 124 | { 125 | cout<<"Enter the root Node data "; 126 | cin>>root->data; 127 | root->lchild=root->rchild=NULL; 128 | q.enqueue(root); 129 | while(!q.isEmpty()) 130 | { 131 | p=q.dequeue(); 132 | cout<<"Enter the value of left child of "<data<<" "; 133 | cin>>x; 134 | if(x!=-1) 135 | { 136 | t=new Node; 137 | t->data=x; 138 | t->lchild=t->rchild=nullptr; 139 | p->lchild=t; 140 | q.enqueue(t); 141 | } 142 | cout<<"Enter the value of right child of "<data<<" "; 143 | cin>>x; 144 | if(x!=-1) 145 | { 146 | t=new Node; 147 | t->data=x; 148 | t->lchild=t->rchild=nullptr; 149 | p->rchild=t; 150 | q.enqueue(t); 151 | } 152 | 153 | } 154 | } 155 | int main() 156 | { 157 | createtree r; 158 | r.inorder(); 159 | return 0; 160 | 161 | } 162 | -------------------------------------------------------------------------------- /13_Trees/03_LevelOrder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #include 5 | #include "Node.h" 6 | 7 | class createtree 8 | { 9 | public: 10 | Node *p,*t; 11 | int x; 12 | queue q; 13 | createtree(); 14 | void Levelorder(); 15 | 16 | }; 17 | void createtree::Levelorder() 18 | { 19 | queue q; 20 | Node *t=root; 21 | q.emplace(t); 22 | while(!q.empty()) 23 | { 24 | t=q.front(); 25 | q.pop(); 26 | std::cout<data<<" "; 27 | if(t->lchild) 28 | { 29 | q.emplace(t->lchild); 30 | } 31 | if(t->rchild) 32 | { 33 | q.emplace(t->rchild); 34 | } 35 | 36 | } 37 | 38 | 39 | } 40 | createtree::createtree() 41 | { 42 | cout<<"Enter the root Node data "; 43 | cin>>root->data; 44 | root->lchild=root->rchild=NULL; 45 | q.emplace(root); 46 | while(!q.empty()) 47 | { 48 | p=q.front(); 49 | q.pop(); 50 | cout<<"Enter the value of left child of "<data<<" "; 51 | cin>>x; 52 | if(x!=-1) 53 | { 54 | t=new Node; 55 | t->data=x; 56 | t->lchild=t->rchild=nullptr; 57 | p->lchild=t; 58 | q.emplace(t); 59 | } 60 | cout<<"Enter the value of right child of "<data<<" "; 61 | cin>>x; 62 | if(x!=-1) 63 | { 64 | t=new Node; 65 | t->data=x; 66 | t->lchild=t->rchild=nullptr; 67 | p->rchild=t; 68 | q.emplace(t); 69 | } 70 | 71 | } 72 | } 73 | 74 | int main() 75 | { 76 | createtree t; 77 | t.Levelorder(); 78 | return 0; 79 | } -------------------------------------------------------------------------------- /13_Trees/04_Height_and_count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #include 5 | #include "Node.h" 6 | 7 | class createtree 8 | { 9 | public: 10 | Node *p,*t; 11 | int x; 12 | queue q; 13 | createtree(); 14 | void Levelorder(); 15 | int count(Node *p); 16 | int count(){ count(root); } 17 | int height(Node *p); 18 | int height(){height(root);} 19 | int leafNodeCount(Node *p); 20 | int leafNodeCount(){ leafNodeCount(root); } 21 | }; 22 | int createtree::leafNodeCount(Node *p) { 23 | int x; 24 | int y; 25 | if (p != nullptr){ 26 | x = leafNodeCount(p->lchild); 27 | y = leafNodeCount(p->rchild); 28 | if (p->lchild == nullptr && p->rchild == nullptr){ 29 | return x + y + 1; 30 | } else { 31 | return x + y; 32 | } 33 | } 34 | return 0; 35 | } 36 | int createtree::count(Node *p) 37 | { 38 | if(p!=NULL) 39 | return count(p->lchild)+count(p->rchild)+1; 40 | 41 | return 0; 42 | } 43 | int createtree::height(Node *p) 44 | { 45 | int x,y; 46 | if(p) 47 | { 48 | x=height(p->lchild); 49 | y=height(p->rchild); 50 | if(x>y) 51 | return x+1; 52 | else 53 | return y+1; 54 | } 55 | return 0; 56 | } 57 | void createtree::Levelorder() 58 | { 59 | queue q; 60 | Node *t=root; 61 | q.emplace(t); 62 | while(!q.empty()) 63 | { 64 | t=q.front(); 65 | q.pop(); 66 | std::cout<data<<" "; 67 | if(t->lchild) 68 | { 69 | q.emplace(t->lchild); 70 | } 71 | if(t->rchild) 72 | { 73 | q.emplace(t->rchild); 74 | } 75 | 76 | } 77 | 78 | 79 | } 80 | createtree::createtree() 81 | { 82 | cout<<"Enter the root Node data "; 83 | cin>>root->data; 84 | root->lchild=root->rchild=NULL; 85 | q.emplace(root); 86 | while(!q.empty()) 87 | { 88 | p=q.front(); 89 | q.pop(); 90 | cout<<"Enter the value of left child of "<data<<" "; 91 | cin>>x; 92 | if(x!=-1) 93 | { 94 | t=new Node; 95 | t->data=x; 96 | t->lchild=t->rchild=nullptr; 97 | p->lchild=t; 98 | q.emplace(t); 99 | } 100 | cout<<"Enter the value of right child of "<data<<" "; 101 | cin>>x; 102 | if(x!=-1) 103 | { 104 | t=new Node; 105 | t->data=x; 106 | t->lchild=t->rchild=nullptr; 107 | p->rchild=t; 108 | q.emplace(t); 109 | } 110 | 111 | } 112 | } 113 | 114 | int main() 115 | { 116 | createtree t; 117 | cout<<"\nleaf Node "<>st->size; 12 | st->top=-1; 13 | st->S=new long int[st->size]; 14 | } 15 | 16 | 17 | 18 | void push(Stack *st,long int x) 19 | { 20 | if(st->size-st->top==1) 21 | { 22 | cout<<"stack is overflow\n"; 23 | } 24 | else{ 25 | st->top++; 26 | st->S[st->top]=x; 27 | } 28 | } 29 | 30 | long int pop(Stack *st) 31 | { 32 | long int x=-1; 33 | if(st->top==-1) 34 | { 35 | cout<<"Stack is underflow\n"; 36 | } 37 | else{ 38 | x=st->S[st->top]; 39 | st->top--; 40 | } 41 | return x; 42 | } 43 | 44 | 45 | 46 | long int StackTop(Stack st) 47 | { 48 | long int x=-1; 49 | if(st.top!=-1){ 50 | x=st.S[st.top]; 51 | } 52 | return x; 53 | } 54 | 55 | int isEmpty(Stack st) 56 | { 57 | if(st.top==-1) 58 | return 1; 59 | else 60 | return 0; 61 | } 62 | 63 | int isFull(Stack st) 64 | { 65 | if(st.size-st.top==1) 66 | return 1; 67 | else 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /14_BST/01_bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct Node { 8 | Node* lchild; 9 | int data; 10 | Node* rchild; 11 | }*root=NULL; 12 | 13 | struct Node* Rinsert(struct Node *p,int key) 14 | { 15 | struct Node *t; 16 | if(p==NULL) 17 | { 18 | t=new Node ; 19 | t->data=key; 20 | t->lchild=t->rchild=NULL; 21 | return t; 22 | } 23 | if(p->data>key) 24 | p->lchild=Rinsert(p->lchild,key); 25 | else if(p->datarchild=Rinsert(p->rchild,key); 27 | return p; 28 | } 29 | void insert(int key) 30 | { 31 | Node *p=root,*r; 32 | if(p==NULL) 33 | { 34 | p=new Node; 35 | p->lchild=p->rchild=NULL; 36 | p->data=key; 37 | root=p; 38 | return; 39 | } 40 | else{ 41 | while(p!=NULL) 42 | { 43 | r=p; 44 | if(p->datarchild; 46 | else if(p->data>key) 47 | p=p->lchild; 48 | else 49 | return; 50 | } 51 | p=new Node; 52 | p->lchild=p->rchild=NULL; 53 | p->data=key; 54 | if(r->datarchild=p; 56 | else 57 | r->lchild=p; 58 | } 59 | } 60 | 61 | void inorder(Node *p) 62 | { 63 | if(p) 64 | { 65 | inorder(p->lchild); 66 | cout<data<<" "<rchild); 68 | } 69 | } 70 | 71 | int Height(struct Node *t) 72 | { 73 | if(t==NULL) 74 | return 0; 75 | int lh=Height(t->lchild); 76 | int rh=Height(t->rchild); 77 | return lhrchild!=NULL) 82 | p=p->rchild; 83 | return p; 84 | } 85 | 86 | struct Node *Insucc(Node *p) 87 | { 88 | while(p && p->lchild!=NULL) 89 | p=p->lchild; 90 | return p; 91 | } 92 | 93 | struct Node *Delete(Node *p,int key) 94 | { 95 | struct Node *t=NULL; 96 | if (p==NULL) 97 | return nullptr; 98 | if(p->lchild==NULL && p->rchild==NULL) 99 | { 100 | if (p == root){ 101 | root = nullptr; 102 | } 103 | delete p; 104 | return nullptr; 105 | } 106 | if(p->data>key) 107 | p->lchild=Delete(p->lchild,key); 108 | else if(p->datarchild=Delete(p->rchild,key); 110 | else 111 | { 112 | if(Height(p->lchild)>Height(p->rchild)) 113 | { 114 | t=Inprec(p->lchild); 115 | p->data=t->data; 116 | p->lchild=Delete(p->lchild,t->data); 117 | } 118 | else 119 | { 120 | t=Insucc(p->rchild); 121 | p->data=t->data; 122 | p->rchild=Delete(p->rchild,t->data); 123 | } 124 | } 125 | return p; 126 | } 127 | 128 | void CreatePre() 129 | { 130 | int pre[]={30,20,10,15,25,40,50,45}; 131 | int n=8; 132 | stack st; 133 | Node *p,*t; 134 | root=new Node; 135 | int i=0; 136 | root->data=pre[i++]; 137 | root->lchild=root->rchild=NULL; 138 | p=root; 139 | while (idata>pre[i]) 142 | { 143 | t=new Node; 144 | t->data=pre[i++]; 145 | t->lchild=t->rchild=NULL; 146 | p->lchild=t; 147 | st.emplace(p); 148 | p=t; 149 | } 150 | else if(p->datadata>pre[i]?1:0)) 151 | { 152 | t=new Node; 153 | t->data=pre[i++]; 154 | t->lchild=t->rchild=NULL; 155 | p->rchild=t; 156 | p=t; 157 | } 158 | else 159 | { 160 | p=st.top(); 161 | st.pop(); 162 | } 163 | } 164 | 165 | } 166 | int main() 167 | { 168 | int A[]={30,20,10,15,25,40,50,45}; 169 | CreatePre(); 170 | inorder(root); 171 | return 0; 172 | } -------------------------------------------------------------------------------- /15_AVL_Tree/01_LL_Rotation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct Node { 8 | Node* lchild; 9 | int data; 10 | int height; 11 | Node* rchild; 12 | }*root=NULL; 13 | 14 | int NodeHeight(struct Node *p) 15 | { 16 | if(p==NULL) 17 | return 0; 18 | int hl,hr; 19 | hl=p && p->lchild?p->lchild->height:0; 20 | hr=p && p->rchild?p->rchild->height:0; 21 | 22 | return hl>hr?hl+1:hr+1; 23 | } 24 | 25 | int BalanceFactor(struct Node *p) 26 | { 27 | int hl,hr; 28 | hl=p && p->lchild?p->lchild->height:0; 29 | hr=p && p->rchild?p->rchild->height:0; 30 | return hl-hr; 31 | } 32 | 33 | struct Node * LLRotation(struct Node *p) 34 | { 35 | Node *pl=p->lchild; 36 | Node *plr=pl->rchild; 37 | 38 | p->lchild=plr; 39 | pl->rchild=p; 40 | 41 | p->height=NodeHeight(p); 42 | pl->height=NodeHeight(pl); 43 | 44 | if(p==root) 45 | { root=pl; 46 | return pl; 47 | } 48 | return p; 49 | 50 | } 51 | 52 | 53 | 54 | 55 | 56 | struct Node * RRRotation(struct Node *p) 57 | { 58 | Node *pr=p->rchild; 59 | Node *prl=pr->lchild; 60 | 61 | p->rchild=prl; 62 | pr->lchild=p; 63 | 64 | p->height=NodeHeight(p); 65 | pr->height=NodeHeight(pr); 66 | 67 | if(p==root) 68 | { 69 | root=pr; 70 | return pr; 71 | } 72 | 73 | return p; 74 | 75 | } 76 | 77 | struct Node * LRRotation(struct Node *p) 78 | { 79 | Node *pl=p->lchild; 80 | Node *plr=pl->rchild; 81 | 82 | pl->rchild=plr->lchild; 83 | p->lchild=plr->rchild; 84 | plr->lchild=pl; 85 | plr->rchild=p; 86 | 87 | p->height=NodeHeight(p); 88 | pl->height=NodeHeight(pl); 89 | plr->height=NodeHeight(plr); 90 | 91 | if(p==root) 92 | root=plr; 93 | 94 | return plr; 95 | 96 | } 97 | 98 | struct Node * RLRotation(struct Node *p) 99 | { 100 | Node *pr=p->rchild; 101 | Node *prl=pr->lchild; 102 | 103 | p->rchild=prl->lchild; 104 | pr->lchild=prl->rchild; 105 | prl->lchild=p; 106 | prl->rchild=pr; 107 | 108 | p->height=NodeHeight(p); 109 | pr->height=NodeHeight(pr); 110 | prl->height=NodeHeight(prl); 111 | 112 | if(p==root) 113 | root=prl; 114 | 115 | return prl; 116 | 117 | } 118 | 119 | 120 | struct Node* Rinsert(struct Node *p,int key) 121 | { 122 | struct Node *t; 123 | if(p==NULL) 124 | { 125 | t=new Node ; 126 | t->data=key; 127 | t->height=1; 128 | t->lchild=t->rchild=NULL; 129 | return t; 130 | } 131 | if(p->data>key) 132 | p->lchild=Rinsert(p->lchild,key); 133 | else if(p->datarchild=Rinsert(p->rchild,key); 135 | p->height=NodeHeight(p); 136 | 137 | if(BalanceFactor(p)==2 && BalanceFactor(p->lchild)==1) 138 | return LLRotation(p); 139 | else if(BalanceFactor(p)==2 && BalanceFactor(p->lchild)==-1) 140 | return LRRotation(p); 141 | else if(BalanceFactor(p)==-2 && BalanceFactor(p->rchild)==1) 142 | return RLRotation(p); 143 | else if(BalanceFactor(p)==-2 && BalanceFactor(p->rchild)==-1) 144 | return RRRotation(p); 145 | return p; 146 | } 147 | struct Node *Inprec(Node *p) 148 | { 149 | while(p && p->rchild!=NULL) 150 | p=p->rchild; 151 | return p; 152 | } 153 | 154 | struct Node *Insucc(Node *p) 155 | { 156 | while(p && p->lchild!=NULL) 157 | p=p->lchild; 158 | return p; 159 | } 160 | struct Node *Delete(Node *p,int key) 161 | { 162 | struct Node *t=NULL; 163 | if (p==NULL) 164 | return nullptr; 165 | if(p->lchild==NULL && p->rchild==NULL) 166 | { 167 | if (p == root){ 168 | root = nullptr; 169 | } 170 | delete p; 171 | return nullptr; 172 | } 173 | if(p->data>key) 174 | { 175 | p->lchild=Delete(p->lchild,key); 176 | } 177 | else if(p->datarchild=Delete(p->rchild,key); 179 | else 180 | { 181 | if(NodeHeight(p->lchild)>NodeHeight(p->rchild)) 182 | { 183 | t=Inprec(p->lchild); 184 | p->data=t->data; 185 | p->lchild=Delete(p->lchild,t->data); 186 | 187 | } 188 | else 189 | { 190 | t=Insucc(p->rchild); 191 | p->data=t->data; 192 | p->rchild=Delete(p->rchild,t->data); 193 | } 194 | } 195 | // Update height 196 | p->height = NodeHeight(p); 197 | 198 | // Balance Factor and Rotation 199 | if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == 1) { // L1 Rotation 200 | return LLRotation(p); 201 | } else if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == -1){ // L-1 Rotation 202 | return LRRotation(p); 203 | } else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == -1){ // R-1 Rotation 204 | return RRRotation(p); 205 | } else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == 1){ // R1 Rotation 206 | return RLRotation(p); 207 | } else if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == 0){ // L0 Rotation 208 | return LLRotation(p); 209 | } else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == 0){ // R0 Rotation 210 | return RRRotation(p); 211 | } 212 | 213 | return p; 214 | } 215 | 216 | void Display(struct Node *p) 217 | { 218 | cout<data<lchild) 220 | { 221 | cout<<"left child of "<data<<" is "; 222 | Display(p->lchild); 223 | } 224 | 225 | if(p->rchild) 226 | { 227 | cout<<"right child of "<data<<" is "; 228 | Display(p->rchild); 229 | } 230 | } 231 | 232 | 233 | int main() 234 | { 235 | root=Rinsert(root,50); 236 | Rinsert(root,60); 237 | Rinsert(root,40); 238 | Rinsert(root,30); 239 | Rinsert(root,55); 240 | Rinsert(root,45); 241 | Rinsert(root,35); 242 | 243 | Delete(root,60); 244 | Display(root); 245 | return 0; 246 | } -------------------------------------------------------------------------------- /17_Heap/create_heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void create_heap(int A[],int n) 7 | { 8 | int i,temp=A[n]; 9 | i=n; 10 | while(i>1 && temp>A[i/2]) 11 | { 12 | A[i]=A[i/2]; 13 | i=i/2; 14 | } 15 | A[i]=temp; 16 | } 17 | 18 | int delete_heap(int A[],int n) 19 | { 20 | int i,temp,j; 21 | int val=A[1]; 22 | A[1]=A[n]; 23 | i=1; 24 | j=i*2; 25 | while(j<=n-1) 26 | { 27 | if(A[j+1]>A[j] && j+1<=n-1)//as it is possible that j=n i.e last element so it may cause 28 | j=j+1; //error so j+1 need to be less than or equal to last element 29 | if(A[i]