├── .gitignore ├── mainimage.png ├── .vscode └── settings.json ├── .github └── FUNDING.yml ├── LICENSE ├── CONTRIBUTING.md ├── README.md ├── Tree.h ├── Numbers.h ├── ArrayList.h ├── LinkedList.h └── String.h /.gitignore: -------------------------------------------------------------------------------- 1 | sample.cpp 2 | llsample.cpp -------------------------------------------------------------------------------- /mainimage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smv1999/custom-header-files-cpp/HEAD/mainimage.png -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "string": "cpp", 4 | "optional": "cpp", 5 | "random": "cpp", 6 | "limits": "cpp", 7 | "memory": "cpp", 8 | "new": "cpp", 9 | "numeric": "cpp", 10 | "*.tcc": "cpp", 11 | "cmath": "cpp", 12 | "utility": "cpp" 13 | } 14 | } -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ["9600064291@paytm"] 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Vaidhyanathan S M 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 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing Guidelines 2 | 3 | Developers are highly welcome to contribute to this project by adding new features or filing bug reports. 4 | 5 | 1. Fork this repository 6 | 1. Clone the repo ```git clone ``` 7 | 1. CD into the specific directory. Create a new branch of the master ```git branch ``` 8 | 1. You can check which branch you are in, using ```git branch``` . Now checkout to the new branch created. ```git checkout ``` 9 | 1. When you are done coding, stage the changes by using the command ```git add``` . 10 | 1. Commit the changes made by you using the command ```git commit -m ""```. Give an appropriate message explaining the changes you made. 11 | 1. Push the changes using ```git push ``` 12 | 1. After you push the changes head over to the forked repo and a ```Compare & pull request``` button will appear. 13 | 1. Click on that button and you will be taken to a page where you can create a pull request. After you have submitted the PR, I will review your work and approve 14 | the PR and merge it with the master branch. 15 | 16 | *Note : **Contributions in any form (Code, Documentation, etc) submitted should be the own work of the contributor and plagiarism is highly discouraged.*** 17 | 18 | Happy Coding!!! 19 | 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Custom Utility Header Files 2 | 3 | This is a repository that contains Custom Utility Header Files created from scratch in C++ for performing various operations on Data Structures. 4 | 5 | ![GitHub followers](https://img.shields.io/github/followers/smv1999?style=for-the-badge) 6 | ![GitHub forks](https://img.shields.io/github/forks/smv1999/custom-header-files-cpp?style=for-the-badge) 7 | ![GitHub Repo stars](https://img.shields.io/github/stars/smv1999/custom-header-files-cpp?style=for-the-badge) 8 | ![Lines of code](https://img.shields.io/tokei/lines/github/smv1999/custom-header-files-cpp?style=for-the-badge) 9 | 10 | ![custom-header-files-cpp](https://socialify.git.ci/smv1999/custom-header-files-cpp/image?font=Source%20Code%20Pro&forks=1&issues=1&language=1&owner=1&pattern=Brick%20Wall&pulls=1&stargazers=1&theme=Dark) 11 | 12 | ## Want to Contribute to this repository? 13 | Head over to [Contributing Guidelines](https://github.com/smv1999/custom-header-files-cpp/blob/master/CONTRIBUTING.md) to know more! 14 | 15 | ## ❤️ Project Admin 16 | 17 | | | 18 | | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | 19 | | **[Vaidhyanathan S M](https://www.linkedin.com/in/vaidhyanathansm/)** | 20 | 21 | *Need help? Feel free to contact me @ vaidhyanathan.sm@gmail.com* 22 | -------------------------------------------------------------------------------- /Tree.h: -------------------------------------------------------------------------------- 1 | 2 | // This header file is under development 3 | 4 | #include 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *left; 10 | Node *right; 11 | }; 12 | class Tree : public Node 13 | { 14 | Node *root = NULL; 15 | void inOrder(); 16 | void preOrder(Node *root, std::vector&); 17 | void postOrder(Node *root, std::vector&); 18 | 19 | public: 20 | Tree(int data) 21 | { 22 | this->root = new Node(); 23 | this->root->data = data; 24 | this->root->left = NULL; 25 | this->root->right = NULL; 26 | } 27 | ~Tree(); 28 | void addNode(int data); 29 | void getInOrderTraversal(); 30 | std::vector getPreOrderTraversal(); 31 | std::vector getPostOrderTraversal(); 32 | 33 | }; 34 | void Tree::inOrder() 35 | { 36 | if(this->root == NULL) 37 | return 38 | this->root->left->inOrder(); 39 | std::cout<data<<" "; 40 | this->root->left->inOrder(); 41 | } 42 | /* 43 | void Tree::preOrder(Node *root, std::vector &process) 44 | { 45 | if(!root) 46 | return 47 | process.push_back(root->data); 48 | inOrder(root->left, process); 49 | inOrder(root->right, process); 50 | } 51 | void Tree::postOrder(Node *root, std::vector &process) 52 | { 53 | if(!root) 54 | return 55 | inOrder(root->left, process); 56 | inOrder(root->right, process); 57 | process.push_back(root->data); 58 | } 59 | */ 60 | void Tree::getInOrderTraversal() 61 | { 62 | if(this->root == NULL) 63 | return; 64 | inOrder(this->root); 65 | 66 | } 67 | void Tree::addNode(int data) 68 | { 69 | if(!(this->root)) 70 | { 71 | this->root = new Node(); 72 | this->root->data = data; 73 | this->root->left = NULL; 74 | this->root->right = NULL; 75 | return; 76 | } 77 | Node *new_node = new Node(); 78 | new_node->data = data; 79 | new_node->left = NULL; 80 | new_node->right = NULL; 81 | 82 | std::queue store; 83 | store.push(this->root); 84 | 85 | while(!store.empty()) 86 | { 87 | Node *temp = store.front(); 88 | std::cout<data<<" "; 89 | store.pop(); 90 | 91 | if (temp->left != NULL) 92 | store.push(temp->left); 93 | else 94 | { 95 | temp->left = new_node; 96 | return; 97 | } 98 | 99 | if (temp->right != NULL) 100 | store.push(temp->right); 101 | else 102 | { 103 | temp->right = new_node; 104 | return; 105 | } 106 | } 107 | } 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Numbers.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "ArrayList.h" 7 | typedef long long int LLI; 8 | 9 | LLI reverse(LLI num); 10 | int sumOfDigits(LLI num); 11 | bool isPrime(LLI num); 12 | int gcd(LLI num1, LLI num2); 13 | int lcm(LLI num1, LLI num2); 14 | int max(int num1, int num2); 15 | int min(int num1, int num2); 16 | std::vector getFactors(int num); 17 | template 18 | std::string type(T ele); 19 | /* 20 | Reverse the digits of the number 21 | */ 22 | LLI reverse(LLI num) 23 | { 24 | LLI rev = 0; 25 | while (num > 0) 26 | { 27 | rev = (rev * 10) + (num % 10); 28 | num /= 10; 29 | } 30 | return rev; 31 | } 32 | 33 | /* 34 | Sum of the digits of the number 35 | */ 36 | int sumOfDigits(LLI num) 37 | { 38 | int sum = 0; 39 | while (num > 0) 40 | { 41 | sum += (num % 10); 42 | num /= 10; 43 | } 44 | return sum; 45 | } 46 | 47 | /* 48 | Checking if the given number is prime or not 49 | */ 50 | bool isPrime(LLI num) 51 | { 52 | int processNum; 53 | for (processNum = 2; processNum <= sqrt(num); processNum++) 54 | { 55 | if (num % processNum == 0) 56 | return false; 57 | } 58 | return true; 59 | } 60 | 61 | /* 62 | Returns the Greatest Common Divisor of the given number 63 | */ 64 | int gcd(LLI num1, LLI num2) 65 | { 66 | if (num2 == 0) 67 | return num1; 68 | else 69 | gcd(num2, num1 % num2); 70 | } 71 | 72 | /* 73 | Returns the Least Common Multiple of a given number 74 | */ 75 | int lcm(LLI num1, LLI num2) 76 | { 77 | return (num1 * num2) / gcd(num1, num2); 78 | } 79 | 80 | /* 81 | Returns the max element among the two 82 | */ 83 | int max(int num1, int num2) 84 | { 85 | return ((num1>num2)?(num1):(num2)); 86 | } 87 | 88 | /* 89 | Returns the min element among the two 90 | */ 91 | int min(int num1, int num2) 92 | { 93 | return ((num1 100 | std::string type(T ele) 101 | { 102 | 103 | if(std::is_integral::value) 104 | return "int"; 105 | else if(std::is_floating_point::value) 106 | return "float"; 107 | else if(std::is_array::value) 108 | return "array"; 109 | else if(std::is_class::value) 110 | return "class"; 111 | else if(std::is_function::value) 112 | return "function"; 113 | else if(std::is_pointer::value) 114 | return "pointer"; 115 | } 116 | 117 | std::vector getFactors(int num) 118 | { 119 | std::vector res; 120 | for(int i=1;i<=sqrt(num);i++) 121 | { 122 | if(num % i == 0) 123 | { 124 | if(num/i == i) 125 | res.push_back(i); 126 | else{ 127 | res.push_back(i); 128 | res.push_back(num/i); 129 | } 130 | } 131 | } 132 | return res; 133 | } 134 | -------------------------------------------------------------------------------- /ArrayList.h: -------------------------------------------------------------------------------- 1 | #include 2 | class ArrayList 3 | { 4 | private: 5 | int *arr; 6 | int size; 7 | int ind; 8 | int partition(int low, int high); 9 | int lcmUtility(int num1, int num2); 10 | int gcdUtility(int num1, int num2); 11 | void swap(int *num1, int *num2); 12 | void sorter(int low, int high); 13 | public: 14 | ArrayList(){ 15 | this->size = 0; 16 | this->arr = new int[this->size]; 17 | this->ind = 0; 18 | } 19 | int sum(); 20 | void add(int data); 21 | void reverse(); 22 | int search(int k); 23 | void sort(); 24 | int min(); 25 | int max(); 26 | int frequency(int k); 27 | int lcmOfArray(); 28 | int gcdOfArray(); 29 | void printArray(); 30 | bool remove(int data_to_remove); 31 | 32 | }; 33 | 34 | /* 35 | Finding the sum of the elements in the array 36 | Returns the sum of the array 37 | */ 38 | int ArrayList::sum() 39 | { 40 | int array_sum = 0, index; 41 | for (index = 0; index < this->size; index++) 42 | array_sum += this->arr[index]; 43 | return array_sum; 44 | } 45 | 46 | /* 47 | Reverse the array 48 | The Reverse operation happens in place 49 | */ 50 | void ArrayList::reverse() 51 | { 52 | int index, safe; 53 | for (index = 0; index < this->size / 2; index++) 54 | { 55 | safe = this->arr[index]; 56 | this->arr[index] = this->arr[(this->size) - index - 1]; 57 | this->arr[(this->size) - index - 1] = safe; 58 | } 59 | } 60 | /* 61 | Add a new item to the ArrayList 62 | */ 63 | void ArrayList::add(int data) 64 | { 65 | this->size++; 66 | this->arr[this->ind++] = data; 67 | } 68 | /* 69 | Prints the array 70 | */ 71 | void ArrayList::printArray() 72 | { 73 | for(int tempInd = 0; tempIndsize ; tempInd++) 74 | std::cout<arr[tempInd]<<" "; 75 | std::cout<ind; tempInd++) 84 | { 85 | if(this->arr[tempInd] == data_to_remove){ 86 | break; 87 | } 88 | } 89 | if(tempInd < this->size){ 90 | (this->size)--; 91 | for(int delInd = tempInd; delIndsize; delInd++ ){ 92 | this->arr[delInd] = this->arr[delInd+1]; 93 | } 94 | return true; 95 | } 96 | return false; 97 | } 98 | /* 99 | searching for an element in the array 100 | Returns the index where the element is found, returns -1 if not found 101 | */ 102 | int ArrayList::search(int k) 103 | { 104 | int first = 0; 105 | int last = this->size - 1; 106 | while (first <= last) 107 | { 108 | int mid = (first + last) / 2; 109 | if (arr[mid] == k) 110 | return mid; 111 | else if (this->arr[mid] > k) 112 | last = mid - 1; 113 | else if (this->arr[mid] < k) 114 | first = mid + 1; 115 | } 116 | return -1; 117 | } 118 | 119 | /* 120 | Swaps two elements 121 | */ 122 | void ArrayList::swap(int *num1, int *num2) 123 | { 124 | int safe; 125 | safe = *num1; 126 | *num1 = *num2; 127 | *num2 = safe; 128 | } 129 | /* 130 | Helper function for sorting 131 | Partitions according to the pivot element 132 | */ 133 | int ArrayList::partition(int low, int high) 134 | { 135 | int pivot = this->arr[high]; 136 | int i = (low - 1); 137 | 138 | for (int j = low; j <= high - 1; j++) 139 | { 140 | if (this->arr[j] <= pivot) 141 | { 142 | i++; 143 | swap(&(this->arr[i]), &(this->arr[j])); 144 | } 145 | } 146 | swap(&(this->arr[i + 1]), &(this->arr[high])); 147 | return (i + 1); 148 | } 149 | 150 | /* 151 | Sorts the array 152 | */ 153 | void ArrayList::sorter(int low, int high) 154 | { 155 | if (low < high) 156 | { 157 | int pos = partition(low, high); 158 | 159 | sorter(low, pos - 1); 160 | sorter(pos + 1, high); 161 | } 162 | } 163 | 164 | void ArrayList::sort() 165 | { 166 | sorter(0, this->size - 1); 167 | } 168 | 169 | /* 170 | Returns the minimum element from the array 171 | */ 172 | int ArrayList::min() 173 | { 174 | int minimum = INT_MAX, index; 175 | for (index = 0; index < this->size; index++) 176 | { 177 | if (this->arr[index] < minimum) 178 | minimum = this->arr[index]; 179 | } 180 | return minimum; 181 | } 182 | 183 | /* 184 | Returns the maximum element from the array 185 | */ 186 | int ArrayList::max() 187 | { 188 | int maximum = INT_MIN, index; 189 | for (index = 0; index < this->size; index++) 190 | { 191 | if (this->arr[index] > maximum) 192 | maximum = this->arr[index]; 193 | } 194 | return maximum; 195 | } 196 | 197 | /* 198 | Returns the frequency of an element in an array 199 | */ 200 | int ArrayList::frequency(int k) 201 | { 202 | int count = 0, index; 203 | for (index = 0; index < this->size; index++) 204 | { 205 | if (this->arr[index] == k) 206 | count++; 207 | } 208 | return count; 209 | } 210 | 211 | /* 212 | Returns the HCF of two numbers 213 | */ 214 | int ArrayList::gcdUtility(int num1, int num2) 215 | { 216 | if (num2 == 0) 217 | return num1; 218 | else 219 | gcdUtility(num2, num1 % num2); 220 | } 221 | 222 | /* 223 | Returns the LCM of two numbers 224 | */ 225 | int ArrayList::lcmUtility(int num1, int num2) 226 | { 227 | return (num1 * num2) / gcdUtility(num1, num2); 228 | } 229 | 230 | /* 231 | Returns the HCF of the entire array 232 | */ 233 | int ArrayList::gcdOfArray() 234 | { 235 | int safe = gcdUtility(this->arr[0], this->arr[1]), index; 236 | for (index = 2; index < this->size; index++) 237 | { 238 | safe = gcdUtility(this->arr[index], safe); 239 | } 240 | return safe; 241 | } 242 | 243 | /* 244 | Returns the LCM of the entire array 245 | */ 246 | int ArrayList::lcmOfArray() 247 | { 248 | int safe = lcmUtility(this->arr[0], this->arr[1]), index; 249 | for (index = 2; index < this->size; index++) 250 | { 251 | safe = lcmUtility(this->arr[index], safe); 252 | } 253 | return safe; 254 | } 255 | 256 | 257 | 258 | -------------------------------------------------------------------------------- /LinkedList.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long int LLI; 4 | 5 | struct array 6 | { 7 | int size; 8 | int *arr; 9 | }; 10 | 11 | class Node 12 | { 13 | public: 14 | int data; 15 | Node *next; 16 | }; 17 | 18 | class LinkedList : public Node 19 | { 20 | private: 21 | /* data */ 22 | Node *head; 23 | 24 | public: 25 | LinkedList(); 26 | ~LinkedList(); 27 | void add(int new_data); 28 | void add(int pos, int new_data); 29 | void clear(); 30 | bool contains(int data); 31 | LLI get(int index); 32 | LLI getFirst(); 33 | LLI getLast(); 34 | LLI getMiddle(); 35 | int printList(); 36 | void removeFirst(); 37 | void removeLast(); 38 | void removeAt(int pos); 39 | void reverse(); 40 | int size(); 41 | array toArray(); 42 | std::string toString(); 43 | }; 44 | 45 | LinkedList::LinkedList() 46 | { 47 | this->head = NULL; 48 | } 49 | 50 | LinkedList::~LinkedList() 51 | { 52 | if(!this->head) 53 | free(this->head); 54 | } 55 | 56 | /* 57 | Inserts the element at the last(append) of the list 58 | */ 59 | void LinkedList::add(int new_data) 60 | { 61 | Node *new_node = new Node(); 62 | new_node->data = new_data; 63 | if(this->head==NULL) 64 | this->head = new_node; 65 | else{ 66 | Node *temp; 67 | temp = this->head; 68 | while(temp->next!=NULL) 69 | { 70 | temp = temp->next; 71 | } 72 | temp->next = new_node; 73 | new_node->next = NULL; 74 | } 75 | } 76 | 77 | /* 78 | Inserts at last when the required position is larger than the linked list size, otherwise inserts at 79 | the required position 80 | */ 81 | void LinkedList::add(int pos, int new_data) 82 | { 83 | Node *new_node = new Node(); 84 | new_node->data = new_data; 85 | int ctr=0; 86 | if(this->head == NULL) 87 | this->head = new_node; 88 | else{ 89 | if(pos==1) 90 | { 91 | new_node->next = this->head; 92 | this->head = new_node; 93 | } 94 | else{ 95 | Node *temp; 96 | temp = this->head; 97 | while(temp->next!=NULL) 98 | { 99 | ctr++; 100 | if(ctr == (pos-1)) 101 | { 102 | new_node->next = temp->next; 103 | temp->next = new_node; 104 | } 105 | temp = temp->next; 106 | } 107 | if(pos >= ctr+1) 108 | { 109 | add(new_data); 110 | } 111 | } 112 | } 113 | } 114 | 115 | /* 116 | Deletes the entire linked list 117 | */ 118 | void LinkedList::clear() 119 | { 120 | Node *temp, *next_node; 121 | temp = this->head; 122 | while(temp!=NULL) 123 | { 124 | next_node = temp->next; 125 | free(temp); 126 | temp = next_node; 127 | } 128 | this->head = NULL; 129 | } 130 | 131 | 132 | /* 133 | Returns true if the list contains the specified element 134 | */ 135 | bool LinkedList::contains(int data) 136 | { 137 | Node *temp; 138 | temp = this->head; 139 | while(temp!=NULL) 140 | { 141 | if(temp->data == data) 142 | return true; 143 | temp = temp->next; 144 | } 145 | return false; 146 | } 147 | 148 | /* 149 | Returns the element at the specified position 150 | */ 151 | LLI LinkedList::get(int index) 152 | { 153 | int ctr=0; 154 | Node *temp; 155 | temp = this->head; 156 | while(temp!=NULL) 157 | { 158 | ctr++; 159 | if(ctr==index) 160 | return temp->data; 161 | temp = temp->next; 162 | } 163 | return -1; 164 | } 165 | 166 | /* 167 | Returns the First element of the list 168 | */ 169 | LLI LinkedList::getFirst() 170 | { 171 | if(!this->head) return -1; 172 | return this->head->data; 173 | } 174 | 175 | /* 176 | Returns the Last element of the list 177 | */ 178 | LLI LinkedList::getLast() 179 | { 180 | Node *temp; 181 | temp = head; 182 | while(temp->next!=NULL) 183 | temp = temp->next; 184 | return temp->data; 185 | } 186 | 187 | /* 188 | Returns the middle element of the linked list 189 | */ 190 | LLI LinkedList::getMiddle() 191 | { 192 | Node *first=NULL, *second=NULL, *temp; 193 | temp = this->head; 194 | if(!temp) return -1; 195 | first = second = temp; 196 | while(second && second->next) 197 | { 198 | first = first->next; 199 | second = second->next; 200 | if(second) 201 | second = second->next; 202 | } 203 | 204 | return first->data; 205 | } 206 | 207 | 208 | /* 209 | prints the data in the linked list 210 | */ 211 | int LinkedList::printList() 212 | { 213 | Node *temp; 214 | int ctr = 0; 215 | temp = this->head; 216 | if(temp==NULL) 217 | std::cout<<"Error: Nothing to print. LinkedList is empty!\n"; 218 | else{ 219 | while(temp!=NULL) 220 | { 221 | std::cout<data<<" "; 222 | ctr++; 223 | temp = temp->next; 224 | } 225 | } 226 | return ctr; 227 | } 228 | 229 | /* 230 | Removes the first element of the linked list 231 | */ 232 | void LinkedList::removeFirst() 233 | { 234 | if(this->head==NULL) 235 | std::cout<<"Error: Nothing to delete. LinkedList is empty!\n"; 236 | else if((this->head)->next==NULL) 237 | { 238 | this->head = NULL; 239 | free(this->head); 240 | } 241 | else 242 | this->head = this->head->next; 243 | } 244 | 245 | /* 246 | Removes the last element of the linked list 247 | */ 248 | void LinkedList::removeLast() 249 | { 250 | Node *temp; 251 | temp = this->head; 252 | if(temp == NULL) 253 | std::cout<<"Error: Nothing to delete. LinkedList is empty!\n"; 254 | else if(temp->next == NULL) 255 | { 256 | temp = NULL; 257 | free(temp); 258 | } 259 | else{ 260 | while(temp->next->next!=NULL) 261 | { 262 | temp = temp->next; 263 | } 264 | free(temp->next); 265 | temp->next = NULL; 266 | } 267 | } 268 | 269 | /* 270 | Removes the last element when the specified position is larger than the linked list size, otherwise removes the 271 | element at the specified position 272 | */ 273 | void LinkedList::removeAt(int pos) 274 | { 275 | Node *temp, *store; 276 | temp = this->head; 277 | int ctr=0; 278 | if(temp==NULL) 279 | std::cout<<"Error: Nothing to delete. LinkedList is empty!\n"; 280 | else if(temp->next == NULL) 281 | { 282 | temp=NULL; 283 | free(temp); 284 | } 285 | else{ 286 | if(pos==1) 287 | { 288 | removeFirst(); 289 | } 290 | else{ 291 | while(temp!=NULL) 292 | { 293 | ctr++; 294 | if(ctr == (pos-1)) 295 | { 296 | store = temp->next->next; 297 | free(temp->next); 298 | temp->next = store; 299 | } 300 | temp = temp->next; 301 | } 302 | } 303 | } 304 | } 305 | 306 | /* 307 | Reverses the linked list 308 | */ 309 | void LinkedList::reverse() 310 | { 311 | Node *curr, *prev=NULL, *next=NULL; 312 | curr = this->head; 313 | while(curr!=NULL) 314 | { 315 | next = curr->next; 316 | curr->next = prev; 317 | prev = curr; 318 | curr = next; 319 | } 320 | this->head = prev; 321 | } 322 | 323 | /* 324 | Returns the size of the linked list 325 | */ 326 | int LinkedList::size() 327 | { 328 | Node *temp; 329 | temp = this->head; 330 | int ctr = 0; 331 | while(temp!=NULL) 332 | { 333 | ctr++; 334 | temp = temp->next; 335 | } 336 | return ctr; 337 | } 338 | 339 | /* 340 | Stores the elements of the linked list in an array and 341 | returns the array 342 | */ 343 | array LinkedList::toArray() 344 | { 345 | int *res_arr, res_arr_ind=0; 346 | array arrObj; 347 | Node *temp; 348 | temp = this->head; 349 | res_arr = (int *)malloc((this->size()) * sizeof(int)); 350 | while(temp!=NULL) 351 | { 352 | res_arr[res_arr_ind++] = temp->data; 353 | temp = temp->next; 354 | } 355 | arrObj.arr = res_arr; 356 | arrObj.size = this->size(); 357 | return arrObj; 358 | } 359 | 360 | /* 361 | Returns a string containing all of the elements in the list in 362 | proper sequence (from first to last), each element is separated 363 | by commas and the string is enclosed in square brackets 364 | */ 365 | std::string LinkedList::toString() 366 | { 367 | std::string resStr = "["; 368 | Node *temp; 369 | temp = this->head; 370 | while(temp!=NULL) 371 | { 372 | resStr = resStr + std::to_string(temp->data)+", "; 373 | temp = temp->next; 374 | } 375 | resStr += "]"; 376 | return resStr; 377 | } 378 | -------------------------------------------------------------------------------- /String.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | // Function prototypes 10 | bool contains(string s1, string s2); 11 | bool startswith(string s1, string s2, int start = 0, int end = 0); 12 | bool endswith(string s1, string s2, int start = 0, int end = 0); 13 | void capitalize(string &s); 14 | int count(string s1, string s2); 15 | int find(string s, char val); 16 | int find(string s, string val); 17 | int findLastOccurence(string s, char val); 18 | int findLastOccurence(string s, string val); 19 | bool isalnum(string s); 20 | bool isalpha(string s); 21 | bool isdigit(string s); 22 | bool islower(string s); 23 | bool isupper(string s); 24 | bool istitle(string s); 25 | void lower(string &s); 26 | void upper(string &s); 27 | vector split(string s, char c = ' '); 28 | void swapcase(string &s); 29 | void title(string &s); 30 | void replace(string &s, char c, char k); 31 | void replace(string &s, string c, string k); 32 | 33 | /* 34 | Returns true if the string s2 is contained in s1 35 | */ 36 | bool contains(string s1, string s2) 37 | { 38 | map mp1, mp2; 39 | for (char c : s2) 40 | { 41 | mp2[c]++; 42 | } 43 | 44 | for (char c : s1) 45 | { 46 | mp1[c]++; 47 | } 48 | 49 | for (auto it : mp2) 50 | { 51 | if (it.second > mp1[it.first]) 52 | { 53 | return false; 54 | } 55 | } 56 | 57 | return true; 58 | } 59 | 60 | /* 61 | end index is not inclusive 62 | */ 63 | bool startswith(string s1, string s2, int start = 0, int end = 0) 64 | { 65 | if (start != 0) 66 | { 67 | string tempStr = ""; 68 | for (int index = start; index < end; index++) 69 | tempStr += s1[index]; 70 | return contains(tempStr, s2); 71 | } 72 | for (int index = 0; index < s2.length(); index++) 73 | { 74 | if (s2[index] != s1[index]) 75 | { 76 | return false; 77 | } 78 | } 79 | return true; 80 | } 81 | 82 | /* 83 | end index is not inclusive 84 | */ 85 | bool endswith(string s1, string s2, int start = 0, int end = 0) 86 | { 87 | if (end != 0) 88 | { 89 | string tempStr = ""; 90 | for (int index = start; index < end; index++) 91 | tempStr += s1[index]; 92 | return startswith(tempStr, s2, tempStr.length() - s2.length(), s1.length() - 1); 93 | } 94 | // normal case 95 | return startswith(s1, s2, s1.length() - s2.length(), s1.length() - 1); 96 | } 97 | 98 | /* 99 | Capitalizes the string passed as argument 100 | */ 101 | void capitalize(string &s) 102 | { 103 | s[0] = toupper(s[0]); 104 | } 105 | 106 | /* 107 | Counts the number of times the string s2 occurs in s1 108 | */ 109 | int count(string s1, string s2) 110 | { 111 | map mp1, mp2; 112 | for (char c : s2) 113 | { 114 | mp2[c]++; 115 | } 116 | 117 | for (char c : s1) 118 | { 119 | mp1[c]++; 120 | } 121 | 122 | for (auto it : mp2) 123 | { 124 | if (mp1[it.first] % it.second == 0) 125 | { 126 | return mp1[it.first]; 127 | } 128 | } 129 | 130 | return 0; 131 | } 132 | 133 | /* 134 | Searches (first occurrence) the string for a specified value and returns the position of where it was found. 135 | Function Overloading 136 | */ 137 | int find(string s, char val) 138 | { 139 | for (int index = 0; index < s.length(); index++) 140 | { 141 | if (s[index] == val) 142 | { 143 | return index; 144 | } 145 | } 146 | return -1; 147 | } 148 | 149 | int find(string s, string val) 150 | { 151 | int searchIndex = 0, valLength = val.length(), firstIndex; 152 | for (int index = 0; index < s.length(); index++) 153 | { 154 | if (s[index] == val[0]) 155 | { 156 | firstIndex = index; 157 | break; 158 | } 159 | } 160 | 161 | for (int index = firstIndex; index < s.length(); index++) 162 | { 163 | if (s[index] == val[searchIndex]) 164 | { 165 | searchIndex++; 166 | if (searchIndex == valLength) 167 | return firstIndex; 168 | } 169 | } 170 | 171 | return -1; 172 | } 173 | 174 | /* 175 | Searches (last occurrence) the string for a specified value and returns the position of where it was found. 176 | Function Overloading 177 | */ 178 | int findLastOccurence(string s, char val) 179 | { 180 | int lastIndex = -1; 181 | for (int index = 0; index < s.length(); index++) 182 | { 183 | if (s[index] == val) 184 | { 185 | lastIndex = index; 186 | } 187 | } 188 | return lastIndex; 189 | } 190 | 191 | int findLastOccurence(string s, string val) 192 | { 193 | int searchIndex = 0, valLength = val.length(), lastIndex = -1; 194 | for (int index = 0; index < s.length(); index++) 195 | { 196 | if (s[index] == val[0]) 197 | { 198 | lastIndex = index; 199 | } 200 | } 201 | 202 | for (int index = lastIndex; index < s.length(); index++) 203 | { 204 | if (s[index] == val[searchIndex]) 205 | { 206 | searchIndex++; 207 | if (searchIndex == valLength) 208 | return lastIndex; 209 | } 210 | } 211 | 212 | return lastIndex; 213 | } 214 | 215 | /* 216 | Returns true if all the characters in the string are alphanumeric 217 | */ 218 | bool isalnum(string s) 219 | { 220 | for (int index = 0; index < s.length(); index++) 221 | { 222 | if (!isalnum(s[index])) 223 | return false; 224 | } 225 | return true; 226 | } 227 | 228 | bool isalpha(string s) 229 | { 230 | for (int index = 0; index < s.length(); index++) 231 | { 232 | if (!isalpha(s[index])) 233 | return false; 234 | } 235 | return true; 236 | } 237 | 238 | bool isdigit(string s) 239 | { 240 | for (int index = 0; index < s.length(); index++) 241 | { 242 | if (!isdigit(s[index])) 243 | return false; 244 | } 245 | return true; 246 | } 247 | 248 | bool islower(string s) 249 | { 250 | for (int index = 0; index < s.length(); index++) 251 | { 252 | if (!islower(s[index])) 253 | return false; 254 | } 255 | return true; 256 | } 257 | 258 | bool isupper(string s) 259 | { 260 | for (int index = 0; index < s.length(); index++) 261 | { 262 | if (!isupper(s[index])) 263 | return false; 264 | } 265 | return true; 266 | } 267 | 268 | /* 269 | Returns true if the string follows the rules of a title 270 | */ 271 | 272 | bool istitle(string s) 273 | { 274 | 275 | bool followsTitle = false; 276 | if (isupper(s[0])) 277 | followsTitle = true; 278 | for (int index = 1; index < s.length(); index++) 279 | { 280 | if (s[index] == ' ') 281 | { 282 | if (isupper(s[index + 1])) 283 | followsTitle = true; 284 | else 285 | followsTitle = false; 286 | } 287 | } 288 | 289 | return followsTitle; 290 | } 291 | 292 | /* 293 | Converts the string passed as input into lowercase 294 | */ 295 | 296 | void lower(string &s) 297 | { 298 | for (int index = 0; index < s.length(); index++) 299 | { 300 | s[index] = tolower(s[index]); 301 | } 302 | } 303 | 304 | /* 305 | Converts the string passed as input into uppercase 306 | */ 307 | 308 | void upper(string &s) 309 | { 310 | for (int index = 0; index < s.length(); index++) 311 | { 312 | s[index] = toupper(s[index]); 313 | } 314 | } 315 | 316 | /* 317 | Splits the string at the specified separator and returns a vector of strings 318 | */ 319 | 320 | vector split(string s, char c = ' ') 321 | { 322 | vector res; 323 | string tempStr = ""; 324 | for (int index = 0; index < s.length(); index++) 325 | { 326 | if (s[index] != c) 327 | { 328 | tempStr += s[index]; 329 | } 330 | else 331 | { 332 | res.push_back(tempStr); 333 | tempStr = ""; 334 | } 335 | } 336 | res.push_back(tempStr); 337 | return res; 338 | } 339 | 340 | /* 341 | Swaps cases in a string, i.e., lowercase becomes uppercase and vice-versa. 342 | */ 343 | 344 | void swapcase(string &s) 345 | { 346 | for (int index = 0; index < s.length(); index++) 347 | { 348 | if (islower(s[index])) 349 | s[index] = toupper(s[index]); 350 | else 351 | s[index] = tolower(s[index]); 352 | } 353 | } 354 | 355 | /* 356 | Converts the first character of each word to uppercase 357 | */ 358 | 359 | void title(string &s) 360 | { 361 | s[0] = toupper(s[0]); 362 | 363 | for (int index = 1; index < s.length(); index++) 364 | { 365 | if (s[index] == ' ') 366 | { 367 | s[index + 1] = toupper(s[index + 1]); 368 | } 369 | } 370 | } 371 | 372 | /* 373 | Replaces the specified value in the string with the specified value passed as arguement 374 | */ 375 | 376 | void replace(string &s, char c, char k) 377 | { 378 | for (int index = 0; index < s.length(); index++) 379 | { 380 | if (s[index] == c) 381 | { 382 | s[index] = k; 383 | } 384 | } 385 | } 386 | 387 | void replace(string &s, string c, string k) 388 | { 389 | int replacePosition = find(s, c); 390 | int replacerIndex = 0, index; 391 | if (c.length() > k.length()) 392 | { 393 | for (index = replacePosition; index < replacePosition + k.length(); index++) 394 | { 395 | s[index] = k[replacerIndex++]; 396 | } 397 | s.erase(index, c.length() - k.length()); 398 | } 399 | else 400 | { 401 | s.insert(replacePosition, k); 402 | 403 | s.erase(replacePosition + k.length(), c.length()); 404 | } 405 | } --------------------------------------------------------------------------------