├── .gitignore ├── LICENSE ├── README.md ├── data_structures-I ├── 01-defaultArgs │ └── defaultArgs.cpp ├── 02-max │ └── max.cpp ├── 03-dyad │ ├── dyad.h │ └── main.cpp ├── 04-AList │ ├── aList.h │ └── main.cpp ├── 05-AList │ ├── aList.h │ └── main.cpp ├── 06-LList │ ├── LList.h │ ├── main.cpp │ └── node.h ├── 07-Stack │ ├── main.cpp │ └── stack.h ├── 08-Queue │ ├── main.cpp │ ├── node.h │ └── queue.h ├── 09-rpnStack │ ├── expressions.txt │ ├── main.cpp │ ├── results.txt │ └── stack.h ├── 10-palindromes │ ├── main.cpp │ ├── palindromes.txt │ └── stack.h ├── 11-pal2 │ ├── main.cpp │ ├── node.h │ ├── palindromes.txt │ ├── queue.h │ └── stack.h ├── 12-DLList │ ├── DLList.h │ ├── main.cpp │ ├── node.h │ ├── student.h │ └── studentFile.txt ├── 13-DLList │ ├── DLList.h │ ├── main.cpp │ ├── node.h │ ├── student.h │ └── studentFile.txt └── docs │ └── header-template.cpp ├── data_structures-II ├── 01-Recursion-StockClass │ ├── part-1 │ │ └── recursion.cpp │ └── part-2 │ │ ├── Makefile │ │ ├── main.cpp │ │ ├── stock.cpp │ │ └── stock.h ├── 02-Reverse-LList-Recursion │ ├── part-1 │ │ └── reverse.cpp │ └── part-2 │ │ ├── LList.h │ │ ├── Makefile │ │ ├── main.cpp │ │ ├── node.h │ │ ├── stock.cpp │ │ └── stock.h ├── 03-AList-BinarySearch-Recursion │ ├── Makefile │ ├── aList.h │ ├── main.cpp │ ├── stock.cpp │ └── stock.h ├── 04-Hashing-Quadratic-Probing │ ├── Makefile │ ├── hashTable.h │ ├── htElement.h │ ├── main.cpp │ ├── stock.cpp │ └── stock.h ├── 05-Updated-AList-BinarySearch-Recursion │ ├── Makefile │ ├── Stock.txt │ ├── Stock_updated.txt │ ├── aList.h │ ├── main.cpp │ ├── stock.cpp │ └── stock.h ├── 06-BST │ ├── Makefile │ ├── Stock.txt │ ├── bst.h │ ├── main.cpp │ ├── node.h │ ├── stock.cpp │ └── stock.h ├── 07-Priority-Queue │ ├── Makefile │ ├── Stock.txt │ ├── main.cpp │ ├── priorityQueue.h │ ├── stock.cpp │ └── stock.h ├── 08-AVLTree │ ├── AVLTree.h │ ├── Makefile │ ├── Stock.txt │ ├── Stock_BF.txt │ ├── main.cpp │ ├── node.h │ ├── stock.cpp │ └── stock.h ├── 09-Huffman-Algorithm │ ├── Decoding │ │ ├── HuffmanStocks.txt │ │ ├── Makefile │ │ ├── main.cpp │ │ ├── node.h │ │ ├── stock.cpp │ │ └── stock.h │ └── Encoding │ │ ├── HuffmanStocks.txt │ │ ├── Makefile │ │ ├── main.cpp │ │ ├── node.h │ │ ├── stock.cpp │ │ └── stock.h ├── 10-AList-Sorting-Algorithms │ ├── Makefile │ ├── Stock.txt │ ├── main.cpp │ ├── sortedAList.h │ ├── stock.cpp │ └── stock.h ├── 11-Updated-AList-Sorting-Algorithms │ ├── Makefile │ ├── Stock.txt │ ├── main.cpp │ ├── sortedAList.h │ ├── stock.cpp │ └── stock.h ├── 12-Graph-Traversal │ └── a12.pdf └── docs │ ├── examples │ ├── AVL Tree Examples.pdf │ ├── AVLTree Examples 1.pdf │ ├── AVLTree Examples 2.pdf │ ├── HeapSort.pdf │ ├── Huffman Examples 1.pdf │ ├── Huffman Examples 2.pdf │ ├── LinkedLists.pdf │ ├── MergeSort.pdf │ ├── PriorityQueue Examples.pdf │ ├── QuickSort.pdf │ ├── Recursion Examples - 1.pdf │ ├── Recursion Examples - 2.pdf │ ├── Recursion Examples - 3.pdf │ ├── Searching Examples - 1.pdf │ ├── Searching Examples - 2.pdf │ ├── Sorting Timing.pdf │ ├── Towers.pdf │ ├── Tree Examples 1.pdf │ ├── Tree Examples 2.pdf │ └── Tree Examples 3.pdf │ └── header-template.cpp ├── programming-I ├── 01-helloworld │ └── HelloWorld.cpp ├── 02-recipe │ └── recipe.cpp ├── 03-ingredients │ └── ingredients.cpp ├── 04-gradeCalc │ └── gradeCalc.cpp ├── 05-shippingCost │ └── shippingCost.cpp ├── 06-morra │ ├── morra.cpp │ └── result.txt ├── 07-morraSeries │ ├── morraSeries.cpp │ └── morraSeriesResults.txt ├── 08-pizza │ └── pizza.cpp ├── 09-circleArea │ └── circleArea.cpp ├── 10-gradeCalc2 │ └── gradeCalc_2.cpp ├── 11-echoList │ └── echoAList.cpp ├── 12-deleteRepeats │ └── deleteRepeats.cpp └── docs │ ├── chapters │ ├── CH 1 FA21.pdf │ ├── CH 2_FA21.pdf │ ├── CH 3_FA21.pdf │ ├── CH 4_FA21.pdf │ ├── CH 5FA21_1.pdf │ ├── CH 5FA21_2.pdf │ └── CH 7FA21.pdf │ └── header-template.cpp └── programming-II ├── 01-twoDimensionalArray ├── Assignment1Data.txt └── testScoreAnalysis..cpp ├── 02-linearSearch-BubbleSort ├── Assignment1Data.txt ├── StudentNames.txt ├── sortSearch.cpp └── testScoreAnalysis.cpp ├── 03-binarySearch-selectionSort_introToPointers ├── StudentNames.txt ├── pointers.cpp └── sortSearch.cpp ├── 04-pointers └── pointers.cpp ├── 05-dynamicMemoryAllocation └── dynamic.cpp ├── 06-cString-string ├── cstrings.cpp └── strings.cpp ├── 07-structures ├── structDynamicArray.cpp └── structStaticArray.cpp ├── 08-fstream-manualUpdate ├── Employees.txt └── employeeSystem.cpp ├── 09-fstream-autoUpdate ├── Testers.txt ├── Translation.txt └── translateTest.cpp ├── 10-fstream-autoUpdate-binaryConversion ├── Testers.dat ├── Testers.txt ├── Translation.txt ├── textToBinary.cpp └── translateTestToBinary.cpp ├── 11-classes-headerFiles ├── Makefile ├── date.cpp ├── date.h ├── main.cpp ├── sport.cpp └── sport.h └── docs ├── examples ├── CString.pdf ├── Char Functions.pdf ├── Search.pdf └── Sort.pdf ├── exercises ├── Pointer Exercise 1.pdf ├── Pointer Exercise 2.pdf ├── Pointer Exercise 3.pdf ├── Pointers Introduction.pdf └── String Exercise .pdf └── header-template.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | ### C++ ### 2 | # Prerequisites 3 | *.d 4 | 5 | # Compiled Object files 6 | *.slo 7 | *.lo 8 | *.o 9 | *.obj 10 | 11 | # Precompiled Headers 12 | *.gch 13 | *.pch 14 | 15 | # Compiled Dynamic libraries 16 | *.so 17 | *.dylib 18 | *.dll 19 | 20 | # Fortran module files 21 | *.mod 22 | *.smod 23 | 24 | # Compiled Static libraries 25 | *.lai 26 | *.la 27 | *.a 28 | *.lib 29 | 30 | # Executables 31 | *.exe 32 | *.out 33 | *.app 34 | 35 | //**************************************************************************************************** 36 | 37 | .vscode/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Nicholas Ragland 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /data_structures-I/01-defaultArgs/defaultArgs.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program demonstrates the use of default arguments in a function. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #include 8 | using namespace std; 9 | 10 | //***************************************************************************************************** 11 | 12 | void displayMsg(const char msg[] = "Decide. Commit. Succeed.", char symbol = ' ', int num = 10); 13 | 14 | //***************************************************************************************************** 15 | 16 | int main() { 17 | displayMsg("I will decide.", '*', 15); 18 | displayMsg("I will commit.", '+'); 19 | displayMsg("I will succeed."); 20 | displayMsg(); 21 | 22 | return 0; 23 | } 24 | 25 | //***************************************************************************************************** 26 | 27 | void displayMsg(const char msg[], char symbol, int num) { 28 | for (int i = 0; i < num; ++i) 29 | cout << symbol; 30 | 31 | cout << msg; 32 | 33 | for (int i = 0; i < num; ++i) 34 | cout << symbol; 35 | 36 | cout << endl; 37 | } 38 | 39 | //***************************************************************************************************** 40 | /* 41 | 42 | ***************I will decide.*************** 43 | ++++++++++I will commit.++++++++++ 44 | I will succeed. 45 | Decide. Commit. Succeed. 46 | 47 | */ 48 | -------------------------------------------------------------------------------- /data_structures-I/02-max/max.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program demonstrates the use of function templates and function overloading which is used 4 | // when the operations need to be different for different types of data. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | //***************************************************************************************************** 14 | 15 | template 16 | void getData(T &a, T &b); 17 | void getData(char a[], char b[]); 18 | 19 | template 20 | T findMax(T a, T b); 21 | char *findMax(char a[], char b[]); 22 | 23 | template 24 | void displayMax(T a, T b, T max); 25 | 26 | //***************************************************************************************************** 27 | 28 | int main() { 29 | int aInt, 30 | maxInt, 31 | bInt; 32 | double aDouble, 33 | bDouble, 34 | maxDouble; 35 | char aChar, 36 | bChar, 37 | maxChar; 38 | char aArray[100], 39 | bArray[100]; 40 | char *maxArray; 41 | 42 | getData(aInt, bInt); 43 | getData(aDouble, bDouble); 44 | getData(aChar, bChar); 45 | getData(aArray, bArray); 46 | 47 | maxInt = findMax(aInt, bInt); 48 | maxDouble = findMax(aDouble, bDouble); 49 | maxChar = findMax(aChar, bChar); 50 | maxArray = findMax(aArray, bArray); 51 | 52 | displayMax(aInt, bInt, maxInt); 53 | displayMax(aDouble, bDouble, maxDouble); 54 | displayMax(aChar, bChar, maxChar); 55 | displayMax(aArray, bArray, maxArray); 56 | 57 | return 0; 58 | } 59 | 60 | //***************************************************************************************************** 61 | 62 | template 63 | void getData(T &a, T &b) { 64 | cout << "\nEnter two " << typeid(T).name() << "s" << endl; 65 | 66 | cout << "First: "; 67 | cin >> a; 68 | 69 | cout << "Second: "; 70 | cin >> b; 71 | } 72 | 73 | //***************************************************************************************************** 74 | 75 | void getData(char a[], char b[]) { 76 | cout << "\nEnter two c-strings" << endl; 77 | 78 | cout << "First: "; 79 | cin.ignore(); 80 | cin.getline(a, 100); 81 | 82 | cout << "Second: "; 83 | cin.getline(b, 100); 84 | } 85 | 86 | //***************************************************************************************************** 87 | 88 | template 89 | T findMax(T a, T b) { 90 | T max = a; 91 | 92 | if (max < b) 93 | max = b; 94 | 95 | return max; 96 | } 97 | 98 | //***************************************************************************************************** 99 | 100 | char *findMax(char a[], char b[]) { 101 | char *max = a; 102 | 103 | if (strcmp(a, b) < 0) 104 | max = b; 105 | 106 | return max; 107 | } 108 | 109 | //***************************************************************************************************** 110 | 111 | template 112 | void displayMax(T a, T b, T max) { 113 | cout << "\nType: " << typeid(T).name() 114 | << "\nFirst: " << a 115 | << "\nSecond: " << b 116 | << "\nMax: " << max << endl; 117 | } 118 | 119 | //***************************************************************************************************** 120 | /* 121 | 122 | Enter two ints 123 | First: 1 124 | Second: 2 125 | 126 | Enter two doubles 127 | First: 1.5 128 | Second: 2.5 129 | 130 | Enter two chars 131 | First: a 132 | Second: b 133 | 134 | Enter two c-strings 135 | First: apple 136 | Second: orange 137 | 138 | Type: int 139 | First: 1 140 | Second: 2 141 | Max: 2 142 | 143 | Type: double 144 | First: 1.5 145 | Second: 2.5 146 | Max: 2.5 147 | 148 | Type: char 149 | First: a 150 | Second: b 151 | Max: b 152 | 153 | Type: char * 154 | First: apple 155 | Second: orange 156 | Max: orange 157 | 158 | */ -------------------------------------------------------------------------------- /data_structures-I/03-dyad/dyad.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a class template for a dyad, which is a pair of values of the same 4 | // type. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef DYAD_H 9 | #define DYAD_H 10 | 11 | //***************************************************************************************************** 12 | 13 | template 14 | class Dyad { 15 | private: 16 | T val1; 17 | T val2; 18 | 19 | public: 20 | Dyad(T v1 = 0, T v2 = 0); 21 | T getFirst() const; 22 | T getSecond() const; 23 | void get2Values(T &v1, T &v2) const; 24 | void swapValues(); 25 | }; 26 | 27 | //***************************************************************************************************** 28 | 29 | template 30 | Dyad::Dyad(T val1, T val2) { 31 | this->val1 = val1; 32 | this->val2 = val2; 33 | } 34 | 35 | //***************************************************************************************************** 36 | 37 | template 38 | T Dyad::getFirst() const { 39 | return val1; 40 | } 41 | 42 | //***************************************************************************************************** 43 | 44 | template 45 | T Dyad::getSecond() const { 46 | return val2; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | template 52 | void Dyad::get2Values(T &v1, T &v2) const { 53 | v1 = val1; 54 | v2 = val2; 55 | } 56 | 57 | //***************************************************************************************************** 58 | 59 | template 60 | void Dyad::swapValues() { 61 | T temp = val1; 62 | 63 | val1 = val2; 64 | val2 = temp; 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | #endif -------------------------------------------------------------------------------- /data_structures-I/03-dyad/main.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program demonstrates the use of the Dyad class by instantiating objects of type int, 4 | // double, and char and performing its methods. 5 | // 6 | // Other files required: 7 | // 1. dyad.h - header file for the Dyad class 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include "dyad.h" 12 | 13 | #include 14 | using namespace std; 15 | 16 | //***************************************************************************************************** 17 | 18 | int main() { 19 | int firstInt, 20 | secondInt; 21 | double firstDouble, 22 | secondDouble; 23 | char firstChar, 24 | secondChar; 25 | 26 | Dyad dyadInt(1, 2); 27 | Dyad dyadDouble(1.5, 2.5); 28 | Dyad dyadChar('A', 'B'); 29 | 30 | cout << "First int: " << dyadInt.getFirst() 31 | << "\nSecond int: " << dyadInt.getSecond() << endl; 32 | 33 | dyadInt.swapValues(); 34 | dyadInt.get2Values(firstInt, secondInt); 35 | 36 | cout << "\nFirst int after swap: " << firstInt 37 | << "\nSecond int after swap: " << secondInt 38 | << "\n\nFirst double : " << dyadDouble.getFirst() 39 | << "\nSecond double: " << dyadDouble.getSecond() << endl; 40 | 41 | dyadDouble.swapValues(); 42 | dyadDouble.get2Values(firstDouble, secondDouble); 43 | 44 | cout << "\nFirst double after swap: " << firstDouble 45 | << "\nSecond double after swap: " << secondDouble 46 | << "\n\nFirst char: " << dyadChar.getFirst() 47 | << "\nSecond char: " << dyadChar.getSecond() << endl; 48 | 49 | dyadChar.swapValues(); 50 | dyadChar.get2Values(firstChar, secondChar); 51 | 52 | cout << "\nFirst char after swap: " << firstChar 53 | << "\nSecond char after swap: " << secondChar << endl; 54 | 55 | return 0; 56 | } 57 | 58 | //***************************************************************************************************** 59 | /* 60 | 61 | First int: 1 62 | Second int: 2 63 | 64 | First int after swap: 2 65 | Second int after swap: 1 66 | 67 | First double: 1.5 68 | Second double: 2.5 69 | 70 | First double after swap: 2.5 71 | Second double after swap: 1.5 72 | 73 | First char: A 74 | Second char: B 75 | 76 | First char after swap: B 77 | Second char after swap: A 78 | 79 | */ -------------------------------------------------------------------------------- /data_structures-I/04-AList/main.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program demonstrates the use of the AList class by instantiating an array-based list of 4 | // short integers and performing its methods. 5 | // 6 | // Other files required: 7 | // 1. aList.h - header file for the AList class 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include "aList.h" 12 | 13 | #include 14 | using namespace std; 15 | 16 | //***************************************************************************************************** 17 | 18 | void displayAList(bool success, AList &shortList); 19 | void checkState(AList &shortList); 20 | 21 | //***************************************************************************************************** 22 | 23 | int main() { 24 | AList shortList(3); 25 | short num; 26 | int i = 0; 27 | bool success; 28 | 29 | checkState(shortList); 30 | 31 | num = 10; 32 | success = shortList.insertFront(num); 33 | if (success) 34 | cout << "insert front: " << num << endl; 35 | displayAList(success, shortList); 36 | 37 | num = 20; 38 | success = shortList.insertAtIndex(num, i); 39 | if (success) 40 | cout << "insert index " << i << ": " << num << endl; 41 | displayAList(success, shortList); 42 | 43 | num = 30; 44 | success = shortList.insertBack(num); 45 | if (success) 46 | cout << "insert back: " << num << endl; 47 | displayAList(success, shortList); 48 | 49 | return 0; 50 | } 51 | 52 | //***************************************************************************************************** 53 | 54 | void displayAList(bool success, AList &shortList) { 55 | short min; 56 | int cap, 57 | numVal; 58 | 59 | if (success) { 60 | shortList.display(); 61 | shortList.getSmallest(min); 62 | cap = shortList.getCapacity(); 63 | numVal = shortList.getNumValues(); 64 | 65 | if (numVal > 0) 66 | cout << "capacity: " << cap 67 | << "\tnumVal: " << numVal 68 | << "\tsmallest: " << min << endl; 69 | else 70 | cout << "capacity: " << cap 71 | << "\tnumValues: " << numVal << endl; 72 | 73 | cout << endl; 74 | } 75 | 76 | checkState(shortList); 77 | } 78 | 79 | //***************************************************************************************************** 80 | 81 | void checkState(AList &shortList) { 82 | if (shortList.isFull()) 83 | cerr << "list is full\n\n"; 84 | else if (shortList.isEmpty()) 85 | cerr << "list is empty\n\n"; 86 | } 87 | 88 | //***************************************************************************************************** 89 | /* 90 | 91 | list is empty 92 | 93 | insert front: 10 94 | [0] 10 95 | capacity: 3 numVal: 1 smallest: 10 96 | 97 | insert index 0: 20 98 | [0] 20 99 | [1] 10 100 | capacity: 3 numVal: 2 smallest: 10 101 | 102 | insert back: 30 103 | [0] 20 104 | [1] 10 105 | [2] 30 106 | capacity: 3 numVal: 3 smallest: 10 107 | 108 | list is full 109 | 110 | */ -------------------------------------------------------------------------------- /data_structures-I/06-LList/main.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program demonstrates the use of the LList class by instantiating a singly linked shortList of 4 | // short integers and performing its methods. 5 | // 6 | // Other files required: 7 | // 1. LList.h - header file for the LList class 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include "LList.h" 12 | 13 | #include 14 | using namespace std; 15 | 16 | //***************************************************************************************************** 17 | 18 | void displayLList(bool success, LList &shortList); 19 | void checkState(LList &shortList); 20 | 21 | //***************************************************************************************************** 22 | 23 | int main() { 24 | LList shortList; 25 | bool success; 26 | short num; 27 | 28 | checkState(shortList); 29 | 30 | for (int i = 0; i < 5; ++i) { 31 | num = 10 * (i + 1); 32 | success = shortList.insert(num); 33 | 34 | if (success) 35 | cout << "inserted " << num << endl; 36 | 37 | displayLList(success, shortList); 38 | } 39 | 40 | cout << endl; 41 | 42 | for (int i = 0; i < 2; ++i) { 43 | num = 10 * (i + 1); 44 | success = shortList.retrieve(num); 45 | 46 | if (success) 47 | cout << "retrieved " << num << endl; 48 | 49 | displayLList(success, shortList); 50 | } 51 | 52 | cout << endl; 53 | 54 | for (int i = 0; i < 5; ++i) { 55 | num = (10 * (5 - i)); 56 | success = shortList.remove(num); 57 | 58 | if (success) 59 | cout << "removed " << num << endl; 60 | 61 | displayLList(success, shortList); 62 | } 63 | 64 | return 0; 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | void displayLList(bool success, LList &shortList) { 70 | short front, 71 | rear; 72 | int numVal; 73 | 74 | if (success) { 75 | shortList.display(); 76 | shortList.viewFront(front); 77 | shortList.viewRear(rear); 78 | numVal = shortList.getNumValues(); 79 | 80 | if (numVal > 0) 81 | cout << "numValues: " << numVal 82 | << "\tfront: " << front 83 | << "\trear: " << rear << endl; 84 | else 85 | cout << "numValues: " << numVal << endl; 86 | } 87 | 88 | checkState(shortList); 89 | } 90 | 91 | //***************************************************************************************************** 92 | 93 | void checkState(LList &shortList) { 94 | if (shortList.isFull()) 95 | cout << "\nlist is full\n\n"; 96 | else if (shortList.isEmpty()) 97 | cout << "\nlist is empty\n\n"; 98 | } 99 | 100 | //***************************************************************************************************** 101 | /* 102 | 103 | list is empty 104 | 105 | inserted 10 106 | 10 107 | numValues: 1 front: 10 rear: 10 108 | inserted 20 109 | 10 -> 20 110 | numValues: 2 front: 10 rear: 20 111 | inserted 30 112 | 10 -> 20 -> 30 113 | numValues: 3 front: 10 rear: 30 114 | inserted 40 115 | 10 -> 20 -> 30 -> 40 116 | numValues: 4 front: 10 rear: 40 117 | inserted 50 118 | 10 -> 20 -> 30 -> 40 -> 50 119 | numValues: 5 front: 10 rear: 50 120 | 121 | retrieved 10 122 | 10 -> 20 -> 30 -> 40 -> 50 123 | numValues: 5 front: 10 rear: 50 124 | retrieved 20 125 | 10 -> 20 -> 30 -> 40 -> 50 126 | numValues: 5 front: 10 rear: 50 127 | 128 | removed 50 129 | 10 -> 20 -> 30 -> 40 130 | numValues: 4 front: 10 rear: 40 131 | removed 40 132 | 10 -> 20 -> 30 133 | numValues: 3 front: 10 rear: 30 134 | removed 30 135 | 10 -> 20 136 | numValues: 2 front: 10 rear: 20 137 | removed 20 138 | 10 139 | numValues: 1 front: 10 rear: 10 140 | removed 10 141 | 142 | numValues: 0 143 | 144 | list is empty 145 | 146 | */ 147 | -------------------------------------------------------------------------------- /data_structures-I/06-LList/node.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a struct template that represents a node in a singly linked list. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #ifndef NODE_H 8 | #define NODE_H 9 | 10 | //***************************************************************************************************** 11 | 12 | template 13 | struct Node { 14 | T data; 15 | Node *next; 16 | 17 | Node(); 18 | Node(const T &d, Node *n = nullptr); 19 | }; 20 | 21 | //***************************************************************************************************** 22 | 23 | template 24 | Node::Node() { 25 | data = T(); // T() - default initialization (0 for numbers, empty string for strings, etc.) 26 | next = nullptr; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | template 32 | Node::Node(const T &d, Node *n) { 33 | data = d; 34 | next = n; 35 | } 36 | 37 | //***************************************************************************************************** 38 | 39 | #endif -------------------------------------------------------------------------------- /data_structures-I/07-Stack/main.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program demonstrates the use of the Stack class by instantiating an array-based list 4 | // stack of short integers and performing its methods. 5 | // 6 | // Other files required: 7 | // 1. stack.h - header file for the Stack class 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include "stack.h" 12 | 13 | #include 14 | using namespace std; 15 | 16 | //***************************************************************************************************** 17 | 18 | void displayStack(bool success, Stack &stack); 19 | void checkState(Stack &stack); 20 | 21 | //***************************************************************************************************** 22 | 23 | int main() { 24 | Stack shortStack(10); 25 | short num; 26 | bool success; 27 | 28 | checkState(shortStack); 29 | 30 | for (int i = 0; i < 10; ++i) { 31 | num = 10 * (i + 1); 32 | success = shortStack.push(num); 33 | 34 | if (success) 35 | cout << "pushed: " << num << endl; 36 | 37 | displayStack(success, shortStack); 38 | } 39 | 40 | for (int i = 0; i < 10; ++i) { 41 | success = shortStack.pop(num); 42 | 43 | if (success) 44 | cout << "popped: " << num << endl; 45 | 46 | displayStack(success, shortStack); 47 | } 48 | 49 | return 0; 50 | } 51 | 52 | //***************************************************************************************************** 53 | 54 | void displayStack(bool success, Stack &stack) { 55 | short num; 56 | int numVal; 57 | 58 | if (success) { 59 | numVal = stack.getNumValues(); 60 | stack.peek(num); 61 | 62 | if (numVal > 0) 63 | cout << "numValues: " << numVal 64 | << "\tpeeked: " << num << endl; 65 | else 66 | cout << "numValues: " << numVal << endl; 67 | } 68 | 69 | checkState(stack); 70 | } 71 | 72 | //***************************************************************************************************** 73 | 74 | void checkState(Stack &stack) { 75 | if (stack.isFull()) 76 | cerr << "\nstack is full\n\n"; 77 | else if (stack.isEmpty()) 78 | cerr << "\nstack is empty\n\n"; 79 | } 80 | 81 | //***************************************************************************************************** 82 | /* 83 | 84 | stack is empty 85 | 86 | pushed: 10 87 | numValues: 1 peeked: 10 88 | pushed: 20 89 | numValues: 2 peeked: 20 90 | pushed: 30 91 | numValues: 3 peeked: 30 92 | pushed: 40 93 | numValues: 4 peeked: 40 94 | pushed: 50 95 | numValues: 5 peeked: 50 96 | pushed: 60 97 | numValues: 6 peeked: 60 98 | pushed: 70 99 | numValues: 7 peeked: 70 100 | pushed: 80 101 | numValues: 8 peeked: 80 102 | pushed: 90 103 | numValues: 9 peeked: 90 104 | pushed: 100 105 | numValues: 10 peeked: 100 106 | 107 | stack is full 108 | 109 | popped: 100 110 | numValues: 9 peeked: 90 111 | popped: 90 112 | numValues: 8 peeked: 80 113 | popped: 80 114 | numValues: 7 peeked: 70 115 | popped: 70 116 | numValues: 6 peeked: 60 117 | popped: 60 118 | numValues: 5 peeked: 50 119 | popped: 50 120 | numValues: 4 peeked: 40 121 | popped: 40 122 | numValues: 3 peeked: 30 123 | popped: 30 124 | numValues: 2 peeked: 20 125 | popped: 20 126 | numValues: 1 peeked: 10 127 | popped: 10 128 | numValues: 0 129 | 130 | stack is empty 131 | 132 | */ -------------------------------------------------------------------------------- /data_structures-I/07-Stack/stack.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a class template for an array-based list using a stack structure. 4 | // Last-in, first-out (LIFO) 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STACK_H 9 | #define STACK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | template 14 | class Stack { 15 | private: 16 | T *stack; 17 | int top; 18 | int capacity; 19 | 20 | public: 21 | Stack(int cap = 100); 22 | ~Stack(); 23 | bool push(const T &dataIn); 24 | bool pop(T &dataOut); 25 | bool peek(T &dataOut) const; 26 | int getNumValues() const; 27 | bool isFull() const; 28 | bool isEmpty() const; 29 | }; 30 | 31 | //***************************************************************************************************** 32 | 33 | template 34 | Stack::Stack(int cap) { 35 | capacity = cap; 36 | top = -1; 37 | stack = new T[capacity]; 38 | } 39 | 40 | //***************************************************************************************************** 41 | 42 | template 43 | Stack::~Stack() { 44 | delete[] stack; 45 | stack = nullptr; 46 | capacity = 0; 47 | top = -1; 48 | } 49 | 50 | //***************************************************************************************************** 51 | 52 | template 53 | bool Stack::push(const T &dataIn) { 54 | bool success = false; 55 | 56 | if ((top + 1) < capacity) { 57 | top++; 58 | stack[top] = dataIn; 59 | success = true; 60 | } 61 | 62 | return success; 63 | } 64 | 65 | //***************************************************************************************************** 66 | 67 | template 68 | bool Stack::pop(T &dataOut) { 69 | bool success = false; 70 | 71 | if (top >= 0) { 72 | dataOut = stack[top]; 73 | top--; 74 | success = true; 75 | } 76 | 77 | return success; 78 | } 79 | 80 | //***************************************************************************************************** 81 | 82 | template 83 | bool Stack::peek(T &dataOut) const { 84 | bool success = false; 85 | 86 | if (top >= 0) { 87 | dataOut = stack[top]; 88 | success = true; 89 | } 90 | 91 | return success; 92 | } 93 | 94 | //***************************************************************************************************** 95 | 96 | template 97 | int Stack::getNumValues() const { 98 | return (top + 1); 99 | } 100 | 101 | //***************************************************************************************************** 102 | 103 | template 104 | bool Stack::isFull() const { 105 | return ((top + 1) == capacity); 106 | } 107 | 108 | //***************************************************************************************************** 109 | 110 | template 111 | bool Stack::isEmpty() const { 112 | return (top < 0); 113 | } 114 | 115 | //***************************************************************************************************** 116 | 117 | #endif -------------------------------------------------------------------------------- /data_structures-I/08-Queue/main.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program demonstrates the use of the Queue class by instantiating a singly linked list 4 | // queue of short integers and performing its methods. 5 | // 6 | // Other files required: 7 | // 1. queue.h - header file for the Queue class 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include "queue.h" 12 | 13 | #include 14 | using namespace std; 15 | 16 | //***************************************************************************************************** 17 | 18 | void displayQueue(bool success, Queue &shortQueue); 19 | void checkState(Queue &shortQueue); 20 | 21 | //***************************************************************************************************** 22 | 23 | int main() { 24 | Queue shortQueue; 25 | short num; 26 | bool success; 27 | 28 | checkState(shortQueue); 29 | 30 | for (int i = 0; i < 10; ++i) { 31 | num = 10 * (i + 1); 32 | success = shortQueue.enqueue(num); 33 | 34 | if (success) 35 | cout << "enqueued: " << num << endl; 36 | 37 | displayQueue(success, shortQueue); 38 | } 39 | 40 | cout << endl; 41 | 42 | for (int i = 0; i < 10; ++i) { 43 | success = shortQueue.dequeue(num); 44 | 45 | if (success) 46 | cout << "dequeued: " << num << endl; 47 | 48 | displayQueue(success, shortQueue); 49 | } 50 | 51 | return 0; 52 | } 53 | 54 | //***************************************************************************************************** 55 | 56 | void displayQueue(bool success, Queue &shortQueue) { 57 | short front, 58 | back; 59 | int numVal; 60 | 61 | if (success) { 62 | numVal = shortQueue.getNumValues(); 63 | shortQueue.viewFront(front); 64 | shortQueue.viewRear(back); 65 | 66 | if (numVal > 0) 67 | cout << "numValues: " << numVal 68 | << "\tfront: " << front 69 | << "\tback: " << back << endl; 70 | else 71 | cout << "numValues: " << numVal << endl; 72 | } 73 | 74 | checkState(shortQueue); 75 | } 76 | 77 | //***************************************************************************************************** 78 | 79 | void checkState(Queue &shortQueue) { 80 | if (shortQueue.isFull()) 81 | cerr << "\nqueue is full\n\n"; 82 | else if (shortQueue.isEmpty()) 83 | cerr << "\nqueue is empty\n\n"; 84 | } 85 | 86 | //***************************************************************************************************** 87 | /* 88 | 89 | queue is empty 90 | 91 | enqueued: 10 92 | numValues: 1 front: 10 back: 10 93 | enqueued: 20 94 | numValues: 2 front: 10 back: 20 95 | enqueued: 30 96 | numValues: 3 front: 10 back: 30 97 | enqueued: 40 98 | numValues: 4 front: 10 back: 40 99 | enqueued: 50 100 | numValues: 5 front: 10 back: 50 101 | enqueued: 60 102 | numValues: 6 front: 10 back: 60 103 | enqueued: 70 104 | numValues: 7 front: 10 back: 70 105 | enqueued: 80 106 | numValues: 8 front: 10 back: 80 107 | enqueued: 90 108 | numValues: 9 front: 10 back: 90 109 | enqueued: 100 110 | numValues: 10 front: 10 back: 100 111 | 112 | dequeued: 10 113 | numValues: 9 front: 20 back: 100 114 | dequeued: 20 115 | numValues: 8 front: 30 back: 100 116 | dequeued: 30 117 | numValues: 7 front: 40 back: 100 118 | dequeued: 40 119 | numValues: 6 front: 50 back: 100 120 | dequeued: 50 121 | numValues: 5 front: 60 back: 100 122 | dequeued: 60 123 | numValues: 4 front: 70 back: 100 124 | dequeued: 70 125 | numValues: 3 front: 80 back: 100 126 | dequeued: 80 127 | numValues: 2 front: 90 back: 100 128 | dequeued: 90 129 | numValues: 1 front: 100 back: 100 130 | dequeued: 100 131 | numValues: 0 132 | 133 | queue is empty 134 | 135 | */ -------------------------------------------------------------------------------- /data_structures-I/08-Queue/node.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a struct template that represents a node in a singly linked list. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #ifndef NODE_H 8 | #define NODE_H 9 | 10 | //***************************************************************************************************** 11 | 12 | template 13 | struct Node { 14 | T data; 15 | Node *next; 16 | 17 | Node(); 18 | Node(const T &d, Node *n = nullptr); 19 | }; 20 | 21 | //***************************************************************************************************** 22 | 23 | template 24 | Node::Node() { 25 | data = T(); // T() - default initialization (0 for numbers, empty string for strings, etc.) 26 | next = nullptr; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | template 32 | Node::Node(const T &d, Node *n) { 33 | data = d; 34 | next = n; 35 | } 36 | 37 | //***************************************************************************************************** 38 | 39 | #endif -------------------------------------------------------------------------------- /data_structures-I/09-rpnStack/expressions.txt: -------------------------------------------------------------------------------- 1 | 2 4 * 5 + ; 2 | 13 5 % 5 + ; 3 | 15 1 + 2 / 1 - ; 4 | 15 + 1 + 2 / 1 - ; 5 | 3 4 + 15 10 - * ; 6 | 3 4 + 6 15 10 - * ; 7 | 2 13 + 14 6 - - 5 * 4 + ; 8 | 35 6 4 2 2 / + * - ; 9 | 3 4 + 1 2 - * 4 2 / 3 - + ; 10 | 3 14 1 2 4 2 3 + % * + - + ; -------------------------------------------------------------------------------- /data_structures-I/09-rpnStack/results.txt: -------------------------------------------------------------------------------- 1 | 2 | (Token: 2) Push 2 3 | (Token: 4) Push 4 4 | (Token: *) Pop 4 Pop 2 Push 8 5 | (Token: 5) Push 5 6 | (Token: +) Pop 5 Pop 8 Push 13 7 | (Token: ;) Pop 13 8 | Valid: result = 13 9 | 10 | 11 | (Token: 13) Push 13 12 | (Token: 5) Push 5 13 | (Token: %) Pop 5 Pop 13 Push 3 14 | (Token: 5) Push 5 15 | (Token: +) Pop 5 Pop 3 Push 8 16 | (Token: ;) Pop 8 17 | Valid: result = 8 18 | 19 | 20 | (Token: 15) Push 15 21 | (Token: 1) Push 1 22 | (Token: +) Pop 1 Pop 15 Push 16 23 | (Token: 2) Push 2 24 | (Token: /) Pop 2 Pop 16 Push 8 25 | (Token: 1) Push 1 26 | (Token: -) Pop 1 Pop 8 Push 7 27 | (Token: ;) Pop 7 28 | Valid: result = 7 29 | 30 | 31 | (Token: 15) Push 15 32 | (Token: +) 33 | (Token: 1) 34 | (Token: +) 35 | (Token: 2) 36 | (Token: /) 37 | (Token: 1) 38 | (Token: -) 39 | (Token: ;) 40 | Invalid RPN expression - not enough operands 41 | 42 | 43 | (Token: 3) Push 3 44 | (Token: 4) Push 4 45 | (Token: +) Pop 4 Pop 3 Push 7 46 | (Token: 15) Push 15 47 | (Token: 10) Push 10 48 | (Token: -) Pop 10 Pop 15 Push 5 49 | (Token: *) Pop 5 Pop 7 Push 35 50 | (Token: ;) Pop 35 51 | Valid: result = 35 52 | 53 | 54 | (Token: 3) Push 3 55 | (Token: 4) Push 4 56 | (Token: +) Pop 4 Pop 3 Push 7 57 | (Token: 6) Push 6 58 | (Token: 15) Push 15 59 | (Token: 10) Push 10 60 | (Token: -) Pop 10 Pop 15 Push 5 61 | (Token: *) Pop 5 Pop 6 Push 30 62 | (Token: ;) 63 | Invalid RPN expression - too many operands 64 | 65 | 66 | (Token: 2) Push 2 67 | (Token: 13) Push 13 68 | (Token: +) Pop 13 Pop 2 Push 15 69 | (Token: 14) Push 14 70 | (Token: 6) Push 6 71 | (Token: -) Pop 6 Pop 14 Push 8 72 | (Token: -) Pop 8 Pop 15 Push 7 73 | (Token: 5) Push 5 74 | (Token: *) Pop 5 Pop 7 Push 35 75 | (Token: 4) Push 4 76 | (Token: +) Pop 4 Pop 35 Push 39 77 | (Token: ;) Pop 39 78 | Valid: result = 39 79 | 80 | 81 | (Token: 35) Push 35 82 | (Token: 6) Push 6 83 | (Token: 4) Push 4 84 | (Token: 2) Push 2 85 | (Token: 2) Push 2 86 | (Token: /) Pop 2 Pop 2 Push 1 87 | (Token: +) Pop 1 Pop 4 Push 5 88 | (Token: *) Pop 5 Pop 6 Push 30 89 | (Token: -) Pop 30 Pop 35 Push 5 90 | (Token: ;) Pop 5 91 | Valid: result = 5 92 | 93 | 94 | (Token: 3) Push 3 95 | (Token: 4) Push 4 96 | (Token: +) Pop 4 Pop 3 Push 7 97 | (Token: 1) Push 1 98 | (Token: 2) Push 2 99 | (Token: -) Pop 2 Pop 1 Push -1 100 | (Token: *) Pop -1 Pop 7 Push -7 101 | (Token: 4) Push 4 102 | (Token: 2) Push 2 103 | (Token: /) Pop 2 Pop 4 Push 2 104 | (Token: 3) Push 3 105 | (Token: -) Pop 3 Pop 2 Push -1 106 | (Token: +) Pop -1 Pop -7 Push -8 107 | (Token: ;) Pop -8 108 | Valid: result = -8 109 | 110 | 111 | (Token: 3) Push 3 112 | (Token: 14) Push 14 113 | (Token: 1) Push 1 114 | (Token: 2) Push 2 115 | (Token: 4) Push 4 116 | (Token: 2) Push 2 117 | (Token: 3) Push 3 118 | (Token: +) Pop 3 Pop 2 Push 5 119 | (Token: %) Pop 5 Pop 4 Push 4 120 | (Token: *) Pop 4 Pop 2 Push 8 121 | (Token: +) Pop 8 Pop 1 Push 9 122 | (Token: -) Pop 9 Pop 14 Push 5 123 | (Token: +) Pop 5 Pop 3 Push 8 124 | (Token: ;) Pop 8 125 | Valid: result = 8 126 | 127 | -------------------------------------------------------------------------------- /data_structures-I/09-rpnStack/stack.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a class template for an array-based list using a stack structure. 4 | // Last-in, first-out (LIFO) 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STACK_H 9 | #define STACK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | template 14 | class Stack { 15 | private: 16 | T *stack; 17 | int top; 18 | int capacity; 19 | 20 | public: 21 | Stack(int cap = 100); 22 | ~Stack(); 23 | bool push(const T &dataIn); 24 | bool pop(T &dataOut); 25 | bool peek(T &dataOut) const; 26 | int getNumValues() const; 27 | bool isFull() const; 28 | bool isEmpty() const; 29 | }; 30 | 31 | //***************************************************************************************************** 32 | 33 | template 34 | Stack::Stack(int cap) { 35 | capacity = cap; 36 | top = -1; 37 | stack = new T[capacity]; 38 | } 39 | 40 | //***************************************************************************************************** 41 | 42 | template 43 | Stack::~Stack() { 44 | delete[] stack; 45 | stack = nullptr; 46 | capacity = 0; 47 | top = -1; 48 | } 49 | 50 | //***************************************************************************************************** 51 | 52 | template 53 | bool Stack::push(const T &dataIn) { 54 | bool success = false; 55 | 56 | if ((top + 1) < capacity) { 57 | top++; 58 | stack[top] = dataIn; 59 | success = true; 60 | } 61 | 62 | return success; 63 | } 64 | 65 | //***************************************************************************************************** 66 | 67 | template 68 | bool Stack::pop(T &dataOut) { 69 | bool success = false; 70 | 71 | if (top >= 0) { 72 | dataOut = stack[top]; 73 | top--; 74 | success = true; 75 | } 76 | 77 | return success; 78 | } 79 | 80 | //***************************************************************************************************** 81 | 82 | template 83 | bool Stack::peek(T &dataOut) const { 84 | bool success = false; 85 | 86 | if (top >= 0) { 87 | dataOut = stack[top]; 88 | success = true; 89 | } 90 | 91 | return success; 92 | } 93 | 94 | //***************************************************************************************************** 95 | 96 | template 97 | int Stack::getNumValues() const { 98 | return (top + 1); 99 | } 100 | 101 | //***************************************************************************************************** 102 | 103 | template 104 | bool Stack::isFull() const { 105 | return ((top + 1) == capacity); 106 | } 107 | 108 | //***************************************************************************************************** 109 | 110 | template 111 | bool Stack::isEmpty() const { 112 | return (top < 0); 113 | } 114 | 115 | //***************************************************************************************************** 116 | 117 | #endif -------------------------------------------------------------------------------- /data_structures-I/10-palindromes/palindromes.txt: -------------------------------------------------------------------------------- 1 | Aha!#Is it I? It is I!#deed#rotors#Never odd or even#No lemons, no melon#Race car#comic#Was it a car or a cat I saw?#daddy# -------------------------------------------------------------------------------- /data_structures-I/10-palindromes/stack.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a class template for an array-based list using a stack structure. 4 | // Last-in, first-out (LIFO) 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STACK_H 9 | #define STACK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | template 14 | class Stack { 15 | private: 16 | T *stack; 17 | int top; 18 | int capacity; 19 | 20 | public: 21 | Stack(int cap = 100); 22 | ~Stack(); 23 | bool push(const T &dataIn); 24 | bool pop(T &dataOut); 25 | bool peek(T &dataOut) const; 26 | int getNumValues() const; 27 | bool isFull() const; 28 | bool isEmpty() const; 29 | }; 30 | 31 | //***************************************************************************************************** 32 | 33 | template 34 | Stack::Stack(int cap) { 35 | capacity = cap; 36 | top = -1; 37 | stack = new T[capacity]; 38 | } 39 | 40 | //***************************************************************************************************** 41 | 42 | template 43 | Stack::~Stack() { 44 | delete[] stack; 45 | stack = nullptr; 46 | capacity = 0; 47 | top = -1; 48 | } 49 | 50 | //***************************************************************************************************** 51 | 52 | template 53 | bool Stack::push(const T &dataIn) { 54 | bool success = false; 55 | 56 | if ((top + 1) < capacity) { 57 | top++; 58 | stack[top] = dataIn; 59 | success = true; 60 | } 61 | 62 | return success; 63 | } 64 | 65 | //***************************************************************************************************** 66 | 67 | template 68 | bool Stack::pop(T &dataOut) { 69 | bool success = false; 70 | 71 | if (top >= 0) { 72 | dataOut = stack[top]; 73 | top--; 74 | success = true; 75 | } 76 | 77 | return success; 78 | } 79 | 80 | //***************************************************************************************************** 81 | 82 | template 83 | bool Stack::peek(T &dataOut) const { 84 | bool success = false; 85 | 86 | if (top >= 0) { 87 | dataOut = stack[top]; 88 | success = true; 89 | } 90 | 91 | return success; 92 | } 93 | 94 | //***************************************************************************************************** 95 | 96 | template 97 | int Stack::getNumValues() const { 98 | return (top + 1); 99 | } 100 | 101 | //***************************************************************************************************** 102 | 103 | template 104 | bool Stack::isFull() const { 105 | return ((top + 1) == capacity); 106 | } 107 | 108 | //***************************************************************************************************** 109 | 110 | template 111 | bool Stack::isEmpty() const { 112 | return (top < 0); 113 | } 114 | 115 | //***************************************************************************************************** 116 | 117 | #endif -------------------------------------------------------------------------------- /data_structures-I/11-pal2/main.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program reads in a file of palindromes separated by # and instantiates both an 4 | // array-based list stack and a singly linked list queue of characters to store the palindromes. 5 | // It then determines if they are valid palindromes using the stack and queue to compare the 6 | // characters. 7 | // Palindromes are words or phrases that read the same forwards and backwards. 8 | // 9 | // Other files required: 10 | // 1. stack.h - header file for the Stack class 11 | // 2. queue.h - header file for the Queue class 12 | // 3. palindromes.txt - file containing palindromes with punctuation and spaces 13 | // 14 | //***************************************************************************************************** 15 | 16 | #include "queue.h" 17 | #include "stack.h" 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | using namespace std; 25 | 26 | //***************************************************************************************************** 27 | 28 | bool testNdisplayPal(Stack &charStack, Queue &charQueue); 29 | char getPalType(bool hasSpace, bool hasPunct); 30 | void displayPalResult(bool isValid, char palType); 31 | 32 | //***************************************************************************************************** 33 | 34 | int main() { 35 | Stack charStack; 36 | Queue charQueue; 37 | bool isValid, 38 | hasPunct, 39 | hasSpace; 40 | int len; 41 | char palType; 42 | char pal[81]; 43 | 44 | ifstream in("palindromes.txt"); 45 | 46 | while (in.getline(pal, 80, '#')) { 47 | len = int(strlen(pal)); 48 | hasSpace = false; 49 | hasPunct = false; 50 | 51 | for (int i = 0; i < len; ++i) { 52 | if (isalpha(pal[i])) { 53 | pal[i] = tolower(pal[i]); 54 | charStack.push(pal[i]); 55 | charQueue.enqueue(pal[i]); 56 | } else if (isspace(pal[i])) { 57 | hasSpace = true; 58 | } else if (ispunct(pal[i])) { 59 | hasPunct = true; 60 | } 61 | } 62 | 63 | isValid = testNdisplayPal(charStack, charQueue); 64 | palType = getPalType(hasSpace, hasPunct); 65 | 66 | displayPalResult(isValid, palType); 67 | } 68 | 69 | in.close(); 70 | 71 | return 0; 72 | } 73 | 74 | //***************************************************************************************************** 75 | 76 | bool testNdisplayPal(Stack &charStack, Queue &charQueue) { 77 | char sTemp, 78 | qTemp; 79 | bool isValid = true; 80 | int numVal = charStack.getNumValues(); 81 | 82 | while ((charStack.pop(sTemp)) && (charQueue.dequeue(qTemp))) { 83 | if (sTemp != qTemp) 84 | isValid = false; 85 | 86 | cout << sTemp; 87 | } 88 | 89 | cout << setw(25 - numVal) << " "; 90 | 91 | return isValid; 92 | } 93 | 94 | //***************************************************************************************************** 95 | 96 | char getPalType(bool hasSpace, bool hasPunct) { 97 | char palType = '1'; 98 | 99 | if (hasPunct) 100 | palType = '3'; 101 | else if (hasSpace) 102 | palType = '2'; 103 | 104 | return palType; 105 | } 106 | 107 | //***************************************************************************************************** 108 | 109 | void displayPalResult(bool isValid, char palType) { 110 | if (isValid) 111 | cout << "type " << palType << endl; 112 | else 113 | cout << "invalid" << endl; 114 | } 115 | 116 | //***************************************************************************************************** 117 | /* 118 | 119 | aha type 3 120 | isitiitisi type 3 121 | deed type 1 122 | srotor invalid 123 | neveroddoreven type 2 124 | nolemonsnomelon type 3 125 | racecar type 2 126 | cimoc invalid 127 | wasitacaroracatisaw type 3 128 | yddad invalid 129 | 130 | */ -------------------------------------------------------------------------------- /data_structures-I/11-pal2/node.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a struct template that represents a node in a doubly linked list. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #ifndef NODE_H 8 | #define NODE_H 9 | 10 | //***************************************************************************************************** 11 | 12 | template 13 | struct Node { 14 | T data; 15 | Node *next; 16 | Node *prev; 17 | 18 | Node(); 19 | Node(const T &d, Node *n = nullptr, Node *p = nullptr); 20 | }; 21 | 22 | //***************************************************************************************************** 23 | 24 | template 25 | Node::Node() { 26 | data = T(); // T() - default initialization (0 for numbers, empty string for strings, etc.) 27 | next = nullptr; 28 | prev = nullptr; 29 | } 30 | 31 | //***************************************************************************************************** 32 | 33 | template 34 | Node::Node(const T &d, Node *n, Node *p) { 35 | data = d; 36 | next = n; 37 | prev = p; 38 | } 39 | 40 | //***************************************************************************************************** 41 | 42 | #endif -------------------------------------------------------------------------------- /data_structures-I/11-pal2/palindromes.txt: -------------------------------------------------------------------------------- 1 | Aha!#Is it I? It is I!#deed#rotors#Never odd or even#No lemons, no melon#Race car#comic#Was it a car or a cat I saw?#daddy# -------------------------------------------------------------------------------- /data_structures-I/11-pal2/stack.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a class template for an array-based list using a stack structure. 4 | // Last-in, first-out (LIFO) 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STACK_H 9 | #define STACK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | template 14 | class Stack { 15 | private: 16 | T *stack; 17 | int top; 18 | int capacity; 19 | 20 | public: 21 | Stack(int cap = 100); 22 | ~Stack(); 23 | bool push(const T &dataIn); 24 | bool pop(T &dataOut); 25 | bool peek(T &dataOut) const; 26 | int getNumValues() const; 27 | bool isFull() const; 28 | bool isEmpty() const; 29 | }; 30 | 31 | //***************************************************************************************************** 32 | 33 | template 34 | Stack::Stack(int cap) { 35 | capacity = cap; 36 | top = -1; 37 | stack = new T[capacity]; 38 | } 39 | 40 | //***************************************************************************************************** 41 | 42 | template 43 | Stack::~Stack() { 44 | delete[] stack; 45 | stack = nullptr; 46 | capacity = 0; 47 | top = -1; 48 | } 49 | 50 | //***************************************************************************************************** 51 | 52 | template 53 | bool Stack::push(const T &dataIn) { 54 | bool success = false; 55 | 56 | if ((top + 1) < capacity) { 57 | top++; 58 | stack[top] = dataIn; 59 | success = true; 60 | } 61 | 62 | return success; 63 | } 64 | 65 | //***************************************************************************************************** 66 | 67 | template 68 | bool Stack::pop(T &dataOut) { 69 | bool success = false; 70 | 71 | if (top >= 0) { 72 | dataOut = stack[top]; 73 | top--; 74 | success = true; 75 | } 76 | 77 | return success; 78 | } 79 | 80 | //***************************************************************************************************** 81 | 82 | template 83 | bool Stack::peek(T &dataOut) const { 84 | bool success = false; 85 | 86 | if (top >= 0) { 87 | dataOut = stack[top]; 88 | success = true; 89 | } 90 | 91 | return success; 92 | } 93 | 94 | //***************************************************************************************************** 95 | 96 | template 97 | int Stack::getNumValues() const { 98 | return (top + 1); 99 | } 100 | 101 | //***************************************************************************************************** 102 | 103 | template 104 | bool Stack::isFull() const { 105 | return ((top + 1) == capacity); 106 | } 107 | 108 | //***************************************************************************************************** 109 | 110 | template 111 | bool Stack::isEmpty() const { 112 | return (top < 0); 113 | } 114 | 115 | //***************************************************************************************************** 116 | 117 | #endif -------------------------------------------------------------------------------- /data_structures-I/12-DLList/node.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a struct template that represents a node in a doubly linked list. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #ifndef NODE_H 8 | #define NODE_H 9 | 10 | //***************************************************************************************************** 11 | 12 | template 13 | struct Node { 14 | T data; 15 | Node *next; 16 | Node *prev; 17 | 18 | Node(); 19 | Node(const T &d, Node *n = nullptr, Node *p = nullptr); 20 | }; 21 | 22 | //***************************************************************************************************** 23 | 24 | template 25 | Node::Node() { 26 | data = T(); // T() - default initialization (0 for numbers, empty string for strings, etc.) 27 | next = nullptr; 28 | prev = nullptr; 29 | } 30 | 31 | //***************************************************************************************************** 32 | 33 | template 34 | Node::Node(const T &d, Node *n, Node *p) { 35 | data = d; 36 | next = n; 37 | prev = p; 38 | } 39 | 40 | //***************************************************************************************************** 41 | 42 | #endif -------------------------------------------------------------------------------- /data_structures-I/13-DLList/node.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a templated struct that represents a node in a doubly linked list. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #ifndef NODE_H 8 | #define NODE_H 9 | 10 | //***************************************************************************************************** 11 | 12 | template 13 | struct Node { 14 | T data; 15 | Node *next; 16 | Node *prev; 17 | 18 | Node(); 19 | Node(const T &d, Node *n = nullptr, Node *p = nullptr); 20 | }; 21 | 22 | //***************************************************************************************************** 23 | 24 | template 25 | Node::Node() { 26 | data = T(); // T() - default initialization (0 for numbers, empty string for strings, etc.) 27 | next = nullptr; 28 | prev = nullptr; 29 | } 30 | 31 | //***************************************************************************************************** 32 | 33 | template 34 | Node::Node(const T &d, Node *n, Node *p) { 35 | data = d; 36 | next = n; 37 | prev = p; 38 | } 39 | 40 | //***************************************************************************************************** 41 | 42 | #endif -------------------------------------------------------------------------------- /data_structures-I/docs/header-template.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program... 4 | // 5 | // Other files required: 6 | // 1. example.h - header file for the example class 7 | // 2. example.txt - a text file containing the example data 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include 12 | using namespace std; 13 | 14 | //***************************************************************************************************** 15 | 16 | int main() { 17 | 18 | return 0; 19 | } 20 | 21 | //***************************************************************************************************** 22 | /* 23 | 24 | // attach sample output here. 25 | 26 | */ -------------------------------------------------------------------------------- /data_structures-II/01-Recursion-StockClass/part-1/recursion.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program demonstrates recursion by computing the value of a number raised to a power and 4 | // reversing a number using recursion. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #include 9 | using namespace std; 10 | 11 | //***************************************************************************************************** 12 | 13 | int f(int a, int b); 14 | void reverse(int n); 15 | 16 | //***************************************************************************************************** 17 | 18 | int main() { 19 | cout << f(5, 3) << endl; 20 | cout << f(5, 4) << endl; 21 | reverse(6543); 22 | 23 | return 0; 24 | } 25 | 26 | //***************************************************************************************************** 27 | 28 | int f(int a, int b) { 29 | int result; 30 | 31 | if (b == 0) // stopping condition 32 | result = 1; 33 | else if (b % 2 == 0) // even 34 | result = f(a * a, b / 2); 35 | else // odd 36 | result = f(a * a, b / 2) * (a + 1); 37 | 38 | return result; 39 | } 40 | 41 | //***************************************************************************************************** 42 | /* 43 | 44 | EXPLANATION: 45 | 46 | This recursive function takes two integer parameters, a and b, passed by value. 47 | - An integer variable `result` is declared to hold and return the computed value 48 | - If `b` is 0, the function returns 1 (stopping condition) 49 | - If `b` is even, recursively calls itself with modified arguments: f(a * a, b / 2) 50 | - If `b` is odd, recursively calls itself with modified arguments: f(a * a, b / 2) * (a + 1) 51 | - The function continues to call itself until it reaches the stopping condition (b == 0) 52 | - Once the stopping condition is met, the recursion starts to "unwind", 53 | processing results from the call stack in the reverse order they were added 54 | - Only calls with an odd 'b' modify the returned result by multiplying with (a + 1) 55 | - Finally, the function returns the final result 56 | 57 | 58 | STEP-BY-STEP: 59 | 60 | f(5, 3) -> f(25, 1) -> f(625, 0) -> 1 -> 26 -> 156 61 | *************************************************************** 62 | b: 3 (odd) | a: 5 63 | Calls f(25, 1) (5^2 = 25; 3/2 = 1) 64 | b: 1 (odd) | a: 25 65 | Calls f(625, 0) (25^2 = 625; 1/2 = 0) 66 | 67 | b: 0 (stopping condition) | a: 625 68 | returns 1 69 | 70 | Result: 1 * (25 + 1) = 26 71 | Final Result: 26 * (5 + 1) = 156 72 | 73 | 74 | f(5, 4) -> f(25, 2) -> f(625, 1) -> f(390625, 0) -> 1 -> 626 75 | *************************************************************** 76 | b: 4 (even) | a: 5 77 | Calls f(25, 2) (5^2 = 25; 4/2 = 2) 78 | b: 2 (even) | a: 25 79 | Calls f(625, 1) (25^2 = 625; 2/2 = 1) 80 | b: 1 (odd) | a: 625 81 | Calls f(390625, 0) (625^2 = 390625; 1/2 = 0) 82 | 83 | b: 0 (stopping condition) | a: 390625 84 | returns 1 85 | 86 | Final Result: 1 * (625 + 1) = 626 87 | 88 | */ 89 | //***************************************************************************************************** 90 | 91 | void reverse(int n) { 92 | if (n < 10) { // stopping condition 93 | cout << n; 94 | } else { 95 | cout << n % 10; // 6543 % 10 = 3 (last digit) 96 | reverse(n / 10); // 6543 / 10 = 654 (remaining digits) 97 | } 98 | } 99 | 100 | //***************************************************************************************************** 101 | /* 102 | 103 | 156 104 | 626 105 | 3456 106 | 107 | */ -------------------------------------------------------------------------------- /data_structures-II/01-Recursion-StockClass/part-2/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/01-Recursion-StockClass/part-2/main.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program demonstrates the Stock class by instantiating Stock objects and performing it's 4 | // methods. 5 | // 6 | // Other files required: 7 | // 1. stock.h - header file for the Stock class 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include "stock.h" 12 | 13 | #include 14 | using namespace std; 15 | 16 | //***************************************************************************************************** 17 | 18 | void compareStocks(const Stock &s1, const Stock &s2); 19 | 20 | //***************************************************************************************************** 21 | 22 | int main() { 23 | Stock apple("Apple Inc.", "AAPL", 150.25); 24 | Stock microsoft("Microsoft Corp.", "MSFT", 280.75); 25 | Stock appleCopy(apple); 26 | 27 | apple.display(); 28 | microsoft.display(); 29 | appleCopy.display(); 30 | 31 | compareStocks(apple, microsoft); 32 | compareStocks(apple, appleCopy); 33 | 34 | return 0; 35 | } 36 | 37 | //***************************************************************************************************** 38 | 39 | void compareStocks(const Stock &s1, const Stock &s2) { 40 | cout << "\n===============================" << endl; 41 | cout << s1.getName() << "\t" << s2.getName() << endl; 42 | cout << s1.getSymbol() << "\t\t" << s2.getSymbol() << endl; 43 | cout << s1.getPrice() << "\t\t" << s2.getPrice() << endl; 44 | 45 | if (s1 == s2) // tests == operator overload 46 | cout << "\nThe stocks are the same." << endl; 47 | else if (s1 != s2) // tests != operator overload 48 | cout << "\nThe stocks are different." << endl; 49 | } 50 | 51 | //***************************************************************************************************** 52 | /* 53 | 54 | Apple Inc. 55 | AAPL 56 | 150.25 57 | Microsoft Corp. 58 | MSFT 59 | 280.75 60 | Apple Inc. 61 | AAPL 62 | 150.25 63 | 64 | =============================== 65 | Apple Inc. Microsoft Corp. 66 | AAPL MSFT 67 | 150.25 280.75 68 | 69 | The stocks are different. 70 | 71 | =============================== 72 | Apple Inc. Apple Inc. 73 | AAPL AAPL 74 | 150.25 150.25 75 | 76 | The stocks are the same. 77 | 78 | */ 79 | -------------------------------------------------------------------------------- /data_structures-II/01-Recursion-StockClass/part-2/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | void Stock::display() const { 32 | std::cout << companyName << std::endl; 33 | std::cout << stockSymbol << std::endl; 34 | std::cout << stockPrice << std::endl; 35 | } 36 | 37 | //***************************************************************************************************** 38 | -------------------------------------------------------------------------------- /data_structures-II/01-Recursion-StockClass/part-2/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | public: 24 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 25 | Stock(const Stock &s); 26 | void display() const; 27 | std::string getName() const; 28 | std::string getSymbol() const; 29 | double getPrice() const; 30 | bool operator==(const Stock &rhs) const; 31 | bool operator!=(const Stock &rhs) const; 32 | }; 33 | 34 | //***************************************************************************************************** 35 | 36 | inline std::string Stock::getName() const { 37 | return companyName; 38 | } 39 | 40 | //***************************************************************************************************** 41 | 42 | inline std::string Stock::getSymbol() const { 43 | return stockSymbol; 44 | } 45 | 46 | //***************************************************************************************************** 47 | 48 | inline double Stock::getPrice() const { 49 | return stockPrice; 50 | } 51 | 52 | //***************************************************************************************************** 53 | 54 | inline bool Stock::operator==(const Stock &rhs) const { 55 | return (stockSymbol == rhs.stockSymbol); 56 | } 57 | 58 | //***************************************************************************************************** 59 | 60 | inline bool Stock::operator!=(const Stock &rhs) const { 61 | return (stockSymbol != rhs.stockSymbol); 62 | } 63 | 64 | //***************************************************************************************************** 65 | 66 | #endif -------------------------------------------------------------------------------- /data_structures-II/02-Reverse-LList-Recursion/part-1/reverse.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program reverses a string using recursion. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | //***************************************************************************************************** 12 | 13 | void reverseString(string &s, int start = 0); 14 | 15 | //***************************************************************************************************** 16 | 17 | int main() { 18 | string s = "ABC"; 19 | 20 | reverseString(s); 21 | cout << "Reversed string: " << s << endl; 22 | 23 | cout << "Enter string to reverse: "; 24 | getline(cin, s); 25 | 26 | reverseString(s); 27 | cout << "Reversed string: " << s << endl; 28 | } 29 | 30 | //***************************************************************************************************** 31 | 32 | void reverseString(string &s, int start) { 33 | size_t end = (s.size() - 1) - start; 34 | char temp; 35 | 36 | if (start < end) { 37 | temp = s[start]; 38 | s[start] = s[end]; 39 | s[end] = temp; 40 | 41 | reverseString(s, start + 1); 42 | } 43 | } 44 | 45 | //***************************************************************************************************** 46 | /* 47 | 48 | Reversed string: CBA 49 | Enter string to reverse: helloworld 50 | Reversed string: dlrowolleh 51 | 52 | ******************************************************************************************************* 53 | 54 | Reversed string: CBA 55 | Enter string to reverse: hello. world. += 56 | Reversed string: =+ .dlrow .olleh 57 | 58 | */ -------------------------------------------------------------------------------- /data_structures-II/02-Reverse-LList-Recursion/part-2/LList.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a class template for a singly linked list with recursive methods. 4 | // 5 | // Other files required: 6 | // 1. node.h - header file for the node struct of the linked list 7 | // 8 | //***************************************************************************************************** 9 | 10 | #ifndef LLIST_H 11 | #define LLIST_H 12 | 13 | //***************************************************************************************************** 14 | 15 | #include "node.h" 16 | 17 | #include 18 | #include 19 | 20 | //***************************************************************************************************** 21 | 22 | template 23 | class LList { 24 | private: 25 | Node *head; 26 | 27 | void _insertOrderRec(Node *&head, const T &item); 28 | void _deleteNodeRec(Node *&head, const T &item); 29 | void _display(Node *head) const; 30 | 31 | public: 32 | LList(); 33 | ~LList(); 34 | void insertOrder(const T &item); 35 | void deleteNode(const T &item); 36 | void display() const; 37 | }; 38 | 39 | //***************************************************************************************************** 40 | 41 | template 42 | LList::LList() { 43 | head = nullptr; 44 | } 45 | 46 | //***************************************************************************************************** 47 | 48 | template 49 | LList::~LList() { 50 | Node *pTemp = nullptr; 51 | 52 | while (head) { 53 | pTemp = head; 54 | head = head->next; 55 | delete pTemp; 56 | } 57 | 58 | head = nullptr; 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | template 64 | void LList::_insertOrderRec(Node *&pCur, const T &item) { 65 | if ((pCur == nullptr) || (item > pCur->value)) { 66 | Node *pTemp = pCur; 67 | pCur = new (std::nothrow) Node; 68 | pCur->value = item; 69 | pCur->next = pTemp; 70 | } else { 71 | _insertOrderRec(pCur->next, item); 72 | } 73 | } 74 | 75 | //***************************************************************************************************** 76 | 77 | template 78 | void LList::insertOrder(const T &item) { 79 | _insertOrderRec(head, item); 80 | } 81 | 82 | //***************************************************************************************************** 83 | 84 | template 85 | void LList::_deleteNodeRec(Node *&pCur, const T &item) { 86 | if (pCur) { 87 | if (item == pCur->value) { 88 | Node *pTemp = pCur; 89 | pCur = pCur->next; 90 | delete pTemp; 91 | } else { 92 | _deleteNodeRec(pCur->next, item); 93 | } 94 | } 95 | } 96 | 97 | //***************************************************************************************************** 98 | 99 | template 100 | void LList::deleteNode(const T &item) { 101 | _deleteNodeRec(head, item); 102 | } 103 | 104 | //***************************************************************************************************** 105 | 106 | template 107 | void LList::_display(Node *head) const { 108 | if (head) { 109 | std::cout << head->value << std::endl; 110 | _display(head->next); 111 | } 112 | } 113 | 114 | //***************************************************************************************************** 115 | 116 | template 117 | void LList::display() const { 118 | _display(head); 119 | } 120 | 121 | //***************************************************************************************************** 122 | 123 | #endif -------------------------------------------------------------------------------- /data_structures-II/02-Reverse-LList-Recursion/part-2/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/02-Reverse-LList-Recursion/part-2/node.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a struct template that represents a node in a singly linked list. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #ifndef NODE_H 8 | #define NODE_H 9 | 10 | //***************************************************************************************************** 11 | 12 | template 13 | struct Node { 14 | T value; 15 | Node *next; 16 | 17 | Node(); 18 | Node(const T &v, Node *n = nullptr); 19 | }; 20 | 21 | //***************************************************************************************************** 22 | 23 | template 24 | Node::Node() { 25 | value = T(); // T() - default initialization (0 for numbers, empty string for strings, etc.) 26 | next = nullptr; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | template 32 | Node::Node(const T &v, Node *n) { 33 | value = v; 34 | next = n; 35 | } 36 | 37 | //***************************************************************************************************** 38 | 39 | #endif -------------------------------------------------------------------------------- /data_structures-II/02-Reverse-LList-Recursion/part-2/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | std::ostream &operator<<(std::ostream &out, const Stock &stock) { 32 | out << stock.companyName << std::endl 33 | << stock.stockSymbol << std::endl 34 | << stock.stockPrice; 35 | 36 | return out; 37 | } 38 | 39 | //***************************************************************************************************** -------------------------------------------------------------------------------- /data_structures-II/02-Reverse-LList-Recursion/part-2/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | friend std::ostream &operator<<(std::ostream &out, const Stock &stock); 24 | 25 | public: 26 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 27 | Stock(const Stock &s); 28 | std::string getName() const; 29 | std::string getSymbol() const; 30 | double getPrice() const; 31 | bool operator==(const Stock &rhs) const; 32 | bool operator!=(const Stock &rhs) const; 33 | bool operator>(const Stock &rhs) const; 34 | bool operator<(const Stock &rhs) const; 35 | }; 36 | 37 | //***************************************************************************************************** 38 | 39 | inline std::string Stock::getName() const { 40 | return companyName; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | inline std::string Stock::getSymbol() const { 46 | return stockSymbol; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | inline double Stock::getPrice() const { 52 | return stockPrice; 53 | } 54 | 55 | //***************************************************************************************************** 56 | 57 | inline bool Stock::operator==(const Stock &rhs) const { 58 | return (stockSymbol == rhs.stockSymbol); 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | inline bool Stock::operator!=(const Stock &rhs) const { 64 | return (stockSymbol != rhs.stockSymbol); 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | inline bool Stock::operator>(const Stock &rhs) const { 70 | return (stockSymbol > rhs.stockSymbol); 71 | } 72 | 73 | //***************************************************************************************************** 74 | 75 | inline bool Stock::operator<(const Stock &rhs) const { 76 | return (stockSymbol < rhs.stockSymbol); 77 | } 78 | 79 | //***************************************************************************************************** 80 | 81 | #endif -------------------------------------------------------------------------------- /data_structures-II/03-AList-BinarySearch-Recursion/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/03-AList-BinarySearch-Recursion/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | std::ostream &operator<<(std::ostream &out, const Stock &stock) { 32 | out << stock.companyName << std::endl 33 | << stock.stockSymbol << std::endl 34 | << stock.stockPrice; 35 | 36 | return out; 37 | } 38 | 39 | //***************************************************************************************************** -------------------------------------------------------------------------------- /data_structures-II/03-AList-BinarySearch-Recursion/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | friend std::ostream &operator<<(std::ostream &out, const Stock &stock); 24 | 25 | public: 26 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 27 | Stock(const Stock &s); 28 | std::string getName() const; 29 | std::string getSymbol() const; 30 | double getPrice() const; 31 | bool operator==(const Stock &rhs) const; 32 | bool operator!=(const Stock &rhs) const; 33 | bool operator>(const Stock &rhs) const; 34 | bool operator<(const Stock &rhs) const; 35 | }; 36 | 37 | //***************************************************************************************************** 38 | 39 | inline std::string Stock::getName() const { 40 | return companyName; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | inline std::string Stock::getSymbol() const { 46 | return stockSymbol; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | inline double Stock::getPrice() const { 52 | return stockPrice; 53 | } 54 | 55 | //***************************************************************************************************** 56 | 57 | inline bool Stock::operator==(const Stock &rhs) const { 58 | return (stockSymbol == rhs.stockSymbol); 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | inline bool Stock::operator!=(const Stock &rhs) const { 64 | return (stockSymbol != rhs.stockSymbol); 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | inline bool Stock::operator>(const Stock &rhs) const { 70 | return (stockSymbol > rhs.stockSymbol); 71 | } 72 | 73 | //***************************************************************************************************** 74 | 75 | inline bool Stock::operator<(const Stock &rhs) const { 76 | return (stockSymbol < rhs.stockSymbol); 77 | } 78 | 79 | //***************************************************************************************************** 80 | 81 | #endif -------------------------------------------------------------------------------- /data_structures-II/04-Hashing-Quadratic-Probing/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/04-Hashing-Quadratic-Probing/hashTable.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a class template for a hash table with quadratic probing to resolve 4 | // collisions when inserting and removing items. 5 | // 6 | // Other files required: 7 | // 1. htElement.h - header file for the HTElement struct 8 | // 9 | //***************************************************************************************************** 10 | 11 | #ifndef HASHTABLE_H 12 | #define HASHTABLE_H 13 | 14 | //***************************************************************************************************** 15 | 16 | #include "htElement.h" 17 | #include 18 | 19 | //***************************************************************************************************** 20 | 21 | template 22 | class HashTable { 23 | private: 24 | HTElement *table; 25 | int size; 26 | int count; 27 | 28 | int _hash(const T &item) const; 29 | int _probe(const T &item) const; 30 | 31 | public: 32 | HashTable(int size = 10); 33 | ~HashTable(); 34 | void remove(const T &item); 35 | void insert(const T &item); 36 | void display() const; 37 | T *search(const T &item) const; 38 | }; 39 | 40 | //***************************************************************************************************** 41 | 42 | template 43 | HashTable::HashTable(int size) { 44 | table = new HTElement[size]; 45 | this->size = size; 46 | count = 0; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | template 52 | HashTable::~HashTable() { 53 | delete[] table; 54 | table = nullptr; 55 | } 56 | 57 | //***************************************************************************************************** 58 | 59 | template 60 | int HashTable::_hash(const T &item) const { 61 | return item.hash(size); 62 | } 63 | 64 | //***************************************************************************************************** 65 | 66 | template 67 | int HashTable::_probe(const T &item) const { 68 | int index = _hash(item); 69 | int hashIndex = index; 70 | int i = 0; 71 | 72 | while ((table[index].status == 1 || table[index].status == -1) && (table[index].item != item)) { 73 | index = (hashIndex + (i * i)) % size; 74 | ++i; 75 | } 76 | 77 | return index; 78 | } 79 | 80 | //***************************************************************************************************** 81 | 82 | template 83 | void HashTable::remove(const T &item) { 84 | int index = _probe(item); 85 | 86 | if (table[index].status == 1) { 87 | table[index].status = -1; 88 | --count; 89 | } 90 | } 91 | 92 | //***************************************************************************************************** 93 | 94 | template 95 | void HashTable::insert(const T &item) { 96 | int index = _probe(item); 97 | 98 | if (table[index].status != 1) { 99 | table[index].item = item; 100 | table[index].status = 1; 101 | count++; 102 | } 103 | } 104 | 105 | //***************************************************************************************************** 106 | 107 | template 108 | void HashTable::display() const { 109 | for (int i = 0; i < size; ++i) 110 | if (table[i].status == 1) 111 | std::cout << table[i].item << std::endl; 112 | } 113 | 114 | //***************************************************************************************************** 115 | 116 | template 117 | T *HashTable::search(const T &item) const { 118 | int index = _probe(item); 119 | T *copy = nullptr; 120 | 121 | if (table[index].status == 1) 122 | copy = new T(table[index].item); 123 | 124 | return copy; 125 | } 126 | 127 | //***************************************************************************************************** 128 | 129 | #endif -------------------------------------------------------------------------------- /data_structures-II/04-Hashing-Quadratic-Probing/htElement.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a template struct for storing elements in a hash table. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #ifndef HTELEMENT_H 8 | #define HTELEMENT_H 9 | 10 | //***************************************************************************************************** 11 | 12 | template 13 | struct HTElement { 14 | T item; 15 | int status; 16 | 17 | HTElement(); 18 | }; 19 | 20 | //***************************************************************************************************** 21 | 22 | template 23 | HTElement::HTElement() { 24 | item = T(); 25 | status = 0; 26 | } 27 | 28 | //***************************************************************************************************** 29 | 30 | #endif -------------------------------------------------------------------------------- /data_structures-II/04-Hashing-Quadratic-Probing/main.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program demonstrates the HashTable class by instantiating a hash table of Stock objects 4 | // and performing it's methods. 5 | // 6 | // Other files required: 7 | // 1. stock.h - header file for the Stock class (includes stock.cpp) 8 | // 2. hashTable.h - header file for the HashTable class (includes htElement.h) 9 | // 10 | //***************************************************************************************************** 11 | 12 | #include "hashTable.h" 13 | #include "stock.h" 14 | 15 | #include 16 | using namespace std; 17 | 18 | //***************************************************************************************************** 19 | 20 | void test(HashTable &table, const Stock &itemToSearchAndRemove); 21 | 22 | //***************************************************************************************************** 23 | 24 | int main() { 25 | HashTable stockTable(10); 26 | 27 | Stock google("Google", "GOOGL", 1500.50); 28 | Stock apple("Apple", "AAPL", 140.30); 29 | Stock microsoft("Microsoft", "MSFT", 220.10); 30 | Stock tesla("Tesla", "TSLA", 420.69); 31 | Stock amazon("Amazon", "AMZN", 3000.00); 32 | Stock googleCopy(google); 33 | Stock amazonCopy("Amazooooon", "AMZN", 9999.00); 34 | 35 | stockTable.insert(google); 36 | stockTable.insert(apple); 37 | stockTable.insert(microsoft); 38 | stockTable.insert(tesla); 39 | stockTable.insert(amazon); 40 | stockTable.insert(googleCopy); // collision (not inserted) 41 | stockTable.insert(amazonCopy); // collision (not inserted) 42 | 43 | stockTable.display(); 44 | 45 | test(stockTable, apple); // found/removed 46 | test(stockTable, apple); // not found/not removed 47 | 48 | return 0; 49 | } 50 | 51 | //***************************************************************************************************** 52 | 53 | void test(HashTable &table, const Stock &item) { 54 | Stock *searchResult = table.search(item); 55 | 56 | cout << "\nSearching for " << item.getSymbol() << ":" << endl; 57 | 58 | if (searchResult) { 59 | cout << "Stock found!\n" 60 | << *searchResult << endl; 61 | 62 | delete searchResult; 63 | searchResult = nullptr; 64 | 65 | table.remove(item); 66 | cout << "\nAfter Removal of " << item.getSymbol() << ":" << endl; 67 | table.display(); 68 | } else { 69 | cerr << "Stock not found!\n"; 70 | } 71 | } 72 | 73 | //***************************************************************************************************** 74 | /* 75 | 76 | Amazon 77 | AMZN 78 | 3000 79 | Microsoft 80 | MSFT 81 | 220.1 82 | Google 83 | GOOGL 84 | 1500.5 85 | Apple 86 | AAPL 87 | 140.3 88 | Tesla 89 | TSLA 90 | 420.69 91 | 92 | Searching for AAPL: 93 | Stock found! 94 | Apple 95 | AAPL 96 | 140.3 97 | 98 | After Removal of AAPL: 99 | Amazon 100 | AMZN 101 | 3000 102 | Microsoft 103 | MSFT 104 | 220.1 105 | Google 106 | GOOGL 107 | 1500.5 108 | Tesla 109 | TSLA 110 | 420.69 111 | 112 | Searching for AAPL: 113 | Stock not found! 114 | 115 | */ -------------------------------------------------------------------------------- /data_structures-II/04-Hashing-Quadratic-Probing/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | std::ostream &operator<<(std::ostream &out, const Stock &stock) { 32 | out << stock.companyName << std::endl 33 | << stock.stockSymbol << std::endl 34 | << stock.stockPrice; 35 | 36 | return out; 37 | } 38 | 39 | //***************************************************************************************************** 40 | 41 | int Stock::hash(int size) const { 42 | int sum = 0; 43 | 44 | for (char c : stockSymbol) 45 | sum += c; 46 | 47 | return sum % size; 48 | } 49 | 50 | //***************************************************************************************************** -------------------------------------------------------------------------------- /data_structures-II/04-Hashing-Quadratic-Probing/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | friend std::ostream &operator<<(std::ostream &out, const Stock &stock); 24 | 25 | public: 26 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 27 | Stock(const Stock &s); 28 | std::string getName() const; 29 | std::string getSymbol() const; 30 | double getPrice() const; 31 | bool operator==(const Stock &rhs) const; 32 | bool operator!=(const Stock &rhs) const; 33 | bool operator>(const Stock &rhs) const; 34 | bool operator<(const Stock &rhs) const; 35 | int hash(int size) const; 36 | }; 37 | 38 | //***************************************************************************************************** 39 | 40 | inline std::string Stock::getName() const { 41 | return companyName; 42 | } 43 | 44 | //***************************************************************************************************** 45 | 46 | inline std::string Stock::getSymbol() const { 47 | return stockSymbol; 48 | } 49 | 50 | //***************************************************************************************************** 51 | 52 | inline double Stock::getPrice() const { 53 | return stockPrice; 54 | } 55 | 56 | //***************************************************************************************************** 57 | 58 | inline bool Stock::operator==(const Stock &rhs) const { 59 | return (stockSymbol == rhs.stockSymbol); 60 | } 61 | 62 | //***************************************************************************************************** 63 | 64 | inline bool Stock::operator!=(const Stock &rhs) const { 65 | return (stockSymbol != rhs.stockSymbol); 66 | } 67 | 68 | //***************************************************************************************************** 69 | 70 | inline bool Stock::operator>(const Stock &rhs) const { 71 | return (stockSymbol > rhs.stockSymbol); 72 | } 73 | 74 | //***************************************************************************************************** 75 | 76 | inline bool Stock::operator<(const Stock &rhs) const { 77 | return (stockSymbol < rhs.stockSymbol); 78 | } 79 | 80 | //***************************************************************************************************** 81 | 82 | #endif -------------------------------------------------------------------------------- /data_structures-II/05-Updated-AList-BinarySearch-Recursion/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/05-Updated-AList-BinarySearch-Recursion/Stock.txt: -------------------------------------------------------------------------------- 1 | Motorola Inc. 2 | MOT 3 | 17.49 4 | Microsoft Corp. 5 | MSFT 6 | 28.11 7 | Tesla 8 | TSLA 9 | 564.33 10 | Intel 11 | INTC 12 | 60.78 13 | Sony 14 | SNE 15 | 105.81 16 | Advanced Micro Devices 17 | AMD 18 | 84.51 19 | NVIDIA 20 | NVDA 21 | 548.58 22 | Apple 23 | AAPL 24 | 121.73 -------------------------------------------------------------------------------- /data_structures-II/05-Updated-AList-BinarySearch-Recursion/Stock_updated.txt: -------------------------------------------------------------------------------- 1 | Apple 2 | AAPL 3 | 121.73 4 | Advanced Micro Devices 5 | AMD 6 | 84.51 7 | Best Buy 8 | BBY 9 | 69.25 10 | GameStop 11 | GME 12 | 17.18 13 | NVIDIA 14 | NVDA 15 | 548.58 16 | Sony 17 | SNE 18 | 105.81 19 | Tesla 20 | TSLA 21 | 564.33 22 | Verizon 23 | VZ 24 | 33.28 25 | -------------------------------------------------------------------------------- /data_structures-II/05-Updated-AList-BinarySearch-Recursion/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | std::ostream &operator<<(std::ostream &out, const Stock &stock) { 32 | out << stock.companyName << std::endl 33 | << stock.stockSymbol << std::endl 34 | << stock.stockPrice; 35 | 36 | return out; 37 | } 38 | 39 | //***************************************************************************************************** -------------------------------------------------------------------------------- /data_structures-II/05-Updated-AList-BinarySearch-Recursion/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | friend std::ostream &operator<<(std::ostream &out, const Stock &stock); 24 | 25 | public: 26 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 27 | Stock(const Stock &s); 28 | std::string getName() const; 29 | std::string getSymbol() const; 30 | double getPrice() const; 31 | bool operator==(const Stock &rhs) const; 32 | bool operator!=(const Stock &rhs) const; 33 | bool operator>(const Stock &rhs) const; 34 | bool operator<(const Stock &rhs) const; 35 | }; 36 | 37 | //***************************************************************************************************** 38 | 39 | inline std::string Stock::getName() const { 40 | return companyName; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | inline std::string Stock::getSymbol() const { 46 | return stockSymbol; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | inline double Stock::getPrice() const { 52 | return stockPrice; 53 | } 54 | 55 | //***************************************************************************************************** 56 | 57 | inline bool Stock::operator==(const Stock &rhs) const { 58 | return (stockSymbol == rhs.stockSymbol); 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | inline bool Stock::operator!=(const Stock &rhs) const { 64 | return (stockSymbol != rhs.stockSymbol); 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | inline bool Stock::operator>(const Stock &rhs) const { 70 | return (stockSymbol > rhs.stockSymbol); 71 | } 72 | 73 | //***************************************************************************************************** 74 | 75 | inline bool Stock::operator<(const Stock &rhs) const { 76 | return (stockSymbol < rhs.stockSymbol); 77 | } 78 | 79 | //***************************************************************************************************** 80 | 81 | #endif -------------------------------------------------------------------------------- /data_structures-II/06-BST/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/06-BST/Stock.txt: -------------------------------------------------------------------------------- 1 | Apple 2 | AAPL 3 | 121.73 4 | Advanced Micro Devices 5 | AMD 6 | 84.51 7 | Intel 8 | INTC 9 | 60.78 10 | Motorola Inc. 11 | MOT 12 | 17.49 13 | Microsoft Corp. 14 | MSFT 15 | 28.11 16 | NVIDIA 17 | NVDA 18 | 548.58 19 | Sony 20 | SNE 21 | 105.81 22 | Tesla 23 | TSLA 24 | 564.33 25 | -------------------------------------------------------------------------------- /data_structures-II/06-BST/node.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a struct template that represents a node in a binary tree. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #ifndef NODE_H 8 | #define NODE_H 9 | 10 | //***************************************************************************************************** 11 | 12 | template 13 | struct Node { 14 | T value; 15 | Node *left; 16 | Node *right; 17 | 18 | Node(); 19 | Node(const T &v, Node *l = nullptr, Node *r = nullptr); 20 | }; 21 | 22 | //***************************************************************************************************** 23 | 24 | template 25 | Node::Node() { 26 | value = T(); // T() - default initialization (0 for numbers, empty string for strings, etc.) 27 | left = nullptr; 28 | right = nullptr; 29 | } 30 | 31 | //***************************************************************************************************** 32 | 33 | template 34 | Node::Node(const T &v, Node *l, Node *r) { 35 | value = v; 36 | left = l; 37 | right = r; 38 | } 39 | 40 | //***************************************************************************************************** 41 | 42 | #endif -------------------------------------------------------------------------------- /data_structures-II/06-BST/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | std::ostream &operator<<(std::ostream &out, const Stock &stock) { 32 | out << stock.companyName << std::endl 33 | << stock.stockSymbol << std::endl 34 | << stock.stockPrice; 35 | 36 | return out; 37 | } 38 | 39 | //***************************************************************************************************** -------------------------------------------------------------------------------- /data_structures-II/06-BST/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | friend std::ostream &operator<<(std::ostream &out, const Stock &stock); 24 | 25 | public: 26 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 27 | Stock(const Stock &s); 28 | std::string getName() const; 29 | std::string getSymbol() const; 30 | double getPrice() const; 31 | bool operator==(const Stock &rhs) const; 32 | bool operator!=(const Stock &rhs) const; 33 | bool operator>(const Stock &rhs) const; 34 | bool operator<(const Stock &rhs) const; 35 | }; 36 | 37 | //***************************************************************************************************** 38 | 39 | inline std::string Stock::getName() const { 40 | return companyName; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | inline std::string Stock::getSymbol() const { 46 | return stockSymbol; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | inline double Stock::getPrice() const { 52 | return stockPrice; 53 | } 54 | 55 | //***************************************************************************************************** 56 | 57 | inline bool Stock::operator==(const Stock &rhs) const { 58 | return (stockSymbol == rhs.stockSymbol); 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | inline bool Stock::operator!=(const Stock &rhs) const { 64 | return (stockSymbol != rhs.stockSymbol); 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | inline bool Stock::operator>(const Stock &rhs) const { 70 | return (stockSymbol > rhs.stockSymbol); 71 | } 72 | 73 | //***************************************************************************************************** 74 | 75 | inline bool Stock::operator<(const Stock &rhs) const { 76 | return (stockSymbol < rhs.stockSymbol); 77 | } 78 | 79 | //***************************************************************************************************** 80 | 81 | #endif -------------------------------------------------------------------------------- /data_structures-II/07-Priority-Queue/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/07-Priority-Queue/Stock.txt: -------------------------------------------------------------------------------- 1 | Motorola Inc. 2 | MOT 3 | 17.49 4 | Microsoft Corp. 5 | MSFT 6 | 28.11 7 | Tesla 8 | TSLA 9 | 564.33 10 | Intel 11 | INTC 12 | 60.78 13 | Sony 14 | SNE 15 | 105.81 16 | Advanced Micro Devices 17 | AMD 18 | 84.51 19 | NVIDIA 20 | NVDA 21 | 548.58 22 | Apple 23 | AAPL 24 | 121.73 25 | Qualcomm 26 | QCOM 27 | 146.10 28 | Alphabet Inc. Class A 29 | GOOGL 30 | 2805.12 31 | Walmart 32 | WMT 33 | 142.51 34 | Amazon.com Inc. 35 | AMZN 36 | 3222.90 37 | Facebook 38 | FB 39 | 333.02 40 | Cisco Systems 41 | CSCO 42 | 54.60 43 | Oracle 44 | ORCL 45 | 94.52 46 | Netflix 47 | NFLX 48 | 628.44 -------------------------------------------------------------------------------- /data_structures-II/07-Priority-Queue/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | std::ostream &operator<<(std::ostream &out, const Stock &stock) { 32 | out << stock.companyName << std::endl 33 | << stock.stockSymbol << std::endl 34 | << stock.stockPrice; 35 | 36 | return out; 37 | } 38 | 39 | //***************************************************************************************************** -------------------------------------------------------------------------------- /data_structures-II/07-Priority-Queue/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | friend std::ostream &operator<<(std::ostream &out, const Stock &stock); 24 | 25 | public: 26 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 27 | Stock(const Stock &s); 28 | std::string getName() const; 29 | std::string getSymbol() const; 30 | double getPrice() const; 31 | bool operator==(const Stock &rhs) const; 32 | bool operator!=(const Stock &rhs) const; 33 | bool operator>(const Stock &rhs) const; 34 | bool operator<(const Stock &rhs) const; 35 | }; 36 | 37 | //***************************************************************************************************** 38 | 39 | inline std::string Stock::getName() const { 40 | return companyName; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | inline std::string Stock::getSymbol() const { 46 | return stockSymbol; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | inline double Stock::getPrice() const { 52 | return stockPrice; 53 | } 54 | 55 | //***************************************************************************************************** 56 | 57 | inline bool Stock::operator==(const Stock &rhs) const { 58 | return (stockSymbol == rhs.stockSymbol); 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | inline bool Stock::operator!=(const Stock &rhs) const { 64 | return (stockSymbol != rhs.stockSymbol); 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | inline bool Stock::operator>(const Stock &rhs) const { 70 | return (stockPrice > rhs.stockPrice); 71 | } 72 | 73 | //***************************************************************************************************** 74 | 75 | inline bool Stock::operator<(const Stock &rhs) const { 76 | return (stockPrice < rhs.stockPrice); 77 | } 78 | 79 | //***************************************************************************************************** 80 | 81 | #endif -------------------------------------------------------------------------------- /data_structures-II/08-AVLTree/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/08-AVLTree/Stock.txt: -------------------------------------------------------------------------------- 1 | Apple 2 | AAPL 3 | 121.73 4 | Advanced Micro Devices 5 | AMD 6 | 84.51 7 | Intel 8 | INTC 9 | 60.78 10 | Motorola Inc. 11 | MOT 12 | 17.49 13 | Microsoft Corp. 14 | MSFT 15 | 28.11 16 | NVIDIA 17 | NVDA 18 | 548.58 19 | Sony 20 | SNE 21 | 105.81 22 | Tesla 23 | TSLA 24 | 564.33 -------------------------------------------------------------------------------- /data_structures-II/08-AVLTree/Stock_BF.txt: -------------------------------------------------------------------------------- 1 | Apple 2 | AAPL 3 | 121.73 (BF: 0) 4 | Advanced Micro Devices 5 | AMD 6 | 84.51 (BF: 0) 7 | Intel 8 | INTC 9 | 60.78 (BF: 0) 10 | Motorola Inc. 11 | MOT 12 | 17.49 (BF: -1) 13 | Microsoft Corp. 14 | MSFT 15 | 28.11 (BF: 0) 16 | NVIDIA 17 | NVDA 18 | 548.58 (BF: -1) 19 | Sony 20 | SNE 21 | 105.81 (BF: -1) 22 | Tesla 23 | TSLA 24 | 564.33 (BF: 0) 25 | -------------------------------------------------------------------------------- /data_structures-II/08-AVLTree/node.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a struct template that represents a node in a self-balancing binary 4 | // search tree (AVL tree). 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef NODE_H 9 | #define NODE_H 10 | 11 | //***************************************************************************************************** 12 | 13 | template 14 | struct Node { 15 | T value; 16 | int bFactor; 17 | Node *left; 18 | Node *right; 19 | 20 | Node(); 21 | Node(const T &v, Node *l = nullptr, Node *r = nullptr); 22 | }; 23 | 24 | //***************************************************************************************************** 25 | 26 | template 27 | Node::Node() { 28 | value = T(); // T() - default initialization (0 for numbers, empty string for strings, etc.) 29 | bFactor = 0; 30 | left = nullptr; 31 | right = nullptr; 32 | } 33 | 34 | //***************************************************************************************************** 35 | 36 | template 37 | Node::Node(const T &v, Node *l, Node *r) { 38 | value = v; 39 | bFactor = 0; 40 | left = l; 41 | right = r; 42 | } 43 | 44 | //***************************************************************************************************** 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /data_structures-II/08-AVLTree/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | std::ostream &operator<<(std::ostream &out, const Stock &stock) { 32 | out << stock.companyName << std::endl 33 | << stock.stockSymbol << std::endl 34 | << stock.stockPrice; 35 | 36 | return out; 37 | } 38 | 39 | //***************************************************************************************************** -------------------------------------------------------------------------------- /data_structures-II/08-AVLTree/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | friend std::ostream &operator<<(std::ostream &out, const Stock &stock); 24 | 25 | public: 26 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 27 | Stock(const Stock &s); 28 | std::string getName() const; 29 | std::string getSymbol() const; 30 | double getPrice() const; 31 | bool operator==(const Stock &rhs) const; 32 | bool operator!=(const Stock &rhs) const; 33 | bool operator>(const Stock &rhs) const; 34 | bool operator<(const Stock &rhs) const; 35 | }; 36 | 37 | //***************************************************************************************************** 38 | 39 | inline std::string Stock::getName() const { 40 | return companyName; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | inline std::string Stock::getSymbol() const { 46 | return stockSymbol; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | inline double Stock::getPrice() const { 52 | return stockPrice; 53 | } 54 | 55 | //***************************************************************************************************** 56 | 57 | inline bool Stock::operator==(const Stock &rhs) const { 58 | return (stockSymbol == rhs.stockSymbol); 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | inline bool Stock::operator!=(const Stock &rhs) const { 64 | return (stockSymbol != rhs.stockSymbol); 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | inline bool Stock::operator>(const Stock &rhs) const { 70 | return (stockSymbol > rhs.stockSymbol); 71 | } 72 | 73 | //***************************************************************************************************** 74 | 75 | inline bool Stock::operator<(const Stock &rhs) const { 76 | return (stockSymbol < rhs.stockSymbol); 77 | } 78 | 79 | //***************************************************************************************************** 80 | 81 | #endif -------------------------------------------------------------------------------- /data_structures-II/09-Huffman-Algorithm/Decoding/HuffmanStocks.txt: -------------------------------------------------------------------------------- 1 | 10 2 | American Express Co 3 | AXP 4 | 118.67 5 | 0.1 6 | Apple Inc 7 | AAPL 8 | 120.3 9 | 0.15 10 | Boeing Co 11 | BA 12 | 202.4 13 | 0.02 14 | Caterpillar Inc 15 | CAT 16 | 173.19 17 | 0.03 18 | Cisco Systems Inc 19 | CSCO 20 | 42.54 21 | 0.05 22 | Goldman Sachs Group Inc 23 | GS 24 | 222.38 25 | 0.25 26 | Home Depot Inc 27 | HD 28 | 279.57 29 | 0.2 30 | Intel Corp 31 | INTC 32 | 46.19 33 | 0.075 34 | Johnson & Johnson 35 | JNJ 36 | 150.85 37 | 0.025 38 | Coca-Cola Co 39 | KO 40 | 53.85 41 | 0.1 -------------------------------------------------------------------------------- /data_structures-II/09-Huffman-Algorithm/Decoding/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/09-Huffman-Algorithm/Decoding/node.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a struct template that represents a node in a Huffman tree. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #ifndef NODE_H 8 | #define NODE_H 9 | 10 | //***************************************************************************************************** 11 | 12 | template 13 | struct Node { 14 | T value; 15 | double frequency; 16 | Node *left; 17 | Node *right; 18 | 19 | Node(); 20 | Node(const T &v, Node *l = nullptr, Node *r = nullptr); 21 | }; 22 | 23 | //***************************************************************************************************** 24 | 25 | template 26 | Node::Node() { 27 | value = T(); // T() - default initialization (0 for numbers, empty string for strings, etc.) 28 | frequency = 0; 29 | left = nullptr; 30 | right = nullptr; 31 | } 32 | 33 | //***************************************************************************************************** 34 | 35 | template 36 | Node::Node(const T &v, Node *l, Node *r) { 37 | value = v; 38 | frequency = 0; 39 | left = l; 40 | right = r; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | #endif -------------------------------------------------------------------------------- /data_structures-II/09-Huffman-Algorithm/Decoding/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | std::ostream &operator<<(std::ostream &out, const Stock &stock) { 32 | out << stock.companyName << std::endl 33 | << stock.stockSymbol << std::endl 34 | << stock.stockPrice; 35 | 36 | return out; 37 | } 38 | 39 | //***************************************************************************************************** -------------------------------------------------------------------------------- /data_structures-II/09-Huffman-Algorithm/Decoding/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | friend std::ostream &operator<<(std::ostream &out, const Stock &stock); 24 | 25 | public: 26 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 27 | Stock(const Stock &s); 28 | std::string getName() const; 29 | std::string getSymbol() const; 30 | double getPrice() const; 31 | bool operator==(const Stock &rhs) const; 32 | bool operator!=(const Stock &rhs) const; 33 | bool operator>(const Stock &rhs) const; 34 | bool operator<(const Stock &rhs) const; 35 | }; 36 | 37 | //***************************************************************************************************** 38 | 39 | inline std::string Stock::getName() const { 40 | return companyName; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | inline std::string Stock::getSymbol() const { 46 | return stockSymbol; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | inline double Stock::getPrice() const { 52 | return stockPrice; 53 | } 54 | 55 | //***************************************************************************************************** 56 | 57 | inline bool Stock::operator==(const Stock &rhs) const { 58 | return (stockSymbol == rhs.stockSymbol); 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | inline bool Stock::operator!=(const Stock &rhs) const { 64 | return (stockSymbol != rhs.stockSymbol); 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | inline bool Stock::operator>(const Stock &rhs) const { 70 | return (stockSymbol > rhs.stockSymbol); 71 | } 72 | 73 | //***************************************************************************************************** 74 | 75 | inline bool Stock::operator<(const Stock &rhs) const { 76 | return (stockSymbol < rhs.stockSymbol); 77 | } 78 | 79 | //***************************************************************************************************** 80 | 81 | #endif -------------------------------------------------------------------------------- /data_structures-II/09-Huffman-Algorithm/Encoding/HuffmanStocks.txt: -------------------------------------------------------------------------------- 1 | 10 2 | American Express Co 3 | AXP 4 | 118.67 5 | 0.1 6 | Apple Inc 7 | AAPL 8 | 120.3 9 | 0.15 10 | Boeing Co 11 | BA 12 | 202.4 13 | 0.02 14 | Caterpillar Inc 15 | CAT 16 | 173.19 17 | 0.03 18 | Cisco Systems Inc 19 | CSCO 20 | 42.54 21 | 0.05 22 | Goldman Sachs Group Inc 23 | GS 24 | 222.38 25 | 0.25 26 | Home Depot Inc 27 | HD 28 | 279.57 29 | 0.2 30 | Intel Corp 31 | INTC 32 | 46.19 33 | 0.075 34 | Johnson & Johnson 35 | JNJ 36 | 150.85 37 | 0.025 38 | Coca-Cola Co 39 | KO 40 | 53.85 41 | 0.1 -------------------------------------------------------------------------------- /data_structures-II/09-Huffman-Algorithm/Encoding/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/09-Huffman-Algorithm/Encoding/node.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a struct template that represents a node in a Huffman tree. 4 | // 5 | //***************************************************************************************************** 6 | 7 | #ifndef NODE_H 8 | #define NODE_H 9 | 10 | //***************************************************************************************************** 11 | 12 | template 13 | struct Node { 14 | T value; 15 | double frequency; 16 | Node *left; 17 | Node *right; 18 | 19 | Node(); 20 | Node(const T &v, Node *l = nullptr, Node *r = nullptr); 21 | }; 22 | 23 | //***************************************************************************************************** 24 | 25 | template 26 | Node::Node() { 27 | value = T(); // T() - default initialization (0 for numbers, empty string for strings, etc.) 28 | frequency = 0; 29 | left = nullptr; 30 | right = nullptr; 31 | } 32 | 33 | //***************************************************************************************************** 34 | 35 | template 36 | Node::Node(const T &v, Node *l, Node *r) { 37 | value = v; 38 | frequency = 0; 39 | left = l; 40 | right = r; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | #endif -------------------------------------------------------------------------------- /data_structures-II/09-Huffman-Algorithm/Encoding/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | std::ostream &operator<<(std::ostream &out, const Stock &stock) { 32 | out << stock.companyName << std::endl 33 | << stock.stockSymbol << std::endl 34 | << stock.stockPrice; 35 | 36 | return out; 37 | } 38 | 39 | //***************************************************************************************************** -------------------------------------------------------------------------------- /data_structures-II/09-Huffman-Algorithm/Encoding/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | friend std::ostream &operator<<(std::ostream &out, const Stock &stock); 24 | 25 | public: 26 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 27 | Stock(const Stock &s); 28 | std::string getName() const; 29 | std::string getSymbol() const; 30 | double getPrice() const; 31 | bool operator==(const Stock &rhs) const; 32 | bool operator!=(const Stock &rhs) const; 33 | bool operator>(const Stock &rhs) const; 34 | bool operator<(const Stock &rhs) const; 35 | }; 36 | 37 | //***************************************************************************************************** 38 | 39 | inline std::string Stock::getName() const { 40 | return companyName; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | inline std::string Stock::getSymbol() const { 46 | return stockSymbol; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | inline double Stock::getPrice() const { 52 | return stockPrice; 53 | } 54 | 55 | //***************************************************************************************************** 56 | 57 | inline bool Stock::operator==(const Stock &rhs) const { 58 | return (stockSymbol == rhs.stockSymbol); 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | inline bool Stock::operator!=(const Stock &rhs) const { 64 | return (stockSymbol != rhs.stockSymbol); 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | inline bool Stock::operator>(const Stock &rhs) const { 70 | return (stockSymbol > rhs.stockSymbol); 71 | } 72 | 73 | //***************************************************************************************************** 74 | 75 | inline bool Stock::operator<(const Stock &rhs) const { 76 | return (stockSymbol < rhs.stockSymbol); 77 | } 78 | 79 | //***************************************************************************************************** 80 | 81 | #endif -------------------------------------------------------------------------------- /data_structures-II/10-AList-Sorting-Algorithms/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/10-AList-Sorting-Algorithms/Stock.txt: -------------------------------------------------------------------------------- 1 | Motorola Inc. 2 | MOT 3 | 17.49 4 | Microsoft Corp. 5 | MSFT 6 | 28.11 7 | Tesla 8 | TSLA 9 | 564.33 10 | Intel 11 | INTC 12 | 60.78 13 | Sony 14 | SNE 15 | 105.81 16 | Advanced Micro Devices 17 | AMD 18 | 84.51 19 | NVIDIA 20 | NVDA 21 | 548.58 22 | Apple 23 | AAPL 24 | 121.73 25 | Qualcomm 26 | QCOM 27 | 146.10 28 | Alphabet Inc. Class A 29 | GOOGL 30 | 2805.12 -------------------------------------------------------------------------------- /data_structures-II/10-AList-Sorting-Algorithms/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | std::ostream &operator<<(std::ostream &out, const Stock &stock) { 32 | out << stock.companyName << std::endl 33 | << stock.stockSymbol << std::endl 34 | << stock.stockPrice; 35 | 36 | return out; 37 | } 38 | 39 | //***************************************************************************************************** -------------------------------------------------------------------------------- /data_structures-II/10-AList-Sorting-Algorithms/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | friend std::ostream &operator<<(std::ostream &out, const Stock &stock); 24 | 25 | public: 26 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 27 | Stock(const Stock &s); 28 | std::string getName() const; 29 | std::string getSymbol() const; 30 | double getPrice() const; 31 | bool operator==(const Stock &rhs) const; 32 | bool operator!=(const Stock &rhs) const; 33 | bool operator>(const Stock &rhs) const; 34 | bool operator<(const Stock &rhs) const; 35 | }; 36 | 37 | //***************************************************************************************************** 38 | 39 | inline std::string Stock::getName() const { 40 | return companyName; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | inline std::string Stock::getSymbol() const { 46 | return stockSymbol; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | inline double Stock::getPrice() const { 52 | return stockPrice; 53 | } 54 | 55 | //***************************************************************************************************** 56 | 57 | inline bool Stock::operator==(const Stock &rhs) const { 58 | return (stockSymbol == rhs.stockSymbol); 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | inline bool Stock::operator!=(const Stock &rhs) const { 64 | return (stockSymbol != rhs.stockSymbol); 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | inline bool Stock::operator>(const Stock &rhs) const { 70 | return (stockSymbol > rhs.stockSymbol); 71 | } 72 | 73 | //***************************************************************************************************** 74 | 75 | inline bool Stock::operator<(const Stock &rhs) const { 76 | return (stockSymbol < rhs.stockSymbol); 77 | } 78 | 79 | //***************************************************************************************************** 80 | 81 | #endif -------------------------------------------------------------------------------- /data_structures-II/11-Updated-AList-Sorting-Algorithms/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /data_structures-II/11-Updated-AList-Sorting-Algorithms/Stock.txt: -------------------------------------------------------------------------------- 1 | Motorola Inc. 2 | MOT 3 | 17.49 4 | Microsoft Corp. 5 | MSFT 6 | 28.11 7 | Tesla 8 | TSLA 9 | 564.33 10 | Intel 11 | INTC 12 | 60.78 13 | Sony 14 | SNE 15 | 105.81 16 | Advanced Micro Devices 17 | AMD 18 | 84.51 19 | NVIDIA 20 | NVDA 21 | 548.58 22 | Apple 23 | AAPL 24 | 121.73 25 | Qualcomm 26 | QCOM 27 | 146.10 28 | Alphabet Inc. Class A 29 | GOOGL 30 | 2805.12 -------------------------------------------------------------------------------- /data_structures-II/11-Updated-AList-Sorting-Algorithms/stock.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // The implementation of the Stock class header file. 4 | // 5 | // Other files required: 6 | // 1. stock.h - header file for the Stock class 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include "stock.h" 11 | #include 12 | 13 | //***************************************************************************************************** 14 | 15 | Stock::Stock(const std::string &name, const std::string &symbol, double price) { 16 | companyName = name; 17 | stockSymbol = symbol; 18 | stockPrice = price; 19 | } 20 | 21 | //***************************************************************************************************** 22 | 23 | Stock::Stock(const Stock &s) { 24 | companyName = s.companyName; 25 | stockSymbol = s.stockSymbol; 26 | stockPrice = s.stockPrice; 27 | } 28 | 29 | //***************************************************************************************************** 30 | 31 | std::ostream &operator<<(std::ostream &out, const Stock &stock) { 32 | out << stock.companyName << std::endl 33 | << stock.stockSymbol << std::endl 34 | << stock.stockPrice; 35 | 36 | return out; 37 | } 38 | 39 | //***************************************************************************************************** -------------------------------------------------------------------------------- /data_structures-II/11-Updated-AList-Sorting-Algorithms/stock.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This header file defines a Stock class with overloaded comparison and output operators to 4 | // facilitate easy comparison and display of stock information. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #ifndef STOCK_H 9 | #define STOCK_H 10 | 11 | //***************************************************************************************************** 12 | 13 | #include 14 | 15 | //***************************************************************************************************** 16 | 17 | class Stock { 18 | private: 19 | std::string companyName; 20 | std::string stockSymbol; 21 | double stockPrice; 22 | 23 | friend std::ostream &operator<<(std::ostream &out, const Stock &stock); 24 | 25 | public: 26 | Stock(const std::string &name = "", const std::string &symbol = "", double price = 0); 27 | Stock(const Stock &s); 28 | std::string getName() const; 29 | std::string getSymbol() const; 30 | double getPrice() const; 31 | bool operator==(const Stock &rhs) const; 32 | bool operator!=(const Stock &rhs) const; 33 | bool operator>(const Stock &rhs) const; 34 | bool operator<(const Stock &rhs) const; 35 | }; 36 | 37 | //***************************************************************************************************** 38 | 39 | inline std::string Stock::getName() const { 40 | return companyName; 41 | } 42 | 43 | //***************************************************************************************************** 44 | 45 | inline std::string Stock::getSymbol() const { 46 | return stockSymbol; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | inline double Stock::getPrice() const { 52 | return stockPrice; 53 | } 54 | 55 | //***************************************************************************************************** 56 | 57 | inline bool Stock::operator==(const Stock &rhs) const { 58 | return (stockSymbol == rhs.stockSymbol); 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | inline bool Stock::operator!=(const Stock &rhs) const { 64 | return (stockSymbol != rhs.stockSymbol); 65 | } 66 | 67 | //***************************************************************************************************** 68 | 69 | inline bool Stock::operator>(const Stock &rhs) const { 70 | return (stockSymbol > rhs.stockSymbol); 71 | } 72 | 73 | //***************************************************************************************************** 74 | 75 | inline bool Stock::operator<(const Stock &rhs) const { 76 | return (stockSymbol < rhs.stockSymbol); 77 | } 78 | 79 | //***************************************************************************************************** 80 | 81 | #endif -------------------------------------------------------------------------------- /data_structures-II/12-Graph-Traversal/a12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/12-Graph-Traversal/a12.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/AVL Tree Examples.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/AVL Tree Examples.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/AVLTree Examples 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/AVLTree Examples 1.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/AVLTree Examples 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/AVLTree Examples 2.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/HeapSort.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/HeapSort.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Huffman Examples 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Huffman Examples 1.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Huffman Examples 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Huffman Examples 2.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/LinkedLists.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/LinkedLists.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/MergeSort.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/MergeSort.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/PriorityQueue Examples.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/PriorityQueue Examples.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/QuickSort.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/QuickSort.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Recursion Examples - 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Recursion Examples - 1.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Recursion Examples - 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Recursion Examples - 2.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Recursion Examples - 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Recursion Examples - 3.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Searching Examples - 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Searching Examples - 1.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Searching Examples - 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Searching Examples - 2.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Sorting Timing.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Sorting Timing.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Towers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Towers.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Tree Examples 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Tree Examples 1.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Tree Examples 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Tree Examples 2.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/examples/Tree Examples 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/data_structures-II/docs/examples/Tree Examples 3.pdf -------------------------------------------------------------------------------- /data_structures-II/docs/header-template.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program... 4 | // 5 | // Other files required: 6 | // 1. example.h - header file for the example class 7 | // 2. example.txt - a text file containing the example data 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include 12 | using namespace std; 13 | 14 | //***************************************************************************************************** 15 | 16 | int main() { 17 | 18 | return 0; 19 | } 20 | 21 | //***************************************************************************************************** 22 | /* 23 | 24 | // attach sample output here. 25 | 26 | */ -------------------------------------------------------------------------------- /programming-I/01-helloworld/HelloWorld.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Hello, World! 3 | // 4 | // The "Hello, World!" program is often used as a first program for people learning to code. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #include 9 | using namespace std; 10 | 11 | //***************************************************************************************************** 12 | 13 | int main() { 14 | cout << "Hello, World!" << endl; 15 | 16 | return 0; 17 | } 18 | 19 | //***************************************************************************************************** 20 | /* 21 | 22 | Hello, World! 23 | 24 | */ -------------------------------------------------------------------------------- /programming-I/02-recipe/recipe.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Chocolate Chip Cookies Recipe 3 | // 4 | // This program is a simple recipe for making Chocolate Chip Cookies. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #include 9 | using namespace std; 10 | 11 | //***************************************************************************************************** 12 | 13 | int main() { 14 | cout << "Chocolate Chip Cookies - 4 dozen cookies" << endl; 15 | 16 | cout << "\n\t1 cup butter\n" 17 | << "\t1 1 / 2 cups white sugar\n" 18 | << "\t2 eggs\n" 19 | << "\t2 tsp vanilla extract\n" 20 | << "\t2 cups all-purpose flour\n" 21 | << "\t3 / 4 baking soda\n" 22 | << "\t1 / 4 tsp salt\n" 23 | << "\t2 cups chocolate chips" << endl; 24 | 25 | cout << "\nPreheat oven to 350 F. In a large bowl, mix butter, sugar, eggs, and vanilla\n" 26 | << "until light and fluffy. In a separate bowl, combine flour, baking soda, and\n" 27 | << "salt; stir into the butter mixture until well-blended stir in chocolate chips.\n" 28 | << "Drop by rounded teaspoons onto ungreased cookie sheets. Bake 8-10 minutes until\n" 29 | << "just set. Cool slightly on cookie sheets before transferring to cooling racks\n" 30 | << "to cool completely." << endl; 31 | 32 | return 0; 33 | } 34 | 35 | //***************************************************************************************************** 36 | /* 37 | 38 | Chocolate Chip Cookies - 4 dozen cookies 39 | 40 | 1 cup butter 41 | 1 1 / 2 cups white sugar 42 | 2 eggs 43 | 2 tsp vanilla extract 44 | 2 cups all-purpose flour 45 | 3 / 4 baking soda 46 | 1 / 4 tsp salt 47 | 2 cups chocolate chips 48 | 49 | Preheat oven to 350 F. In a large bowl, mix butter, sugar, eggs, and vanilla 50 | until light and fluffy. In a separate bowl, combine flour, baking soda, and 51 | salt; stir into the butter mixture until well-blended stir in chocolate chips. 52 | Drop by rounded teaspoons onto ungreased cookie sheets. Bake 8-10 minutes until 53 | just set. Cool slightly on cookie sheets before transferring to cooling racks 54 | to cool completely. 55 | 56 | */ -------------------------------------------------------------------------------- /programming-I/03-ingredients/ingredients.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Chocolate Chip Cookies Recipe - Scaling Ingredients 3 | // 4 | // This program is a simple recipe for making Chocolate Chip Cookies that can be scaled to any 5 | // number of dozens. 6 | // 7 | //***************************************************************************************************** 8 | 9 | #include 10 | using namespace std; 11 | 12 | //***************************************************************************************************** 13 | 14 | int main() { 15 | const int BUTTER = 1, 16 | EGG = 2, 17 | VANILLA = 2, 18 | FLOUR = 2, 19 | CHOC_CHIP = 2; 20 | const double BAKING_SODA = 0.75, 21 | SUGAR = 1.5, 22 | SALT = 0.25; 23 | int dozen, 24 | quantity; 25 | 26 | cout << "Chocolate Chip Cookies - 4 dozen cookies" << endl; 27 | 28 | cout << "\n\t" << BUTTER << " cup butter\n" 29 | << "\t" << SUGAR << " cups white sugar\n" 30 | << "\t" << EGG << " eggs\n" 31 | << "\t" << VANILLA << " tsp vanilla extract\n" 32 | << "\t" << FLOUR << " cups all-purpose flour\n" 33 | << "\t" << BAKING_SODA << " baking soda\n" 34 | << "\t" << SALT << " tsp salt\n" 35 | << "\t" << CHOC_CHIP << " cups chocolate chips" << endl; 36 | 37 | cout << "\nPreheat oven to 350 F. In a large bowl, mix butter, sugar, eggs, and vanilla\n" 38 | << "until light and fluffy. In a separate bowl, combine flour, baking soda, and\n" 39 | << "salt; stir into the butter mixture until well-blended stir in chocolate chips.\n" 40 | << "Drop by rounded teaspoons onto ungreased cookie sheets. Bake 8-10 minutes until\n" 41 | << "just set. Cool slightly on cookie sheets before transferring to cooling racks\n" 42 | << "to cool completely." << endl; 43 | 44 | cout << "\nHow many dozens are you planning to make (Please give a multiple of four)?" << endl; 45 | cin >> dozen; 46 | 47 | quantity = dozen / 4; 48 | 49 | cout << "\n\n\t" << quantity * BUTTER << " cup butter\n" 50 | << "\t" << quantity * SUGAR << " cups white sugar\n" 51 | << "\t" << quantity * EGG << " eggs\n" 52 | << "\t" << quantity * VANILLA << " tsp vanilla extract\n" 53 | << "\t" << quantity * FLOUR << " cups all-purpose flour\n" 54 | << "\t" << quantity * BAKING_SODA << " baking soda\n" 55 | << "\t" << quantity * SALT << " tsp salt\n" 56 | << "\t" << quantity * CHOC_CHIP << " cups chocolate chips" << endl; 57 | 58 | return 0; 59 | } 60 | 61 | //***************************************************************************************************** 62 | /* 63 | 64 | Chocolate Chip Cookies - 4 dozen cookies 65 | 66 | 1 cup butter 67 | 1.5 cups white sugar 68 | 2 eggs 69 | 2 tsp vanilla extract 70 | 2 cups all-purpose flour 71 | 0.75 baking soda 72 | 0.25 tsp salt 73 | 2 cups chocolate chips 74 | 75 | Preheat oven to 350 F. In a large bowl, mix butter, sugar, eggs, and vanilla 76 | until light and fluffy. In a separate bowl, combine flour, baking soda, and 77 | salt; stir into the butter mixture until well-blended stir in chocolate chips. 78 | Drop by rounded teaspoons onto ungreased cookie sheets. Bake 8-10 minutes until 79 | just set. Cool slightly on cookie sheets before transferring to cooling racks 80 | to cool completely. 81 | 82 | How many dozens are you planning to make (Please give a multiple of four)? 83 | 12 84 | 85 | 86 | 3 cup butter 87 | 4.5 cups white sugar 88 | 6 eggs 89 | 6 tsp vanilla extract 90 | 6 cups all-purpose flour 91 | 2.25 baking soda 92 | 0.75 tsp salt 93 | 6 cups chocolate chips 94 | 95 | */ -------------------------------------------------------------------------------- /programming-I/05-shippingCost/shippingCost.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Online Purchase Cost Calculator 3 | // 4 | // This program calculates the total cost of an online purchase based on the item's price and 5 | // weight. 6 | // 7 | //***************************************************************************************************** 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | //***************************************************************************************************** 14 | 15 | int main() { 16 | const float TAXRATE = 4.225 / 100, 17 | LESS_ONE_LB = 10, 18 | LESS_FIVE_LB = 7, 19 | LESS_EIGHT_LB = 5, 20 | LESS_TEN_LB = 3, 21 | LESS_TWENTY_LB = 2, 22 | ABOVE_TWENTY = 1; 23 | float price, 24 | tax, 25 | shipping, 26 | totalCost, 27 | weight; 28 | 29 | cout << "Calculate the total cost of an online purchase" << endl; 30 | 31 | cout << "\nEnter the item's price: "; 32 | cin >> price; 33 | 34 | cout << "Enter the item's weight: "; 35 | cin >> weight; 36 | 37 | if (weight < 1) 38 | shipping = LESS_ONE_LB * weight; 39 | else if (weight < 5) 40 | shipping = LESS_FIVE_LB * weight; 41 | else if (weight < 8) 42 | shipping = LESS_EIGHT_LB * weight; 43 | else if (weight < 10) 44 | shipping = LESS_TEN_LB * weight; 45 | else if (weight < 20) 46 | shipping = LESS_TWENTY_LB * weight; 47 | else 48 | shipping = ABOVE_TWENTY * weight; 49 | 50 | tax = price * TAXRATE; 51 | totalCost = price + tax + shipping; 52 | 53 | cout << "\nTotal price: " << fixed << setprecision(2) << totalCost << endl; 54 | 55 | return 0; 56 | } 57 | 58 | //***************************************************************************************************** 59 | /* 60 | 61 | Calculate the total cost of an online purchase 62 | 63 | Enter the item's price: 10 64 | Enter the item's weight: 50 65 | 66 | Total price: 60.42 67 | 68 | ***************************************************************************************************** 69 | 70 | Calculate the total cost of an online purchase 71 | 72 | Enter the item's price: 900 73 | Enter the item's weight: 45.50 74 | 75 | Total price: 983.53 76 | 77 | */ -------------------------------------------------------------------------------- /programming-I/06-morra/morra.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Morra Game Simulation 3 | // 4 | // This program is a simple game of Morra (a game played with fingers) between two players and 5 | // the results of each game are written to a file. 6 | // 7 | //***************************************************************************************************** 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | //***************************************************************************************************** 16 | 17 | int main() { 18 | const int MAX_FINGERS = 5., 19 | MIN_FINGERS = 1, 20 | MAX_GUESS = 10, 21 | MIN_GUESS = 0; 22 | int playerOneFingers, 23 | playerOneGuess, 24 | playerTwoFingers, 25 | playerTwoGuess, 26 | sum; 27 | ofstream out("result.txt"); 28 | 29 | short seed; 30 | seed = time(0); 31 | srand(seed); 32 | 33 | playerOneFingers = rand() % (MAX_FINGERS - MIN_FINGERS) + MIN_FINGERS; 34 | playerOneGuess = rand() % (MAX_GUESS - MIN_GUESS) + MIN_GUESS; 35 | playerTwoFingers = rand() % (MAX_FINGERS - MIN_FINGERS) + MIN_FINGERS; 36 | playerTwoGuess = rand() % (MAX_GUESS - MIN_GUESS) + MIN_GUESS; 37 | 38 | out << "Fingers\tTotal" << endl; 39 | 40 | out << playerOneFingers << "\t\t" << playerOneGuess << endl; 41 | 42 | out << playerTwoFingers << "\t\t" << playerTwoGuess << endl; 43 | 44 | sum = playerOneFingers + playerTwoFingers; 45 | out << "\nCorrect total is " << sum << endl; 46 | 47 | if (sum == playerOneGuess && sum == playerTwoGuess) 48 | out << "TIE" << endl; 49 | else if (sum == playerOneGuess) 50 | out << "PLAYER 1 WINS" << endl; 51 | else if (sum == playerTwoGuess) 52 | out << "PLAYER 2 WINS" << endl; 53 | else 54 | out << "NO ONE WINS" << endl; 55 | 56 | return 0; 57 | } 58 | 59 | //***************************************************************************************************** 60 | /* 61 | 62 | Fingers Total 63 | 4 5 64 | 3 9 65 | 66 | Correct total is 7 67 | NO ONE WINS 68 | 69 | ***************************************************************************************************** 70 | 71 | Fingers Total 72 | 1 2 73 | 1 6 74 | 75 | Correct total is 2 76 | PLAYER 1 WINS 77 | 78 | ***************************************************************************************************** 79 | 80 | Fingers Total 81 | 2 1 82 | 4 6 83 | 84 | Correct total is 6 85 | PLAYER 2 WINS 86 | 87 | */ -------------------------------------------------------------------------------- /programming-I/06-morra/result.txt: -------------------------------------------------------------------------------- 1 | Fingers Total 2 | 3 1 3 | 2 8 4 | 5 | Correct total is 5 6 | NO ONE WINS 7 | -------------------------------------------------------------------------------- /programming-I/07-morraSeries/morraSeriesResults.txt: -------------------------------------------------------------------------------- 1 | ===================================== 2 | Summary of the series 3 | ===================================== 4 | Player 1 won 1 games 5 | Player 2 won 0 games 6 | 7 | Winner of the series: Player 1 8 | -------------------------------------------------------------------------------- /programming-I/09-circleArea/circleArea.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Circumference Calculation 3 | // 4 | // This program is a simple circumference calculator for a circle. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #define _USE_MATH_DEFINES // needed to define M_PI with math.h or cmath on some platforms 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | //***************************************************************************************************** 14 | 15 | void greet(); // function prototype 16 | float readRadius(); 17 | float findCircumf(float); 18 | void printResult(float, float); 19 | void signOff(); 20 | 21 | //***************************************************************************************************** 22 | 23 | int main() { 24 | float radius, 25 | circumf; 26 | 27 | greet(); 28 | 29 | radius = readRadius(); 30 | circumf = findCircumf(radius); 31 | 32 | printResult(radius, circumf); 33 | signOff(); 34 | 35 | return 0; 36 | } 37 | 38 | //***************************************************************************************************** 39 | 40 | void greet() { 41 | cout << "Welcome to the Circumference Calculation Program\n" 42 | << "Enter the radius and I'll find the circumference of the circle!" << endl; 43 | } 44 | 45 | //***************************************************************************************************** 46 | 47 | float readRadius() { 48 | float posInput; 49 | 50 | do { 51 | cout << "\nPlease enter a positive radius: "; 52 | cin >> posInput; 53 | } while (posInput <= 0); 54 | 55 | return posInput; 56 | } 57 | 58 | //***************************************************************************************************** 59 | 60 | float findCircumf(float radius) { 61 | float circumf; 62 | 63 | circumf = (2 * M_PI) * radius; 64 | 65 | return circumf; // return (2 * M_PI) * radius; 66 | } 67 | 68 | //***************************************************************************************************** 69 | 70 | void printResult(float radius, float circumf) { 71 | cout << "\nYou entered the radius: " << radius 72 | << "\nThe circumference of the circle: " << circumf << endl; 73 | } 74 | 75 | //***************************************************************************************************** 76 | 77 | void signOff() { 78 | cout << "\nHave A Great Day!" << endl; 79 | } 80 | 81 | //***************************************************************************************************** 82 | /* 83 | 84 | Welcome to the Circumference Calculation Program 85 | Enter the radius and I'll find the circumference of the circle! 86 | 87 | Please enter a positive radius: 0 88 | 89 | Please enter a positive radius: -2.2 90 | 91 | Please enter a positive radius: 3.21 92 | 93 | You entered the radius: 3.21 94 | The circumference of the circle: 20.169 95 | 96 | Have A Great Day! 97 | 98 | */ -------------------------------------------------------------------------------- /programming-I/11-echoList/echoAList.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Array Sum Calculator 3 | // 4 | // This program calculates the sum of 10 numbers entered by the user. 5 | // 6 | //***************************************************************************************************** 7 | 8 | #include 9 | using namespace std; 10 | 11 | //***************************************************************************************************** 12 | 13 | int main() { 14 | const int SIZE = 10; 15 | int nums[SIZE]; 16 | int sum = 0; 17 | 18 | cout << "Enter 10 numbers:" << endl; 19 | 20 | for (int i = 0; i < SIZE; i++) { 21 | cin >> nums[i]; 22 | 23 | sum += nums[i]; 24 | } 25 | 26 | cout << "\nSum is: " << sum << "\nThe list of numbers were: "; 27 | 28 | for (int i = 0; i < SIZE; i++) 29 | cout << nums[i] << " "; 30 | 31 | return 0; 32 | } 33 | 34 | //***************************************************************************************************** 35 | /* 36 | 37 | Enter 10 numbers 38 | 39 | Enter #1: 1 40 | Enter #2: 2 41 | Enter #3: 3 42 | Enter #4: 4 43 | Enter #5: 5 44 | Enter #6: 6 45 | Enter #7: 7 46 | Enter #8: 8 47 | Enter #9: 9 48 | Enter #10: 10 49 | 50 | Sum is: 55 51 | The list of numbers were: 1 2 3 4 5 6 7 8 9 10 52 | 53 | ***************************************************************************************************** 54 | 55 | Enter 10 numbers 56 | 57 | Enter #1: 10 58 | Enter #2: 9 59 | Enter #3: 8 60 | Enter #4: 7 61 | Enter #5: 6 62 | Enter #6: 5 63 | Enter #7: 4 64 | Enter #8: 3 65 | Enter #9: 2 66 | Enter #10: 1 67 | 68 | Sum is: 55 69 | The list of numbers were: 10 9 8 7 6 5 4 3 2 1 70 | 71 | */ -------------------------------------------------------------------------------- /programming-I/docs/chapters/CH 1 FA21.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-I/docs/chapters/CH 1 FA21.pdf -------------------------------------------------------------------------------- /programming-I/docs/chapters/CH 2_FA21.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-I/docs/chapters/CH 2_FA21.pdf -------------------------------------------------------------------------------- /programming-I/docs/chapters/CH 3_FA21.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-I/docs/chapters/CH 3_FA21.pdf -------------------------------------------------------------------------------- /programming-I/docs/chapters/CH 4_FA21.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-I/docs/chapters/CH 4_FA21.pdf -------------------------------------------------------------------------------- /programming-I/docs/chapters/CH 5FA21_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-I/docs/chapters/CH 5FA21_1.pdf -------------------------------------------------------------------------------- /programming-I/docs/chapters/CH 5FA21_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-I/docs/chapters/CH 5FA21_2.pdf -------------------------------------------------------------------------------- /programming-I/docs/chapters/CH 7FA21.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-I/docs/chapters/CH 7FA21.pdf -------------------------------------------------------------------------------- /programming-I/docs/header-template.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program... 4 | // 5 | // Other files required: 6 | // 1. example.h - header file for the example class 7 | // 2. example.txt - a text file containing the example data 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include 12 | using namespace std; 13 | 14 | //***************************************************************************************************** 15 | 16 | int main() { 17 | 18 | return 0; 19 | } 20 | 21 | //***************************************************************************************************** 22 | /* 23 | 24 | // attach sample output here. 25 | 26 | */ -------------------------------------------------------------------------------- /programming-II/01-twoDimensionalArray/Assignment1Data.txt: -------------------------------------------------------------------------------- 1 | 87 90 65 45 88 89 75 80 2 | 89 85 78 56 90 91 99 82 3 | 80 90 72 95 99 66 77 88 4 | 90 65 80 98 99 76 77 88 5 | 55 75 66 99 99 56 87 86 6 | 30 60 80 90 99 56 67 88 7 | 90 55 95 35 98 86 37 88 8 | 99 97 99 98 98 96 87 83 9 | 90 65 80 98 99 76 87 88 10 | 35 75 60 99 99 56 87 88 -------------------------------------------------------------------------------- /programming-II/02-linearSearch-BubbleSort/Assignment1Data.txt: -------------------------------------------------------------------------------- 1 | 87 90 65 45 88 89 75 80 2 | 89 85 78 56 90 91 99 82 3 | 80 90 72 95 99 66 77 88 4 | 90 65 80 98 99 76 77 88 5 | 55 75 66 99 99 56 87 86 6 | 30 60 80 90 99 56 67 88 7 | 90 55 95 35 98 86 37 88 8 | 99 97 99 98 98 96 87 83 9 | 90 65 80 98 99 76 87 88 10 | 35 75 60 99 99 56 87 88 -------------------------------------------------------------------------------- /programming-II/02-linearSearch-BubbleSort/StudentNames.txt: -------------------------------------------------------------------------------- 1 | Smith, John 2 | Song, Mona 3 | Jones, Trevor 4 | Li, Na 5 | Zhang, Xiu Ying 6 | Saleem, Mohammad 7 | Lloyd, Arthur 8 | Jones, Rhys 9 | Evans, Olivia 10 | Davies, Emily -------------------------------------------------------------------------------- /programming-II/03-binarySearch-selectionSort_introToPointers/StudentNames.txt: -------------------------------------------------------------------------------- 1 | Smith, John 2 | Song, Mona 3 | Jones, Trevor 4 | Li, Na 5 | Zhang, Xiu Ying 6 | Saleem, Mohammad 7 | Lloyd, Arthur 8 | Jones, Rhys 9 | Evans, Olivia 10 | Davies, Emily -------------------------------------------------------------------------------- /programming-II/03-binarySearch-selectionSort_introToPointers/pointers.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Pointer Variables and Pointer Arithmetic Example 3 | // 4 | // This program demonstrates the use of pointers and pointer arithmetic to manipulate and perform 5 | // operations 6 | // 7 | //***************************************************************************************************** 8 | 9 | #include 10 | using namespace std; 11 | 12 | //***************************************************************************************************** 13 | 14 | int main() { 15 | int n1 = 10, 16 | n2 = 25, 17 | n3 = 50, 18 | sum; 19 | int *p1 = &n1, // p1 points to n1 20 | *p2 = &n2; 21 | 22 | cout << "Three variables: n1 n2 n3\n" 23 | << "n1 = " << n1 24 | << "\nMemory address: " << &n1 // memory address of n1 25 | << "\n\nn2 = " << n2 26 | << "\nMemory address: " << &n2 27 | << "\n\nn3 = " << n3 28 | << "\nMemory address: " << &n3 << endl; 29 | 30 | cout << "\nTwo pointers: p1 p2\n" 31 | << "p1\n" 32 | << "Memory address: " << &p1 // memory address of p1 33 | << "\np1 is pointing to n1\n\n" 34 | << "p2\n" 35 | << "Memory address: " << &p2 36 | << "\np2 is pointing to n2" << endl; 37 | 38 | cout << "\nValue at the address (n1) to which p1 points\n" 39 | << "*p1 = " << *p1 // dereference p1 (n1 value) 40 | << "\nMemory address (n1) to which p1 points\n" 41 | << "p1 = " << p1 // memory address of n1 42 | << "\n\nValue at the address (n2) to which p2 points\n" 43 | << "*p2 = " << *p2 44 | << "\nMemory address (n2) to which p2 points\n" 45 | << "p2 = " << p2 << endl; 46 | 47 | *p1 *= 2; // multiply value at the address to which p1 points (n1) by 2 48 | 49 | cout << "\nValue of n1 has been multiplied by two using pointer arithmetic (*p1 *= 2)\n" 50 | << "n1 = " << n1 << endl; 51 | 52 | sum = *p1 + *p2; // add values at the addresses to which p1 and p2 point (n1 n2) 53 | 54 | cout << "\nSum of values (n1 n2) to which p1 p2 point using pointer arithmetic (sum = *p1 + *p2)\n" 55 | << "p1(20) + p2(25) = " << sum << endl; 56 | 57 | p1 = &n3; // p1 points to n3 58 | 59 | cout << "\np1 re-assigned pointing to n3\n" 60 | << "Value at the address (n3) to which p1 points\n" 61 | << "*p1 = " << *p1 // value at the address to which p1 points (n3 value) 62 | << "\nMemory address (n3) to which p1 points\n" 63 | << "p1 = " << p1 << endl; 64 | 65 | sum = *p1 + *p2; 66 | 67 | cout << "\nSum of values (n3 n2) to which p1 p2 point using pointer arithmetic (sum = *p1 + *p2)\n" 68 | << "p1(50) + p2(25) = " << sum << endl; 69 | 70 | return 0; 71 | } 72 | 73 | //***************************************************************************************************** 74 | /* 75 | 76 | Three variables: n1 n2 n3 77 | n1 = 10 78 | Memory address: 0xc1be3ff978 79 | 80 | n2 = 25 81 | Memory address: 0xc1be3ff974 82 | 83 | n3 = 50 84 | Memory address: 0xc1be3ff970 85 | 86 | Two pointers: p1 p2 87 | p1 88 | Memory address: 0xc1be3ff968 89 | p1 is pointing to n1 90 | 91 | p2 92 | Memory address: 0xc1be3ff960 93 | p2 is pointing to n2 94 | 95 | Value at the address (n1) to which p1 points 96 | *p1 = 10 97 | Memory address (n1) to which p1 points 98 | p1 = 0xc1be3ff978 99 | 100 | Value at the address (n2) to which p2 points 101 | *p2 = 25 102 | Memory address (n2) to which p2 points 103 | p2 = 0xc1be3ff974 104 | 105 | Value of n1 has been multiplied by two using pointer arithmetic (*p1 *= 2) 106 | n1 = 20 107 | 108 | Sum of values (n1 n2) to which p1 p2 point using pointer arithmetic (sum = *p1 + *p2) 109 | p1(20) + p2(25) = 45 110 | 111 | p1 re-assigned pointing to n3 112 | Value at the address (n3) to which p1 points 113 | *p1 = 50 114 | Memory address (n3) to which p1 points 115 | p1 = 0xc1be3ff970 116 | 117 | Sum of values (n3 n2) to which p1 p2 point using pointer arithmetic (sum = *p1 + *p2) 118 | p1(50) + p2(25) = 75 119 | 120 | */ -------------------------------------------------------------------------------- /programming-II/06-cString-string/cstrings.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Counting Words in C-Strings 3 | // 4 | // This program takes a user input c-string and counts the number of words ending with a period 5 | // and ignores any punctuation, spaces, numbers, special characters, mispelled words, etc. 6 | // 7 | //***************************************************************************************************** 8 | 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | //***************************************************************************************************** 15 | 16 | int numberWords(const char *words, int length); 17 | 18 | //***************************************************************************************************** 19 | 20 | int main() { 21 | const int SIZE = 50; 22 | int result, 23 | numInput = 3, 24 | length; 25 | char *words = nullptr; 26 | 27 | words = new char[SIZE]; 28 | 29 | for (int i = 0; i < numInput; ++i) { 30 | cout << "\nEnter a line of words (less than 50 characters), ended by '.': " << endl; 31 | cin.getline(words, SIZE); // cin.getline() is used for c-strings (getline() is used for strings) 32 | 33 | length = strlen(words); 34 | 35 | while (words[length - 1] != '.') { 36 | cerr << "ERROR: Input must end with a period '.'\n"; 37 | cout << "\nEnter a line of words (less than 50 characters), ended by '.': " << endl; 38 | cin.getline(words, SIZE); 39 | 40 | length = strlen(words); 41 | } 42 | 43 | result = numberWords(words, length); 44 | cout << "Total words: " << result << endl; 45 | } 46 | 47 | delete[] words; 48 | words = nullptr; 49 | 50 | return 0; 51 | } 52 | 53 | //***************************************************************************************************** 54 | 55 | int numberWords(const char *words, int length) { 56 | int total = 0; 57 | bool isLetter = false; 58 | 59 | for (int i = 0; i < length; ++i) { 60 | if (isalpha(words[i])) { 61 | isLetter = true; 62 | } else if (isLetter == true) { 63 | total++; 64 | isLetter = false; 65 | } 66 | 67 | if (words[i] == '.') 68 | break; 69 | } 70 | 71 | return total; 72 | } 73 | 74 | //***************************************************************************************************** 75 | /* 76 | 77 | Enter a line of words (less than 50 characters), ended by '.': 78 | 79 | ERROR: Input must end with a period '.' 80 | 81 | Enter a line of words (less than 50 characters), ended by '.': 82 | . 83 | Total words: 0 84 | 85 | Enter a line of words (less than 50 characters), ended by '.': 86 | This contains a name,address, and phone number. 87 | Total words: 8 88 | 89 | Enter a line of words (less than 50 characters), ended by '.': 90 | ,,,,,,,This contains,, a name. 91 | Total words: 4 92 | 93 | ***************************************************************************************************** 94 | 95 | Enter a line of words (less than 50 characters), ended by '.': 96 | ,,, DEF,,, ABC,, 1234. 97 | Total words: 2 98 | 99 | Enter a line of words (less than 50 characters), ended by '.': 100 | Hello, this is a test. test 101 | ERROR: Input must end with a period '.' 102 | 103 | Enter a line of words (less than 50 characters), ended by '.': 104 | Hello, this is a test. 105 | Total words: 5 106 | 107 | Enter a line of words (less than 50 characters), ended by '.': 108 | hi,, HEY,, hello ,, . 109 | Total words: 3 110 | 111 | */ -------------------------------------------------------------------------------- /programming-II/06-cString-string/strings.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // String Processing 3 | // 4 | // This program takes a string and uses various string functions to extract and display 5 | // information about the string such as the length, number of letters, digits, upper and lower 6 | // case letters, specific characters and indexes of certain characters. 7 | // 8 | //***************************************************************************************************** 9 | 10 | #include 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | //***************************************************************************************************** 16 | 17 | void sentenceData(const string sentence); 18 | 19 | //***************************************************************************************************** 20 | 21 | int main() { 22 | string sentence("This is a 101 SAMPLE to demonstrate string processing"); 23 | 24 | cout << "------------------------------------------------------\n" 25 | << sentence 26 | << "\n------------------------------------------------------" << endl; 27 | sentenceData(sentence); 28 | 29 | return 0; 30 | } 31 | 32 | //***************************************************************************************************** 33 | 34 | void sentenceData(const string sentence) { 35 | int length, 36 | alphaCount = 0, 37 | digitCount = 0, 38 | lowerCount = 0, 39 | upperCount = 0, 40 | first_s, 41 | second_s; 42 | 43 | length = sentence.length(); // get the length of the string 44 | first_s = sentence.find('s'); // find the first 's' in the string 45 | second_s = sentence.find('s', first_s + 1); // find the second 's' in the string 46 | 47 | for (int i = 0; i < length; ++i) { // bool return types: isalpha(), isdigit(), islower(), isupper() 48 | if (isalpha(sentence[i])) 49 | alphaCount++; 50 | if (isdigit(sentence[i])) 51 | digitCount++; 52 | if (islower(sentence[i])) 53 | lowerCount++; 54 | if (isupper(sentence[i])) 55 | upperCount++; 56 | } 57 | 58 | cout << "The size of the string: " << length 59 | << "\nThe number of letters in the string: " << alphaCount 60 | << "\nThe number of digits in the string: " << digitCount 61 | << "\nThe number of lower case letters in the string: " << lowerCount 62 | << "\nThe number of upper case letters in the string: " << upperCount 63 | << "\nThe character that is at index 12 of the string: " << sentence.at(12) // at() is the same as [] 64 | << "\nThe first character of the string: " << sentence.front() // front() is the same as [0] 65 | << "\nThe last character of the string: " << sentence.back() // back() is the same as [length - 1] 66 | << "\nThe index of the first 's' in the string: " << first_s 67 | << "\nThe index of the second 's' in the string: " << second_s << endl; 68 | } 69 | 70 | //***************************************************************************************************** 71 | /* 72 | 73 | ------------------------------------------------------ 74 | This is a 101 SAMPLE to demonstrate string processing 75 | ------------------------------------------------------ 76 | The size of the string: 53 77 | The number of letters in the string: 42 78 | The number of digits in the string: 3 79 | The number of lower case letters in the string: 35 80 | The number of upper case letters in the string: 7 81 | The character that is at index 12 of the string: 1 82 | The last character of the string: g 83 | The index of the first 's' in the string: 3 84 | The index of the second 's' in the string: 6 85 | 86 | */ -------------------------------------------------------------------------------- /programming-II/08-fstream-manualUpdate/Employees.txt: -------------------------------------------------------------------------------- 1 | 3 2 | Stephen Smith,25,12/15/2001 3 | Susan Kim-Jones,33,1/9/2012 4 | Praew Chaem Choi,52,8/22/1999 5 | -------------------------------------------------------------------------------- /programming-II/09-fstream-autoUpdate/Testers.txt: -------------------------------------------------------------------------------- 1 | 10 2 | Steve Smith 3 | 56.6,11/11/2019 4 | Sue Jones 5 | 10,11/11/2011 6 | Li Ying 7 | 0,11/11/2011 8 | Kun Joom 9 | 20,11/11/2021 10 | Joe Bush 11 | 10,11/11/2021 12 | Kim Long 13 | 0,11/11/2011 14 | Fred Ring 15 | 33.3,5/5/2019 16 | Frank Pearse 17 | 20,11/11/2011 18 | Helen Hu 19 | 0.1,11/11/2011 20 | Mark James 21 | 0,11/11/2021 -------------------------------------------------------------------------------- /programming-II/09-fstream-autoUpdate/Translation.txt: -------------------------------------------------------------------------------- 1 | 20 2 | chips,crisps 3 | french fries,chips 4 | sidewalk,pavement 5 | cookie,biscuit 6 | apartment,flat 7 | elevator,lift 8 | gas,petrol 9 | soccer,football 10 | movie,film 11 | sweater,jumper 12 | trunk,boot 13 | hood,bonnet 14 | last name,surname 15 | restroom,toilet 16 | cart,trolley 17 | candy,sweet 18 | vacation,holiday 19 | subway,underground 20 | pants,trousers 21 | mailbox,postbox -------------------------------------------------------------------------------- /programming-II/10-fstream-autoUpdate-binaryConversion/Testers.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-II/10-fstream-autoUpdate-binaryConversion/Testers.dat -------------------------------------------------------------------------------- /programming-II/10-fstream-autoUpdate-binaryConversion/Testers.txt: -------------------------------------------------------------------------------- 1 | 10 2 | Steve Smith 3 | 56.6,11/11/2019 4 | Sue Jones 5 | 10,11/11/2011 6 | Li Ying 7 | 0,11/11/2011 8 | Kun Joom 9 | 20,11/11/2021 10 | Joe Bush 11 | 10,11/11/2021 12 | Kim Long 13 | 0,11/11/2011 14 | Fred Ring 15 | 33.3,5/5/2019 16 | Frank Pearse 17 | 20,11/11/2011 18 | Helen Hu 19 | 0.1,11/11/2011 20 | Mark James 21 | 0,11/11/2021 22 | -------------------------------------------------------------------------------- /programming-II/10-fstream-autoUpdate-binaryConversion/Translation.txt: -------------------------------------------------------------------------------- 1 | 20 2 | chips,crisps 3 | french fries,chips 4 | sidewalk,pavement 5 | cookie,biscuit 6 | apartment,flat 7 | elevator,lift 8 | gas,petrol 9 | soccer,football 10 | movie,film 11 | sweater,jumper 12 | trunk,boot 13 | hood,bonnet 14 | last name,surname 15 | restroom,toilet 16 | cart,trolley 17 | candy,sweet 18 | vacation,holiday 19 | subway,underground 20 | pants,trousers 21 | mailbox,postbox -------------------------------------------------------------------------------- /programming-II/10-fstream-autoUpdate-binaryConversion/textToBinary.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Text File to Binary Conversion 3 | // 4 | // This program reads tester information from a text file and writes the information to a binary 5 | // file. 6 | // 7 | // Other files required: 8 | // 1. Testers.txt - number of testers and the tester information 9 | // 10 | //***************************************************************************************************** 11 | 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | //***************************************************************************************************** 18 | 19 | const int NAME_SIZE = 20; 20 | 21 | struct Date { 22 | int month; 23 | int day; 24 | int year; 25 | }; 26 | struct Person { 27 | char name[NAME_SIZE]; 28 | double score; 29 | Date testTaken; 30 | }; 31 | 32 | Person *readTesters(const string &fileName, int &num); 33 | void writeTesters(const string &fileName, Person people[], int numP); 34 | 35 | //***************************************************************************************************** 36 | 37 | int main() { 38 | int numP = 0; 39 | string fileName; 40 | Person *people = nullptr; 41 | 42 | people = readTesters("Testers.txt", numP); // read data from text file 43 | writeTesters("Testers.dat", people, numP); // write data to binary file 44 | 45 | delete[] people; 46 | people = nullptr; 47 | 48 | return 0; 49 | } 50 | 51 | //***************************************************************************************************** 52 | 53 | Person *readTesters(const string &fileName, int &num) { 54 | Person *p = nullptr; 55 | ifstream f(fileName); 56 | 57 | if (f.is_open()) { 58 | f >> num; 59 | f.ignore(); 60 | 61 | p = new Person[num]; 62 | 63 | for (int i = 0; i < num; ++i) { 64 | f.getline(p[i].name, NAME_SIZE); 65 | f >> p[i].score; 66 | f.ignore(); 67 | f >> p[i].testTaken.month; 68 | f.ignore(); 69 | f >> p[i].testTaken.day; 70 | f.ignore(); 71 | f >> p[i].testTaken.year; 72 | f.ignore(); 73 | } 74 | 75 | f.close(); 76 | } else { 77 | cerr << "Error: Unable to open file\n"; 78 | } 79 | 80 | return p; 81 | } 82 | 83 | //***************************************************************************************************** 84 | 85 | void writeTesters(const string &fileName, Person people[], int numP) { 86 | ofstream f(fileName, ios::binary); // open file for writing in binary mode 87 | f.write(reinterpret_cast(&numP), sizeof(int)); 88 | f.write(reinterpret_cast(people), sizeof(Person) * numP); 89 | f.close(); 90 | } 91 | 92 | //***************************************************************************************************** 93 | /* 94 | 95 | reinterpret_cast(expression) 96 | 97 | C++ type cast operator used to cast an expression to a different type, regardless of the relationship 98 | between the original type and the new type. 99 | 100 | ***************************************************************************************************** 101 | 102 | f.write(reinterpret_cast(&numP), sizeof(int)); 103 | 104 | (&numP) - casts it to a char* pointer, allowing it to be written to the binary file 105 | sizeof(int) - specifies the size of the data being written is an int type 106 | 107 | ***************************************************************************************************** 108 | 109 | f.write(reinterpret_cast(people), sizeof(Person) * numP); 110 | 111 | (people) - casts it to a char* pointer, allowing it to be written to the binary file. 112 | sizeof(Person) * numP - specifies the size of the data being written is an array of Person structs with 113 | numP number of elements. 114 | 115 | */ -------------------------------------------------------------------------------- /programming-II/11-classes-headerFiles/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | # 3 | # Makefile for c++ projects with multiple source files and headers 4 | # 5 | # Commands: 6 | # - make 7 | # - make run 8 | # - make clean 9 | # 10 | #**************************************************************************** 11 | 12 | CXX = g++ 13 | CXXFLAGS = -Wall -std=c++17 14 | TARGET = main.exe 15 | SOURCES = $(wildcard *.cpp) 16 | HEADERS = $(wildcard *.h) 17 | OBJECTS = $(SOURCES:.cpp=.o) 18 | 19 | all: $(TARGET) 20 | 21 | $(TARGET): $(OBJECTS) 22 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) 23 | 24 | %.o: %.cpp $(HEADERS) 25 | $(CXX) $(CXXFLAGS) -c $< -o $@ 26 | 27 | run: $(TARGET) 28 | ./$(TARGET) 29 | 30 | clean: 31 | rm -f $(TARGET) $(OBJECTS) -------------------------------------------------------------------------------- /programming-II/11-classes-headerFiles/date.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Date Class Implementation File 3 | // 4 | // This class implementation file defines the methods (member functions) of the Date class. 5 | // 6 | // Other files required: 7 | // 1. date.h - header file for Date class 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include "date.h" 12 | #include 13 | 14 | //***************************************************************************************************** 15 | // default constructor initializes member variables 16 | 17 | Date::Date() { 18 | day = 1; 19 | month = 1; 20 | year = 2000; 21 | } 22 | 23 | //***************************************************************************************************** 24 | 25 | Date::~Date() { 26 | std::cout << "\nDate object destroyed\n"; 27 | } 28 | 29 | //***************************************************************************************************** 30 | // checks for valid date (leap years, days in month) and sets date 31 | 32 | void Date::inputDate() { 33 | std::cout << "\nEnter Year: "; 34 | std::cin >> year; 35 | 36 | while (year < 2022 || year > 2100) { 37 | std::cerr << "\nInvalid\n\n"; 38 | std::cout << "Enter Year(2022- ): "; 39 | std::cin >> year; 40 | } 41 | 42 | setYear(year); 43 | 44 | std::cout << "Enter Month: "; 45 | std::cin >> month; 46 | 47 | while (month < 1 || month > 12) { 48 | std::cerr << "\nInvalid\n\n"; 49 | std::cout << "Enter Month(1-12): "; 50 | std::cin >> month; 51 | } 52 | 53 | setMonth(month); 54 | 55 | std::cout << "Enter Day: "; 56 | std::cin >> day; 57 | 58 | if (month == 1 || month == 3 || month == 5 || month == 7 || 59 | month == 8 || month == 10 || month == 12) { 60 | while (day < 1 || day > 31) { 61 | std::cerr << "\nInvalid\n\n"; 62 | std::cout << "Enter Day(1-31): "; 63 | std::cin >> day; 64 | } 65 | } else if (month == 4 || month == 6 || month == 9 || month == 11) { 66 | while (day < 1 || day > 30) { 67 | std::cerr << "\nInvalid\n\n"; 68 | std::cout << "Enter Day(1-30): "; 69 | std::cin >> day; 70 | } 71 | } else if (year % 4 == 0) { // February :: tests leap years 72 | while (day < 1 || day > 29) { 73 | std::cerr << "\nInvalid\n\n"; 74 | std::cout << "Enter Day(1-29): "; 75 | std::cin >> day; 76 | } 77 | } else { 78 | while (day < 1 || day > 28) { 79 | std::cerr << "\nInvalid\n\n"; 80 | std::cout << "Enter Day(1-28): "; 81 | std::cin >> day; 82 | } 83 | } 84 | 85 | setDay(day); 86 | } 87 | 88 | //***************************************************************************************************** 89 | 90 | void Date::displayDate() { 91 | std::cout << getMonth() << "/" << getDay() << "/" << getYear(); 92 | } 93 | 94 | //***************************************************************************************************** -------------------------------------------------------------------------------- /programming-II/11-classes-headerFiles/date.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Date class header file 3 | // 4 | // This header file defines the private and public members and methods of the Date class and 5 | // defines the inline functions (getters and setters). 6 | // 7 | //***************************************************************************************************** 8 | 9 | #ifndef DATE_H 10 | #define DATE_H 11 | 12 | //***************************************************************************************************** 13 | 14 | class Date { 15 | private: // private can only be accessed within the class 16 | int day; 17 | int month; 18 | int year; 19 | 20 | public: // public can be accessed within and outside the class 21 | Date(); // constructor 22 | ~Date(); // destructor 23 | int getDay() const; 24 | void setDay(const int &d); 25 | int getMonth() const; 26 | void setMonth(const int &m); 27 | int getYear() const; 28 | void setYear(const int &y); 29 | void inputDate(); 30 | void displayDate(); 31 | }; 32 | 33 | //***************************************************************************************************** 34 | 35 | inline int Date::getDay() const { // inline is a keyword for simple methods 36 | return day; 37 | } 38 | 39 | //***************************************************************************************************** 40 | 41 | inline void Date::setDay(const int &d) { 42 | day = d; 43 | } 44 | 45 | //***************************************************************************************************** 46 | 47 | inline int Date::getYear() const { 48 | return year; 49 | } 50 | 51 | //***************************************************************************************************** 52 | 53 | inline void Date::setYear(const int &y) { 54 | year = y; 55 | } 56 | 57 | //***************************************************************************************************** 58 | 59 | inline int Date::getMonth() const { 60 | return month; 61 | } 62 | 63 | //***************************************************************************************************** 64 | 65 | inline void Date::setMonth(const int &m) { 66 | month = m; 67 | } 68 | 69 | //***************************************************************************************************** 70 | 71 | #endif -------------------------------------------------------------------------------- /programming-II/11-classes-headerFiles/sport.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Sport Class Implementation File 3 | // 4 | // This class implementation file defines the methods (member functions) of the Sport class. 5 | // 6 | // Other files required: 7 | // 1. sport.h - header file for Sport class 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include "sport.h" 12 | #include 13 | #include 14 | #include 15 | 16 | //***************************************************************************************************** 17 | 18 | Sport::Sport(const std::string &n) { 19 | setName(n); 20 | numTeams = 0; 21 | teamNames = nullptr; 22 | } 23 | 24 | //***************************************************************************************************** 25 | 26 | Sport::~Sport() { 27 | delete[] teamNames; // deallocate memory in destructor 28 | std::cout << "\nSport object destroyed\n"; 29 | } 30 | 31 | //***************************************************************************************************** 32 | 33 | void Sport::display() const { 34 | Date d1 = getDate(); // sport.h includes Date.h so Date class is available to Sport class 35 | 36 | std::cout << "\t\t" << std::setfill('.') << std::setw(30) << std::left 37 | << "Sport Name " 38 | << " " << getName() 39 | << "\n\t\t" << std::setw(30) << "Scheduled Date (M/D/YY) " 40 | << " "; 41 | 42 | d1.displayDate(); 43 | 44 | std::cout << "\n\n\t\t" << std::setfill('.') << std::setw(30) << std::left 45 | << "Number of Teams " 46 | << " " << getNumTeams() << std::endl; 47 | 48 | for (int i = 0; i < getNumTeams(); ++i) 49 | std::cout << "\t\tTeam " << i + 1 << std::setw(24) << " " 50 | << " " << teamNames[i] << std::endl; 51 | } 52 | 53 | //***************************************************************************************************** 54 | 55 | void Sport::populate() { 56 | char entry; 57 | 58 | std::cout << "\nEnter the name of the sport: "; 59 | std::getline(std::cin, name); 60 | 61 | setName(name); 62 | 63 | std::cout << "Sport has a scheduled game? (Y/N)" << std::endl; 64 | std::cin >> entry; 65 | 66 | if (entry == 'Y' || entry == 'y') { 67 | std::cout << "\nNext Scheduled Game"; 68 | nextGame.inputDate(); 69 | setDate(nextGame); 70 | } else { 71 | std::cout << "\nDefault date will be set to January 1, 2000" << std::endl; 72 | } 73 | 74 | std::cout << "\nEnter the number of teams: "; 75 | std::cin >> numTeams; 76 | 77 | setNumTeams(numTeams); 78 | teamNames = new std::string[numTeams]; 79 | 80 | std::cin.ignore(); 81 | 82 | for (int i = 0; i < numTeams; ++i) { 83 | std::cout << "Enter the name of team " << i + 1 << ": "; 84 | std::getline(std::cin, teamNames[i]); 85 | } 86 | } 87 | 88 | //***************************************************************************************************** 89 | 90 | void Sport::addTeam(const std::string &n) { 91 | std::string *temp = new std::string[numTeams + 1]; 92 | 93 | for (int i = 0; i < numTeams; ++i) 94 | temp[i] = teamNames[i]; 95 | 96 | temp[numTeams] = n; 97 | delete[] teamNames; 98 | teamNames = temp; 99 | numTeams++; 100 | } 101 | 102 | //***************************************************************************************************** 103 | /* 104 | 105 | numTeams++; 106 | string* tempTNames = new string[numTeams]; 107 | 108 | for (int i = 0; i < numTeams - 1; i++) 109 | tempTNames[i] = teamNames[i]; 110 | 111 | tempTNames[numTeams - 1] = n; 112 | teamNames = new string[numTeams]; 113 | teamNames = tempTNames; 114 | 115 | */ -------------------------------------------------------------------------------- /programming-II/11-classes-headerFiles/sport.h: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // Sport Class Header File 3 | // 4 | // This header file defines the private and public members and methods of the Sport class and 5 | // defines the inline functions (getters and setters). 6 | // 7 | // Other files required: 8 | // 1. date.h - header file for Date class 9 | // 10 | //***************************************************************************************************** 11 | 12 | #ifndef SPORT_H 13 | #define SPORT_H 14 | 15 | //***************************************************************************************************** 16 | 17 | #include "date.h" 18 | #include 19 | 20 | //***************************************************************************************************** 21 | 22 | class Sport { 23 | private: 24 | std::string name; 25 | Date nextGame; // use Date class to store date of next game 26 | int numTeams; 27 | std::string *teamNames; 28 | 29 | public: 30 | Sport(const std::string &n = ""); // default argument so that Sport object can be created without arguments 31 | ~Sport(); 32 | std::string getName() const; // const used when methods do not change the object's data members 33 | void setName(const std::string &n); 34 | Date getDate() const; 35 | void setDate(const Date &d); 36 | int getNumTeams() const; 37 | void setNumTeams(const int &n); 38 | void display() const; 39 | void populate(); 40 | void addTeam(const std::string &n); 41 | }; 42 | 43 | //***************************************************************************************************** 44 | 45 | inline std::string Sport::getName() const { 46 | return name; 47 | } 48 | 49 | //***************************************************************************************************** 50 | 51 | inline void Sport::setName(const std::string &n) { 52 | name = n; 53 | } 54 | 55 | //***************************************************************************************************** 56 | 57 | inline Date Sport::getDate() const { 58 | return nextGame; 59 | } 60 | 61 | //***************************************************************************************************** 62 | 63 | inline void Sport::setDate(const Date &d) { 64 | nextGame = d; 65 | } 66 | 67 | //***************************************************************************************************** 68 | inline int Sport::getNumTeams() const { 69 | return numTeams; 70 | } 71 | 72 | //***************************************************************************************************** 73 | 74 | inline void Sport::setNumTeams(const int &n) { 75 | numTeams = n; 76 | } 77 | 78 | //***************************************************************************************************** 79 | 80 | #endif -------------------------------------------------------------------------------- /programming-II/docs/examples/CString.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-II/docs/examples/CString.pdf -------------------------------------------------------------------------------- /programming-II/docs/examples/Char Functions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-II/docs/examples/Char Functions.pdf -------------------------------------------------------------------------------- /programming-II/docs/examples/Search.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-II/docs/examples/Search.pdf -------------------------------------------------------------------------------- /programming-II/docs/examples/Sort.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-II/docs/examples/Sort.pdf -------------------------------------------------------------------------------- /programming-II/docs/exercises/Pointer Exercise 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-II/docs/exercises/Pointer Exercise 1.pdf -------------------------------------------------------------------------------- /programming-II/docs/exercises/Pointer Exercise 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-II/docs/exercises/Pointer Exercise 2.pdf -------------------------------------------------------------------------------- /programming-II/docs/exercises/Pointer Exercise 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-II/docs/exercises/Pointer Exercise 3.pdf -------------------------------------------------------------------------------- /programming-II/docs/exercises/Pointers Introduction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-II/docs/exercises/Pointers Introduction.pdf -------------------------------------------------------------------------------- /programming-II/docs/exercises/String Exercise .pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nragland37/cpp-projects/52f6e37da9720878001ad9c61bd33f17ab1c82f6/programming-II/docs/exercises/String Exercise .pdf -------------------------------------------------------------------------------- /programming-II/docs/header-template.cpp: -------------------------------------------------------------------------------- 1 | //***************************************************************************************************** 2 | // 3 | // This program... 4 | // 5 | // Other files required: 6 | // 1. example.h - header file for the example class 7 | // 2. example.txt - a text file containing the example data 8 | // 9 | //***************************************************************************************************** 10 | 11 | #include 12 | using namespace std; 13 | 14 | //***************************************************************************************************** 15 | 16 | int main() { 17 | 18 | return 0; 19 | } 20 | 21 | //***************************************************************************************************** 22 | /* 23 | 24 | // attach sample output here. 25 | 26 | */ --------------------------------------------------------------------------------