├── DS_Course_Spring_21_&_Fall_22 ├── LAB │ ├── Ms. Aqsa Zahid │ │ ├── LAB#1 │ │ │ ├── LAB-1.pdf │ │ │ ├── Q.1(L1).cpp │ │ │ └── Q.2(L1).cpp │ │ ├── LAB#11 │ │ │ ├── LAB-11.pdf │ │ │ └── Q.1 (L11).cpp │ │ ├── LAB#12 │ │ │ ├── LAB-12.pdf │ │ │ ├── Q.1 (L12).cpp │ │ │ ├── Vector.hpp │ │ │ └── input_validator.hpp │ │ ├── LAB#2 │ │ │ ├── LAB-2.pdf │ │ │ ├── Q.1 (L2).cpp │ │ │ ├── Q.2 (L2).cpp │ │ │ └── Q.3 (L2).cpp │ │ ├── LAB#3 │ │ │ ├── LAB-3.pdf │ │ │ ├── Q.1 (L3).cpp │ │ │ ├── Q.10 (L3).cpp │ │ │ ├── Q.12 (L3).cpp │ │ │ ├── Q.2 (L3).cpp │ │ │ ├── Q.3 (L3).cpp │ │ │ └── Q.8 (L3).cpp │ │ ├── LAB#4 │ │ │ ├── LAB-4.pdf │ │ │ ├── Q.1 (L4).cpp │ │ │ ├── Q.2 (L4).cpp │ │ │ └── Q.3 (L4).cpp │ │ ├── LAB#5 │ │ │ ├── LAB-5.pdf │ │ │ ├── Q.1 (L5).cpp │ │ │ ├── Q.2 (L5).cpp │ │ │ ├── Q.3 (L5).cpp │ │ │ ├── Q.4 (L5).cpp │ │ │ └── Q.5 (L5).cpp │ │ ├── LAB#6 │ │ │ ├── LAB-6.pdf │ │ │ ├── Q.1 (L6).cpp │ │ │ ├── Q.2 (L6).cpp │ │ │ ├── Q.3 (L6).cpp │ │ │ └── Q.4 (L6).cpp │ │ ├── LAB#7 │ │ │ ├── Array.hpp │ │ │ ├── Doubly_Linked_List.hpp │ │ │ ├── LAB-7.pdf │ │ │ ├── Q.1A (L7).cpp │ │ │ ├── Q.1A-Alternate (L7).cpp │ │ │ ├── Q.1B (L7).cpp │ │ │ ├── Q.2A (L7).cpp │ │ │ ├── Q.2A-Alternate (L7).cpp │ │ │ ├── Q.2B (L7).cpp │ │ │ ├── Q.3A (L7).cpp │ │ │ ├── Q.3A-Alternate (L7).cpp │ │ │ ├── Q.3B (L7).cpp │ │ │ ├── Singly_Linked_List.hpp │ │ │ └── Vector.hpp │ │ ├── LAB#8 │ │ │ ├── DLL.hpp │ │ │ ├── Iterator.hpp │ │ │ ├── LAB-8 (Queue Tasks).pdf │ │ │ ├── LAB-8 (Stack Tasks).pdf │ │ │ ├── Q.1 (L8,Queue).cpp │ │ │ ├── Q.1 (L8,Stack).cpp │ │ │ ├── Q.2 (L8,Queue).cpp │ │ │ ├── Q.2 (L8,Stack).cpp │ │ │ ├── Q.3 (L8,Queue).cpp │ │ │ ├── Q.3 (L8,Stack).cpp │ │ │ ├── Q.4 (L8,Queue).cpp │ │ │ ├── Q.4 (L8,Stack).cpp │ │ │ ├── SLL.hpp │ │ │ ├── String.hpp │ │ │ └── Vector.hpp │ │ └── LAB#9 │ │ │ ├── BST.cpp │ │ │ ├── LAB-9.pdf │ │ │ └── List.hpp │ └── Sir Ali Shah Fatimi (Improvement) │ │ ├── LAB#1,2 │ │ ├── L1,2#1.cpp │ │ ├── L1,2#2.cpp │ │ ├── L1,2#3a.cpp │ │ ├── L1,2#3b.cpp │ │ ├── L1,2#4.cpp │ │ ├── L1,2#5a.cpp │ │ ├── L1,2#5b.cpp │ │ ├── L1,2#6a.cpp │ │ ├── L1,2#6b.cpp │ │ ├── L1,2#7.cpp │ │ ├── Lab_Manual#1,2.docx │ │ ├── dynamic_array.hpp │ │ └── examples │ │ │ ├── basics.cpp │ │ │ └── static_jagged_array.cpp │ │ ├── LAB#10 │ │ ├── Huffman.hpp │ │ ├── L10#1.cpp │ │ ├── L10#2.cpp │ │ ├── L10#3,4.cpp │ │ ├── L10#5.cpp │ │ ├── L10#6.cpp │ │ └── Lab_Manual#10.docx │ │ ├── LAB#11 │ │ ├── Forward_List.hpp │ │ ├── Hashing.hpp │ │ ├── L11#1.cpp │ │ ├── L11#2.cpp │ │ ├── L11#3.cpp │ │ ├── L11#4.cpp │ │ ├── Lab_Manual#11.doc │ │ └── Pair.hpp │ │ ├── LAB#12 │ │ ├── Graph.hpp │ │ ├── Iterator.hpp │ │ ├── L12#1.cpp │ │ ├── L12#2.cpp │ │ ├── L12#3.cpp │ │ ├── L12#4.cpp │ │ ├── L12#5.cpp │ │ ├── Lab_Manual#12.docx │ │ ├── Swap.hpp │ │ ├── Vector.hpp │ │ └── Weighted_Graph.hpp │ │ ├── LAB#3 │ │ ├── L3#1.cpp │ │ ├── L3#2.cpp │ │ ├── L3#3.cpp │ │ ├── L3#4.cpp │ │ ├── L3#4_stack_activation_record.jpg │ │ ├── L3#5a.cpp │ │ ├── Lab_Manual#3.docx │ │ └── warning.hpp │ │ ├── LAB#4 │ │ ├── L4#1.cpp │ │ ├── L4#2.cpp │ │ ├── L4#3.cpp │ │ ├── L4#4.cpp │ │ ├── L4#5.cpp │ │ ├── L4#6.txt │ │ ├── Lab_Manual#4.docx │ │ └── Sorts.hpp │ │ ├── LAB#5 │ │ ├── L5#1,2,3,4,5,6.cpp │ │ ├── L5#7.cpp │ │ ├── Lab_Manual#5.docx │ │ └── SLL.hpp │ │ ├── LAB#6 │ │ ├── Circular_Forward_List.hpp │ │ ├── Circular_List.hpp │ │ ├── L6#1.cpp │ │ ├── L6#2.cpp │ │ ├── L6#3.cpp │ │ ├── L6#4.cpp │ │ ├── Lab_Manual#6.doc │ │ ├── List.hpp │ │ ├── Node.hpp │ │ └── Swap.hpp │ │ ├── LAB#8 │ │ ├── D_Deque.hpp │ │ ├── D_Stack.hpp │ │ ├── F_Deque.hpp │ │ ├── F_Stack.hpp │ │ ├── L8#1.cpp │ │ ├── L8#2.cpp │ │ ├── L8#3.cpp │ │ ├── L8#4.cpp │ │ ├── L8#5.cpp │ │ ├── L8#6.cpp │ │ ├── L8#7.cpp │ │ └── Lab_Manual#8.docx │ │ ├── LAB#9 │ │ ├── AVL.hpp │ │ ├── BST.hpp │ │ ├── L9#1.cpp │ │ ├── L9#2,3.cpp │ │ ├── L9#4.cpp │ │ ├── L9#5.cpp │ │ ├── L9#6.cpp │ │ ├── L9#7.cpp │ │ ├── L9#8.cpp │ │ └── Lab_Manual#9.docx │ │ ├── Lab#7 │ │ ├── L7#1.cpp │ │ ├── L7#2.cpp │ │ ├── L7#5.cpp │ │ └── Lab_Manual#7.docx │ │ └── Weekly_Lab_Plan.pdf └── THEORY │ ├── Assignments_&_Practice_Tasks │ ├── Ms. Farah Sadia & Sir Shoaib Rauf │ │ ├── Practice_Problems_week#1_to_5.pdf │ │ ├── prac_week#1_ADT_Arrays │ │ │ ├── Prac_Probs_W#1_Q1.cpp │ │ │ ├── Prac_Probs_W#1_Q3.cpp │ │ │ ├── Prac_Probs_W#1_Q5_P6.cpp │ │ │ ├── Quiz1_SectionD.jpeg │ │ │ ├── Qz1_ScD_Q1.cpp │ │ │ ├── Qz1_ScD_Q1_Alt.cpp │ │ │ ├── README.txt │ │ │ └── W1_T1.cpp │ │ ├── prac_week#2_Recursion │ │ │ ├── README.txt │ │ │ └── W2_T1.cpp │ │ └── prac_week#5_Elementary_Sorts │ │ │ └── Sorting_Important_Notes.txt │ └── Sir Basit Jasani │ │ ├── Assg#1 │ │ ├── Assignment # 1.pdf │ │ ├── Q.1 (Assg#1).cpp │ │ └── Q.2 (Assg#1).cpp │ │ ├── Assg#2 │ │ ├── DLL.hpp │ │ ├── Iterator.hpp │ │ ├── Q.1 (Assg#2).cpp │ │ ├── Q.2 (Assg#2).cpp │ │ ├── Q.3 (Assg#2).cpp │ │ ├── String.hpp │ │ └── Vector.hpp │ │ └── Assg#3 │ │ ├── Q.1 (Assg#3).cpp │ │ └── String.hpp │ ├── Books │ ├── DataStructures_&_Algorithms_in_Cpp_4th_Ed_by_Adam_Drozdek.pdf │ ├── Ideal_Road_Map_for_Reading_Books.txt │ ├── SAMS_Teach_Yourself_Cpp_in_One_Hour_a_Day_6th_Ed_using_Cpp98_&_Cpp03.pdf │ ├── SAMS_Teach_Yourself_Cpp_in_One_Hour_a_Day_7th_Ed_sample_using_Cpp11.pdf │ └── SAMS_Teach_Yourself_Cpp_in_One_Hour_a_Day_8th_Ed_using_Cpp14_&Cpp17.pdf │ ├── Course Outline.docx │ ├── Lectures │ ├── Miss_Nida_Perwaiz.zip │ └── Sir Shoaib Rauf │ │ ├── Week#10_Binary_Search_Tree.pdf │ │ ├── Week#12_AVL_Tree.pptx │ │ ├── Week#13_Priority_Queues_&_Heap.pdf │ │ ├── Week#14_Hashing.pdf │ │ ├── Week#15_Graphs.pptx │ │ ├── Week#1_DMA.docx │ │ ├── Week#1_Data_vs_Info_vs_Knowledge.txt │ │ ├── Week#1_Introduction_to_DS.pptx │ │ ├── Week#2_Recursion_&_Backtracking.pptx │ │ ├── Week#3,4_Linked_Lists.pptx │ │ ├── Week#5_Elementary_Sorting_Algorithms.pdf │ │ ├── Week#7_Advance_Sorting_Algorithms.pdf │ │ ├── Week#8_Searching_Algorithms.pdf │ │ └── Week#9_Stacks_&_Queues.pdf │ ├── Resources │ ├── DS_Past_Papers.zip │ ├── DS_Source_Codes.zip │ └── SAMS_Teach_Yourself_Cpp_7th_Ed_Source_Codes.zip │ └── Rule_of_Three.pdf ├── MY_DS_LIBRARY ├── Algorithms │ ├── Recursion │ │ ├── Binary_Combinations(R).cpp │ │ ├── Dec_to_Bin(R).cpp │ │ ├── Digit_Sum(R).cpp │ │ ├── Factorial(R).cpp │ │ ├── Fibonacci_Series(R).cpp │ │ ├── GCD(R).cpp │ │ ├── Indirect_Recursion_Exp.cpp │ │ ├── Maze_Runner(R).cpp │ │ ├── N_Queen_Problem(R).cpp │ │ ├── Nested_Recursion_Exp.cpp │ │ ├── Polynomial_Power(R).cpp │ │ ├── Prime_Factor(R).cpp │ │ ├── String_Permutation(R).cpp │ │ ├── Sum_of_N_Nums(R).cpp │ │ ├── Syracuse_Sequence(R).cpp │ │ ├── Tower_Of_Hanoi(R).cpp │ │ ├── Tree_Recursion_Exp.cpp │ │ └── string_permutation_algorithm.jpg │ ├── Sorting │ │ ├── All_Elementary_Sorts.cpp │ │ ├── Comb_Sort.cpp │ │ ├── Count_Sort.cpp │ │ ├── Heap_Sort.cpp │ │ ├── Merge_Sort.cpp │ │ ├── Quick_Sort.cpp │ │ ├── Shell_Sort.cpp │ │ └── Topological_Sort (Graphs).cpp │ └── Sorts.hpp ├── Contiguous_Structures │ ├── Segmented_Vector.cpp │ ├── Segmented_Vector.hpp │ ├── String.cpp │ ├── String.hpp │ ├── Vector.cpp │ └── Vector.hpp ├── Hashing │ ├── Hashing_notes.txt │ ├── closed_address_hashing_implementation.cpp │ ├── closed_address_hashing_using_singly_linked_list.cpp │ ├── double_hashing (open_address).cpp │ ├── hashing (SIR BASIT).cpp │ ├── hashing_with_linear_probing.cpp │ ├── hashing_with_quadratic_probing.cpp │ └── using_tuple_for_multi_index_hash_table.cpp ├── Library_Log.txt ├── Linked_Lists │ ├── Circular_Forward_List.cpp │ ├── Circular_Forward_List.hpp │ ├── Circular_List.cpp │ ├── Circular_List.hpp │ ├── Forward_List.cpp │ ├── Forward_List.hpp │ ├── List.cpp │ ├── List.hpp │ ├── Node.hpp │ ├── README.txt │ ├── Skip_List.cpp │ ├── Xor_List.cpp │ └── Xor_List.hpp ├── Miscellaneous │ ├── Library_Management.cpp │ ├── graph_implementation.cpp │ ├── heap+insertion.cpp │ ├── node_swapping.cpp │ ├── non_extreme_node_swapping.cpp │ ├── q.1 (lab_mid_exam_spring_21).cpp │ ├── sort_random_dates.cpp │ └── stack_using_array.cpp ├── Special_Structures │ ├── Data_Files │ │ ├── df_1.txt │ │ ├── df_2.txt │ │ ├── df_2_sorted.txt │ │ ├── df_3.csv │ │ ├── df_4.csv │ │ └── large_dataset_uncleaned.csv │ ├── Documentation_Files │ │ ├── Pointers for Meeting.pptx │ │ ├── Project Report.docx │ │ └── Simplified Concept Diagram (MAATA).png │ ├── Indexed_Struct.cpp │ ├── Indexed_Struct.hpp │ ├── Multi_Attribute_AVL_Tree_Adapter.cpp │ ├── Multi_Attribute_AVL_Tree_Adapter.hpp │ ├── Optimized_Multi_Attribute_AVL_Tree_Adapter.cpp │ ├── Optimized_Multi_Attribute_AVL_Tree_Adapter.hpp │ └── Testing │ │ ├── Data_Files │ │ ├── df_4.csv │ │ └── sales_target.csv │ │ ├── README.txt │ │ ├── out.txt │ │ ├── out_2.txt │ │ └── test_csv_files.cpp ├── Stacks_&_Queues │ ├── Deque.hpp │ ├── Deques.cpp │ ├── F_Deque.hpp │ ├── F_Stack.hpp │ ├── HF_Stack.hpp │ └── Stacks.cpp ├── Trees │ ├── AVL.hpp │ ├── BST.hpp │ └── Trees.cpp └── Utilities │ ├── BST_to_Heap_Convertor.hpp │ ├── Comparator.hpp │ ├── Comparator_Adapter.hpp │ ├── Date.hpp │ ├── Garbage_Collector.hpp │ ├── Instance_Counter.hpp │ ├── Iterator.hpp │ ├── Pair.hpp │ ├── Reverse_Iterator_Adapter.hpp │ ├── Swap.hpp │ ├── Template_Meta_Programming.hpp │ └── Type_Convertors.hpp ├── Mastering_Data_Structures_and_Algorithms_using_C_and_C++.torrent └── README.md /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#1/LAB-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#1/LAB-1.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#11/LAB-11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#11/LAB-11.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#12/LAB-12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#12/LAB-12.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#12/input_validator.hpp: -------------------------------------------------------------------------------- 1 | #if __cplusplus >= 201103L 2 | 3 | #ifndef _GLIBCXX_IOSTREAM 4 | #include 5 | using namespace std; 6 | #endif 7 | 8 | 9 | inline void Print_Multiple_Args(){}//i.e. base case condition func 10 | 11 | template 12 | void Print_Multiple_Args(const T& Arg1,const Types&... Args){//Note: Parameter-Pack (...)-> takes multiple args 13 | cout< 20 | void Validate_Input(D& Dest,const L1& Lower_Limit,const L2& Upper_Limit,bool clear_flag,const T& Arg1,const Types&... Arg2){//Note: Dest should only be any integer type 21 | while(true){ 22 | if(clear_flag) 23 | system("cls"); 24 | Print_Multiple_Args(Arg1,Arg2...);//i.e. displays complete string 25 | cin>>Dest; 26 | if(Dest>=Lower_Limit&&Dest<=Upper_Limit) 27 | break; 28 | cout<<"\a";//i.e. system sound 29 | cin.clear(); 30 | fflush(stdin); 31 | //Note: Here, we have to use both cin.clear() & fflush(stdin) to clear buffer 32 | } 33 | } 34 | #endif 35 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#2/LAB-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#2/LAB-2.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#2/Q.1 (L2).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class quadratic_eqn{ 5 | int a,b,c; 6 | public: 7 | quadratic_eqn(int a,int b,int c):a(a),b(b),c(c){} 8 | void find_roots(){ 9 | if (!a){ 10 | cout << "Invalid"; 11 | return; 12 | } 13 | int d=b*b-4*a*c; 14 | double sqrt_val=sqrt(abs(d)); 15 | if (d>0){ 16 | cout<<"Roots are real and different \n" 17 | <<(double)(-b+sqrt_val)/(2*a)<<"\n" 18 | <<(double)(-b-sqrt_val)/(2*a); 19 | } 20 | else if(!d){ 21 | cout<<"Roots are real and same \n" 22 | <<-(double)b/(2*a); 23 | } 24 | else{ 25 | cout<<"Roots are complex "< 2 | using namespace std; 3 | 4 | int get_missing_no(int arr[], int N){ 5 | int total=1; 6 | for (int i=2;i<=N+1;++i){ 7 | total+=i; 8 | total-=arr[i-2]; 9 | } 10 | return total; 11 | } 12 | int main(){ 13 | int arr[]={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14 | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 15 | 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 16 | 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 17 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 18 | 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 19 | 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 20 | 71, 72, 73, 74,/*75,*/76, 77, 78, 79, 80, 21 | 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 22 | 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}; 23 | #define N sizeof(arr)/sizeof(arr[0]) 24 | cout<<"\nMissing No: "< 2 | using namespace std; 3 | 4 | void reverse_arr(int arr[],int start,int end){ 5 | if (start>=end) 6 | return; 7 | int temp=arr[start];//i.e. swap start & end indexes 8 | arr[start]=arr[end]; 9 | arr[end]=temp; 10 | reverse_arr(arr,start+1,end-1); 11 | } 12 | 13 | int main(){ 14 | int arr[]={1,2,3,4,5,6}; 15 | #define N sizeof(arr)/sizeof(arr[0]) 16 | 17 | for (int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | //Note: Treats array elements as indexes 6 | void find_duplicates(int arr[], int N){//i.e. for unsorted array 7 | cout<<"The repeating elements are:"<=0) 10 | arr[abs(arr[i])]=-arr[abs(arr[i])]; 11 | else 12 | cout< 2 | using namespace std; 3 | 4 | int find_duplicates_R(int arr[],int low,int high){//i.e. for sorted array (Recursive method) 5 | if (low>high) 6 | return -1; 7 | int mid=(low+high)/2; 8 | //i.e. checks if the mid element is repeating one 9 | if (arr[mid]!=mid+1){ 10 | if (mid>0&&arr[mid]==arr[mid-1]) 11 | return mid; 12 | //i.e. checks duplicates present in left side 13 | return find_duplicates_R(arr,low,mid-1); 14 | } 15 | //i.e. checks duplicates present in right side 16 | return find_duplicates_R(arr,mid+1,high); 17 | } 18 | 19 | int main(){ 20 | int arr[]={1,2,3,4,4,5}; 21 | #define N sizeof(arr)/sizeof(arr[0]) 22 | int index=find_duplicates_R(arr,0,N-1); 23 | if (index!=-1) 24 | cout< 2 | using namespace std; 3 | 4 | struct _pair{ 5 | int val1,val2; 6 | _pair(int val1,int val2):val1(val1),val2(val2){}//i.e. defalut ctor 7 | }; 8 | _pair find_largest_and_smallest(int arr[],int N){ 9 | int largest=0,smallest=(~((unsigned int)0)/2);//i.e. max int size 10 | for(int i=0;ilargest) 12 | largest=arr[i]; 13 | else if(arr[i] 2 | #include 3 | using namespace std; 4 | 5 | void swap(int &xp,int &yp){ 6 | int temp=xp; 7 | xp=yp; 8 | yp=temp; 9 | } 10 | void bubble_sort(int arr[], int N){ 11 | for (int i=0;iarr[j+1]){ 15 | swap(arr[j],arr[j+1]); 16 | swapped=true; 17 | } 18 | if(!swapped) 19 | break; 20 | } 21 | } 22 | int find_longest_consecitive_sequence(int arr[],int N){ 23 | bubble_sort(arr,N); 24 | vector v; 25 | v.push_back(arr[0]); 26 | //insert repeated elements only once in the vector 27 | for (int i=1;i0&&v[i]==v[i-1]+1) 34 | count++; 35 | else// reset the count 36 | count=1; 37 | ans=(ans>count)?ans:count;// update the maximum 38 | } 39 | return ans; 40 | } 41 | 42 | int main(){ 43 | int arr[]={1,2,2,9,3,5,4,7}; 44 | #define N sizeof(arr)/sizeof(arr[0]) 45 | cout<<"Length of the Longest contiguous subsequence is: " 46 | < 2 | using namespace std; 3 | bool recursive_palindrome(int n,int num,int rev){ 4 | if(rev==num) 5 | return true; 6 | if(n) 7 | return recursive_palindrome(n/10,num,rev*10+(n%10)); 8 | return false; 9 | } 10 | bool iterative_palindrome(int num){ 11 | int n=num,rev=0; 12 | do{ 13 | rev=rev*10+(n%10); 14 | n/=10; 15 | }while(n); 16 | if (num==rev) 17 | return true; 18 | return false; 19 | } 20 | int main(){ 21 | #define N 12345 22 | //if (iterative_palindrome(N)) 23 | if (recursive_palindrome(N,N,0)) 24 | cout<<"\nNote: your number is a palindrome"; 25 | else 26 | cout<<"\nNote: your number is not a palindrome"; 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#4/Q.2 (L4).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void print_power_set_iteratively(char set[],int set_size,int limit){ 5 | if(limit>set_size){ 6 | cout<<"Limit is out of range!"<set_size){ 25 | cout<<"Limit is out of range!"< 2 | using namespace std; 3 | 4 | #if __cplusplus < 201103L 5 | const short nullptr=0L; 6 | #endif 7 | 8 | class linked_list{ 9 | struct node{ 10 | int data; 11 | node *next; 12 | 13 | node(int data):data(data),next(nullptr){}//i.e. ctor 14 | 15 | }*head; 16 | public: 17 | linked_list():head(nullptr){}//i.e. default ctor 18 | 19 | #if __cplusplus >= 201103L 20 | linked_list(initializer_list list):head(nullptr){ 21 | for(const auto& it:list)//i.e. traversing nodes through iterator 22 | add_node_at_back(it); 23 | } 24 | #endif 25 | void add_node_at_back(const int& new_data){//Complexity: O(n) 26 | if(!head) 27 | head=new node(new_data); 28 | else{ 29 | node *temp=head; 30 | while(temp->next) 31 | temp=temp->next; 32 | temp->next=new node(new_data); 33 | } 34 | } 35 | int count_duplicates(int search_for){ 36 | if(!head){ 37 | cout<<"List is empty!"<data==search_for) 44 | ++counter; 45 | temp=temp->next; 46 | } 47 | return counter; 48 | } 49 | ~linked_list(){ 50 | node* temp; 51 | while(head){ 52 | temp=head->next; 53 | delete head; 54 | head=temp; 55 | } 56 | } 57 | }; 58 | 59 | int main(){ 60 | #if __cplusplus >= 201103L 61 | linked_list my_list={1,2,3}; 62 | #else 63 | linked_list my_list; 64 | my_list.add_node_at_back(1); 65 | my_list.add_node_at_back(2); 66 | my_list.add_node_at_back(3); 67 | #endif 68 | cout<<"Total Duplicates of 2: "< 2 | using namespace std; 3 | 4 | #if __cplusplus < 201103L 5 | const short nullptr=0L; 6 | #endif 7 | 8 | #define N 12 9 | 10 | template 11 | class linked_list{ 12 | struct node{ 13 | T data; 14 | node *next; 15 | 16 | node(T data):data(data),next(nullptr){}//i.e. ctor 17 | 18 | }*head; 19 | public: 20 | linked_list():head(nullptr){}//i.e. default ctor 21 | 22 | void add_node_at_back(const T& new_data){//Complexity: O(n) 23 | if(!head) 24 | head=new node(new_data); 25 | else{ 26 | node *temp=head; 27 | while(temp->next) 28 | temp=temp->next; 29 | temp->next=new node(new_data); 30 | } 31 | } 32 | void display_nodes()const{ 33 | if(!head){ 34 | cout<<"List is empty!"<data<<", "; 40 | temp=temp->next; 41 | } 42 | cout<data; 54 | temp1=temp1->next; 55 | } 56 | list2.add_node_at_back(sum/4); 57 | } 58 | } 59 | ~linked_list(){ 60 | node* temp; 61 | while(head){ 62 | temp=head->next; 63 | delete head; 64 | head=temp; 65 | } 66 | } 67 | }; 68 | 69 | int main(){ 70 | linked_list list1,list2; 71 | int val; 72 | cout<<"Enter "<>val; 75 | list1.add_node_at_back(val); 76 | } 77 | 78 | linked_list::calc_avg_for_list2(list1,list2); 79 | cout<<"\nlist1 (user-input): "< 6 | class Array{ 7 | typedef unsigned int __uint32; 8 | 9 | static const __uint32 max_capacity=~((__uint32)0); 10 | //Tip: negate the min. value with extension (by typecasting) to get max. of any predefined datatype. 11 | __uint32 _size; 12 | T data[__size]; 13 | public: 14 | Array()noexcept://i.e. variadic template ctor as initializer-list ctor 15 | _size((__size>max_capacity)?throw:__size),data(){} 16 | #if __cplusplus >= 201103L 17 | template 18 | Array(_T&&... data)noexcept://i.e. default ctor 19 | _size((__size>max_capacity)?throw:__size),data{std::forward<_T>(data)...}{} 20 | #endif 21 | 22 | T& operator[](__uint32 n)const{ 23 | if (n>=_size){//i.e. parameter is of unsigned type, hence condition 'n<0' is excluded 24 | cout<<"\nError: Given Index is Out of Bound!\n"; 25 | std::terminate(); 26 | } 27 | return data[n]; 28 | } 29 | __uint32 size()const{ return _size; } 30 | 31 | void sort(bool=true); 32 | 33 | static void swap(T &xp,T &yp){ 34 | T temp=xp; 35 | xp=yp; 36 | yp=temp; 37 | } 38 | 39 | friend ostream& operator<<(ostream& out,const Array& arr){ 40 | for(__uint32 i=0;i>(istream& in,Array& arr){ 46 | for(__uint32 i=0;i>arr.data[i]; 48 | return in; 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#7/LAB-7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#7/LAB-7.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#7/Q.1A (L7).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Note: include "Array.hpp" after including iostream & using namespace std 5 | #include"Array.hpp" 6 | 7 | template 8 | void Array::sort(bool direction_flag){//i.e. improved bubble-sort 9 | bool swapped=true; 10 | for (__uint32 i=0;swapped&&(i<_size-1);++i){ 11 | swapped=false; 12 | for (__uint32 j=0;j<_size-i-1;++j){ 13 | if(direction_flag){ 14 | if(data[j]>data[j+1]){//i.e. ascending (by default) 15 | swap(data[j],data[j+1]); 16 | swapped=true; 17 | } 18 | } 19 | else if (data[j]= 201103L 29 | Array arr{3,-1,7,5,4}; 30 | #else 31 | Array arr; 32 | cin>>arr; 33 | #endif 34 | cout< 2 | using namespace std; 3 | 4 | //Note: include "Vector.hpp" after including iostream & using namespace std 5 | #include"Vector.hpp" 6 | 7 | template 8 | void Vector::sort(bool direction_flag){//i.e. improved bubble_sort 9 | bool swapped=true; 10 | for (__uint64 i=0;swapped&&(i<_size-1);++i){ 11 | swapped=false; 12 | for (__uint64 j=0;j<_size-i-1;++j){ 13 | if(direction_flag){ 14 | if(*(T*)(data+sizeof(T)*j)>*(T*)(data+sizeof(T)*(j+1))){//i.e. ascending (by default) 15 | swap(*(T*)(data+sizeof(T)*j),*(T*)(data+sizeof(T)*(j+1))); 16 | swapped=true; 17 | } 18 | } 19 | else if(*(T*)(data+sizeof(T)*j)<*(T*)(data+sizeof(T)*(j+1))){//i.e. descending 20 | swap(*(T*)(data+sizeof(T)*j),*(T*)(data+sizeof(T)*(j+1))); 21 | swapped=true; 22 | } 23 | } 24 | } 25 | } 26 | 27 | int main(){ 28 | #if __cplusplus >= 201103L 29 | Vector vec{3,-1,7,5,4}; 30 | #else 31 | Vector vec; 32 | vec.push_back(3); 33 | vec.push_back(-1); 34 | vec.push_back(7); 35 | vec.push_back(5); 36 | vec.push_back(4); 37 | #endif 38 | cout< 2 | using namespace std; 3 | 4 | //Note: include "Singly_Linked_List.hpp" after including iostream & using namespace std 5 | #include"Singly_Linked_List.hpp" 6 | 7 | template 8 | void SLL::sort(bool direction_flag){//i.e. improved bubble sort, using data-swap 9 | if (!head||!head->next)//i.e. do nothing if size is 0 or 1 10 | return; 11 | bool swapped=true; 12 | for(node *i=nullptr,*j=nullptr;swapped;j=i){ 13 | swapped=false; 14 | for (i=head;i->next!=j;i=i->next){ 15 | if(direction_flag){ 16 | if (i->data>i->next->data){//i.e. ascending order 17 | node::swap_data(*i,*i->next); 18 | swapped=true; 19 | } 20 | } 21 | else{ 22 | if (i->datanext->data){//i.e. descending order 23 | node::swap_data(*i,*i->next); 24 | swapped=true; 25 | } 26 | } 27 | } 28 | } 29 | } 30 | 31 | int main(){ 32 | #if __cplusplus >= 201103L 33 | SLL list{3,-1,7,5,4}; 34 | #else 35 | SLL list; 36 | list.push_back(3); 37 | list.push_back(-1); 38 | list.push_back(7); 39 | list.push_back(5); 40 | list.push_back(4); 41 | #endif 42 | cout<<"Before Sort:"< 2 | using namespace std; 3 | 4 | //Note: include "Array.hpp" after including iostream & using namespace std 5 | #include"Array.hpp" 6 | 7 | template 8 | void Array::sort(bool direction_flag){//i.e. selection sort 9 | for (__uint32 i=0;i<_size-1;++i){ 10 | __uint32 min_index=i; 11 | for (__uint32 j=i+1;j<_size;++j){ 12 | if(direction_flag){//i.e. ascending (by default) 13 | if(data[j]data[min_index]) 18 | min_index=j; 19 | } 20 | } 21 | swap(data[i],data[min_index]); 22 | } 23 | } 24 | 25 | int main(){ 26 | #if __cplusplus >= 201103L 27 | Array arr{3,-1,7,5,4}; 28 | #else 29 | Array arr; 30 | cin>>arr; 31 | #endif 32 | cout< 2 | using namespace std; 3 | 4 | //Note: include "Vector.hpp" after including iostream & using namespace std 5 | #include"Vector.hpp" 6 | 7 | template 8 | void Vector::sort(bool direction_flag){//i.e. selection sort 9 | for (__uint32 i=0;i<_size-1;++i){ 10 | __uint32 min_index=i; 11 | for (__uint32 j=i+1;j<_size;++j){ 12 | if(direction_flag){//i.e. ascending (by default) 13 | if(*(T*)(data+sizeof(T)*j)<*(T*)(data+sizeof(T)*min_index)) 14 | min_index=j; 15 | } 16 | else{//i.e. descending 17 | if(*(T*)(data+sizeof(T)*j)>*(T*)(data+sizeof(T)*min_index)) 18 | min_index=j; 19 | } 20 | } 21 | swap(*(T*)(data+sizeof(T)*i),*(T*)(data+sizeof(T)*min_index)); 22 | } 23 | } 24 | 25 | int main(){ 26 | #if __cplusplus >= 201103L 27 | Vector vec{3,-1,7,5,4}; 28 | #else 29 | Vector vec; 30 | vec.push_back(3); 31 | vec.push_back(-1); 32 | vec.push_back(7); 33 | vec.push_back(5); 34 | vec.push_back(4); 35 | #endif 36 | cout< 2 | using namespace std; 3 | 4 | //Note: include "Singly_Linked_List.hpp" after including iostream & using namespace std 5 | #include"Singly_Linked_List.hpp" 6 | 7 | template 8 | void SLL::sort(bool direction_flag){//i.e. selection sort 9 | for (node* i_ptr=head;i_ptr;i_ptr=i_ptr->next){ 10 | node* min_ptr=i_ptr; 11 | for (node* j_ptr=i_ptr->next;j_ptr;j_ptr=j_ptr->next){ 12 | if(direction_flag){//i.e. ascending (by default) 13 | if(j_ptr->datadata) 14 | min_ptr=j_ptr; 15 | } 16 | else{//i.e. descending 17 | if(j_ptr->data>min_ptr->data) 18 | min_ptr=j_ptr; 19 | } 20 | } 21 | node::swap_data(*i_ptr,*min_ptr); 22 | } 23 | } 24 | 25 | int main(){ 26 | #if __cplusplus >= 201103L 27 | SLL list{3,-1,7,5,4}; 28 | #else 29 | SLL list; 30 | list.push_back(3); 31 | list.push_back(-1); 32 | list.push_back(7); 33 | list.push_back(5); 34 | list.push_back(4); 35 | #endif 36 | cout<<"Before Sort:"< 2 | using namespace std; 3 | 4 | //Note: include "Array.hpp" after including iostream & using namespace std 5 | #include"Array.hpp" 6 | 7 | template 8 | void Array::sort(bool direction_flag){//i.e. insertion-sort 9 | for (__int64 j,i=1;i<_size;++i){ 10 | T key=data[i]; 11 | if(direction_flag) 12 | for (j=i-1;j>=0&&data[j]>key;--j) 13 | data[j+1]=data[j]; 14 | else 15 | for (j=i-1;j>=0&&data[j]= 201103L 23 | Array arr{3,-1,7,5,4}; 24 | #else 25 | Array arr; 26 | cin>>arr; 27 | #endif 28 | cout< 2 | using namespace std; 3 | 4 | //Note: include "Vector.hpp" after including iostream & using namespace std 5 | #include"Vector.hpp" 6 | 7 | template 8 | void Vector::sort(bool direction_flag){//i.e. insertion-sort 9 | for (__int64 j,i=1;i<_size;++i){ 10 | T key=*(T*)(data+sizeof(T)*i); 11 | if(direction_flag) 12 | for (j=i-1;j>=0&&*(T*)(data+sizeof(T)*j)>key;--j) 13 | *(T*)(data+sizeof(T)*(j+1))=*(T*)(data+sizeof(T)*j); 14 | else 15 | for (j=i-1;j>=0&&*(T*)(data+sizeof(T)*j)= 201103L 23 | Vector vec{3,-1,7,5,4}; 24 | #else 25 | Vector vec; 26 | vec.push_back(3); 27 | vec.push_back(-1); 28 | vec.push_back(7); 29 | vec.push_back(5); 30 | vec.push_back(4); 31 | #endif 32 | cout< 2 | using namespace std; 3 | 4 | //Note: include "Doubly_Linked_List.hpp" after including iostream & using namespace std 5 | #include"Doubly_Linked_List.hpp" 6 | 7 | int main(){ 8 | #if __cplusplus >= 201103L 9 | DLL list{3,-1,7,5,4}; 10 | #else 11 | DLL list; 12 | list.push_back(3); 13 | list.push_back(-1); 14 | list.push_back(7); 15 | list.push_back(5); 16 | list.push_back(4); 17 | #endif 18 | cout<<"Before Sort:"< 7 | class SLL{//i.e. Singly Linked-List 8 | struct node{ 9 | T data; 10 | node *next; 11 | 12 | node(const T& data=T())noexcept://i.e. default ctor 13 | data(data),next(nullptr){} 14 | 15 | static void swap_data(node &xp,node &yp){//i.e. swaps current data 16 | T temp=xp.data; 17 | xp.data=yp.data; 18 | yp.data=temp; 19 | } 20 | }; 21 | 22 | node *head; 23 | public: 24 | SLL()noexcept:head(nullptr){}//i.e. default ctor 25 | 26 | #if __cplusplus >= 201103L 27 | //i.e.initializer_list based ctor 28 | SLL(initializer_list list)noexcept:head(nullptr){ 29 | for(const auto& it:list)//i.e. traversing list through iterator 30 | push_back(it); 31 | } 32 | #endif 33 | void sort(bool=true); 34 | 35 | void push_back(const T& new_data){//Complexity: O(n) 36 | node *new_node=new node(new_data); 37 | if(!head){ 38 | head=new_node; 39 | return; 40 | } 41 | node *temp=head; 42 | while(temp->next) 43 | temp=temp->next; 44 | temp->next=new_node; 45 | } 46 | void traverse()const{ 47 | if(!head){ 48 | cout<<"List is empty!"<next) 52 | cout<data<<" "; 53 | cout<next; 59 | delete head; 60 | head=temp; 61 | } 62 | } 63 | ~SLL(){ clear(); }//i.e. dtor 64 | }; 65 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#8/LAB-8 (Queue Tasks).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#8/LAB-8 (Queue Tasks).pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#8/LAB-8 (Stack Tasks).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#8/LAB-8 (Stack Tasks).pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#8/Q.1 (L8,Queue).cpp: -------------------------------------------------------------------------------- 1 | #include"DLL.hpp" 2 | 3 | int main(){ 4 | //Note: using Queue FIFO/ascending order (i.e. push_front & pop_back only) 5 | #if __cplusplus >= 201103L 6 | DLL queue_Q,queue_D={1,2,3,4,5,6,7,8}; 7 | #else 8 | DLL queue_Q,queue_D; 9 | for(int i=1;i<=8;++i) 10 | queue_D.push_back(i); 11 | #endif 12 | cout<<"Before:\nD: "; 13 | queue_D.traverse_forward(); 14 | cout<<"Q: "; 15 | queue_Q.traverse_forward(); 16 | 17 | for(int i=0;i<3;++i) 18 | queue_Q.push_back(queue_D.pop_front(1)); 19 | 20 | queue_D.push_back(queue_D.pop_front(1)); 21 | 22 | queue_Q.push_back(queue_D.pop_front(1)); 23 | 24 | for(int i=0;i<3;++i) 25 | queue_D.push_back(queue_D.pop_front(1)); 26 | 27 | while(!queue_Q.empty()) 28 | queue_D.push_back(queue_Q.pop_front(1)); 29 | 30 | for(int i=0;i<4;++i) 31 | queue_D.push_back(queue_D.pop_front(1)); 32 | 33 | cout<<"\nAfter:\nD: "; 34 | queue_D.traverse_forward(); 35 | cout<<"Q: "; 36 | queue_Q.traverse_forward(); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#8/Q.2 (L8,Queue).cpp: -------------------------------------------------------------------------------- 1 | #include"DLL.hpp" 2 | 3 | template 4 | T DLL::get_minimum(){ 5 | if(!head) 6 | throw false; 7 | node *temp=head; 8 | T min=temp->data; 9 | for(;temp;temp=temp->next) 10 | if(temp->datadata; 12 | return min; 13 | } 14 | 15 | int main(){ 16 | #if __cplusplus >= 201103L 17 | DLL queue={1,2,3,4,5,6,7,8}; 18 | #else 19 | DLL queue; 20 | for(int i=1;i<=8;++i) 21 | queue.push_back(i); 22 | #endif 23 | cout<<"after 8 push_backs: "; 24 | queue.traverse_forward(); 25 | 26 | queue.pop_back(); 27 | queue.pop_back(); 28 | cout<<"after 2 pop_backs: "; 29 | queue.traverse_forward(); 30 | 31 | queue.pop_front(); 32 | queue.pop_front(); 33 | cout<<"after 2 pop_fronts: "; 34 | queue.traverse_forward(); 35 | 36 | queue.push_front(-7); 37 | queue.push_front(77); 38 | cout<<"after 2 push_fronts: "; 39 | queue.traverse_forward(); 40 | 41 | cout<<"size: "< stack;//i.e. stack in Descending/LIFO order 28 | try{ 29 | for(int i=0;i 4 | void DLL::find_first_non_repeating_element(){ 5 | if(head){ 6 | bool flag=true; 7 | if(head->next) 8 | find_first_non_repeating_element(head,flag); 9 | if(flag) 10 | head->data='1'; 11 | } 12 | } 13 | template 14 | void DLL::find_first_non_repeating_element(node* temp,bool& flag){ 15 | if (temp->next){ 16 | if (temp->data==temp->next->data){ 17 | find_first_non_repeating_element(temp->next,flag); 18 | if(flag){ 19 | temp->next->data='1'; 20 | flag=false; 21 | } 22 | return; 23 | } 24 | } 25 | } 26 | 27 | int main(){ 28 | #if __cplusplus >= 201103L 29 | DLL queue={'a','a','b','c'}; 30 | #else 31 | DLL queue; 32 | queue.push_back('a'); 33 | queue.push_back('a'); 34 | queue.push_back('b'); 35 | queue.push_back('c'); 36 | #endif 37 | cout<<"Input:"< 4 | bool SLL::operator==(const SLL& other)const{ 5 | if(_size!=other._size) 6 | return false; 7 | for(node *l1=head,*l2=other.head;l1;l1=l1->next,l2=l2->next) 8 | if (l1->data!=l2->data) 9 | return false; 10 | return true; 11 | } 12 | 13 | template//i.e. extra 14 | bool SLL::operator!=(const SLL& other)const{ return !(*this==other); } 15 | 16 | int main(){ 17 | SLL stack1,stack2; 18 | stack1.push_front(404); 19 | stack1.push_front(1); 20 | stack1.push_front(2); 21 | stack1.push_front(3); 22 | 23 | stack2.push_front(404); 24 | stack2.push_front(1); 25 | stack2.push_front(2); 26 | stack2.push_front(3); 27 | 28 | cout<<(stack1==stack2?"true":"false"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Ms. Aqsa Zahid/LAB#8/Q.4 (L8,Stack).cpp: -------------------------------------------------------------------------------- 1 | #include"SLL.hpp" 2 | 3 | template//i.e. extra 4 | void SLL::fill_other_reversely(SLL &other){//i.e. transfers nodes to 'other' in reverse order 5 | other.clear(); 6 | while(head) 7 | other.push_front(pop_front(1)); 8 | } 9 | 10 | template 11 | void SLL::fill_other_reversely(SLL &other,bool)const{//i.e. copies nodes to 'other'.... 12 | other.clear(); 13 | #if __cplusplus >= 201103L 14 | for(const auto& it:*this) 15 | other.push_front(it.data); 16 | #else 17 | for(iterator it=head;it!=nullptr;++it) 18 | other.push_front(it->data); 19 | #endif 20 | } 21 | 22 | int main(){ 23 | SLL stack1,stack2; 24 | 25 | cout<<"Transfering Nodes Reversely (stack1-> stack2):-"< stack2):-"< 2 | #include 3 | #include 4 | 5 | using std::cout; 6 | using std::string; 7 | 8 | class Animal{ 9 | int ID, age; 10 | string name; 11 | 12 | public: 13 | Animal(int ID=0,int age=0,string name="")://default & parameterized ctor 14 | ID(ID),age(age),name(name){} 15 | 16 | /* setters */ 17 | void set_ID(int ID){ this->ID=ID; } 18 | void set_age(int age){ this->age=age; } 19 | void set_name(string name){ this->name=name; } 20 | 21 | /* getters */ 22 | int get_ID()const{ return ID; } 23 | int get_age()const{ return age; } 24 | string get_name()const{ return name; } 25 | 26 | /* extras */ 27 | void eat()const{ cout< 2 | 3 | using std::cout; 4 | using std::cin; 5 | 6 | class Matrix2DArray{ 7 | int height, width; 8 | int** data; 9 | public: 10 | Matrix2DArray(int height, int width)noexcept://ctor 11 | height(height), width(width), data(new int*[height]){ 12 | for (int i=0;i>data[i][j]; 21 | } 22 | 23 | void display()const{ 24 | cout<<"\nmatrix:\n"; 25 | for(int i=0,j;i 2 | 3 | using std::cout; 4 | 5 | //part#1: generalized approach for sorting given array in both order 6 | 7 | template 8 | void _swap(T& a, T& b){ 9 | T temp=std::move(a); 10 | a=std::move(b); 11 | b=std::move(temp); 12 | } 13 | 14 | // ascending & descending functions => act as comparators 15 | template 16 | void ascending(T* arr, int j, bool& swap_flag){ 17 | if(arr[j+1] 23 | void descending(T* arr, int j, bool& swap_flag){ 24 | if(arr[j+1]>arr[j]){ 25 | _swap(arr[j+1],arr[j]); 26 | swap_flag=false; 27 | } 28 | } 29 | 30 | // generalized sort function (for both ascending & descending orders) 31 | template 32 | void sort(T *arr,int size, void (*compare_func)(T*, int, bool&)){ 33 | int i,j; //iterators 34 | bool swap_flag; 35 | for(i=0;i 45 | void display(T* arr,int size){ 46 | for (int i=0;i>arr1[i]; //keyboard input 58 | 59 | //heap/dynamic 1d array 60 | int *arr2=new int[N]{2,7,8,4,1,5,6,10,9,3}; 61 | // for (i=0;i>arr2[i]; 63 | 64 | sort(arr1, N, ascending); 65 | display(arr1, N); 66 | 67 | sort(arr2, N, descending); 68 | display(arr2, N); 69 | 70 | delete []arr2;// deallocating heap memory 71 | 72 | //extra 73 | // char arr3[]{'S','g','a','b','v','C','c'}; 74 | // N=sizeof(arr3)/sizeof(arr3[0]); 75 | 76 | // sort(arr3, N, ascending); 77 | // display(arr3, N); 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#1,2/L1,2#4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | 5 | // problem analysis: 6 | // assuming sorted in ascending order 7 | // indexes: 0,2,4,... -> max values 8 | // indexes: 1,3,5,... -> min values 9 | // 10, 11 -> two case: equal sizes, non-equal sizes 10 | 11 | //original: 1,2,3,4,5,6,7,8,9,10 (n=10) 12 | //temp1: 1,2,3,4,5 (n/2) 13 | //temp2: 6,7,8,9,10 (n/2) 14 | //rearranged: 10,1,9,2,8,3,7,4,6,5 15 | 16 | //original: 1,2,3,4,5,6,7,8,9,10,11 (n=11) 17 | //temp1: 1,2,3,4,5 (n/2) 18 | //temp2: 6,7,8,9,10,11 (n/2+1) 19 | //rearranged: 11,1,10,2,9,3,8,4,7,5,6 20 | 21 | void rearrange(int *arr,int size){ 22 | int n1,n2; //temporary-array sizes 23 | if(size%2){ //odd 24 | n1=size/2; 25 | n2=size/2+1; 26 | } 27 | else //even 28 | n1=n2=size/2; 29 | 30 | int arr1[n1],arr2[n2]; //temporary/sub arrays 31 | int i,j; //iterators 32 | 33 | // copying 1st half i.e. sorted min values (from start) 34 | for(i=0;i-1;--i,j+=2) 45 | arr[j]=arr2[i]; 46 | } 47 | 48 | void display(int* arr,int size){ 49 | for (int i=0;i 2 | 3 | using std::cout; 4 | 5 | //method#1: using (automatic/stack-based) 2d arrays as matrix 6 | 7 | template 8 | void display(bool (*arr)[N]){ //paramater: pointer (that can only point) to array of N size 9 | cout<<"student course relation matrix:\n"; 10 | for(int i=0,j;i 18 | void check_common_course_mates(bool (*arr)[N], int i, int j){ 19 | cout<<'\n'< true, 0 -> false 41 | 42 | display(arr); 43 | 44 | // check_common_course_mates(arr, 1, 2); //case-1: no common courses 45 | // check_common_course_mates(arr, 0, 1); //case-2: one common course only 46 | check_common_course_mates(arr, 0, 4); //case-3: multiple common courses 47 | 48 | return 0; 49 | } 50 | /* side-notes: 51 | 1) we can also use 'bool *ptr = arr[0];' & then treat/traverse it as a linear 1d array, 52 | since memory allocation of 'N*M bools' is sequential/contiguous 53 | 54 | 2) or we can pass base/starting address of 2D array in 'check_common_course_mates' 55 | function as 1D ptr & still treat it as 2D array inside function, 56 | since we can identify no. of colomns by 'M' size. 57 | */ 58 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#1,2/L1,2#5b.cpp: -------------------------------------------------------------------------------- 1 | #include "dynamic_array.hpp" 2 | 3 | using std::cout; 4 | 5 | //method#2 (extra): using (dynamic/heap-based) 2d jagged arrays 6 | 7 | //=> memory & performance efficient approach: only stores 'less dense' indexes of matrix 8 | //=> best use-case: when having large set of data less densely populated inside matrix 9 | 10 | typedef dynamic_array da_int;//redefining as more simplified name 11 | 12 | void check_common_course_mates(const DA& arr, int s1, int s2){ 13 | cout<<"\nstudent '"< true, 0 -> false] 30 | 31 | i/j 0, 1, 2, 3, 4 32 | 0 {0, 1, 0, 1, 1} 33 | 1 {1, 0, 1, 0, 1} 34 | 2 {0, 1, 0, 0, 0} 35 | 3 {1, 0, 0, 0, 1} 36 | 4 {1, 1, 0, 1, 0} 37 | */ 38 | 39 | DA arr{//2D jagged dynamic_array initialization as 'sparse matrix' 40 | da_int{1, 3, 4}, 41 | da_int{0, 2, 4}, 42 | da_int{1}, 43 | da_int{0, 4}, 44 | da_int{0, 1, 3} 45 | };//assuming indexes will taken in sorted order 46 | 47 | for(short i=0;i> arr{//2D jagged dynamic_array initialization 7 | DA{3.66, 3.33, 4.0, 3.0, 2.66}, 8 | DA{3.33, 3.0, 3.66, 3.0}, 9 | DA{4.0, 3.66, 2.66}, 10 | DA{2.66, 2.33, 4.0}, 11 | DA{3.33, 3.66, 4.0, 3.0, 3.33} 12 | }; 13 | 14 | std::string SN[]{"Ali", "Hiba", "Asma", "Zain", "Faisal"}; //student names 15 | 16 | cout<<"Students' Course GPA's:\n"; 17 | for(short i=0;i 1byte -> range: -128 to 127 23 | 24 | float tgpa, //total GPA 25 | tch; //total credit hours 26 | 27 | for(char i=0,j;i> arr{//2D jagged dynamic_array initialization 7 | DA{366, 333, 400, 300, 266}, 8 | DA{333, 300, 366, 300}, 9 | DA{400, 366, 266}, 10 | DA{266, 233, 400}, 11 | DA{333, 366, 400, 300, 333} 12 | }; 13 | 14 | std::string AN[]{"British Airways", "Eastern Airways", "Easy Jet", "FlyBe", "Ryanair"}; //airline names 15 | 16 | cout<<"Airline's Covered Distances (of each City):\n"; 17 | for(short i=0;i 2 | 3 | using std::cout; 4 | 5 | class Fraction{ 6 | int x,y; 7 | public: 8 | Fraction(int x=0, int y=0): 9 | x(x),y(y){}//ctor 10 | 11 | /* setter */ 12 | int input(int x=0, int y=0){ 13 | this->x=x; 14 | this->y=y; 15 | } 16 | 17 | /* getters */ 18 | int numerator()const{ return x; } 19 | int denominator()const{ return y; } 20 | 21 | void show()const{ cout<<"hi i'm show!\n"; } 22 | }; 23 | 24 | int main(){ 25 | 26 | /* Dynamic Allocation of Objects */ 27 | Fraction * fp1, * fp2, * flist; 28 | fp1 = new Fraction; // uses default constructor 29 | fp2 = new Fraction(3,5); // uses constructor with two parameters 30 | flist = new Fraction[20]; // dynamic array of 20 Fraction objects 31 | // default constructor used on each 32 | 33 | /* Notation: dot-operator vs. arrow-operator */ 34 | //for dynamic objs 35 | fp1->show(); 36 | (*fp1).show(); 37 | //for automatic objs 38 | //fp1.show(); //gives error (since dynamic) 39 | 40 | 41 | /* Deallocation with delete works the same as for basic types */ 42 | delete fp1, fp2; 43 | delete [] flist; 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#1,2/examples/static_jagged_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | 5 | void display_jagged_arrays(int** j_arrs,const int& rows,const int* cols){ 6 | for(int i=0,j;i ptr_type */, 22 | cols[]{sizeof(r1)/sizeof(int)/* 4 bytes */, 23 | sizeof(r2)/sizeof(int), 24 | sizeof(r3)/sizeof(int)}; 25 | 26 | display_jagged_arrays(jagged_arrays, rows, cols); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#10/Huffman.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HUFFMAN_GUARD 2 | #define HUFFMAN_GUARD 1 3 | 4 | #include 5 | #include 6 | 7 | #define MAX_SIZE 100 8 | 9 | template 10 | struct Huff_Node{ 11 | T data; 12 | int freq; 13 | Huff_Node* left; 14 | Huff_Node* right; 15 | 16 | Huff_Node(const T& data, int freq): 17 | data(data), freq (freq), left(nullptr), right(nullptr){} 18 | }; 19 | 20 | typedef Huff_Node HN; 21 | 22 | struct comparator{ 23 | bool operator()(HN* ob1,HN* ob2){ 24 | return ob1->freq > ob2->freq; 25 | } 26 | }; 27 | 28 | HN* generateTree(std::priority_queue, comparator> s){ 29 | while(s.size()!=1){ 30 | HN* left=s.top(); s.pop(); 31 | HN* right=s.top(); s.pop(); 32 | HN* node=new HN('$', left->freq+right->freq); 33 | 34 | node->left=left; 35 | node->right=right; 36 | s.push(node); 37 | } 38 | return s.top(); 39 | } 40 | 41 | void print(HN* root, int *arr, int top){ 42 | if(root->left){ 43 | arr[top]=0; 44 | print(root->left,arr, top + 1); 45 | } 46 | if(root->right){ 47 | arr[top]=1; 48 | print(root->right, arr, top + 1); 49 | } 50 | if(!root->left&&!root->right){ 51 | std::cout<data<<' '; 52 | for(int i=0;i, comparator> pq; 60 | for(int i=0;i 5 | void Huff_Compare(HN* root, const int (&arr)[n]){ 6 | for(int i=0;ileft&¤t->right) 9 | current=(arr[i++]?current->right:current->left); 10 | cout<data; 11 | } 12 | } 13 | 14 | int main(){ 15 | char data[]="shahzaib";//{'s','h','a','h','z','a','i','b'} 16 | int freq[]={ 5, 9, 11,12,13,16, 45 }, 17 | size=sizeof(data)/sizeof(data[0])-1; 18 | 19 | HN* root=HuffmanCoding(data, freq, size); 20 | 21 | cout<<"Decoding of 1010: "; 22 | Huff_Compare(root, {1,0,1,0}); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#10/L10#5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAX_SIZE 10 5 | 6 | void heapify(int *arr, int size, int Data){ 7 | int key=Data, left=2*Data+1, right=2*Data+2; 8 | if(left arr[key]) 9 | key=left; 10 | if(right arr[key]) 11 | key=right; 12 | if(key!=Data){ 13 | std::swap(arr[Data], arr[key]); 14 | heapify(arr, size, key); 15 | } 16 | } 17 | 18 | void insert(int *arr, int& size, int Data){ 19 | if(size>=MAX_SIZE) 20 | cout<<"\n<-----OVERFLOW OR MEMORY FULL----->\n"; 21 | else{ 22 | arr[size]=Data; 23 | if(size++) 24 | for(int x=(size/2)-1;x>=0;x--) 25 | heapify(arr, size, x); 26 | } 27 | } 28 | 29 | void delete_node(int *arr, int& size, int Data){ 30 | int index; 31 | for(int i=0;i=0;x--) 39 | heapify(arr, size, x); 40 | } 41 | 42 | void print_heap(int *arr, int size){ 43 | for(int i=0;i "<\n"; 60 | print_heap(arr, size); 61 | 62 | delete_node(arr, size, 88); 63 | cout<<"\n<=====AFTER DELETION======>\n"; 64 | print_heap(arr, size); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#10/L10#6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int calc_no_of_operations(int *arr, int size){ 6 | priority_queue, greater> pq; 7 | int n=0, d=0; 8 | for(int x=0;x 5 | 6 | #define MAX_SIZE 10 7 | 8 | template 9 | struct hash_table{ 10 | int key; 11 | T value; 12 | 13 | hash_table(int key=0, const T& value=T()): 14 | key(key), value(value){} 15 | }; 16 | 17 | template 18 | class Hash_Map{ 19 | typedef hash_table HT; 20 | 21 | HT **table; 22 | int count; 23 | 24 | public: 25 | Hash_Map(): 26 | count(0), table(new HT*[MAX_SIZE]{}){} 27 | 28 | int hash(int)const; 29 | 30 | int get_count()const{ return count; } 31 | bool isEmpty()const{ return !count; } 32 | 33 | 34 | void insert(int key, const T& value){ 35 | if(count>=10){ 36 | std::cout<<"Hash Table is Already Full.\n"; 37 | return; 38 | } 39 | int p=hash(key); 40 | HT *newEntry=new HT(key, value); 41 | count++; 42 | if(!table[p]){ 43 | table[p]=newEntry; 44 | return; 45 | } 46 | for(int i=0;ikey==key) 61 | return pos%MAX_SIZE; 62 | else ++pos; 63 | } 64 | return -1; 65 | } 66 | 67 | void remove(int key){ 68 | int pos=search(key); 69 | if(pos==-1){ 70 | std::cout<<"Key doesnt exist\n"; 71 | return; 72 | } 73 | --count; 74 | delete table[pos]; 75 | table[pos]=nullptr; 76 | } 77 | 78 | void traverse()const{ 79 | std::cout<<"Key Value\n"; 80 | for(int i=0;ikey<<" "<value<<'\n'; 83 | } 84 | 85 | ~Hash_Map(){ delete []table; } 86 | }; 87 | 88 | template<> 89 | int Hash_Map::hash(int key)const{ return key%MAX_SIZE; } 90 | template<> 91 | int Hash_Map::hash(int key)const{ return key%MAX_SIZE; } 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#11/L11#1.cpp: -------------------------------------------------------------------------------- 1 | #include "Hashing.hpp" 2 | using namespace std; 3 | 4 | int main(){ 5 | Hash_Map ob; 6 | 7 | for(int i=15;i<25;++i) 8 | ob.insert(i+8, i); 9 | 10 | ob.traverse(); 11 | cout<<"\nKey was placed on "< ob; 6 | int s; 7 | string value; 8 | for(int i=0;i<5;++i){ 9 | cout<<"Enter ID: "; 10 | cin>>s; 11 | cout<<"Enter number: "; 12 | cin>>value; 13 | ob.insert(s, value); 14 | } 15 | 16 | cout<<(ob.isEmpty()?"empty\n":"Not Empty\n") 17 | <<"Size of contact bookey: "<>s; 20 | 21 | cout<<"Pair found at index : "<>s; 24 | 25 | ob.remove(s); 26 | 27 | cout<<"Size of contact book: "< 5 | 6 | #if __cplusplus < 201103L 7 | #define noexcept 8 | #endif 9 | 10 | template 11 | struct Pair{//custom minimal implementation of std::pair 12 | T1 first; 13 | T2 second; 14 | 15 | Pair()noexcept://default ctor 16 | first(), second(){} 17 | //Note: for objs, it invokes default ctor & for primitives, it sets default values 18 | 19 | #if __cplusplus >= 201103L 20 | template//_T1&& , _T2&& both are universal references 21 | Pair(_T1&& first, _T2&&... second)noexcept://parametized ctor 22 | first(std::forward<_T1>(first)), second(std::forward<_T2>(second)...){} 23 | #else 24 | Pair(const T1& first, const T2& second): 25 | first(first), second(second){} 26 | #endif 27 | 28 | friend std::ostream& operator<<(std::ostream& out, const Pair& self){//overloaded cout method 29 | out<>(std::istream& in, Pair& self){//overloaded cin method 33 | in>>self.first>>self.second; 34 | return in; 35 | } 36 | }; 37 | 38 | #if __cplusplus >= 201103L 39 | template 40 | auto Make_Pair(T1&& first, T2&& second)->Pair{//construct Pair without providing their types 41 | return Pair(std::forward(first), std::forward(second)); 42 | }//Note: doesn't deduce complex dependant (template) types 43 | #endif 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#12/L12#1.cpp: -------------------------------------------------------------------------------- 1 | #include "Graph.hpp" 2 | using std::cout; 3 | 4 | void un_directed_graph(Graph& gu){ 5 | cout<<"[INFO] UNDIRECTED GRAPH\n\n"< *searched=gu.search_node(2);//returns the address of searched node 8 | 9 | if(searched){ 10 | cout<<"[INFO] Found Node ("<<2<<"): "; 11 | for(auto& element:*searched) 12 | cout< *searched=gd.search_node(2);//returns the address of searched node 25 | 26 | if(searched){ 27 | cout<<"[INFO] Found Node (" << 2 << "): "; 28 | for(auto& element:*searched) 29 | cout< 1 8 | 1 -> 2 and 3 9 | 2 -> 4 and 5 10 | 3 -> 6 and 7 11 | 4 -> 8 and 9 12 | 5 -> 10 and 11 13 | 11 -> 1 14 | */ 15 | Graph g(true, 12); // directed graph of size 12 16 | 17 | g.add_edge(0, 1); 18 | g.add_edge(1, 2); 19 | g.add_edge(1, 3); 20 | g.add_edge(2, 4); 21 | g.add_edge(2, 5); 22 | g.add_edge(3, 6); 23 | g.add_edge(3, 7); 24 | g.add_edge(4, 8); 25 | g.add_edge(4, 9); 26 | g.add_edge(5, 10); 27 | g.add_edge(5, 11); 28 | g.add_edge(11, 1); 29 | 30 | cout< 1 8 | 1 -> 2 and 3 9 | 2 -> 4 and 5 10 | 3 -> 6 and 7 11 | 4 -> 8 and 9 12 | 5 -> 10 and 11 13 | 11 -> 1 14 | */ 15 | Graph g(true, 12); // directed graph of size 12 16 | 17 | g.add_edge(0, 1); 18 | g.add_edge(1, 2); 19 | g.add_edge(1, 3); 20 | g.add_edge(2, 4); 21 | g.add_edge(2, 5); 22 | g.add_edge(3, 6); 23 | g.add_edge(3, 7); 24 | g.add_edge(4, 8); 25 | g.add_edge(4, 9); 26 | g.add_edge(5, 10); 27 | g.add_edge(5, 11); 28 | g.add_edge(11, 1); 29 | 30 | cout< 5 | 6 | /* Generalized & optimized swap (for move constructible heap objects): */ 7 | 8 | #if __cplusplus >= 201103L 9 | template 10 | void swap(T& a, T& b){ 11 | T temp=std::move(a); 12 | a=std::move(b); 13 | b=std::move(temp); 14 | } 15 | #else 16 | template 17 | void swap(T& a, T& b){ 18 | T temp=a; 19 | a=b; 20 | b=temp; 21 | } 22 | #endif 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#12/Weighted_Graph.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WEIGHTED_GRAPH_GUARD 2 | #define WEIGHTED_GRAPH_GUARD 1 3 | 4 | #include 5 | 6 | class Weighted_Graph{ 7 | int **adj;// adjacency matrix 8 | int size; 9 | 10 | public: 11 | Weighted_Graph(int size=10)://default 7 parametized ctor 12 | size(size), adj(new int*[size]){ 13 | for(int i=0;i 2 | 3 | using std::cout; 4 | 5 | /** Direct Recursion **/ 6 | 7 | /* part 'a': */ 8 | //given series_1: 1, 3, 6, 10, 15, 21, 28,... (size = 7) 9 | //=> 0+1=1, 1+2=3, 3+3=6, 6+4=10, 10+5=15, 15+6=21, 21+7=28 10 | 11 | /* part 'b': */ 12 | //given series_2: 1, 1, 2, 4, 7, 11, 16, 22,... (size = 8) 13 | //=> 0+1=1, 1+1=2, 2+2=4, 4+3=7, 7+4=11, 11+5=16, 16+6=22 14 | 15 | /* problem analysis & algorithm: */ 16 | //M#1=> add n+i upto i==size, where n=accumulator {starts with min. value} & i=forward_iterator 17 | //M#2=> subtract n-i upto i==lower_limit, where n=accumulator {starts with max. value} & i=backward_iterator 18 | 19 | void sequence_generator(int n, //current_value 20 | int i, //iterator 21 | const int tc){//terminating_condition 22 | if(i==tc) return; 23 | sequence_generator(n-i, i-1, tc); 24 | cout< 2 | 3 | using std::cout; 4 | 5 | /** Indirect Recursion **/ 6 | //note: for problem description & analysis, see previous task#1 (i.e. L3#1.cpp) 7 | 8 | void sequence_generator(int, int, const int);//forward_declaration 9 | 10 | void __sequence_generator(int n, int i, const int tc){ 11 | if(i==tc) return; 12 | sequence_generator(n-i, i-1, tc); 13 | cout< 2 | 3 | /* Nested Recursion */ 4 | //A recursive function passing parameter as a 5 | //recursive call or recursion inside the recursion 6 | int fun(int n){ return (n>100)?n-10:fun(fun(n+11)); } 7 | 8 | int main(){ 9 | int r=fun(95); 10 | 11 | std::cout< int r=fun(95) {waiting for '200' => returns 91} 18 | //200: fun(95) => int n=95, condition faLse: fun(fun(95+11)) {waiting for '400' => returns 91} 19 | 20 | //300: fun(95+11) => int n=106, condition true: returns 106-10=96 21 | //400: fun(fun(95+11)) => int n=96, condition faLse: fun(fun(96+11)) {waiting for '600' => returns 91} 22 | 23 | //500: fun(96+11) => int n=107, condition true: returns 107-10=97 24 | //600: fun(fun(96+11)) => int n=97, condition faLse: fun(fun(97+11)) {waiting for '800' => returns 91} 25 | 26 | //700: fun(97+11) => int n=108, condition true: returns 108-10=98 27 | //800: fun(fun(97+11)) => int n=98, condition faLse: fun(fun(98+11)) {waiting for '1000' => returns 91} 28 | 29 | //900: fun(98+11) => int n=109, condition true: returns 109-10=99 30 | //1000: fun(fun(98+11)) => int n=99, condition faLse: fun(fun(99+11)) {waiting for '1200' => returns 91} 31 | 32 | //1100: fun(99+11) => int n=110, condition true: returns 110-10=100 33 | //1200: fun(fun(99+11)) => int n=100, condition faLse: fun(fun(100+11)) {waiting for '1400' => returns 91} 34 | 35 | //1300: fun(99+11) => int n=110, condition true: returns 110-10=100 36 | //1400: fun(fun(99+11)) => int n=100, condition faLse: fun(fun(100+11)) {waiting for '1600' => returns 91} 37 | 38 | //1500: fun(99+11) => int n=111, condition true: returns 111-10=101 39 | //1600: fun(fun(99+11)) => int n=101, condition true: returns 101-10=91 40 | 41 | //Note: for more understanding, see 'L#3#4_stack_activation_record.jpg' 42 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#3/L3#4_stack_activation_record.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#3/L3#4_stack_activation_record.jpg -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#3/L3#5a.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#3/L3#5a.cpp -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#3/Lab_Manual#3.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#3/Lab_Manual#3.docx -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#3/warning.hpp: -------------------------------------------------------------------------------- 1 | #error This file requires compiler and library support for the ISO C++ 2011 standard \ 2 | and must be enabled with the -std=c++11 or -std=gnu++11 compiler options. 3 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#4/L4#1.cpp: -------------------------------------------------------------------------------- 1 | #include "Sorts.hpp" 2 | 3 | int main(){ 4 | std::string arr[]={"good","lol","taebak","fun","great", "k4"};//stack/local 1d array 5 | const int N=sizeof(arr)/sizeof(arr[0]);//array size 6 | 7 | sort::bubble(arr, N);//ascending order 8 | // sort::bubble(arr, arr+N);//iterator version 9 | 10 | // sort::bubble(arr, N, lesser);//descending order 11 | // sort::bubble(arr, arr+N, lesser); 12 | 13 | for(size_t i=0;i(const date& other)const{ return other.yearyear; } 12 | 13 | /* overloaded 'cin/cout' methods */ 14 | friend std::ostream& operator<<(std::ostream& out,const date& self){ 15 | out<>(std::istream& in,date& self){ 19 | in>>self.day>>self.month>>self.year; 20 | return in; 21 | } 22 | }; 23 | 24 | int main(){ 25 | const int N=5;//array size 26 | // date arr[N];//array of N date objects 27 | 28 | // for(size_t i=0;i>arr[i]; 30 | 31 | date arr[]={//hard-code approach for testing 32 | date(1, 12, 2020), 33 | date(11, 11, 2011), 34 | date(7, 3, 2001), 35 | date(3, 4, 1999), 36 | date(18, 7, 2011) 37 | }; 38 | 39 | sort::selection(arr, N);//ascending order 40 | // sort::selection(arr, arr+N);//iterator version 41 | 42 | // sort::selection(arr, N, lesser);//descending order 43 | // sort::selection(arr, arr+N, lesser); 44 | 45 | for(size_t i=0;i array of cost of toys 8 | K => given amount for buying toys 9 | N => array size 10 | */ 11 | 12 | /* test_case 1: */ 13 | u_int K=50, arr[]={1, 12, 5, 111, 200, 1000, 10, K+1}; 14 | 15 | /* test_case 2: */ 16 | // u_int K=100, arr[]={20, 30, 50, K+1}; 17 | 18 | /* important_note: 19 | here, last position of array is set as 'sentinal value' in order to avoid 20 | array bound (range) check for optimized results in below while loop [at line: 26]. 21 | hence, when 'i' reaches (N+1)th index, K will become less, hence loop will terminate 22 | */ 23 | 24 | u_int N=sizeof(arr)/4-1;//value set as N-1 to skip 'sentinal value' 25 | 26 | sort::bubble(arr, N);//optimized, ascending-order 27 | /* important_note: 28 | in order to find the maximum no. of buyable toys with K amount, we need to sort costs first 29 | note that, this will break the queue order, as costs will be rearranged in ascending order 30 | */ 31 | u_int i=0; //iterator for counting maximum buyable toys 32 | while(K>=arr[i])//if given amount is sufficient for buying current toy 33 | K-=arr[i++];//then subtract its cost from given amount 34 | N=i; //otherwise break the loop & set the count to N 35 | 36 | std::cout<<"maximum buyable toys: "< 2 | 3 | struct indexes{ 4 | int intial, final; 5 | 6 | indexes(int i=0, int f=0)://default & parameterized ctor 7 | intial(i),final(f){} 8 | 9 | friend std::ostream& operator<<(std::ostream& out,const indexes& self){ 10 | if(self.intial) 11 | out<arr[s+1]) 23 | break; 24 | if (s==n-1) 25 | return indexes(); 26 | 27 | //step 1(b) 28 | for(e=n-1;e>0;--e) 29 | if(arr[e]max) 36 | max=arr[i]; 37 | if(arr[i]min){ 44 | s=i; 45 | break; 46 | } 47 | 48 | //step 2(c) 49 | for(i=n-1;i>=e+1;i--) 50 | if(arr[i] 3 | 4 | /* Doubly Linked List Operations: */ 5 | int main(){ 6 | List DLL; 7 | 8 | DLL.push_back(11); //i. Insert a new node at the end of the list 9 | //11 10 | 11 | DLL.push_front(22); //ii. Insert a new node at the beginning of list. 12 | //22,11 13 | 14 | DLL.insert(2-1, 33);//iii. Insert a new node at given position. 15 | //22,33,11 // [here, inserting node at 2nd position OR 1st index] 16 | 17 | DLL.erase(0ull); //iv. Delete any node. (note: 0ull insead of 0 to resolve overload ambiguity error) 18 | //33,11 // [here, deleting 1st position node] 19 | 20 | DLL.traverse(); //v. Print the complete doubly link list. 21 | //33->11->N // [here, traversing in ascending order] 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#6/L6#2.cpp: -------------------------------------------------------------------------------- 1 | #include "Circular_Forward_List.hpp" 2 | 3 | /* Circular Singly Linked List Operations: */ 4 | int main(){ 5 | Circular_Forward_List CSLL; 6 | 7 | CSLL.push_back(11); //i. Insert a new node at the end of the list 8 | //11 9 | 10 | CSLL.push_front(22);//ii. Insert a new node at the beginning of list. 11 | //22,11 12 | 13 | CSLL.insert(2, 33); //iii. Insert a new node at given position. 14 | //22,33,11 // [here, inserting node at 2nd position] 15 | 16 | CSLL.erase(0); //iv. Delete any node. 17 | //33,11 // [here, deleting 1st position node] 18 | 19 | CSLL.traverse(); //v. Print the complete circular link list. 20 | //33->11->N // [here, traversing in ascending order] 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#6/L6#3.cpp: -------------------------------------------------------------------------------- 1 | #include "Circular_List.hpp" 2 | 3 | /* Circular Doubly Linked List Operations: */ 4 | int main(){ 5 | Circular_List CDLL; 6 | 7 | CDLL.push_back(11); //i. Insert a new node at the end of the list 8 | //11 9 | 10 | CDLL.push_front(22); //ii. Insert a new node at the beginning of list. 11 | //22,11 12 | 13 | CDLL.insert(2, 33); //iii. Insert a new node at given position. 14 | //22,33,11 // [here, inserting node at 2nd position] 15 | 16 | CDLL.erase(0); //iv. Delete any node. 17 | //33,11 // [here, deleting 1st position node] 18 | 19 | CDLL.traverse_forward();//v. Print the complete circular doubly link list. 20 | //33->11->N // [here, traversing in ascending order] 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#6/L6#4.cpp: -------------------------------------------------------------------------------- 1 | #include "List.hpp" 2 | //#include 3 | 4 | using std::cout; 5 | 6 | /* Doubly Linked List Merge Operation: */ 7 | int main(){ 8 | #if __cplusplus >= 201103L 9 | List list({1,2,3,4,5,6,7,8,9}), 10 | list2({99,88,77,66,55}); 11 | #else 12 | List list, list2; 13 | for(int i=1;i<=9;++i) 14 | list.push_back(i); 15 | for(int i=99;i>=55;i-=11) 16 | list2.push_back(i); 17 | #endif 18 | 19 | cout<<"BEFORE:-\n" 20 | <<"list1: "< 5 | 6 | #if __cplusplus < 201103L 7 | #define nullptr 0L 8 | #define noexcept 9 | #endif 10 | 11 | template 12 | struct SLL_Node{//Singly Linked List Node 13 | T data; 14 | SLL_Node *next; 15 | 16 | #if __cplusplus < 201103L 17 | SLL_Node(const T& data=T())noexcept://default & parameterized ctor 18 | data(data), next(nullptr){} 19 | #else 20 | template//variadic template 21 | SLL_Node(_T&&... data)noexcept://emplaced ctor 22 | data{std::forward<_T>(data)...},//perfect forwarding 23 | next(nullptr){} 24 | #endif 25 | }; 26 | 27 | template 28 | struct DLL_Node{//Doubly Linked List Node 29 | T data; 30 | DLL_Node *next, *prev; 31 | 32 | #if __cplusplus < 201103L 33 | DLL_Node(const T& data=T())noexcept: 34 | data(data), next(nullptr), prev(nullptr){} 35 | #else 36 | template 37 | DLL_Node(_T&&... data)noexcept: 38 | data{std::forward<_T>(data)...}, 39 | next(nullptr), prev(nullptr){} 40 | #endif 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#6/Swap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SWAP_GUARD 2 | #define SWAP_GUARD 1 3 | 4 | #include 5 | 6 | /* Generalized & optimized swap (for move constructible heap objects): */ 7 | 8 | #if __cplusplus >= 201103L 9 | template 10 | void swap(T& a, T& b){ 11 | T temp=std::move(a); 12 | a=std::move(b); 13 | b=std::move(temp); 14 | } 15 | #else 16 | template 17 | void swap(T& a, T& b){ 18 | T temp=a; 19 | a=b; 20 | b=temp; 21 | } 22 | #endif 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#8/L8#1.cpp: -------------------------------------------------------------------------------- 1 | #include "F_Stack.hpp" 2 | using std::cout; 3 | 4 | int main(){ 5 | const int N=10;//Stack Limit 6 | F_Stack s; 7 | 8 | for(int i=1;i<=N;++i){//step 1 9 | if(s.full()){//step 2 10 | cout<<"Stack overflow!"; 11 | break; 12 | } 13 | s.push(i); 14 | } 15 | 16 | s.clear();//step 3: similar to 'N' pops 17 | if(s.empty()) 18 | cout<<"Stack is empty!"; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#8/L8#2.cpp: -------------------------------------------------------------------------------- 1 | #include "D_Stack.hpp" 2 | using std::cout; 3 | 4 | int main(){ 5 | D_Stack s({1,2,3,4,5,6,7,8,9,10});//step 1: similar to push 10 times 6 | 7 | s.pop();//step 3 8 | 9 | //step 2: 10 | cout< 13 | string InfixToPostfix(const char (&infix)[N]){ 14 | F_Stack stack; 15 | string postfix; 16 | 17 | for(int i=0;i='a'&&infix[i]<='z')||(infix[i]>='A'&&infix[i]<='Z')) 19 | postfix+=infix[i]; 20 | else if(infix[i] == '(') 21 | stack.push(infix[i]); 22 | else if(infix[i] == ')'){ 23 | while(stack.peek()!='(' && !stack.empty()){ 24 | char temp=stack.peek(); 25 | postfix+=temp; 26 | stack.pop(); 27 | } 28 | if(stack.peek()=='(') 29 | stack.pop(); 30 | } 31 | else if(isOperator(infix[i])){ 32 | if(stack.empty()) 33 | stack.push(infix[i]); 34 | else{ 35 | if(precedence(infix[i])>precedence(stack.peek())) 36 | stack.push(infix[i]); 37 | else if(precedence(infix[i])==precedence(stack.peek()) && infix[i]=='^') 38 | stack.push(infix[i]); 39 | else{ 40 | while(!stack.empty() && precedence(infix[i])<=precedence(stack.peek())){ 41 | postfix+=stack.peek(); 42 | stack.pop(); 43 | } 44 | stack.push(infix[i]); 45 | } 46 | } 47 | } 48 | } 49 | while(!stack.empty()){ 50 | postfix+=stack.peek(); 51 | stack.pop(); 52 | } 53 | 54 | return postfix; 55 | } 56 | 57 | int main(){ 58 | cout<<"POSTFIX EXPRESSION: " 59 | < q;//using fixed size circular queue for normal queue operations 7 | 8 | for(int i=1;i<=N;++i){ 9 | if(q.full()){//step 1 10 | cout<<"queue capacity is full!"; 11 | break; 12 | } 13 | q.push_back(i);//step 2: same as enqueue in FIFO order 14 | } 15 | 16 | q.pop_front();//step 3: same as dequeue in FIFO order 17 | 18 | cout< q({1,2,3,4,5,6,7,8,9,10});//step 5: same as enqueue 10 times in FIFO order 9 | 10 | q.pop_front();//step 6: same as dequeue in FIFO order 11 | 12 | cout<& Undo, D_Stack& Redo){ 5 | Redo.push(Undo.pop(true)); 6 | } 7 | 8 | void REDO(D_Stack& Undo, D_Stack& Redo){ 9 | Undo.push(Redo.pop(true)); 10 | } 11 | 12 | void READ(D_Stack& Undo){ 13 | D_Stack revOrder; 14 | while(!Undo.empty()) 15 | revOrder.push(Undo.pop(true)); 16 | 17 | while(!revOrder.empty()){//Traverse Undo stack in ascending order 18 | cout<& Undo, char X){ Undo.push(X); } 25 | 26 | template 27 | void QUERY(const char* (&&Q)[N]){ 28 | D_Stack Undo,//Stores the history of all the queries that have been processed on the document 29 | Redo;//Stores the elements of REDO query 30 | 31 | for(const auto& iter:Q){//Traverse all the query 32 | if(iter=="UNDO") 33 | UNDO(Undo, Redo); 34 | else if(iter=="REDO") 35 | REDO(Undo, Redo); 36 | else if(iter=="READ") 37 | READ(Undo); 38 | else 39 | WRITE(Undo, iter[6]); 40 | } 41 | } 42 | 43 | int main(){ 44 | QUERY({"WRITE A", "WRITE B", "WRITE C", "UNDO", "READ", "REDO", "READ"}); 45 | 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#8/L8#7.cpp: -------------------------------------------------------------------------------- 1 | #include "F_Stack.hpp" 2 | using namespace std; 3 | 4 | template 5 | bool is_valid_parenthesis(const char (&str)[N]){ 6 | F_Stack s; 7 | for(char c: str){ 8 | if(c=='(') s.push(')'); 9 | else if(c=='{') s.push('}'); 10 | else if(c=='[') s.push(']'); 11 | else if(!s.empty()){ 12 | if(s.peek()==c) s.pop(); 13 | else return false; 14 | } 15 | } 16 | return s.empty(); 17 | } 18 | 19 | template 20 | void check_expression(const char (&expr)[N]){ 21 | std::cout< 5 | int maxValue(T* node){ 6 | return node? std::max(node->key, std::max(maxValue(node->left), maxValue(node->right))): INT16_MIN; 7 | } 8 | template 9 | int minValue(T* node){ 10 | return node? std::min(node->key, std::min(minValue(node->left), minValue(node->right))): INT16_MAX; 11 | } 12 | template 13 | bool isBST(T* node){ 14 | return !( node && 15 | ( node->left&&maxValue(node->left)>node->key || node->right&&minValue(node->right)key || 16 | !( isBST(node->left)&&isBST(node->right) ) ) ); 17 | } 18 | 19 | typedef BST::node N; 20 | 21 | void test_case_1(N** root){ 22 | *root=new N(4); 23 | (*root)->left=new N(2); 24 | (*root)->right=new N(5); 25 | // (*root)->right->left=new N(7);//makes it non-bst tree 26 | (*root)->left->left=new N(1); 27 | (*root)->left->right=new N(3); 28 | } 29 | 30 | void test_case_2(N** root){ 31 | *root=new N(5); 32 | (*root)->left=new N(2); 33 | (*root)->right=new N(3); 34 | (*root)->left->left=new N(1); 35 | (*root)->right->right=new N(7); 36 | } 37 | 38 | int main(){ 39 | N* root=nullptr; 40 | 41 | test_case_1(&root);//bst tree 42 | // test_case_2(&root);//non-bst tree 43 | 44 | cout<<(isBST(root)?"is BST (10 points)":"isn't BST (0 points)");//acts as auto grader 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#9/L9#2,3.cpp: -------------------------------------------------------------------------------- 1 | #include "BST.hpp" 2 | #include 3 | using std::cout; 4 | 5 | template 6 | void BST::iterative_pre_order_traversal()const{//task 3 (part B) 7 | std::stack s; 8 | if(!root) return; 9 | s.push(root); 10 | while(!s.empty()){ 11 | node *curr=s.top(); 12 | s.pop(); 13 | cout<key<<' '; 14 | if(curr->right) 15 | s.push(curr->right); 16 | if(curr->left) 17 | s.push(curr->left); 18 | } 19 | } 20 | 21 | int main(){ 22 | #if __cplusplus >= 201103L 23 | BST tree({15,10,20,8,12,16,25});//task 2: using move_initializer_list format 24 | #else 25 | BST tree; 26 | 27 | int arr[]={15,10,20,8,12,16,25}, N=sizeof(arr)/sizeof(arr[0]); 28 | 29 | for(int i=0;i//specialized for int datatype only 5 | BST::node* BST::remove(node *current, const int (&range)[2]){ 6 | if(!current) return nullptr; 7 | current->left=remove(current->left, range); 8 | current->right=remove(current->right, range); 9 | if(current->keyright; 11 | delete current; 12 | return rChild; 13 | } 14 | if(current->key>range[1]){//max case 15 | node *lChild=current->left; 16 | delete current; 17 | return lChild; 18 | } 19 | return current; 20 | } 21 | template<>//specialized for int datatype only 22 | void BST::remove(const int (&range)[2]){//task 4 23 | root=remove(root, range); 24 | } 25 | 26 | int main(){ 27 | #if __cplusplus >= 201103L 28 | BST tree({15,10,20,8,12,18,25}); 29 | #else 30 | BST tree; 31 | 32 | int arr[]={15,10,20,8,12,18,25}, 33 | N=sizeof(arr)/sizeof(arr[0]); 34 | 35 | for(int i=0;i= 201103L 44 | tree.remove({9, 12}); 45 | #else 46 | int range[]={9, 12}; 47 | tree.remove(range);//no concept of direct use of r-values in C++03 48 | #endif 49 | 50 | cout<<"\nAfter: "; 51 | tree.in_order_traversal(); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#9/L9#5.cpp: -------------------------------------------------------------------------------- 1 | #include "BST.hpp" 2 | using std::cout; 3 | 4 | template 5 | __int64 height(T *current){ 6 | return current?std::max(height(current->left), height(current->right))+1:0; 7 | } 8 | template 9 | __int64 balance_factor(T *current){ 10 | return current?height(current->left)-height(current->right):0; 11 | } 12 | 13 | template 14 | void BST::display_in_ordered_bf(node *current)const{ 15 | if(current){ 16 | display_in_ordered_bf(current->left); 17 | std::cout<key<<":\t"<right); 19 | } 20 | } 21 | template 22 | void BST::display_in_ordered_bf()const{ 23 | std::cout<<"key: balance_factor\n"; 24 | display_in_ordered_bf(root); 25 | } 26 | 27 | int main(){ 28 | #if __cplusplus >= 201103L 29 | BST tree({5,10,2,4,9,11,12,13}); 30 | #else 31 | BST tree; 32 | 33 | int arr[]={5,10,2,4,9,11,12,13}, 34 | N=sizeof(arr)/sizeof(arr[0]); 35 | 36 | for(int i=0;i 5 | void AVL::display_in_ordered_bf(node *current)const{ 6 | if(current){ 7 | display_in_ordered_bf(current->left); 8 | std::cout<data<<":\t"<right); 10 | } 11 | } 12 | template 13 | void AVL::display_in_ordered_bf()const{ 14 | std::cout<<"key: balance_factor\n"; 15 | display_in_ordered_bf(root); 16 | } 17 | 18 | int main(){ 19 | #if __cplusplus >= 201103L 20 | AVL tree({5,10,2,4,9,11,12,13}); 21 | #else 22 | AVL tree; 23 | 24 | int arr[]={5,10,2,4,9,11,12,13}, 25 | N=sizeof(arr)/sizeof(arr[0]); 26 | 27 | for(int i=0;i 5 | __int64 height(T *current){ 6 | return current?std::max(height(current->left), height(current->right))+1:0; 7 | } 8 | template 9 | __int64 balance_factor(T *current){ 10 | return current?height(current->left)-height(current->right):0; 11 | } 12 | 13 | template 14 | int maxValue(T* node){ 15 | return node? std::max(node->key, std::max(maxValue(node->left), maxValue(node->right))): INT16_MIN; 16 | } 17 | template 18 | int minValue(T* node){ 19 | return node? std::min(node->key, std::min(minValue(node->left), minValue(node->right))): INT16_MAX; 20 | } 21 | template 22 | bool isBST(T* node){ 23 | return !( node && 24 | ( node->left&&maxValue(node->left)>node->key || node->right&&minValue(node->right)key || 25 | !( isBST(node->left)&&isBST(node->right) ) ) ); 26 | } 27 | 28 | typedef BST::node N; 29 | 30 | N* get_tree(){ 31 | N* root=new N(4); 32 | root->left=new N(2); 33 | root->right=new N(5); 34 | // root->right->left=new N(7);//1 35 | root->left->left=new N(1); 36 | root->left->right=new N(3); 37 | // root->left->left->left=new N(-1);//2 38 | return root; 39 | } 40 | 41 | /*Notes:- 42 | 1) if 1 & 2 both are commented, then its: 43 | both AVL & BST 44 | 2) if only 1 is commented, then its: 45 | NOT AVL but BST 46 | 3) if only 2 is commented or non of them, then its: 47 | Neither BST nor AVL 48 | */ 49 | 50 | int main(){ 51 | N* root=get_tree(); __int64 bf; 52 | 53 | cout<<(isBST(root)? 54 | ((bf=balance_factor(root))==-1||bf==0||bf==1)? 55 | "both AVL and BST":"NOT AVL but BST":"Neither BST nor AVL"); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#9/L9#8.cpp: -------------------------------------------------------------------------------- 1 | #include "BST.hpp" 2 | using std::cout; 3 | 4 | template 5 | typename BST::node* BST::convert_sorted_array(const T* arr, __int64 s, __int64 e){ 6 | if(s>e) return nullptr; 7 | __int64 mid=(s+e)/2; 8 | node* current=new node(arr[mid]); 9 | current->left=convert_sorted_array(arr, s, mid-1); 10 | current->right=convert_sorted_array(arr, mid+1, e); 11 | return current; 12 | } 13 | 14 | template 15 | template 16 | void BST::convert_sorted_array(const T (&arr)[N]){ 17 | clear(); 18 | root=convert_sorted_array(arr, 0, N-1); 19 | } 20 | 21 | int main(){ 22 | BST tree; 23 | tree.convert_sorted_array({1,2,3,4,5,6,7,8,9,10}); 24 | tree.post_order_traversal(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#9/Lab_Manual#9.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/LAB#9/Lab_Manual#9.docx -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/LAB/Sir Ali Shah Fatimi (Improvement)/Lab#7/L7#1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //Merges back the partitions of actual array 5 | void merge(int arr[], int beg, int mid, int end, int max){ 6 | int i=beg, j=mid+1, k=beg; 7 | while(i<=mid&&j<=end) 8 | arr[k++]+=(arr[(arr[i]%max<=arr[j]%max)?i++:j++]%max)*max; 9 | while(i<=mid) 10 | arr[k++]+=(arr[i++]%max)*max; 11 | while(j<=end) 12 | arr[k++]+=(arr[j++]%max)*max; 13 | i=beg; 14 | while(i<=end)//obtains actual values 15 | arr[i++]/=max; 16 | } 17 | 18 | //Recursive Merge Sort (with extra parameter for storing max. element) 19 | void RMS(int arr[], int beg, int end, int max){ 20 | if(beg 30 | void merge_sort(int (&&arr)[n]){ 31 | RMS(arr, 0, n-1, *std::max_element(arr, arr+n)+1); 32 | 33 | std::cout<<"Sorted array \n"; 34 | for (auto& it: arr) 35 | std::cout< 2 | 3 | void quick_sort(int arr[], int low, int high){ 4 | if(low>=high) return; 5 | int i=low-1, j=high+1, pivot=arr[(low+high)/2]; 6 | while(true){ 7 | while(arr[++i]pivot); 9 | if(i>=j) break; 10 | std::swap(arr[i], arr[j]); 11 | } 12 | quick_sort(arr, low, j); 13 | quick_sort(arr, j+1, high); 14 | } 15 | 16 | int main(){ 17 | /* test case 1: */ 18 | // int arr[]={5, 4, 3, 2, 1}; 19 | 20 | /* test case 2: */ 21 | int arr[]={999, 612, 589, 856, 56, 945, 243}; 22 | 23 | quick_sort(arr, 0, sizeof(arr)/sizeof(arr[0])-1); 24 | 25 | std::cout<<"Sorted array \n"; 26 | for (auto& it: arr) 27 | std::cout< 2 | 3 | using std::cout; 4 | 5 | int counter=0;//global variable for tracking iterations 6 | 7 | int interpolation_search(int arr[], int low, int high, int key){ 8 | ++counter; 9 | if(low>high||keyarr[high]) return -1;//bound exception safety 10 | int pos=low+(((double)(high-low)/(arr[high]-arr[low]))*(key-arr[low]));//Probing position w.r.t uniform distribution. 11 | return (arr[pos]==key?pos://Condition of target found 12 | (arr[pos] 2 | 3 | int find_local_minimum(int* arr, int size){ 4 | if (size<2) return -1;//expects atleast 2 elements 5 | if(arr[0]<=arr[1]) return 0;//left boundary/corner element should be less than or equals to right-most neighbor 6 | for(int i=0;i 2 | 3 | class bool_2d_array{ 4 | int rows, cols; 5 | bool** data; 6 | 7 | public: 8 | template 9 | bool_2d_array(const int (&arr)[N][M])://holds temporary 2d array of integers of N*M size 10 | rows(N),cols(M),data(new bool*[N]){ 11 | for(int i=0,j;i 2 | 3 | void shift_array_circularly(int* arr, int size, int K, bool flag=false){ 4 | if(flag) 5 | while(K--) 6 | for(int i=0;i 2 | #include 3 | 4 | //using std::vector for Final_Array, since we can't assume its actual size 5 | typedef std::vector int_vec; 6 | 7 | int_vec add_split_sum_of_two_arrays(int* Arr1,int* Arr2, int size1, int size2, const int max_digit_size=2){ 8 | int_vec Arr3;//Final_Array 9 | Arr3.reserve((size1+size2)*max_digit_size);//set to handle max possible split for 2 digits 10 | 11 | int i=0, N=(size1>=size2?size2:size1), sum, rem; 12 | 13 | while(i10)//store divisors of sum until only 1 digit is left 17 | Arr3.push_back(sum/=10); 18 | Arr3.push_back(rem); 19 | } 20 | 21 | //for remaining elements of either Arr1 or Arr2, do the same operations as above: 22 | if(size1>=size2) 23 | while(i10) 27 | Arr3.push_back(sum/=10); 28 | Arr3.push_back(rem); 29 | } 30 | else 31 | while(i10) 35 | Arr3.push_back(sum/=10); 36 | Arr3.push_back(rem); 37 | } 38 | 39 | #if __cplusplus >= 201103L 40 | Arr3.shrink_to_fit();//sets capacity = size, i.e. deallocates unused memory 41 | #endif 42 | return Arr3;//returns as rvalue (because of R.V.O) 43 | } 44 | 45 | int main(){ 46 | int Arr1[]={23,5,2,7}, 47 | Arr2[]={4,67,2}; 48 | 49 | int_vec Arr3=add_split_sum_of_two_arrays(Arr1, Arr2, 4, 3);//move (shallow copy) constructed 50 | 51 | for(int i=0,N=Arr3.size();i Week1_Task1 3 | 2) W1_T2.cpp is already done in 'Sir Ali Shah Fatimi (Improvement)/LAB#1,2' as L1,2#1.cpp 4 | 3) W1_T3.cpp is already done in 'Sir Ali Shah Fatimi (Improvement)/LAB#1,2' as L1,2#7.cpp 5 | 6 | 4) Qz1_ScD_Q1.cpp => Quiz1_SectionD_Question1 7 | 5) Qz1_ScD_Q1_Alt.cpp => Quiz1_SectionD_Question1_alternate 8 | 9 | 6) Prac_Probs_W#1_Q1.cpp => Practice_Problems_Week#1_Question1 (simple array problem) 10 | 7) Prac_Probs_W#1_Q3.cpp => Practice_Problems_Week#1_Question3 (DMA) 11 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Assignments_&_Practice_Tasks/Ms. Farah Sadia & Sir Shoaib Rauf/prac_week#1_ADT_Arrays/W1_T1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // assuming un-sorted array 5 | 6 | void MinMax(int* arr,int N, int* min,int* max){ 7 | *min=*max=arr[0]; 8 | for(int i=1,j; i*max) 12 | *max=arr[i]; 13 | } 14 | } 15 | 16 | int main(){ 17 | int arr[]={11,15,156,44,9,17,8,76,17}, 18 | N=sizeof(arr)/sizeof(arr[0]), 19 | min=-1, max=-1; 20 | 21 | MinMax(arr, N, &min, &max); 22 | 23 | cout<<"min: "< 2 | using namespace std; 3 | 4 | void print_natural_nos(int n){//ascending order 5 | if(!n) return; 6 | print_natural_nos(n-1);//non-tail recursion 7 | cout< 2 | using namespace std; 3 | 4 | #if __cplusplus < 201103L 5 | #define nullptr 0L 6 | #define noexcept 7 | #endif 8 | 9 | template 10 | struct node{ 11 | T data; 12 | node *next; 13 | 14 | node(const T& data=T())noexcept: 15 | data(data),next(nullptr){} 16 | 17 | static node* intersection(node* head1,node* head2){ 18 | if (!head1||!head2) 19 | return nullptr; 20 | node *temp1=head1,*temp2=head2; 21 | while(temp1!=temp2){ 22 | temp1=temp1->next; 23 | temp2=temp2->next; 24 | if(temp1==temp2) 25 | return temp1; 26 | if (!temp1) 27 | temp1=head2; 28 | if (!temp2) 29 | temp2=head1; 30 | } 31 | return temp1; 32 | } 33 | }; 34 | 35 | template 36 | struct stack{ 37 | node *head; 38 | 39 | stack()noexcept: 40 | head(nullptr){} 41 | 42 | void push_front(node *new_node){ 43 | new_node->next=head; 44 | head=new_node; 45 | } 46 | void push_back(node *new_node){ 47 | if(!head) 48 | head=new_node; 49 | else{ 50 | node *temp=head; 51 | while(temp->next) 52 | temp=temp->next; 53 | temp->next=new_node; 54 | } 55 | } 56 | friend ostream& operator<<(ostream& out,const stack& self){ 57 | for(node* temp=self.head;temp;temp=temp->next) 58 | out<data<<" "; 59 | return out; 60 | } 61 | }; 62 | 63 | int main(){ 64 | stack stack1; 65 | stack1.push_front(new node(30)); 66 | node* temp=new node(15); 67 | stack1.push_front(temp); 68 | stack1.push_front(new node(9)); 69 | stack1.push_front(new node(6)); 70 | stack1.push_front(new node(3)); 71 | 72 | stack stack2; 73 | stack2.push_back(temp);//Note: can't use push_front for more than 1 node 74 | stack2.push_front(new node(10)); 75 | 76 | cout<<"Stack1: "<::intersection(stack1.head,stack2.head); 80 | cout<<"Intersecting Node: "; 81 | if(temp) 82 | cout<data; 83 | else 84 | cout<<"N.I.L"; 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Books/DataStructures_&_Algorithms_in_Cpp_4th_Ed_by_Adam_Drozdek.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Books/DataStructures_&_Algorithms_in_Cpp_4th_Ed_by_Adam_Drozdek.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Books/SAMS_Teach_Yourself_Cpp_in_One_Hour_a_Day_6th_Ed_using_Cpp98_&_Cpp03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Books/SAMS_Teach_Yourself_Cpp_in_One_Hour_a_Day_6th_Ed_using_Cpp98_&_Cpp03.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Books/SAMS_Teach_Yourself_Cpp_in_One_Hour_a_Day_7th_Ed_sample_using_Cpp11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Books/SAMS_Teach_Yourself_Cpp_in_One_Hour_a_Day_7th_Ed_sample_using_Cpp11.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Books/SAMS_Teach_Yourself_Cpp_in_One_Hour_a_Day_8th_Ed_using_Cpp14_&Cpp17.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Books/SAMS_Teach_Yourself_Cpp_in_One_Hour_a_Day_8th_Ed_using_Cpp14_&Cpp17.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Course Outline.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Course Outline.docx -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Miss_Nida_Perwaiz.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Miss_Nida_Perwaiz.zip -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#10_Binary_Search_Tree.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#10_Binary_Search_Tree.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#12_AVL_Tree.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#12_AVL_Tree.pptx -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#13_Priority_Queues_&_Heap.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#13_Priority_Queues_&_Heap.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#14_Hashing.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#14_Hashing.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#15_Graphs.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#15_Graphs.pptx -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#1_DMA.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#1_DMA.docx -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#1_Introduction_to_DS.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#1_Introduction_to_DS.pptx -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#2_Recursion_&_Backtracking.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#2_Recursion_&_Backtracking.pptx -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#3,4_Linked_Lists.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#3,4_Linked_Lists.pptx -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#5_Elementary_Sorting_Algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#5_Elementary_Sorting_Algorithms.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#7_Advance_Sorting_Algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#7_Advance_Sorting_Algorithms.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#8_Searching_Algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#8_Searching_Algorithms.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#9_Stacks_&_Queues.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Lectures/Sir Shoaib Rauf/Week#9_Stacks_&_Queues.pdf -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Resources/DS_Past_Papers.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Resources/DS_Past_Papers.zip -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Resources/DS_Source_Codes.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Resources/DS_Source_Codes.zip -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Resources/SAMS_Teach_Yourself_Cpp_7th_Ed_Source_Codes.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Resources/SAMS_Teach_Yourself_Cpp_7th_Ed_Source_Codes.zip -------------------------------------------------------------------------------- /DS_Course_Spring_21_&_Fall_22/THEORY/Rule_of_Three.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/DS_Course_Spring_21_&_Fall_22/THEORY/Rule_of_Three.pdf -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Algorithms/Recursion/Binary_Combinations(R).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void Bin(char arr[],int n){ 4 | static int count=0; 5 | if (n<1) 6 | cout<<++count<<") "< 2 | using namespace std; 3 | 4 | void binconversion(int num){ 5 | if(num == 0) 6 | return; 7 | else 8 | binconversion(num/2); //non-tail recursion 9 | 10 | if((num%2) == 0){ 11 | cout<< "0"; 12 | } 13 | else{ 14 | cout<< "1"; 15 | } 16 | } 17 | 18 | int main(){ 19 | int dec; 20 | cout<< "DECIMAL NUMBER: "; 21 | cin>> dec; 22 | cout<< "BINARY EQUIVALENT: "; 23 | binconversion(dec); 24 | 25 | cout<< endl<< endl; 26 | system("PAUSE"); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Algorithms/Recursion/Digit_Sum(R).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sumdig(int num, int dig){ 5 | static int sum = 0; 6 | 7 | if(num <= 0 ){ 8 | return 0; 9 | } 10 | else{ 11 | dig = num%10; 12 | num/= 10; 13 | sum = sum + dig; 14 | sumdig(num, dig); 15 | } 16 | cout<< sum; 17 | exit(1); 18 | } 19 | 20 | int main(){ 21 | int num; 22 | 23 | cout<< endl<< "ENTER A NUMBER: "; 24 | cin>> num; 25 | 26 | int sum = sumdig(num, 0); 27 | cout<< "THE SUM OF THE DIGITS OF THE NUMBER YOU ENTERED IS: "<< sum<< endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Algorithms/Recursion/Factorial(R).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int Fact(int n){ 4 | if(n==0) 5 | return 1; 6 | return Fact(n-1)*n; 7 | } 8 | int Ifact(int n){ 9 | int i=1,f=1; 10 | for(;i<=n;++i) 11 | f*=i; 12 | return f; 13 | } 14 | 15 | int main(){ 16 | short n=6; 17 | cout<<"Factorial of "< 2 | using namespace std; 3 | int Fib(int n){ 4 | if (n==1||n==2) 5 | return 1; 6 | return (Fib(n-1)+Fib(n-2)); 7 | } 8 | int main(){ 9 | for(int i=1;i<=10;++i) 10 | cout< 2 | using namespace std; 3 | int GCD(int a,int b){ 4 | if (a==b||!b)//i.e. b=0 5 | return a; 6 | if (!a) 7 | return b; 8 | if (a>b) 9 | return GCD(a%b,b);//Alternate: use 'a-b' instead of 'a%b' 10 | return GCD(a,b%a);//'b-a' -> 'b%a' 11 | } 12 | int main(){ 13 | cout<<"GCD: "< 2 | using namespace std; 3 | void funB(int n); 4 | void funA(int n){ 5 | if(n>0){ 6 | cout<1){ 12 | cout< 2 | #include 3 | using namespace std; 4 | 5 | #define N 5 6 | typedef array array_1d; 7 | array sol,maze={ //i.e. final solution of the maze path is stored in 'sol' 8 | array_1d{1, 0, 0, 0, 0}, 9 | array_1d{1, 1, 0, 1, 0}, 10 | array_1d{1, 1, 1, 1, 0}, 11 | array_1d{1, 0, 0, 1, 0}, 12 | array_1d{1, 1, 1, 1, 1} 13 | }; 14 | 15 | void Print_Path(){ 16 | for(const auto& i:sol){ 17 | for(const auto& j:i) 18 | cout << j << " "; 19 | cout<=0&&x=0&&y 2 | using namespace std; 3 | 4 | class ChessBoard{ 5 | //Note: Normal diagonal is growing towards the left side & reverse diagonal towards right side 6 | bool *col, *norm_diag, *rev_diag; 7 | int *pos_row, squares; 8 | 9 | void disp_sol()const{ 10 | for(int cnt = 0; cnt> size; 69 | 70 | ChessBoard C1(size); 71 | C1.findsolutions(); 72 | 73 | system("PAUSE"); 74 | return 0; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Algorithms/Recursion/Nested_Recursion_Exp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int fun(int n){ 4 | if(n>100) 5 | return n-10; 6 | return fun(fun(n+11)); 7 | } 8 | int main(){ 9 | cout<<"Result: "< 2 | using namespace std; 3 | 4 | class Polynomial{ 5 | int x; 6 | int powr; 7 | int res; 8 | 9 | public: 10 | Polynomial(){ 11 | 12 | } 13 | 14 | Polynomial(const int x, const int powr){ 15 | this->x = x; 16 | this->powr = powr; 17 | res = 0; 18 | } 19 | 20 | long poly_pow(int res, int pwr){ 21 | if(pwr == 1){ //basecase 22 | return res; 23 | } 24 | if(pwr%2 == 0){ 25 | return poly_pow(res*res, pwr/2); 26 | }else{ 27 | return res*poly_pow(res*res, (pwr-1)/2); 28 | } 29 | } 30 | }; 31 | 32 | int main(){ 33 | int x = 2, powr = 5; 34 | Polynomial P(x, powr); 35 | cout<< endl<< "x^y = "<< x<< "^"<< powr<< " = "<< P.poly_pow(x, powr)<< endl<< endl; 36 | system("PAUSE"); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Algorithms/Recursion/Prime_Factor(R).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int primefactor(const int number, int div){ 5 | if( (div == 1) ){ 6 | cout<< endl<< "The following number is a prime number"<< endl; 7 | return 1; 8 | } 9 | else{ 10 | if(number%div == 0){ 11 | cout<< endl<< "The following number is not a prime number"<< endl; 12 | return 0; 13 | } 14 | div-= 1; 15 | primefactor(number, div); 16 | } 17 | } 18 | 19 | int main(){ 20 | int number; 21 | cin>> number; 22 | primefactor(number, number-1); 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Algorithms/Recursion/String_Permutation(R).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | void Swap(T* x,T* y){ 5 | T temp=*x; 6 | *x=*y; 7 | *y=temp; 8 | } 9 | void Permutation(char* str,int i,int n){ 10 | static int count=0; 11 | if(i==n) 12 | cout<<++count<<") "< 2 | using namespace std; 3 | int sum(int n){ 4 | if(n==0) 5 | return 0; 6 | return sum(n-1)+n; 7 | } 8 | int Isum(int n){ 9 | for(int s=0,i=1;i<=n;i++) 10 | s+=i; 11 | return s; 12 | } 13 | int main(){ 14 | cout< 9 | using namespace std; 10 | 11 | class Syracuse{ 12 | int elem; //n0 = elem; 13 | 14 | public: 15 | Syracuse(const int elem):elem(elem){} 16 | 17 | void print(int num){ 18 | if(num == 1){ 19 | cout<< num; 20 | return; 21 | } 22 | cout<< num<< " "; 23 | if(num%2 == 0) 24 | num/= 2; 25 | else 26 | num = (3*num)+1; 27 | 28 | print(num); 29 | } 30 | }; 31 | 32 | int main(){ 33 | Syracuse S1(65); 34 | S1.print(65); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Algorithms/Recursion/Tower_Of_Hanoi(R).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void TOH(int n,char src,char temp,char dest){//i.e. temporary/auxilary rod 4 | if (n>=1){ 5 | TOH(n-1,src,dest,temp); 6 | cout< 2 | using namespace std; 3 | void fun(int n){ 4 | if(n>0){ 5 | cout< 2 | using namespace std; 3 | 4 | void bubblesort(int arr[], int size){ 5 | int arrt[size]; 6 | for(int cnt = 0; cntarrt[cnt2+1]) 12 | swap(arrt[cnt2], arrt[cnt2+1]); 13 | } 14 | 15 | for(int cnt = 0; cnt=0 && arrt[cnt2]>tmp){ 30 | arrt[cnt2+1] = arrt[cnt2]; 31 | cnt2--; 32 | } 33 | arrt[cnt2+1] = tmp; 34 | } 35 | 36 | for(int cnt = 0; cnt 2 | using namespace std; 3 | 4 | // To find gap between elements 5 | int getNextGap(int gap) 6 | { 7 | // Shrink gap by Shrink factor 8 | gap = (gap*10)/13; 9 | 10 | if (gap < 1) 11 | return 1; 12 | return gap; 13 | } 14 | 15 | // Function to sort a[0..n-1] using Comb Sort 16 | void combSort(int a[], int n) 17 | { 18 | // Initialize gap 19 | int gap = n; 20 | 21 | // Initialize swapped as true to make sure that 22 | // loop runs 23 | bool swapped = true; 24 | 25 | // Keep running while gap is more than 1 and last 26 | // iteration caused a swap 27 | while (gap != 1 || swapped == true) 28 | { 29 | // Find next gap 30 | gap = getNextGap(gap); 31 | 32 | // Initialize swapped as false so that we can 33 | // check if swap happened or not 34 | swapped = false; 35 | 36 | // Compare all elements with current gap 37 | for (int i=0; i a[i+gap]) 40 | { 41 | swap(a[i], a[i+gap]); 42 | swapped = true; 43 | } 44 | } 45 | } 46 | } 47 | 48 | int main() 49 | { 50 | int a[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0}; 51 | int n = sizeof(a)/sizeof(a[0]); 52 | 53 | combSort(a, n); 54 | 55 | printf("Sorted array: \n"); 56 | for (int i=0; i 2 | using namespace std; 3 | 4 | void countSort(int array[], int size) { 5 | // The size of count must be at least the (max+1) but 6 | // we cannot assign declare it as int count(max+1) in C++ as 7 | // it does not support dynamic memory allocation. 8 | // So, its size is provided statically. 9 | int output[10]; 10 | int count[10]; 11 | int max = array[0]; 12 | 13 | // Find the largest element of the array 14 | for (int i = 1; i < size; i++) { 15 | if (array[i] > max) 16 | max = array[i]; 17 | } 18 | 19 | // Initialize count array with all zeros. 20 | for (int i = 0; i <= max; ++i) { 21 | count[i] = 0; 22 | } 23 | 24 | // Store the count of each element 25 | for (int i = 0; i < size; i++) { 26 | count[array[i]]++; 27 | } 28 | 29 | // Store the cummulative count of each array 30 | for (int i = 1; i <= max; i++) { 31 | count[i] += count[i - 1]; 32 | } 33 | 34 | // Find the index of each element of the original array in count array, and 35 | // place the elements in output array 36 | for (int i = size - 1; i >= 0; i--) { 37 | output[count[array[i]] - 1] = array[i]; 38 | count[array[i]]--; 39 | } 40 | 41 | // Copy the sorted elements into original array 42 | for (int i = 0; i < size; i++) { 43 | array[i] = output[i]; 44 | } 45 | } 46 | 47 | // Function to print an array 48 | void printArray(int array[], int size) { 49 | for (int i = 0; i < size; i++) 50 | cout << array[i] << " "; 51 | cout << endl; 52 | } 53 | 54 | // Driver code 55 | int main() 56 | { 57 | int m; 58 | int array[] = {4, 2, 2, 8, 3, 3, 1}; 59 | //int n = sizeof(array) / sizeof(array[0]); 60 | countSort(array, 7); 61 | printArray(array, 7); 62 | cin>>m; 63 | } 64 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Algorithms/Sorting/Heap_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void heapify(int arr[], int n, int i) { 5 | // Find largest among root, left child and right child 6 | int largest = i; 7 | int left = 2 * i + 1; 8 | int right = 2 * i + 2; 9 | 10 | if (left < n && arr[left] > arr[largest]) 11 | largest = left; 12 | 13 | if (right < n && arr[right] > arr[largest]) 14 | largest = right; 15 | 16 | // Swap and continue heapifying if root is not largest 17 | if (largest != i) { 18 | swap(arr[i], arr[largest]); 19 | heapify(arr, n, largest); 20 | } 21 | } 22 | 23 | // main function to do heap sort 24 | void heapSort(int arr[], int n) { 25 | // Build max heap 26 | for (int i = n; i >= 0; i--) 27 | heapify(arr, n, i); 28 | 29 | // Heap sort 30 | for (int i = n - 1; i >= 0; i--) { 31 | swap(arr[0], arr[i]); 32 | 33 | // Heapify root element to get highest element at root again 34 | heapify(arr, i, 0); 35 | } 36 | } 37 | 38 | // Print an array 39 | void printArray(int arr[], int n) { 40 | for (int i = 0; i < n; ++i) 41 | cout << arr[i] << " "; 42 | cout << "\n"; 43 | } 44 | 45 | // Driver code 46 | int main() { 47 | int i; 48 | int arr[] = {1, 12, 9, 5, 6, 10}; 49 | int n = sizeof(arr) / sizeof(arr[0]); 50 | heapSort(arr, n); 51 | 52 | cout << "Sorted array is \n"; 53 | printArray(arr, n); 54 | 55 | cin>>i; 56 | } 57 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Algorithms/Sorting/Merge_Sort.cpp: -------------------------------------------------------------------------------- 1 | // Merge sort in C++ 2 | 3 | #include 4 | using namespace std; 5 | 6 | // Merge two subarrays L and M into arr 7 | void merge(int arr[], int p, int q, int r) { 8 | 9 | // Create L ? A[p..q] and M ? A[q+1..r] 10 | int n1 = q - p + 1; 11 | int n2 = r - q; 12 | 13 | int L[n1], M[n2]; 14 | 15 | for (int i = 0; i < n1; i++) 16 | L[i] = arr[p + i]; 17 | for (int j = 0; j < n2; j++) 18 | M[j] = arr[q + 1 + j]; 19 | 20 | // Maintain current index of sub-arrays and main array 21 | int i, j, k; 22 | i = 0; 23 | j = 0; 24 | k = p; 25 | 26 | // Until we reach either end of either L or M, pick larger among 27 | // elements L and M and place them in the correct position at A[p..r] 28 | while (i < n1 && j < n2) { 29 | if (L[i] <= M[j]) { 30 | arr[k] = L[i]; 31 | i++; 32 | } else { 33 | arr[k] = M[j]; 34 | j++; 35 | } 36 | k++; 37 | } 38 | 39 | // When we run out of elements in either L or M, 40 | // pick up the remaining elements and put in A[p..r] 41 | while (i < n1) { 42 | arr[k] = L[i]; 43 | i++; 44 | k++; 45 | } 46 | 47 | while (j < n2) { 48 | arr[k] = M[j]; 49 | j++; 50 | k++; 51 | } 52 | } 53 | 54 | // Divide the array into two subarrays, sort them and merge them 55 | void mergeSort(int arr[], int l, int r) { 56 | if (l < r) { 57 | // m is the point where the array is divided into two subarrays 58 | int m = l + (r - l) / 2; 59 | 60 | mergeSort(arr, l, m); 61 | mergeSort(arr, m + 1, r); 62 | 63 | // Merge the sorted subarrays 64 | merge(arr, l, m, r); 65 | } 66 | } 67 | 68 | // Print the array 69 | void printArray(int arr[], int size) { 70 | for (int i = 0; i < size; i++) 71 | cout << arr[i] << " "; 72 | cout << endl; 73 | } 74 | 75 | // Driver program 76 | int main() { 77 | int i; 78 | int arr[] = {6, 5, 12, 10, 9, 1}; 79 | //int size = sizeof(arr) / sizeof(arr[0]); 80 | 81 | mergeSort(arr, 0, 5); 82 | 83 | cout << "Sorted array: \n"; 84 | printArray(arr, 6); 85 | cin>>i; 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Algorithms/Sorting/Quick_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int partition(int lb, int ub, int arr[]){ 6 | int start = lb; 7 | int end = ub; 8 | int pivot = arr[lb]; 9 | 10 | while(startpivot) 14 | end--; 15 | if(start 4 | using namespace std; 5 | 6 | // Shell sort 7 | void shellSort(int array[], int n) { 8 | // Rearrange elements at each n/2, n/4, n/8, ... intervals 9 | for (int interval = n / 2; interval > 0; interval /= 2) { 10 | for (int i = interval; i < n; i += 1) { 11 | int temp = array[i]; 12 | int j; 13 | for (j = i; j >= interval && array[j - interval] > temp; j -= interval) { 14 | array[j] = array[j - interval]; 15 | } 16 | array[j] = temp; 17 | } 18 | } 19 | } 20 | 21 | // Print an array 22 | void printArray(int array[], int size) { 23 | int i; 24 | for (i = 0; i < size; i++) 25 | cout << array[i] << " "; 26 | cout << endl; 27 | } 28 | 29 | // Driver code 30 | int main() { 31 | int i; 32 | int data[] = {9, 8, 3, 7, 5, 6, 4, 1}; 33 | int size = sizeof(data) / sizeof(data[0]); 34 | shellSort(data, size); 35 | cout << "Sorted array: \n"; 36 | printArray(data, size); 37 | cin>>i; 38 | } 39 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Contiguous_Structures/String.cpp: -------------------------------------------------------------------------------- 1 | #include "String.hpp" 2 | 3 | void test_case_1(){ 4 | String s="abcd"; 5 | 6 | // s.reserve(10); 7 | 8 | s.push_back('z'); 9 | // s.pop_back(); 10 | 11 | s.shrink_to_fit(); 12 | 13 | // s.insert('z'); 14 | // s.erase(); 15 | 16 | s.insert('k', 2); 17 | // s.erase(2); 18 | 19 | s.resize(8, 'o'); 20 | 21 | std::cout<<"string: "<=s?"true":"false"); 31 | // std::cout<<(s2>=s?"true":"false"); 32 | } 33 | 34 | int main(){ 35 | // test_case_1(); 36 | test_case_2(); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Hashing/closed_address_hashing_using_singly_linked_list.cpp: -------------------------------------------------------------------------------- 1 | using namespace std; 2 | 3 | class Hash 4 | { 5 | int BUCKET; // No. of buckets 6 | 7 | // Pointer to an array containing buckets 8 | list *table; 9 | public: 10 | Hash(int V); // Constructor 11 | 12 | // inserts a key into hash table 13 | void insertItem(int x); 14 | 15 | // deletes a key from hash table 16 | void deleteItem(int key); 17 | 18 | // hash function to map values to key 19 | int hashFunction(int x) { 20 | return (x % BUCKET); 21 | } 22 | 23 | void displayHash(); 24 | }; 25 | 26 | Hash::Hash(int b) 27 | { 28 | this->BUCKET = b; 29 | table = new list[BUCKET]; 30 | } 31 | 32 | void Hash::insertItem(int key) 33 | { 34 | int index = hashFunction(key); 35 | table[index].push_back(key); 36 | } 37 | 38 | void Hash::deleteItem(int key) 39 | { 40 | // get the hash index of key 41 | int index = hashFunction(key); 42 | 43 | // find the key in (inex)th list 44 | list :: iterator i; 45 | for (i = table[index].begin(); 46 | i != table[index].end(); i++) { 47 | if (*i == key) 48 | break; 49 | } 50 | 51 | // if key is found in hash table, remove it 52 | if (i != table[index].end()) 53 | table[index].erase(i); 54 | } 55 | 56 | // function to display hash table 57 | void Hash::displayHash() { 58 | for (int i = 0; i < BUCKET; i++) { 59 | cout << i; 60 | for (auto x : table[i]) 61 | cout << " --> " << x; 62 | cout << endl; 63 | } 64 | } 65 | 66 | // Driver program 67 | int main() 68 | { 69 | // array that contains keys to be mapped 70 | int a[] = {15, 11, 27, 8, 12}; 71 | int n = sizeof(a)/sizeof(a[0]); 72 | 73 | // insert the keys into the hash table 74 | Hash h(7); // 7 is count of buckets in 75 | // hash table 76 | for (int i = 0; i < n; i++) 77 | h.insertItem(a[i]); 78 | 79 | // delete 12 from hash table 80 | h.deleteItem(12); 81 | 82 | // display the Hash table 83 | h.displayHash(); 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Hashing/double_hashing (open_address).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Hashing{ 5 | int buckets; 6 | int *table; 7 | 8 | public: 9 | Hashing(int cap){ 10 | buckets = cap; 11 | table = new int[buckets]; 12 | for(int cnt = 0; cnt 2 | #include 3 | using namespace std; 4 | 5 | class Hashtable{ 6 | int buckets; 7 | int *table; 8 | 9 | public: 10 | Hashtable(int num){ 11 | buckets = num; 12 | table = NULL; 13 | table = new int [buckets]; 14 | for(int cnt = 0; cnt 2 | using namespace std; 3 | 4 | class Hashing{ 5 | int buckets; 6 | int *table; 7 | 8 | public: 9 | Hashing(int cap){ 10 | buckets = cap; 11 | table = new int[buckets]; 12 | for(int cnt = 0; cnt 2 | #include 3 | #include //tree 4 | #include //hash-table 5 | #include 6 | using namespace std; 7 | 8 | typedef tuple type; 9 | 10 | namespace std{ 11 | template <> 12 | struct hash{ 13 | auto operator()(const type& t)const->size_t{ return hash{}(get<0>(t)*get<1>(t)); } 14 | }; 15 | } 16 | 17 | bool operator==(const type& lhs,const type& rhs){ return (get<0>(lhs)*get<1>(lhs))==(get<0>(rhs)*get<1>(rhs)); } 18 | 19 | tuple getNameAge(){ return make_tuple("rosemary",17); } 20 | 21 | int main(){ 22 | auto p1=make_pair(8,"appricot"); 23 | cout<<"p1.first -> "< "<(t1)<<'\n' 27 | <<"2nd element: "<(t1)<<'\n' 28 | <<"3rd element: "<(t1)<<'\n'; 29 | 30 | auto name=string{}; auto age=int{}; //string name; int age; 31 | tie(name,age)=getNameAge(); 32 | cout<<"\nName: "< 35 | cout<<(t2= t3\n"); 36 | 37 | auto m1=map{}; 38 | m1[make_tuple(17,'f',7.864)]="Faith will move mountains."; 39 | 40 | auto m2=unordered_map{}; 41 | m2[make_tuple(17,'f',7.864)]="Faith will move mountains."; 42 | 43 | int a=1,b=2,c=3; 44 | tie(b,c,a)=make_tuple(a,b,c); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Library_Log.txt: -------------------------------------------------------------------------------- 1 | SUMMARIZED STATUS:- 2 | - Vector: revised recently on 10/11/22, resize method's capacity growth needs to be similar to std::vector 3 | - String: revised recently on 7/10/22, no improvements needed as for now 4 | - Segmented_Vector: revised recently on 10/13/22, insert_chunk needs to be completed (i.e. not ready for all cases) 5 | - F_Stack: revised recently on 10/9/22, no improvements needed as for now 6 | - HF_Stack: revised recently on 10/9/22, needs concept revision for more optimized Segmented_Vector [might be dead-end approach] 7 | - F_Deque: revised recently on 10/12/22, no improvements needed as for now 8 | - Deque: revised recently on 10/12/22, no improvements needed as for now 9 | - Segmented_Deque: needs base concept implementation from Segmented_Vector.hpp & form data concept from Deque.hpp & HF_Deque.hpp [future work] 10 | - Indexed_Struct: revised recently on 10/19/22, no improvements needed as for now 11 | - M.A.A.T.A: revised recently on 12/17/22, need deep copy {a.k.a. copy semantics} operations for more flexible data manipulation 12 | - O.M.A.A.T.A: revised recently on 12/17/22, more indexing methods needed for range search queries with indexer [future work: deque.hpp => segmented_deque.hpp] 13 | - Contiguous_Structures: require separate primitive/object type handling (using template specialization or SFINAE rule) [future work] 14 | - Hashing: more material need to be added 15 | - Priority_Queues, Heaps & Graphs: new folders along with material need to be added 16 | 17 | TIP (tested on Windows 10/11):- 18 | - To create a log file, open notepad and type .LOG on 1st line and then press ENTER to move on the next line. After that, 19 | just save file with any descriptive name. The next time you'll open it, it will work as log file. TADA!!! 20 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Linked_Lists/Circular_Forward_List.cpp: -------------------------------------------------------------------------------- 1 | #include "Circular_Forward_List.hpp" 2 | 3 | /* Circular Singly Linked List Operations: */ 4 | int main(){ 5 | #if __cplusplus >= 201103L 6 | Circular_Forward_List list({1,2,3,4,5}), list2; 7 | #else 8 | Circular_Forward_List list; 9 | for(int i=1;i<=5;++i) 10 | list.push_back(i); 11 | #endif 12 | 13 | // for(int i=1;i<=5;++i) 14 | // list.push_front(i); 15 | 16 | // list.pop_front(); 17 | // list.pop_back(); 18 | 19 | // list2=list; 20 | // list2=std::move(list);//C++11 21 | 22 | // list.push_middle(404); 23 | // list.pop_middle(); 24 | 25 | // list.clear(); 26 | 27 | // list.insert(3, 404); 28 | // list.erase(3); 29 | 30 | list.traverse(); 31 | // list2.traverse(); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Linked_Lists/Circular_List.cpp: -------------------------------------------------------------------------------- 1 | #include "Circular_List.hpp" 2 | 3 | /* Circular Doubly Linked List Operations: */ 4 | int main(){ 5 | #if __cplusplus >= 201103L 6 | Circular_List list({1,2,3,4,5}), list2({44,55,66}); 7 | #else 8 | Circular_List list, list2; 9 | for(int i=1;i<=5;++i) 10 | list.push_back(i); 11 | for(int i=44;i<=66;i+=11) 12 | list2.push_back(i); 13 | #endif 14 | 15 | // for(int i=1;i<=5;++i) 16 | // list.push_front(i); 17 | 18 | // list.pop_front(); 19 | // list.pop_back(); 20 | 21 | // list2=list; 22 | // list2=std::move(list);//C++11 23 | 24 | // list.push_middle(404); 25 | // list.pop_middle(); 26 | 27 | // list.clear(); 28 | 29 | // list.reverse(); 30 | 31 | // list.insert(3, 404); 32 | // list.erase(3); 33 | 34 | // list.merge(list2, 5);//tail boundary case not handled! 35 | 36 | list.traverse_forward(); 37 | list.traverse_backward(); 38 | 39 | // list2.traverse_forward(); 40 | // list2.traverse_backward(); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Linked_Lists/Forward_List.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/MY_DS_LIBRARY/Linked_Lists/Forward_List.hpp -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Linked_Lists/Node.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NODE_GUARD 2 | #define NODE_GUARD 1 3 | 4 | #include 5 | 6 | #if __cplusplus < 201103L 7 | #define nullptr 0L 8 | #define noexcept 9 | #endif 10 | 11 | template 12 | struct SLL_Node{//Singly Linked List Node 13 | T data; 14 | SLL_Node *next; 15 | 16 | #if __cplusplus < 201103L 17 | SLL_Node(const T& data=T())noexcept://default & parameterized ctor 18 | data(data), next(nullptr){} 19 | #else 20 | template//variadic template 21 | SLL_Node(_T&&... data)noexcept://emplaced ctor 22 | data{std::forward<_T>(data)...},//perfect forwarding 23 | next(nullptr){} 24 | #endif 25 | }; 26 | 27 | template 28 | struct DLL_Node{//Doubly Linked List Node 29 | T data; 30 | DLL_Node *next, *prev; 31 | 32 | #if __cplusplus < 201103L 33 | DLL_Node(const T& data=T())noexcept: 34 | data(data), next(nullptr), prev(nullptr){} 35 | #else 36 | template 37 | DLL_Node(_T&&... data)noexcept: 38 | data{std::forward<_T>(data)...}, 39 | next(nullptr), prev(nullptr){} 40 | #endif 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Linked_Lists/README.txt: -------------------------------------------------------------------------------- 1 | NOTE: you can also perform cross class as well as cross object operations, 2 | but cross node type isn't possible without type convertor. 3 | 4 | 1) Singly Linked List (SLL) Node Transfer:- 5 | pushing node from Forward_List (FL) to Circular_Forward_List (CFL): 6 | e.g. CFL.push_back(FL.pop_back(true)); 7 | e.g. CFL.push_front(FL.pop_back(true)); 8 | e.g. CFL.push_back(FL.pop_middle(true)); 9 | e.g. CFL.push_middle(FL.pop_middle(true)); 10 | e.g. CFL.push_middle(FL.erase([any_index], true)); 11 | 12 | and, ofcourse its vice-versa is also possible (if return_node overloads are present): 13 | e.g. FL.push_back(CFL.pop_back(true)); 14 | e.g. FL.push_front(CFL.pop_back(true)); 15 | e.g. FL.push_back(CFL.pop_middle(true)); 16 | e.g. FL.push_middle(CFL.pop_middle(true)); 17 | e.g. FL.insert([any_index], CFL.pop_back(true)); 18 | 19 | cross object transfer: 20 | e.g. FL.push_front(FL.pop_back(true)); 21 | e.g. CFL.push_front(CFL.pop_back(true)); 22 | 23 | 2) Doubly Linked List (DLL) Node Transfer:- 24 | similarly, pushing node from List (L) to Circular_List (FL): 25 | e.g. CL.push_back(L.pop_back(true)); 26 | e.g. CL.push_front(L.pop_back(true)); 27 | e.g. CL.push_back(L.pop_middle(true)); 28 | e.g. CL.push_middle(L.pop_middle(true)); 29 | e.g. CL.push_middle(L.erase([any_index], true)); 30 | 31 | and, ofcourse its vice-versa is also possible (if return_node overloads are present): 32 | e.g. L.push_back(CL.pop_back(true)); 33 | e.g. L.push_front(CL.pop_back(true)); 34 | e.g. L.push_back(CL.pop_middle(true)); 35 | e.g. L.push_middle(CL.pop_middle(true)); 36 | e.g. L.insert([any_index], CL.pop_back(true)); 37 | 38 | cross object transfer: 39 | e.g. L.push_front(L.pop_back(true)); 40 | e.g. CL.push_front(CL.pop_back(true)); 41 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Linked_Lists/Skip_List.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/MY_DS_LIBRARY/Linked_Lists/Skip_List.cpp -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Linked_Lists/Xor_List.cpp: -------------------------------------------------------------------------------- 1 | #include "Xor_List.hpp" 2 | 3 | /* XOR Doubly Linked List Operations: */ 4 | int main(){ 5 | #if __cplusplus >= 201103L 6 | xor_list xlist({1,2,3,4}); 7 | #else 8 | xor_list xlist; 9 | for(int i=1;i<5;++i) 10 | xlist.push_back(i); 11 | #endif 12 | xlist.push_front(0);//0 1 2 3 4 13 | 14 | xlist.push_back(xlist.pop_front(true));//0 1 2 3 4-> 1 2 3 4 0 (node transfer) 15 | 16 | xlist.pop_back();//1 2 3 4 0-> 1 2 3 4 17 | 18 | xlist.reverse();//1 2 3 4-> 4 3 2 1 19 | 20 | xlist.traverse_forward(); 21 | xlist.traverse_backward(); 22 | return 0; 23 | } 24 | /* References: 25 | https://www.geeksforgeeks.org/xor-linked-list-a-memory-efficient-doubly-linked-list-set-1/ 26 | https://www.youtube.com/watch?v=hMcHVfu3E8U 27 | */ 28 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Miscellaneous/graph_implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | Node *nxt; 7 | int data; 8 | 9 | Node(const int data){ 10 | this->data = data; 11 | this->nxt = NULL; 12 | } 13 | 14 | Node(){} 15 | }; 16 | 17 | Node* createnode(int val){ 18 | Node *tmp = new Node(val); 19 | return tmp; 20 | } 21 | 22 | class LinkedList{ 23 | Node *root; 24 | 25 | public: 26 | LinkedList(){ 27 | root = NULL; 28 | } 29 | 30 | void addnode(Node *tmp, int val){ 31 | if(tmp->nxt == NULL) 32 | tmp->nxt = createnode(val); 33 | else 34 | addnode(tmp->nxt, val); 35 | } 36 | 37 | void drive_addnode(int val){ 38 | if(root == NULL) 39 | root = createnode(val); 40 | else 41 | addnode(root, val); 42 | } 43 | 44 | void print(){ 45 | for(Node *tmp = root; tmp != NULL; tmp = tmp->nxt) 46 | cout<< tmp->data<< " "; 47 | cout<< endl; 48 | } 49 | }; 50 | 51 | class Graph{ 52 | int num_vtx; 53 | LinkedList *list; 54 | 55 | public: 56 | Graph(int num_vtx){ 57 | this->num_vtx = num_vtx; 58 | list = new LinkedList[num_vtx]; 59 | } 60 | 61 | //adjacency list: 62 | void addedge(int vertex1, int vertex2){ //an edge between vertex1 & vertex2 63 | list[vertex1].drive_addnode(vertex2); 64 | } 65 | 66 | void display_graph(){ 67 | for(int cnt = 0; cnt 2 | using namespace std; 3 | 4 | class Heap{ 5 | int h_size; 6 | int *arr; 7 | int max_size; 8 | 9 | public: 10 | Heap(int max){ 11 | max_size = max; 12 | arr = new int(max_size); 13 | h_size = 0; 14 | } 15 | 16 | int get_parent(int cnt){ 17 | return ((cnt-1)/2); 18 | } 19 | 20 | //function for insert: 21 | bool insert(const int key){ 22 | if(h_size == max_size){ 23 | cout<< endl<< "Heap Overflow!"<< endl; 24 | return false; 25 | } 26 | h_size+= 1; 27 | int cnt = h_size-1; 28 | arr[cnt] = key; 29 | 30 | //min-heap 31 | while( (cnt != 0) && (arr[get_parent(cnt)] > arr[cnt]) ){ 32 | swap(arr[get_parent(cnt)], arr[cnt]); 33 | cnt = get_parent(cnt); 34 | } 35 | 36 | return true; 37 | } 38 | 39 | int get_lft(const int prt) const{ 40 | return ((prt*2)+1); 41 | } 42 | 43 | int get_rt(const int prt) const{ 44 | return ((prt*2)+2); 45 | } 46 | 47 | void display() const{ 48 | for(int cnt = 0; cnt< h_size; cnt++) 49 | cout<< arr[cnt]<< " "; 50 | cout<< endl; 51 | } 52 | }; 53 | 54 | //scope of main: 55 | int main(){ 56 | Heap HDS(10); 57 | 58 | HDS.insert(11); 59 | HDS.insert(3); 60 | HDS.insert(2); 61 | HDS.insert(1); 62 | 63 | HDS.display(); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Miscellaneous/non_extreme_node_swapping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node *nxt; 8 | Node *prv; 9 | 10 | Node(){ 11 | data = 0; 12 | nxt = prv = NULL; 13 | } 14 | }; 15 | 16 | class DLL{ 17 | Node *hd, *tl; 18 | 19 | public: 20 | void addnode_hd(const int val){ 21 | Node *newnode = new Node; 22 | newnode->data = val; 23 | if(hd == NULL){ 24 | hd = tl = newnode; 25 | }else{ 26 | hd->prv = newnode; 27 | newnode->nxt = hd; 28 | hd = newnode; 29 | } 30 | } 31 | 32 | void const disp(){ 33 | Node *tmp = new Node; 34 | for(tmp = hd; tmp!=NULL; tmp = tmp->nxt) 35 | cout<< tmp->data<< " "; 36 | cout<< endl; 37 | } 38 | 39 | bool swap_adjacent(const int val){ 40 | int fg = false; 41 | if( (hd->data != val) ){ 42 | Node *tmp, *tmp2, *tmp3; 43 | for(tmp = hd; tmp!=NULL; tmp = tmp->nxt){ 44 | if(tmp->data == val){ 45 | Node *tmp3 = tmp; 46 | Node *tmp2 = tmp->nxt; 47 | tmp->prv->nxt = tmp2; 48 | tmp2->prv = tmp->prv; 49 | Node *tmp4 = tmp2->nxt; 50 | tmp4->prv = tmp; 51 | tmp2->nxt->prv = tmp; 52 | tmp->nxt = tmp2->nxt; 53 | tmp->prv = tmp2; 54 | tmp2->nxt = tmp; 55 | fg = true; 56 | } 57 | } 58 | } 59 | return fg; //in-case of extreme nodes 60 | } 61 | }; 62 | 63 | int main(){ 64 | DLL List; 65 | 66 | List.addnode_hd(5); 67 | List.addnode_hd(6); 68 | List.addnode_hd(7); 69 | List.addnode_hd(8); 70 | List.addnode_hd(9); 71 | List.addnode_hd(11); 72 | List.addnode_hd(12); 73 | List.addnode_hd(31); 74 | 75 | List.disp(); 76 | 77 | bool chk = List.swap_adjacent(11); 78 | if(chk == false) 79 | cout<< endl<< "FAILED TO SWAP THE NODES!"<< endl; 80 | else 81 | List.disp(); 82 | 83 | cout<< endl<< endl; 84 | system("PAUSE"); 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Miscellaneous/sort_random_dates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../Utilities/Date.hpp" 3 | #include "../Algorithms/Sorts.hpp" 4 | 5 | using std::cout; 6 | 7 | int main(){//sorts array of random dates 8 | srand(time(0)); 9 | 10 | Date arr[6]; 11 | 12 | for(int i=0;i<5;++i) 13 | arr[i]=Date(rand()%30+1, rand()%12+1, rand()%(2030-1990+1)+1990); 14 | 15 | arr[5]=arr[4]; 16 | arr[4].day=31; 17 | 18 | for(auto& it:arr) 19 | cout< 2 | using namespace std; 3 | 4 | //-404 = element not found 5 | #define STACKSIZE 10 //to give the size of our stack 6 | 7 | class Stack{ 8 | int top; 9 | int elem_stack[STACKSIZE]; 10 | 11 | public: 12 | Stack(){ 13 | top = -1; //when the stack is empty 14 | } 15 | 16 | bool isstack_empty(){ 17 | return (top == -1); 18 | } 19 | 20 | bool isstack_full(){ 21 | return (top == (STACKSIZE-1) ); 22 | } 23 | 24 | bool push_stack(const int elem){ 25 | if(!isstack_full()){ 26 | top++; 27 | elem_stack[top] = elem; 28 | return true; 29 | } 30 | return false; 31 | } 32 | 33 | int pop_stack(){ 34 | if(!isstack_empty()){ 35 | int elem = elem_stack[top]; 36 | top--; 37 | return elem; 38 | } 39 | return -404; 40 | } 41 | 42 | void disp(){ 43 | cout<< endl; 44 | if(!isstack_empty()){ 45 | while(1){ 46 | int elem = pop_stack(); 47 | if(elem != -404) 48 | cout<< elem<< " "; 49 | else 50 | break; 51 | } 52 | } 53 | } 54 | }; 55 | 56 | //scope of main: 57 | int main(){ 58 | Stack Stack_arr; 59 | int elem; 60 | 61 | while(1){ 62 | cout<< endl<< "ELEMENT: "; 63 | cin>> elem; 64 | if(elem == 0) 65 | break; 66 | bool chk = Stack_arr.push_stack(elem); 67 | if(chk == false) 68 | cout<< endl<< "Stack Overflow!"<< endl; 69 | fflush(stdin); 70 | } 71 | 72 | while(1){ 73 | cout<< endl<< "Y/N (1/0): "; 74 | cin>> elem; 75 | if(elem == 0) 76 | break; 77 | int popped = Stack_arr.pop_stack(); 78 | if(popped == -404) 79 | cout<< endl<< "Stack Underflow!"<< endl; 80 | else 81 | cout<< endl<< popped<< " has been popped!"<< endl; 82 | fflush(stdin); 83 | } 84 | 85 | Stack_arr.disp(); 86 | 87 | system("PAUSE"); 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Special_Structures/Data_Files/df_1.txt: -------------------------------------------------------------------------------- 1 | 1,123,axa vv,23.3,Y,rahat 2 | 1,919,noice,43.33,k,ali 3 | 1,919,h1,23.5,R,fateh 4 | 0,191,h1,2112.2,Y,subhan 5 | 0,403,stranded,23.5,Z,ali 6 | 0,332,right,345.1,e,dawood 7 | 0,404,h1,345.1,q,shahzaib 8 | 1,444,stoif,444.444,k,"""new"" friend" 9 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Special_Structures/Data_Files/df_2.txt: -------------------------------------------------------------------------------- 1 | 1,123,"""Great"",""Work""",77.11,f,hamza 2 | 1,919,noice,444.444,c,ali 3 | 1,919,hello,444.444,z,asad 4 | 0,191,hello,919.19,d,subhan 5 | 0,403,cOXO,555.555,F,haris 6 | 0,332,right,345.1,e,taha 7 | 0,404,left,345.1,q,shahzaib 8 | 1,321,"stranded, left-alone",0.1,f,sufiyan 9 | 1,404,OXO,555.555,f,zaidan 10 | 0,404,OXO,111.111,C,shariq 11 | 0,404,OXO,111.111,f,hamza 12 | 1,909,copied,555.555,f,shahzaib 13 | 1,123,Great Work!,77.11,f,bibi 14 | 1,919,noice,444.444,c,ali 15 | 1,919,hello,444.444,z,asad 16 | 0,191,hello,919.19,d,subhan 17 | 0,403,cOXO,555.555,F,colt 18 | 0,332,right,345.1,e,taha 19 | 1,919,noice,444.444,c,piper 20 | 1,919,hello,444.444,z,asad 21 | 0,191,hello,919.19,d,subhan 22 | 0,403,cOXO,555.555,F,haris 23 | 0,332,right,345.1,e,P.A.M 24 | 0,404,left,345.1,q,shahzaib 25 | 1,321,stranded,0.1,f,sufiyan 26 | 1,909,copied,555.555,f,shahzaib 27 | 1,123,Great Work!,77.11,f,bibi 28 | 1,919,noice,444.444,c,ali 29 | 1,919,hello,444.444,z,asad 30 | 0,191,hello,919.19,d,subhan 31 | 0,403,cOXO,555.555,F,haris 32 | 0,332,right,345.1,e,taha 33 | 0,404,left,345.1,q,shahzaib 34 | 1,321,stranded,0.1,f,sufiyan 35 | 1,404,OXO,555.555,f,zaidan 36 | 0,404,OXO,111.111,C,shariq 37 | 0,404,OXO,111.111,f,hamza 38 | 1,393,copied,555.555,f,shahzaib 39 | 1,123,Great Work!,77.11,f,hamza 40 | 1,919,noice,444.444,c,ali 41 | 1,919,hello,444.444,z,asad 42 | 0,191,hello,919.19,d,subhan 43 | 0,404,left,345.1,q,shahzaib 44 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Special_Structures/Data_Files/df_3.csv: -------------------------------------------------------------------------------- 1 | 1,919,noice,444.444,c,ali 2 | 0,191,hello,919.19,d,subhan 3 | 0,403,cOXO,555.555,F,haris 4 | 0,332,right,345.1,e,taha 5 | 0,404,left,345.1,q,shahzaib 6 | 1,321,"stranded, left-alone",0.1,f,sufiyan 7 | 1,404,OXO,555.555,f,zaidan 8 | 0,404,OXO,111.111,f,hamza 9 | 1,909,copied,555.555,f,shahzaib 10 | 1,919,noice,444.444,c,ali 11 | 0,191,hello,919.19,d,subhan 12 | 0,403,cOXO,555.555,F,colt 13 | 0,332,right,345.1,e,taha 14 | 1,919,noice,444.444,c,piper 15 | 0,191,hello,919.19,d,subhan 16 | 0,403,cOXO,555.555,F,haris 17 | 0,332,right,345.1,e,P.A.M 18 | 0,404,left,345.1,q,shahzaib 19 | 1,321,stranded,0.1,f,sufiyan 20 | 1,909,copied,555.555,f,shahzaib 21 | 1,919,noice,444.444,c,ali 22 | 0,191,hello,919.19,d,subhan 23 | 0,403,cOXO,555.555,F,haris 24 | 0,332,right,345.1,e,taha 25 | 0,404,left,345.1,q,shahzaib 26 | 1,321,stranded,0.1,f,sufiyan 27 | 1,404,OXO,555.555,f,zaidan 28 | 0,404,OXO,111.111,f,hamza 29 | 1,393,copied,555.555,f,shahzaib 30 | 1,919,noice,444.444,c,ali 31 | 0,191,hello,919.19,d,subhan 32 | 0,404,left,345.1,q,shahzaib 33 | 1,321,hello,345.1,c,n.i.l 34 | 1,123,axa vv,23.3,Y,rahat 35 | 1,919,noice,43.33,k,ali 36 | 1,919,h1,23.5,R,fateh 37 | 0,191,h1,2112.2,Y,subhan 38 | 0,403,stranded,23.5,Z,ali 39 | 0,332,right,345.1,e,dawood 40 | 0,404,h1,345.1,q,shahzaib 41 | 1,444,stoif,444.444,k,"""new"" friend" 42 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Special_Structures/Data_Files/df_4.csv: -------------------------------------------------------------------------------- 1 | 1,919,noice,444.444,c,ali,444.444 2 | 0,191,hello,919.19,d,subhan,919.19 3 | 0,403,cOXO,555.555,F,haris,555.555 4 | 0,332,right,345.1,e,taha,345.1 5 | 0,404,left,345.1,q,shahzaib,345.1 6 | 1,321,"stranded, left-alone",0.1,f,sufiyan,0.1 7 | 1,404,OXO,555.555,f,zaidan,555.555 8 | 0,404,OXO,111.111,f,hamza,111.111 9 | 1,909,copied,555.555,f,shahzaib,555.555 10 | 1,919,noice,444.444,c,ali,444.444 11 | 0,191,hello,919.19,d,subhan,919.19 12 | 0,403,cOXO,555.555,F,colt,555.555 13 | 0,332,right,345.1,e,taha,345.1 14 | 1,919,noice,444.444,c,piper,444.444 15 | 0,191,hello,919.19,d,subhan,919.19 16 | 0,403,cOXO,555.555,F,haris,555.555 17 | 0,332,right,345.1,e,P.A.M,345.1 18 | 0,404,left,345.1,q,shahzaib,345.1 19 | 1,321,stranded,0.1,f,sufiyan,0.1 20 | 1,909,copied,555.555,f,shahzaib,555.555 21 | 1,919,noice,444.444,c,ali,444.444 22 | 0,191,hello,919.19,d,subhan,919.19 23 | 0,403,cOXO,555.555,F,haris,555.555 24 | 0,332,right,345.1,e,taha,345.1 25 | 0,404,left,345.1,q,shahzaib,345.1 26 | 1,321,stranded,0.1,f,sufiyan,0.1 27 | 1,404,OXO,555.555,f,zaidan,555.555 28 | 0,404,OXO,111.111,f,hamza,111.111 29 | 1,393,copied,555.555,f,shahzaib,555.555 30 | 1,919,noice,444.444,c,ali,444.444 31 | 0,191,hello,919.19,d,subhan,919.19 32 | 0,404,left,345.1,q,shahzaib,345.1 33 | 1,321,hello,345.1,c,n.i.l,345.1 34 | 1,123,axa vv,23.3,Y,rahat,23.3 35 | 1,919,noice,43.33,k,ali,43.33 36 | 1,919,h1,23.5,R,fateh,23.5 37 | 0,191,h1,2112.2,Y,subhan,2112.2 38 | 0,403,stranded,23.5,Z,ali,23.5 39 | 0,332,right,345.1,e,dawood,345.1 40 | 0,404,h1,345.1,q,shahzaib,345.1 41 | 1,444,stoif,444.444,k,"""new"" friend",444.444 42 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Special_Structures/Documentation_Files/Pointers for Meeting.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/MY_DS_LIBRARY/Special_Structures/Documentation_Files/Pointers for Meeting.pptx -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Special_Structures/Documentation_Files/Project Report.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/MY_DS_LIBRARY/Special_Structures/Documentation_Files/Project Report.docx -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Special_Structures/Documentation_Files/Simplified Concept Diagram (MAATA).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/MY_DS_LIBRARY/Special_Structures/Documentation_Files/Simplified Concept Diagram (MAATA).png -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Special_Structures/Indexed_Struct.cpp: -------------------------------------------------------------------------------- 1 | #include"Indexed_Struct.hpp" 2 | #include"../Contiguous_Structures/String.hpp" 3 | 4 | using std::cout; 5 | 6 | int main(){ 7 | indexed_struct obj1{123,"noice",123.123}; 8 | //indexes: 0 1 2 9 | indexed_struct obj2{909,"zzzz",919.19,"multiple",'j'}; 10 | //indexes: 0 1 2 3 4 11 | 12 | int i=404; String s="4-0-4"; double d=404.404; 13 | obj2.set({3,0,2}, std::move(s),404,404.404);//works for all types of parameters (in any order) 14 | 15 | cout<()<<'\n'<()<<"\n\n";//compile-time single attribute access 20 | 21 | indexed_struct map[]{ 22 | {1,'o',"one"},{2,'t',"two"},{3,'t',"three"},{4,'f',"four"},{5,'f',"five"} 23 | }; 24 | for(const auto& iter:map) 25 | cout< out.txt 9 | 10 | Note: this trick is extremely beneficial for large output results as console buffer may ran out of space -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Special_Structures/Testing/out.txt: -------------------------------------------------------------------------------- 1 | RECORDS: 2 | List-View: 3 | 1-10-2018 Clothing 16000 4 | 1-11-2018 Clothing 16000 5 | 1-12-2018 Clothing 16000 6 | 1-1-2019 Clothing 16000 7 | 1-2-2019 Clothing 16000 8 | 1-3-2019 Clothing 16000 9 | 1-1-2019 Electronics 16000 10 | 1-2-2019 Electronics 16000 11 | 1-3-2019 Electronics 16000 12 | 1-7-2018 Clothing 14000 13 | 1-8-2018 Clothing 14000 14 | 1-9-2018 Clothing 14000 15 | 1-4-2018 Clothing 12000 16 | 1-5-2018 Clothing 12000 17 | 1-6-2018 Clothing 12000 18 | 1-3-2019 Furniture 11800 19 | 1-2-2019 Furniture 11600 20 | 1-1-2019 Furniture 11500 21 | 1-12-2018 Furniture 11400 22 | 1-11-2018 Furniture 11300 23 | 1-10-2018 Furniture 11100 24 | 1-9-2018 Furniture 11000 25 | 1-8-2018 Furniture 10900 26 | 1-7-2018 Furniture 10800 27 | 1-6-2018 Furniture 10600 28 | 1-5-2018 Furniture 10500 29 | 1-4-2018 Furniture 10400 30 | 1-4-2018 Electronics 9000 31 | 1-5-2018 Electronics 9000 32 | 1-6-2018 Electronics 9000 33 | 1-7-2018 Electronics 9000 34 | 1-8-2018 Electronics 9000 35 | 1-9-2018 Electronics 9000 36 | 1-10-2018 Electronics 9000 37 | 1-11-2018 Electronics 9000 38 | 1-12-2018 Electronics 9000 39 | 40 | Trees-View: 41 | 1-10-2018(1) 1-11-2018(1) 1-12-2018(1) 1-1-2019(2) 1-2-2019(2) 1-3-2019(2) 1-7-2018(1) 1-8-2018(2) 1-9-2018(1) 1-4-2018(1) 1-5-2018(1) 1-6-2018(1) 1-3-2019(1) 1-2-2019(1) 1-1-2019(1) 1-12-2018(2) 1-11-2018(1) 1-10-2018(1) 1-9-2018(1) 1-7-2018(2) 1-6-2018(1) 1-5-2018(2) 1-4-2018(2) 1-6-2018(1) 1-8-2018(1) 1-9-2018(1) 1-10-2018(1) 1-11-2018(1) 42 | Clothing(12) Electronics(12) Furniture(12) 43 | 9000(9) 10400(1) 10500(1) 10600(1) 10800(1) 10900(1) 11000(1) 11100(1) 11300(1) 11400(1) 11500(1) 11600(1) 11800(1) 12000(3) 14000(3) 16000(9) 44 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Special_Structures/Testing/test_csv_files.cpp: -------------------------------------------------------------------------------- 1 | //#include "../Multi_Attribute_AVL_Tree_Adapter.hpp" 2 | #include "../Optimized_Multi_Attribute_AVL_Tree_Adapter.hpp" 3 | #include "D:/FAST DATA/SEM-III/Data Structures/MY_DS_LIBRARY/Utilities/Date.hpp" 4 | #include "D:/FAST DATA/SEM-III/Data Structures/MY_DS_LIBRARY/Contiguous_Structures/String.hpp" 5 | 6 | using std::cout; 7 | 8 | //typedef MAATA_O tree_adapter_type;//df_4.csv 9 | 10 | typedef MAATA_O tree_adapter_type;//sales_target.csv 11 | 12 | tree_adapter_type get_obj(){ 13 | return tree_adapter_type("sales_target.csv", true, 50);//using input_file_stream ctor 14 | //Note: pass 'true' in 2nd parameter if file is .csv 15 | } 16 | 17 | void basic_operations(tree_adapter_type& obj){//for df_4.csv right now 18 | //data-types: bool,int,String,double,char,String 19 | //indexes: 0 1 2 3 4 5 20 | 21 | // cout<<"\n-----\nSearch all 'f' after removing all 77.11 :-\n"; 22 | // obj.remove({3}, 77.11);//remove any record by any attribute in O(logN+M*logN) time 23 | // obj.view_filtered_records(obj.search({4}, 'f'));//search record by any attribute in O(logN) time 24 | 25 | cout<<"\nData Sorted by Atrribute: {int: 1}\n"; 26 | // int index=1; obj.sort(index);//run-time 27 | obj.sort<1>();//compile time 28 | 29 | // obj.clear(); 30 | } 31 | 32 | void traverse_data(tree_adapter_type& obj){ 33 | cout<<"RECORDS:-\nList-View:\n"; 34 | obj.view_records(true);//bool version available for indexer view 35 | cout<<"\nTrees-View:"<<'\n';//Note: always prefer to use '\n' over std::endl 36 | obj.view_by_trees(); 37 | } 38 | 39 | int main(){ 40 | auto obj=get_obj();//by file-stream 41 | 42 | // basic_operations(obj); 43 | 44 | traverse_data(obj); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Stacks_&_Queues/Stacks.cpp: -------------------------------------------------------------------------------- 1 | #include "F_Stack.hpp" 2 | #include "HF_Stack.hpp" 3 | #include "../Contiguous_Structures/String.hpp" 4 | 5 | using std::cout; 6 | using std::cin; 7 | 8 | void Fixed_Size_Array_Stack_Operations(){ 9 | const int cc=5;//chunk capacity 10 | 11 | #if __cplusplus >= 201103L 12 | F_Stack s({"lol", "great", "woah!"}); 13 | #else 14 | F_Stack s; 15 | 16 | s.push("lol"); 17 | s.push("great"); 18 | s.push("woah!"); 19 | #endif 20 | 21 | s.pop(); 22 | 23 | s.push("taebak!"); 24 | 25 | s.erase(1); 26 | 27 | s.insert("ss1"); 28 | 29 | s.insert("ss2", 1); 30 | 31 | F_Stack s2=s;//copy construction 32 | 33 | cout<<"values: "<>s; 40 | 41 | cout<<"\nvalues: "<= 201103L 46 | void Heap_based_Fixed_Size_Array_Stack_Operations(){ 47 | const int cc=5;//chunk capacity 48 | 49 | HF_Stack s({"lol", "great", "woah!"}); 50 | // HF_Stack s; s.push("lol"); s.push("great"); s.push("woah!"); 51 | 52 | s.pop(); 53 | 54 | s.push("taebak!"); 55 | 56 | s.erase(1); 57 | 58 | s.insert("ss1"); 59 | 60 | s.insert("ss2", 1); 61 | 62 | HF_Stack s2=s;//copy construction 63 | 64 | cout<<"values: "<>s; 71 | 72 | cout<<"\nvalues: "< 6 | inline bool equal(const T& a, const T& b){ return a==b; } 7 | template 8 | inline bool not_equal(const T& a, const T& b){ return a!=b; } 9 | template 10 | inline bool greater(const T& a, const T& b){ return a>b; } 11 | template 12 | inline bool lesser(const T& a, const T& b){ return a 14 | inline bool greater_equal(const T& a, const T& b){ return a>=b; } 15 | template 16 | inline bool lesser_equal(const T& a, const T& b){ return a<=b; } 17 | 18 | //comparators (3 args based or range based): 19 | template 20 | inline bool is_in_range(const T& a, const T& b, const T& c){ return a>=b&&a<=c; } 21 | template 22 | inline bool isnt_in_range(const T& a, const T& b, const T& c){ return ac; } 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Utilities/Comparator_Adapter.hpp: -------------------------------------------------------------------------------- 1 | #if __cplusplus >= 201103L 2 | 3 | #ifndef COMPARATOR_ADAPTER_GUARD 4 | #define COMPARATOR_ADAPTER_GUARD 1 5 | 6 | #include "Comparator.hpp" 7 | 8 | template 9 | using comparator_t=bool(*)(const T&, const T&);//storing function pointer type 10 | template 11 | using comparator_t2=bool(*)(const T&, const T&, const T&); 12 | 13 | template class> 14 | struct comparator_adapter;//forward declaration (for template specialization) 15 | 16 | template 17 | struct comparator_adapter{//partially specialized struct 18 | 19 | comparator_adapter(const T& key, comparator_t comp)noexcept://parameterized ctor 20 | comparator(comp), source_key(key){} 21 | 22 | bool operator()(const T& target_key)const{//function call operator 23 | return comparator(target_key, source_key); 24 | } 25 | 26 | private: 27 | comparator_t comparator; 28 | const T& source_key; 29 | }; 30 | 31 | template 32 | struct comparator_adapter{ 33 | 34 | comparator_adapter(const T& key, const T& key2, comparator_t2 comp)noexcept: 35 | comparator(comp), source_key(key), source_key2(key2){} 36 | 37 | bool operator()(const T& target_key)const{ 38 | return comparator(target_key, source_key, source_key2); 39 | } 40 | 41 | private: 42 | const T& source_key, source_key2; 43 | comparator_t2 comparator; 44 | }; 45 | 46 | template 47 | auto Make_CA(const T& key, comparator_t comp=equal)//makes comparator_adapter object 48 | ->comparator_adapter{ 49 | return comparator_adapter(key, comp); 50 | } 51 | template 52 | auto Make_CA(const T& key, const T& key2, comparator_t2 comp=is_in_range)//overload for range based comparators 53 | ->comparator_adapter{ 54 | return comparator_adapter(key, key2, comp); 55 | } 56 | 57 | #endif 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Utilities/Date.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DATE_GUARD 2 | #define DATE_GUARD 1 3 | 4 | #include 5 | #include 6 | 7 | class Date{ 8 | typedef Date __D; 9 | 10 | public: 11 | /* Member Attributes */ 12 | short day, month, year; 13 | 14 | /* Default & Parameterized Constructor (a.k.a ctor) */ 15 | Date(short day=0, short month=0, short year=0): 16 | day(day), month(month), year(year){} 17 | 18 | /* Comparators (comparison operators) */ 19 | bool operator==(const __D& other)const{ 20 | return day==other.day&&month==other.month&&year==other.year; 21 | } 22 | bool operator!=(const __D& other)const{ return !(*this==other); } 23 | 24 | bool operator>(const __D& other)const{ 25 | return year==other.year?month==other.month?day>other.day:month>other.month:year>other.year; 26 | } 27 | bool operator<=(const __D& other)const{ return !(*this>other); } 28 | 29 | bool operator<(const __D& other)const{ 30 | return year==other.year?month==other.month?day=(const __D& other)const{ return !(*this>(std::istream& in, __D& self){ 40 | in>>self.day>>self.month>>self.year; 41 | return in; 42 | } 43 | 44 | /* Overloaded 'fout & fin' Methods (works for .csv files too) */ 45 | friend std::ofstream& operator<<(std::ofstream& out, const __D& self){//'fout' operator 46 | out<>(std::ifstream& in, __D& self){//'fin' operator 50 | in>>self.day; in.get(); in>>self.month; in.get(); in>>self.year; 51 | return in; 52 | } 53 | }; 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Utilities/Pair.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PAIR_GUARD 2 | #define PAIR_GUARD 1 3 | 4 | #include 5 | 6 | #if __cplusplus < 201103L 7 | #define noexcept 8 | #endif 9 | 10 | template 11 | struct Pair{//custom minimal implementation of std::pair 12 | T1 first; 13 | T2 second; 14 | 15 | Pair()noexcept://default ctor 16 | first(), second(){} 17 | //Note: for objs, it invokes default ctor & for primitives, it sets default values 18 | 19 | #if __cplusplus >= 201103L 20 | template//_T1&& , _T2&& both are universal references 21 | Pair(_T1&& first, _T2&&... second)noexcept://parametized ctor 22 | first(std::forward<_T1>(first)), second(std::forward<_T2>(second)...){} 23 | #else 24 | Pair(const T1& first, const T2& second): 25 | first(first), second(second){} 26 | #endif 27 | 28 | friend std::ostream& operator<<(std::ostream& out, const Pair& self){//overloaded cout method 29 | out<>(std::istream& in, Pair& self){//overloaded cin method 33 | in>>self.first>>self.second; 34 | return in; 35 | } 36 | }; 37 | 38 | #if __cplusplus >= 201103L 39 | template 40 | auto Make_Pair(T1&& first, T2&& second)->Pair{//construct Pair without providing their types 41 | return Pair(std::forward(first), std::forward(second)); 42 | }//Note: doesn't deduce complex dependant (template) types 43 | #endif 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Utilities/Reverse_Iterator_Adapter.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REVERSE_ITERATOR_ADAPTER_GUARD 2 | #define REVERSE_ITERATOR_ADAPTER_GUARD 1 3 | 4 | /* Adapter for using const/non_const reverse_iterators inside 'range-based' loop: */ 5 | 6 | template//reverse_iterator_type 8 | struct reverse_iterator_adapter{ 9 | reverse_iterator_adapter(CT& reference): 10 | ref(reference){} 11 | 12 | RIT begin(){ return ref.rbegin(); } 13 | RIT end(){ return ref.rend(); } 14 | 15 | private: 16 | CT& ref; 17 | }; 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Utilities/Swap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SWAP_GUARD 2 | #define SWAP_GUARD 1 3 | 4 | #include 5 | 6 | /* Generalized & optimized swap (for move constructible heap objects): */ 7 | 8 | #if __cplusplus >= 201103L 9 | template 10 | void swap(T& a, T& b){ 11 | T temp=std::move(a); 12 | a=std::move(b); 13 | b=std::move(temp); 14 | } 15 | #else 16 | template 17 | void swap(T& a, T& b){ 18 | T temp=a; 19 | a=b; 20 | b=temp; 21 | } 22 | #endif 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /MY_DS_LIBRARY/Utilities/Type_Convertors.hpp: -------------------------------------------------------------------------------- 1 | //NOTE: this header must be included before required type-container headers in order to use thier respective convertor methods 2 | 3 | #if __cplusplus < 201103L 4 | #error: This file requires compiler and library support for the ISO C++ 2011 standard,\ 5 | which can be enabled with the -std=c++11 or -std=gnu++11 compiler options. 6 | #else 7 | 8 | #ifndef TYPE_CONVERTORS_GUARD 9 | #define TYPE_CONVERTORS_GUARD 1 10 | 11 | #include 12 | 13 | //forward declarations: 14 | template class HF_Stack; 15 | template class Vector; 16 | 17 | class convert{//non instantiating class (i.e. acts as interface) 18 | 19 | convert();//disabled default ctor 20 | public: 21 | 22 | template 23 | static Vector to_Vector(HF_Stack&& s){ 24 | Vector v(s.data, s.current, C); 25 | s.data=nullptr; s.current=0; 26 | return v; 27 | } 28 | 29 | template 30 | static HF_Stack to_HF_Stack(Vector&& v){ 31 | if(v.capacity()!=C) throw false;//exception safety 32 | HF_Stack s(v.data, v._size); 33 | v.data=nullptr; v._size=v._capacity=0; 34 | return s; 35 | } 36 | 37 | }; 38 | 39 | #endif 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /Mastering_Data_Structures_and_Algorithms_using_C_and_C++.torrent: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HypertextAssassin0273/Data_Structures_in_Cpp/03a9c13465a51377eea54ef4e1aac2a23002ee79/Mastering_Data_Structures_and_Algorithms_using_C_and_C++.torrent --------------------------------------------------------------------------------