├── Array Based List.txt ├── Balanced Parentheses.txt ├── Binary Search Tree.txt ├── Binary Search.txt ├── Bubble Sort.txt ├── Circular Queue.txt ├── Doubly Linked List.txt ├── Expression Evaluation.txt ├── Heap Sort.txt ├── Infix To Postfix.txt ├── Insertion sort.txt ├── Linear Search.txt ├── Linked List.txt ├── Linked Queue.txt ├── Merge Sort.txt ├── Quick Sort.txt ├── README.md ├── Selection Sort.txt ├── Stack Based Array.txt └── Stack Using Linked List.txt /Array Based List.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class arrayListType 7 | { 8 | public: 9 | arrayListType(int size = 100); 10 | arrayListType(arrayListType& otherList); //copy constructor 11 | ~arrayListType(); // destructor 12 | 13 | bool isEmpty(); 14 | bool isFull(); 15 | int listSize(); 16 | int maxListSize(); 17 | void print(); 18 | bool isItemAtEqual(int loc, int item); 19 | void insertAt(int loc, int item); 20 | void insertEnd(int item); 21 | void removeAt(int loc); 22 | void retrieveAt(int loc, int& item); 23 | void replaceAt(int loc, int item); 24 | void clearList(); 25 | int seqSearch(int item); 26 | void insertNoDuplicate(int item); 27 | void remove(int item); 28 | private: 29 | int *list; //array to hold the list elements 30 | int length; //to store the length of the list 31 | int maxSize; //to store the maximum size of the list 32 | }; 33 | 34 | arrayListType::arrayListType(int size) 35 | { 36 | /* initilize the private members */ 37 | if(size <= 0) 38 | { 39 | cout << " Wrong Size " << endl; 40 | maxSize = 100; 41 | } 42 | else 43 | maxSize = size; 44 | 45 | length = 0; 46 | list = new int [maxSize]; 47 | assert(list != NULL); //terminate if unable to allocate memory space 48 | } 49 | 50 | arrayListType::arrayListType(arrayListType& otherList) 51 | { 52 | maxSize = otherList.maxSize; 53 | length = otherList.length; 54 | list = new int [maxSize]; //create the array 55 | assert(list != NULL); //terminate if unable to allocate memory space 56 | 57 | for(int j = 0; j < length; j++) //copy otherList 58 | list [j] = otherList.list[j]; 59 | } 60 | 61 | arrayListType::~arrayListType() 62 | { 63 | delete [] list; 64 | } 65 | 66 | bool arrayListType::isEmpty() 67 | { 68 | return (length == 0); 69 | } 70 | 71 | bool arrayListType::isFull() 72 | { 73 | return (length == maxSize); 74 | } 75 | 76 | int arrayListType::listSize() 77 | { 78 | return length; 79 | } 80 | 81 | int arrayListType::maxListSize() 82 | { 83 | return maxSize; 84 | } 85 | 86 | void arrayListType::print() 87 | { 88 | for(int i = 0; i < length; i++) 89 | cout<= length) 96 | return false; 97 | else 98 | return (list[loc] == item); 99 | } 100 | 101 | void arrayListType::insertAt(int loc, int item) 102 | { 103 | if(isFull()) 104 | cout<<" The List is Full " << endl; 105 | else if(loc < 0 || loc > length) 106 | cout << "Out of Range " << endl; 107 | else 108 | { 109 | for(int i = length; i > loc; i--) 110 | list[i] = list[i - 1]; //shift right 111 | 112 | list[loc] = item; //insert the item at the specified position 113 | length++; //increment the length 114 | } 115 | } 116 | 117 | void arrayListType::insertEnd(int item) 118 | { 119 | if(isFull()) 120 | cout<<" The List is Full " << endl; 121 | else 122 | list[length++] = item; 123 | } 124 | void arrayListType::retrieveAt(int loc, int& item) 125 | { 126 | if(loc < 0 || loc >= length) 127 | cout << "Out of Range " << endl; 128 | else 129 | item = list[loc]; 130 | } 131 | 132 | void arrayListType::replaceAt(int loc, int item) 133 | { 134 | if(loc < 0 || loc >= length) 135 | cout << "Out of Range " << endl; 136 | else 137 | list[loc] = item; 138 | } 139 | 140 | void arrayListType::clearList() 141 | { 142 | length = 0; 143 | } 144 | 145 | int arrayListType::seqSearch(int item) 146 | { 147 | for(int loc = 0; loc < length; loc++) 148 | if(list[loc] == item) 149 | return loc; 150 | return -1; 151 | } 152 | 153 | void arrayListType::insertNoDuplicate(int item) 154 | { 155 | if(isFull()) 156 | cout<<" The List is Full " << endl; 157 | else 158 | { 159 | int flag = seqSearch(item); 160 | if(flag == -1) 161 | list[length++] = item; 162 | else 163 | cout<<"No duplicates are allowed."<= length) 179 | cout<<"The location of the item to be removed is out of range."< 2 | #include 3 | #include 4 | using namespace std; 5 | bool ArePair(char open, char close) 6 | { 7 | if (open == '(' && close == ')') 8 | return true; 9 | else if (open == '{' && close == '}') 10 | return true; 11 | else if (open == '[' && close == ']') 12 | return true; 13 | return false; 14 | } 15 | bool AreBalanced(string exp) 16 | { 17 | stack S; 18 | int length = exp.length(); 19 | for (int i = 0; i < length; i++) 20 | { 21 | if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[') 22 | S.push(exp[i]); 23 | else if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']') 24 | { 25 | if (S.empty() || !ArePair(S.top(), exp[i])) 26 | return false; 27 | else 28 | S.pop(); 29 | } 30 | } 31 | return S.empty() ? true : false; 32 | } 33 | 34 | int main() 35 | { 36 | 37 | string expression; 38 | cout << "Enter an expression:"; 39 | cin >> expression; 40 | if (AreBalanced(expression)) 41 | cout << "Balanced\n"; 42 | else 43 | cout << "Not Balanced\n"; 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Binary Search Tree.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct nodeType 7 | { 8 | int info; 9 | nodeType *left; 10 | nodeType *right; 11 | }; 12 | 13 | 14 | class binarySearchTreeType 15 | { 16 | public: 17 | bool isEmpty(); 18 | bool search(int); 19 | bool searchRec(int); 20 | void insert(int); 21 | void remove(int); 22 | void inorderTraversal(); 23 | void preorderTraversal(); 24 | void postorderTraversal(); 25 | int treeHeight(); 26 | int treeNodeCount(); 27 | int treeLeavesCount(); 28 | void clearTree(); 29 | binarySearchTreeType(); 30 | ~binarySearchTreeType(); 31 | private: 32 | nodeType *root; 33 | void clear(nodeType* &p); 34 | void inorder(nodeType *p); 35 | void preorder(nodeType *p); 36 | void postorder(nodeType *p); 37 | int height(nodeType *p); 38 | int max(int x, int y); 39 | int nodeCount(nodeType *p); 40 | int leavesCount(nodeType *p); 41 | void deleteFromTree(nodeType * &p); 42 | bool searchRecPriv(nodeType *, int); 43 | }; 44 | 45 | 46 | binarySearchTreeType::binarySearchTreeType() 47 | { 48 | root = NULL; 49 | } 50 | 51 | 52 | bool binarySearchTreeType::isEmpty() 53 | { 54 | return (root == NULL); 55 | } 56 | 57 | 58 | void binarySearchTreeType::inorderTraversal() 59 | { 60 | inorder(root); 61 | } 62 | 63 | 64 | void binarySearchTreeType::preorderTraversal() 65 | { 66 | preorder(root); 67 | } 68 | 69 | 70 | void binarySearchTreeType::postorderTraversal() 71 | { 72 | postorder(root); 73 | } 74 | 75 | 76 | int binarySearchTreeType::treeHeight() 77 | { 78 | return height(root); 79 | } 80 | 81 | 82 | int binarySearchTreeType::treeNodeCount() 83 | { 84 | return nodeCount(root); 85 | } 86 | 87 | 88 | int binarySearchTreeType::treeLeavesCount() 89 | { 90 | return leavesCount(root); 91 | } 92 | 93 | 94 | void binarySearchTreeType::inorder(nodeType *p) 95 | { 96 | if(p != NULL) 97 | { 98 | inorder(p->left); 99 | cout<info<<" "; 100 | inorder(p->right); 101 | } 102 | } 103 | 104 | 105 | void binarySearchTreeType::preorder(nodeType *p) 106 | { 107 | if(p != NULL) 108 | { 109 | cout<info<<" "; 110 | preorder(p->left); 111 | preorder(p->right); 112 | } 113 | } 114 | 115 | 116 | void binarySearchTreeType::postorder(nodeType *p) 117 | { 118 | if(p != NULL) 119 | { 120 | postorder(p->left); 121 | postorder(p->right); 122 | cout<info<<" "; 123 | } 124 | } 125 | 126 | 127 | void binarySearchTreeType::clear(nodeType* &p) 128 | { 129 | if(p != NULL) 130 | { 131 | clear(p->left); 132 | clear(p->right); 133 | delete p; 134 | p = NULL; 135 | } 136 | } 137 | 138 | 139 | void binarySearchTreeType::clearTree() 140 | { 141 | clear(root); 142 | } 143 | 144 | binarySearchTreeType::~binarySearchTreeType() 145 | { 146 | clear(root); 147 | } 148 | 149 | 150 | int binarySearchTreeType::height(nodeType *p) 151 | { 152 | if(p == NULL) 153 | return 0; 154 | else 155 | return 1 + max(height(p->left), height(p->right)); 156 | } 157 | 158 | 159 | int binarySearchTreeType::max(int x, int y) 160 | { 161 | if(x >= y) 162 | return x; 163 | else 164 | return y; 165 | } 166 | 167 | 168 | int binarySearchTreeType::nodeCount(nodeType *p) 169 | { 170 | if(p == NULL) 171 | return 0; 172 | else 173 | return 1 + nodeCount(p->left) + nodeCount(p->right); 174 | } 175 | 176 | 177 | int binarySearchTreeType::leavesCount(nodeType *p) 178 | { 179 | if(p == NULL) 180 | return 0; 181 | else if ((p->left == NULL) && (p->right == NULL)) 182 | return 1; 183 | else 184 | return leavesCount(p->left) + leavesCount(p->right); 185 | } 186 | 187 | bool binarySearchTreeType::search(int item) 188 | { 189 | nodeType *current = root; 190 | 191 | while(current != NULL) 192 | { 193 | if(current->info == item) 194 | return true; 195 | else if(current->info > item) 196 | current = current->left; 197 | else 198 | current = current->right; 199 | } 200 | 201 | return false; 202 | } 203 | 204 | bool binarySearchTreeType::searchRec(int item) 205 | { 206 | return searchRecPriv(root, item); 207 | } 208 | 209 | bool binarySearchTreeType::searchRecPriv(nodeType *p, int item) 210 | { 211 | if(p == NULL) 212 | return false; 213 | else if (p->info == item) 214 | return true; 215 | else if(p->info > item) 216 | return searchRecPriv(p->left, item); 217 | else 218 | return searchRecPriv(p->right, item); 219 | } 220 | 221 | void binarySearchTreeType::insert(int item) 222 | { 223 | nodeType *current; //pointer to traverse the tree 224 | nodeType *trailCurrent; //pointer behind current 225 | nodeType *newNode; //pointer to create the node 226 | 227 | newNode = new nodeType; 228 | assert(newNode != NULL); 229 | newNode->info = item; 230 | newNode->left = NULL; 231 | newNode->right = NULL; 232 | 233 | if(root == NULL) 234 | root = newNode; 235 | else 236 | { 237 | current = root; 238 | 239 | while(current != NULL) 240 | { 241 | trailCurrent = current; 242 | 243 | if(current->info == item) 244 | { 245 | cout<<"The insert item is already in the list -- "; 246 | cout<<"duplicates are not allowed."<info > item) 251 | current = current->left; 252 | else 253 | current = current->right; 254 | }//end while 255 | 256 | if(trailCurrent->info > item) 257 | trailCurrent->left = newNode; 258 | else 259 | trailCurrent->right = newNode; 260 | } 261 | } 262 | //this function only determines the node to be deleted 263 | void binarySearchTreeType::remove(int item) 264 | { 265 | nodeType *current; //pointer to traverse the tree 266 | nodeType *trailCurrent; //pointer behind current 267 | 268 | if(root == NULL) 269 | { 270 | cout<<"Cannot delete from the empty tree."<info == item) 274 | { 275 | deleteFromTree(root); 276 | return; 277 | } 278 | 279 | //if you get here, then the item to be deleted is not the root 280 | trailCurrent = root; 281 | 282 | if(root->info > item) 283 | current = root->left; 284 | else 285 | current = root->right; 286 | 287 | //search for the item to be deleted. 288 | while(current != NULL) 289 | { 290 | if(current->info == item) 291 | break; 292 | else 293 | { 294 | trailCurrent = current; 295 | if(current->info > item) 296 | current = current->left; 297 | else 298 | current = current->right; 299 | } 300 | }// once the while is done, current points to either NULL or to the node to be deleted 301 | 302 | if(current == NULL) 303 | cout<<"The delete item is not in the tree."<info > item) 305 | deleteFromTree(trailCurrent->left); 306 | else 307 | deleteFromTree(trailCurrent->right); 308 | } 309 | 310 | void binarySearchTreeType::deleteFromTree 311 | (nodeType* &p) 312 | { 313 | nodeType *current; //pointer to traverse 314 | //the tree 315 | nodeType *trailCurrent; //pointer behind current 316 | nodeType *temp; //pointer to delete the node 317 | 318 | if(p->left == NULL && p->right == NULL) 319 | { 320 | delete p; 321 | p = NULL; 322 | } 323 | else if(p->left == NULL) 324 | { 325 | temp = p; 326 | p = p->right; 327 | delete temp; 328 | } 329 | else if(p->right == NULL) 330 | { 331 | temp = p; 332 | p = p->left; 333 | delete temp; 334 | } 335 | else 336 | { 337 | current = p->left; 338 | trailCurrent = NULL; 339 | 340 | while(current->right != NULL) 341 | { 342 | trailCurrent = current; 343 | current = current->right; 344 | }//end while 345 | 346 | p->info = current->info; 347 | 348 | if(trailCurrent == NULL) //current did not move; 349 | //current == p->left; adjust p 350 | p->left = current->left; 351 | else 352 | trailCurrent->right = current->left; 353 | 354 | delete current; 355 | }//end else 356 | }//end deleteFromTree 357 | 358 | int main() 359 | { 360 | binarySearchTreeType b; 361 | b.insert(10); 362 | b.insert(20); 363 | b.insert(5); 364 | b.remove(10); 365 | b.inorderTraversal(); 366 | b.postorderTraversal(); 367 | b.preorderTraversal(); 368 | return 0; 369 | } -------------------------------------------------------------------------------- /Binary Search.txt: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int binarySearch(int arr[], int l, int h, int element) 4 | { 5 | while (l <= h) { 6 | 7 | int m = (l + h) / 2;//both=>m = l + (r - l) / 2 both=>m = (2 l + r -l)/2 8 | 9 | 10 | if (arr[m] == element) 11 | return m; 12 | 13 | if (arr[m] > element) 14 | 15 | h = m - 1; 16 | 17 | else 18 | 19 | l = m + 1; 20 | 21 | } 22 | 23 | return -1; 24 | } 25 | 26 | /* 27 | int binarySearchRec(int arr[], int l, int h, int element) 28 | { 29 | if (h >= l) { 30 | int mid = (l + h) / 2; 31 | 32 | if (arr[mid] == element) 33 | return mid; 34 | 35 | if (arr[mid] > element) 36 | return binarySearch(arr, l, mid - 1, element); 37 | 38 | return binarySearch(arr, mid + 1, h, element); 39 | } 40 | return -1; 41 | } 42 | */ 43 | 44 | int main() 45 | { 46 | int arr[] = { 2, 3, 4, 10, 40 }; 47 | int n = sizeof(arr) / sizeof(arr[0]); 48 | 49 | int num; 50 | cout << "Enter an Integer :"; 51 | cin >> num; 52 | 53 | int result = binarySearch(arr, 0, n - 1, num); 54 | if(result == -1) 55 | cout << "The Number : (" << num << ") Was Not Found." << endl; 56 | else 57 | cout << "The Number : (" << arr[result] << ") Was Found At Index : (" << result << ")" << endl; 58 | 59 | return 0; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /Bubble Sort.txt: -------------------------------------------------------------------------------- 1 | #include 2 | //#include 3 | using namespace std; 4 | void bubbleSort(int arr[], int n) 5 | { 6 | bool flag = true; 7 | int c = 0; 8 | for (int i = 0; i < n - 1; i++) 9 | { 10 | for (int j = 0; j < n - i - 1; j++) 11 | { 12 | if (arr[j] > arr[j + 1]) 13 | { 14 | swap(arr[j], arr[j + 1]); 15 | flag = false; 16 | } 17 | c++; 18 | } 19 | if (flag == true) 20 | break; 21 | 22 | } 23 | cout << "# of rounds :" << c << endl; 24 | } 25 | /* 26 | void bubbleSortRec(int arr[], int n) 27 | { 28 | 29 | if (n == 1)//base case 30 | return; 31 | 32 | for (int i = 0; i < n - 1; i++) 33 | if (arr[i] > arr[i + 1]) 34 | swap(arr[i], arr[i + 1]); 35 | 36 | bubbleSortRec(arr, n - 1); 37 | } 38 | */ 39 | 40 | void printArray(int arr[], int n) 41 | { 42 | for (int i = 0; i < n; i++) 43 | cout << arr[i] << " "; 44 | cout << endl; 45 | } 46 | 47 | 48 | 49 | int main() 50 | { 51 | 52 | int arr[] = { 30,20,40,5,60,2 }; 53 | int n = sizeof(arr) / sizeof(arr[0]);//24/4=6 54 | bubbleSort(arr, n); 55 | //bubbleSortRec(arr, n); 56 | printArray(arr, n); 57 | return 0; 58 | 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Circular Queue.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class arrayQueueType 5 | { 6 | int rear; 7 | int front; 8 | int length; 9 | int *arr; 10 | int maxSize; 11 | 12 | public: 13 | arrayQueueType(int size) { 14 | if (size <= 0) 15 | maxSize = 100; 16 | else 17 | maxSize = size; 18 | 19 | front = 0; 20 | 21 | arr = new int[maxSize]; 22 | rear = maxSize - 1; 23 | length = 0; 24 | } 25 | int isEmpty() 26 | { 27 | return length == 0; 28 | } 29 | bool isFull() 30 | { 31 | return length == maxSize; 32 | } 33 | void addQueue(int Element) 34 | { 35 | if (isFull()) 36 | { 37 | cout << "Queue Full Can't Enqueue ...!"; 38 | } 39 | else 40 | { 41 | rear = (rear + 1) % maxSize; 42 | arr[rear] = Element; 43 | length++; 44 | } 45 | 46 | 47 | } 48 | void deleteQueue() 49 | { 50 | if (isEmpty()) 51 | { 52 | cout << "Empty Queue Can't Dequeue ...!"; 53 | } 54 | else 55 | { 56 | front = (front + 1) % maxSize; 57 | --length; 58 | 59 | } 60 | 61 | 62 | } 63 | int frontQueue() 64 | { 65 | assert(!isEmpty()); 66 | return arr[front]; 67 | } 68 | 69 | int rearQueue() 70 | { 71 | assert(!isEmpty()); 72 | return arr[rear]; 73 | } 74 | 75 | 76 | void printQueue() 77 | { 78 | if (!isEmpty()) { 79 | for (size_t i = front; i != rear; i = (i + 1) % maxSize) 80 | { 81 | cout << arr[i] << " "; 82 | } 83 | cout << arr[rear]; 84 | } 85 | else 86 | cout << "Empty Queue"; 87 | } 88 | 89 | 90 | int queueSearch(int element) { 91 | int pos = -1; 92 | if (!isEmpty()) 93 | { 94 | for (int i = front; i != rear; i = (i + 1) % maxSize) 95 | if (arr[i] == element) 96 | { 97 | pos = i; 98 | break; 99 | } 100 | if (pos == -1) 101 | { 102 | if (arr[rear] == element) 103 | pos = rear; 104 | } 105 | } 106 | else 107 | cout << "Q is empty ! " << endl; 108 | return pos; 109 | } 110 | 111 | 112 | ~arrayQueueType() { 113 | delete[]arr; 114 | } 115 | }; 116 | 117 | int main() 118 | { 119 | arrayQueueType q1(5); 120 | q1.addQueue(10); 121 | q1.addQueue(20); 122 | q1.addQueue(30); 123 | q1.addQueue(40); 124 | q1.addQueue(50); 125 | q1.printQueue(); 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /Doubly Linked List.txt: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class doublyLinkedList { 4 | private: 5 | struct Node { 6 | int item; 7 | Node* next; 8 | Node* prev; 9 | }; 10 | Node* first; 11 | Node* last; 12 | int count; 13 | 14 | public: 15 | doublyLinkedList() { 16 | first = NULL; 17 | last = NULL; 18 | count = 0; 19 | } 20 | bool isEmpty() 21 | { 22 | return (first == NULL); 23 | } 24 | void destroy() 25 | { 26 | Node *temp; 27 | while (first != NULL) 28 | { 29 | temp = first; 30 | first = first->next; 31 | delete temp; 32 | } 33 | last = NULL; 34 | count = 0; 35 | } 36 | 37 | void insertLast(int val) { 38 | Node* newNode = new Node; 39 | newNode->item = val; 40 | if (count == 0) { 41 | first = last = newNode; 42 | newNode->next = newNode->prev = NULL; 43 | } 44 | else { 45 | newNode->next = NULL; 46 | newNode->prev = last; 47 | last->next = newNode; 48 | last = newNode; 49 | 50 | 51 | 52 | } 53 | count++; 54 | } 55 | 56 | 57 | 58 | void insertFirst(int item) 59 | { 60 | Node*newNode = new Node; 61 | newNode->item = item; 62 | if (count == 0) { 63 | first = last = newNode; 64 | newNode->next = newNode->prev = NULL; 65 | } 66 | else { 67 | newNode->next = first; 68 | newNode->prev = NULL; 69 | first->prev = newNode; 70 | first = newNode; 71 | 72 | } 73 | count++; 74 | } 75 | 76 | 77 | 78 | 79 | 80 | 81 | void insertAt(int pos, int item) 82 | { 83 | if (pos < 0 || pos > count) 84 | cout << "Out Of Range ...!" << endl; 85 | else 86 | { 87 | Node *newNode = new Node; 88 | newNode->item = item; 89 | if (pos == 0) 90 | insertFirst(item); 91 | else if (pos == count) 92 | insertLast(item); 93 | else 94 | { 95 | Node *current = first; 96 | for (int i = 1; i < pos; i++) 97 | { 98 | current = current->next; 99 | } 100 | 101 | newNode->next = current->next; 102 | newNode->prev = current; 103 | current->next->prev = newNode; 104 | current->next = newNode; 105 | count++; 106 | } 107 | 108 | } 109 | } 110 | 111 | 112 | void removeFirst() 113 | { 114 | if (count == 0) 115 | cout << "Empty List" << endl; 116 | else if (count == 1)//first == last 117 | { 118 | delete first; 119 | last = first = NULL; 120 | } 121 | else 122 | { 123 | Node* current = first; 124 | first = first->next; 125 | first->prev = NULL; 126 | delete current; 127 | 128 | } 129 | count--; 130 | } 131 | 132 | void deleteNthNode(int pos) 133 | { 134 | if (pos < 0 || pos >= count) { 135 | cout << "Out Of Range" << endl; 136 | return; 137 | } 138 | else if (pos == 0) 139 | { 140 | removeFirst(); 141 | } 142 | else if (pos == count - 1) 143 | { 144 | removeLast(); 145 | } 146 | else { 147 | Node *current = first->next; 148 | 149 | 150 | for (int i = 1; i < pos; i++) 151 | { 152 | current = current->next; 153 | } 154 | current->prev->next = current->next; 155 | current->next->prev = current->prev; 156 | delete current; 157 | } 158 | count--; 159 | } 160 | void removeLast() 161 | { 162 | if (count == 0) 163 | cout << "Empty List" << endl; 164 | else if (count == 1) 165 | { 166 | delete first; 167 | // delete last; 168 | last = first = NULL; 169 | count--; 170 | } 171 | else 172 | { 173 | Node *current = last; 174 | last = last->prev; 175 | last->next = NULL; 176 | delete current; 177 | count--; 178 | } 179 | } 180 | 181 | 182 | 183 | void remove(int item) 184 | { 185 | if (isEmpty()) 186 | { 187 | cout << "Empty List Can't Remove "; 188 | return; 189 | } 190 | Node*current = first->next; 191 | 192 | if (first->item == item)//delete the first element, special case 193 | { 194 | /* 195 | first = current->next; 196 | if (first != NULL) 197 | first->prev = NULL; 198 | delete current; 199 | return; 200 | */ 201 | removeFirst(); 202 | return; 203 | } 204 | else 205 | { 206 | 207 | 208 | while (current != NULL)//current->item!=element 209 | { 210 | if (current->item == item) 211 | break; 212 | current = current->next; 213 | } 214 | 215 | if (current == NULL) { 216 | cout << "The item is not there\n"; 217 | return; 218 | } 219 | else if (current->next == NULL) { 220 | removeLast(); 221 | return; 222 | } 223 | 224 | else 225 | { 226 | current->prev->next = current->next; 227 | //if (current->next != NULL) 228 | current->next->prev = current->prev; 229 | delete current; 230 | count--; 231 | } 232 | 233 | } 234 | 235 | 236 | } 237 | 238 | void display() { 239 | if (isEmpty()) 240 | { 241 | cout << "Empty List Can't Display...!"; 242 | } 243 | else { 244 | Node* temp = first; 245 | while (temp != nullptr) { 246 | cout << temp->item << " "; 247 | temp = temp->next; 248 | } 249 | } 250 | cout << endl; 251 | 252 | } 253 | 254 | 255 | 256 | void reverse_display() { 257 | if (isEmpty()) 258 | { 259 | cout << "Empty List Can't Display Reverse...!"; 260 | } 261 | else 262 | { 263 | Node* temp = last; 264 | while (temp != NULL) { 265 | cout << temp->item << " "; 266 | temp = temp->prev; 267 | } 268 | } 269 | 270 | } 271 | 272 | 273 | }; 274 | int main() { 275 | doublyLinkedList dl; 276 | dl.insertAt(0, 4); 277 | dl.insertAt(1, 6); 278 | dl.insertAt(2, 7); 279 | dl.insertFirst(2); 280 | dl.insertLast(10); 281 | dl.remove(7); 282 | dl.display(); 283 | dl.reverse_display(); 284 | } 285 | 286 | -------------------------------------------------------------------------------- /Expression Evaluation.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | bool isOperator(char ch) 6 | { 7 | if (ch == '+' || ch == '-' || ch == '*' || ch == '/') 8 | return true; 9 | else 10 | return false; 11 | } 12 | int performOperation(int op1, int op2, char op) 13 | { 14 | int ans; 15 | switch (op) { 16 | case '+': 17 | ans = op2 + op1; 18 | break; 19 | case '-': 20 | ans = op2 - op1; 21 | break; 22 | case '*': 23 | ans = op2 * op1; 24 | break; 25 | case '/': 26 | ans = op2 / op1; 27 | break; 28 | } 29 | return ans; 30 | } 31 | 32 | 33 | int main() 34 | { 35 | char exp[1000], buffer[15]; 36 | int i, op1, op2, len, j, x; 37 | stack s; 38 | cout<<"Enter a Postfix Expression: ( e.g. 4 5 * )\n"; 39 | cin.getline(exp,1000); 40 | len = strlen(exp); 41 | j = 0; 42 | for (i = 0; i < len; i++) { 43 | 44 | if (exp[i] >= '0' && exp[i] <= '9') { 45 | buffer[j++] = exp[i]; 46 | } 47 | else if (exp[i] == ' ') { 48 | if (j > 0) { 49 | buffer[j] = '\0'; 50 | x = atoi(buffer); 51 | s.push(x); 52 | j = 0; 53 | } 54 | } 55 | 56 | else if (isOperator(exp[i])) { 57 | op1 = s.top(); 58 | s.pop(); 59 | op2 = s.top(); 60 | s.pop(); 61 | s.push(performOperation(op1, op2, exp[i])); 62 | } 63 | } 64 | cout<<"Answer = "< 2 | using namespace std; 3 | void heapify(int arr[],int n,int i) 4 | { 5 | int l=2*i+1; 6 | int r=2*i+2; 7 | int max=i; 8 | if(larr[max]) 9 | max=l; 10 | if(rarr[max]) 11 | max=r; 12 | 13 | if(max != i){ 14 | swap(arr[i],arr[max]); 15 | heapify(arr,n,max); 16 | } 17 | } 18 | 19 | void buildHeap(int arr[],int n) 20 | { 21 | for (int i = n / 2 - 1; i >= 0; i--) 22 | heapify(arr, n, i); 23 | } 24 | 25 | void heapSort(int arr[], int n) 26 | { 27 | 28 | buildHeap(arr, n); 29 | for (int i = n-1; i >=0; i--) 30 | { 31 | swap(arr[0], arr[i]); 32 | heapify(arr, i, 0); 33 | } 34 | 35 | } 36 | 37 | void print(int arr[], int n) 38 | { 39 | for (int i = 0; i < n; i++) 40 | { 41 | cout << arr[i] << " "; 42 | } 43 | cout << endl; 44 | 45 | } 46 | int main() 47 | { 48 | int arr[] = { 90,10,40,70,5 }; 49 | int n = sizeof(arr) / sizeof(arr[0]); 50 | 51 | heapSort(arr, n); 52 | print(arr, n); 53 | 54 | return 0; 55 | } 56 | 57 | -------------------------------------------------------------------------------- /Infix To Postfix.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /* 5 | Infix to postfix conversion in C++ 6 | Input Postfix expression must be in a desired format. 7 | Operands and operator, both must be single character. 8 | Only '+' , '-' , '*', '/' and '$' (for exponentiation) operators are expected. 9 | */ 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | // Function to convert Infix expression to postfix 17 | string InfixToPostfix(string expression); 18 | 19 | // Function to verify whether an operator has higher precedence over other 20 | int HasHigherPrecedence(char operator1, char operator2); 21 | 22 | // Function to verify whether a character is operator symbol or not. 23 | bool IsOperator(char C); 24 | 25 | // Function to verify whether a character is alphanumeric chanaracter (letter or numeric digit) or not. 26 | bool IsOperand(char C); 27 | 28 | int main() 29 | { 30 | string expression; 31 | cout << "Enter Infix Expression \n"; 32 | getline(cin, expression); 33 | string postfix = InfixToPostfix(expression); 34 | cout << "Output = " << postfix << "\n"; 35 | return 0; 36 | } 37 | 38 | // Function to evaluate Postfix expression and return output 39 | string InfixToPostfix(string expression) 40 | { 41 | // Declaring a Stack from Standard template library in C++. 42 | stack S; 43 | string postfix = ""; // Initialize postfix as empty string. 44 | for (unsigned int i = 0; i < expression.length(); i++) { 45 | 46 | // Scanning each character from left. 47 | // If character is a delimitter, move on. 48 | if (expression[i] == ' ' || expression[i] == ',') continue; 49 | 50 | // If character is operator, pop two elements from stack, perform operation and push the result back. 51 | else if (IsOperator(expression[i])) 52 | { 53 | while (!S.empty() && S.top() != '(' && HasHigherPrecedence(S.top(), expression[i])) 54 | { 55 | postfix += S.top(); 56 | S.pop(); 57 | } 58 | S.push(expression[i]); 59 | } 60 | // Else if character is an operand 61 | else if (IsOperand(expression[i])) 62 | { 63 | postfix += expression[i]; 64 | } 65 | 66 | else if (expression[i] == '(') 67 | { 68 | S.push(expression[i]); 69 | } 70 | 71 | else if (expression[i] == ')') 72 | { 73 | while (!S.empty() && S.top() != '(') { 74 | postfix += S.top(); 75 | S.pop(); 76 | } 77 | S.pop(); 78 | } 79 | } 80 | 81 | while (!S.empty()) { 82 | postfix += S.top(); 83 | S.pop(); 84 | } 85 | 86 | return postfix; 87 | } 88 | 89 | // Function to verify whether a character is english letter or numeric digit. 90 | // We are assuming in this solution that operand will be a single character 91 | bool IsOperand(char C) 92 | { 93 | if (C >= '0' && C <= '9') return true; 94 | if (C >= 'a' && C <= 'z') return true; 95 | if (C >= 'A' && C <= 'Z') return true; 96 | return false; 97 | } 98 | 99 | // Function to verify whether a character is operator symbol or not. 100 | bool IsOperator(char C) 101 | { 102 | if (C == '+' || C == '-' || C == '*' || C == '/' || C == '$') 103 | return true; 104 | 105 | return false; 106 | } 107 | 108 | // Function to verify whether an operator is right associative or not. 109 | int IsRightAssociative(char op) 110 | { 111 | if (op == '$') return true; 112 | return false; 113 | } 114 | 115 | // Function to get weight of an operator. An operator with higher weight will have higher precedence. 116 | int GetOperatorWeight(char op) 117 | { 118 | int weight = -1; 119 | switch (op) 120 | { 121 | case '+': 122 | case '-': 123 | weight = 1; 124 | case '*': 125 | case '/': 126 | weight = 2; 127 | case '$': 128 | weight = 3; 129 | } 130 | return weight; 131 | } 132 | 133 | // Function to perform an operation and return output. 134 | int HasHigherPrecedence(char op1, char op2) 135 | { 136 | int op1Weight = GetOperatorWeight(op1); 137 | int op2Weight = GetOperatorWeight(op2); 138 | 139 | // If operators have equal precedence, return true if they are left associative. 140 | // return false, if right associative. 141 | // if operator is left-associative, left one should be given priority. 142 | if (op1Weight == op2Weight) 143 | { 144 | if (IsRightAssociative(op1)) return false; 145 | else return true; 146 | } 147 | return op1Weight > op2Weight ? true : false; 148 | } 149 | 150 | 151 | -------------------------------------------------------------------------------- /Insertion sort.txt: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void insertionSort(int arr[], int n) 4 | { 5 | int key, j; 6 | for (int i = 1; i < n; i++) 7 | { 8 | key = arr[i]; 9 | j = i - 1; 10 | 11 | while (j >= 0 && arr[j] < key) 12 | { 13 | arr[j + 1] = arr[j]; 14 | j = j - 1; 15 | } 16 | arr[j + 1] = key; 17 | } 18 | } 19 | void printArray(int arr[], int n) 20 | { 21 | for (int i = 0; i < n; i++) 22 | cout << arr[i] << " "; 23 | cout << endl; 24 | } 25 | 26 | 27 | /* 28 | void insertionSortRecursive(int arr[], int n) 29 | { 30 | // base case 31 | if (n <= 1) 32 | return; 33 | 34 | insertionSortRecursive(arr, n - 1); 35 | int last = arr[n - 1]; 36 | int j = n - 2; 37 | 38 | while (j >= 0 && arr[j] > last) 39 | { 40 | arr[j + 1] = arr[j]; 41 | j--; 42 | } 43 | arr[j + 1] = last; 44 | } 45 | */ 46 | int main() 47 | { 48 | 49 | int arr[] = { 80 , 90 ,60 ,30 ,50 ,70 ,40 }; 50 | int n = sizeof(arr) / sizeof(arr[0]);//28/4=7 51 | 52 | insertionSort(arr, n); 53 | //insertionSortRecursive(arr, n); 54 | printArray(arr, n); 55 | 56 | return 0; 57 | 58 | } 59 | 60 | -------------------------------------------------------------------------------- /Linear Search.txt: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int linearSearch(int arr[], int n, int key) 5 | { 6 | for (int i = 0; i < n; i++) 7 | { 8 | if (arr[i] == key) 9 | return i; 10 | } 11 | return-1; 12 | } 13 | 14 | 15 | int main() 16 | { 17 | int arr[] = { 90,10,40,70,5 }; 18 | int n = sizeof(arr) / sizeof(arr[0]);//5*4=20 => 20/4 =>5 19 | 20 | int num; 21 | cout << "Enter an Integer :"; 22 | cin >> num; 23 | 24 | int result = linearSearch(arr,n,num); 25 | if (result == -1) 26 | cout << "The Number : (" << num << ") Was Not Found." << endl; 27 | else 28 | cout << "The Number : (" << arr[result] << ") Was Found At Index : (" < 2 | using namespace std; 3 | struct nodeType 4 | { 5 | int info; 6 | nodeType *next; 7 | }; 8 | 9 | class linkedListType 10 | { 11 | public: 12 | linkedListType(); 13 | int listSize(); 14 | bool isEmpty(); 15 | int seqSearch(int); 16 | void remove(int); 17 | void insertFirst(int); 18 | void insertEnd(int); 19 | void insertAt(int, int); 20 | void removeAt(int); 21 | void print(); 22 | void clearList(); 23 | void insertOrdered(int); 24 | void removeFirst(); 25 | void removeLast(); 26 | void removeLast2(); 27 | int removeOddSumEven(); 28 | void reverse(); 29 | ~linkedListType(); 30 | 31 | 32 | private: 33 | nodeType *first, *last; 34 | int length; 35 | }; 36 | 37 | int linkedListType::removeOddSumEven() 38 | { 39 | int sum = first->info; 40 | nodeType *current = first->next; 41 | nodeType *trailCurrent = first; 42 | 43 | while (current != NULL) 44 | { 45 | if (current->info % 2 == 0) 46 | { 47 | sum += current->info; 48 | trailCurrent = current; 49 | current = current->next; 50 | } 51 | else 52 | { 53 | trailCurrent->next = current->next; 54 | delete current; 55 | length--; 56 | current = trailCurrent->next; 57 | } 58 | } 59 | return sum; 60 | } 61 | 62 | void linkedListType::removeLast() 63 | { 64 | if (length == 0) 65 | cout << "ERROR: EMPTY LIST" << endl; 66 | else if (length == 1) 67 | { 68 | delete first; 69 | last = first = NULL; 70 | length--; 71 | } 72 | else 73 | { 74 | nodeType *current = first->next; 75 | nodeType *trailCurrent = first; 76 | while (current != last) 77 | { 78 | trailCurrent = current; 79 | current = current->next; 80 | } 81 | delete current; 82 | trailCurrent->next = NULL; 83 | last = trailCurrent; 84 | length--; 85 | } 86 | } 87 | void linkedListType::removeLast2() 88 | { 89 | if (length == 0) 90 | cout << "ERROR: EMPTY LIST" << endl; 91 | else if (length == 1) 92 | { 93 | delete first; 94 | last = first = NULL; 95 | length--; 96 | } 97 | else 98 | { 99 | nodeType *current = first; 100 | while (current->next != last) 101 | current = current->next; 102 | 103 | delete last; 104 | current->next = NULL; 105 | last = current; 106 | length--; 107 | } 108 | } 109 | 110 | void linkedListType::removeFirst() 111 | { 112 | if (length == 0) 113 | cout << "ERROR: EMPTY LIST" << endl; 114 | else if (length == 1) 115 | { 116 | delete first; 117 | last = first = NULL; 118 | length--; 119 | } 120 | else 121 | { 122 | nodeType *current = first; 123 | first = first->next; 124 | delete current; 125 | length--; 126 | } 127 | } 128 | linkedListType::linkedListType() 129 | { 130 | first = last = NULL; 131 | length = 0; 132 | } 133 | 134 | int linkedListType::listSize() 135 | { 136 | return length; 137 | } 138 | 139 | bool linkedListType::isEmpty() 140 | { 141 | return (length == 0); 142 | } 143 | 144 | int linkedListType::seqSearch(int item) 145 | { 146 | nodeType *current = first; 147 | int loc = 0; 148 | while (current != NULL) 149 | { 150 | if (current->info == item) 151 | return loc; 152 | current = current->next; 153 | loc++; 154 | } 155 | return -1; 156 | } 157 | void linkedListType::remove(int item) 158 | { 159 | if (isEmpty()) 160 | { 161 | cout << "Can not remove from empty list\n"; 162 | return; 163 | } 164 | 165 | nodeType *current, *trailCurrent; 166 | if (first->info == item)//delete the first element, special case 167 | { 168 | current = first; 169 | 170 | first = first->next; 171 | delete current; 172 | length--; 173 | if (length == 0) 174 | last = NULL; 175 | } 176 | else 177 | { 178 | current = first->next; 179 | trailCurrent = first; 180 | while (current != NULL) 181 | { 182 | if (current->info == item) 183 | break; 184 | trailCurrent = current; 185 | current = current->next; 186 | } 187 | 188 | if (current == NULL) 189 | cout << "The item is not there\n"; 190 | else 191 | { 192 | trailCurrent->next = current->next; 193 | if (last == current) //delete the last item 194 | last = trailCurrent; 195 | delete current; 196 | length--; 197 | } 198 | } 199 | } 200 | 201 | 202 | 203 | void linkedListType::insertFirst(int item) 204 | { 205 | nodeType *newNode = new nodeType; 206 | newNode->info = item; 207 | if (length == 0) { 208 | first = last = newNode; 209 | newNode->next = NULL; 210 | } 211 | else { 212 | newNode->next = first; 213 | first = newNode; 214 | } 215 | length++; 216 | } 217 | 218 | void linkedListType::insertEnd(int item) 219 | { 220 | nodeType *newNode = new nodeType; 221 | newNode->info = item; 222 | if (length == 0) { 223 | first = last = newNode; 224 | newNode->next = NULL; 225 | } 226 | else { 227 | last->next = newNode; 228 | newNode->next = NULL; 229 | last = newNode; 230 | } 231 | length++; 232 | } 233 | 234 | 235 | void linkedListType::insertAt(int loc, int item) 236 | { 237 | if (loc < 0 || loc > length) 238 | cout << "ERROR: Out of range" << endl; 239 | else 240 | { 241 | nodeType *newNode = new nodeType; 242 | newNode->info = item; 243 | if (loc == 0) //insert at the begining 244 | insertFirst(item); 245 | else if (loc == length) //insert at the end; 246 | insertEnd(item); 247 | else 248 | { 249 | nodeType *current = first; 250 | for (int i = 1; i < loc; i++) 251 | current = current->next; 252 | 253 | newNode->next = current->next; 254 | current->next = newNode; 255 | length++; 256 | } 257 | 258 | } 259 | } 260 | 261 | void linkedListType::removeAt(int loc) 262 | { 263 | if (loc < 0 || loc >= length) 264 | cout << "ERROR: Out of range" << endl; 265 | else 266 | { 267 | nodeType *current, *trailCurrent; 268 | if (loc == 0) 269 | { 270 | current = first; 271 | first = first->next; 272 | delete current; 273 | length--; 274 | if (length == 0) 275 | last = NULL; 276 | } 277 | else 278 | { 279 | current = first->next; 280 | trailCurrent = first; 281 | for (int i = 1; i < loc; i++) 282 | { 283 | trailCurrent = current; 284 | current = current->next; 285 | } 286 | 287 | trailCurrent->next = current->next; 288 | if (last == current) //delete the last item 289 | last = trailCurrent; 290 | delete current; 291 | length--; 292 | } 293 | } 294 | } 295 | 296 | 297 | void linkedListType::print() 298 | { 299 | nodeType *current = first; 300 | while (current != NULL) 301 | { 302 | cout << current->info << endl; 303 | current = current->next; 304 | } 305 | } 306 | 307 | void linkedListType::clearList() 308 | { 309 | nodeType *current; 310 | while (first != NULL) 311 | { 312 | current = first; 313 | first = first->next; 314 | delete current; 315 | } 316 | last = NULL; 317 | length = 0; 318 | } 319 | 320 | void linkedListType::insertOrdered(int item) 321 | { 322 | nodeType *newNode = new nodeType; 323 | newNode->info = item; 324 | 325 | if (first == NULL) 326 | { 327 | first = last = newNode; 328 | newNode->next = NULL; 329 | length++; 330 | } 331 | else if (first->info >= item) 332 | { 333 | newNode->next = first; 334 | first = newNode; 335 | length++; 336 | } 337 | else 338 | { 339 | nodeType *current = first->next; 340 | nodeType *trailCurrent = first; 341 | 342 | while (current != NULL) 343 | { 344 | if (current->info >= item) 345 | break; 346 | current = current->next; 347 | trailCurrent = trailCurrent->next; 348 | } 349 | if (current == NULL){ 350 | last->next = newNode; 351 | newNode->next = NULL; 352 | last = newNode; 353 | length++; 354 | } 355 | else 356 | { 357 | trailCurrent->next = newNode; 358 | newNode->next = current; 359 | length++; 360 | } 361 | } 362 | } 363 | 364 | void linkedListType::reverse() { 365 | nodeType *prev, *next, *curr; 366 | prev = NULL; 367 | curr = first; 368 | next = curr->next; 369 | while (next != NULL)//curr!=NULL 370 | { 371 | next = curr->next; 372 | curr->next = prev; 373 | prev = curr; 374 | curr = next; 375 | } 376 | first = prev; 377 | } 378 | linkedListType::~linkedListType() 379 | { 380 | clearList(); 381 | } 382 | 383 | int main() 384 | { 385 | linkedListType l1; 386 | l1.insertAt(0, 10); 387 | l1.insertAt(1, 15); 388 | l1.insertAt(2, 20); 389 | l1.insertAt(3, 25); 390 | l1.print(); 391 | 392 | return 0; 393 | } 394 | -------------------------------------------------------------------------------- /Linked Queue.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | template 5 | class linkedQueue 6 | { 7 | private: 8 | struct Node 9 | { 10 | t item; 11 | Node *next; 12 | }; 13 | int length; 14 | Node *frontPtr, *rearPtr; 15 | 16 | public: 17 | 18 | linkedQueue():frontPtr(NULL), rearPtr(NULL), length(0) 19 | {} 20 | bool isEmpty() 21 | { 22 | return (length == 0); 23 | } 24 | 25 | void dequeue() 26 | { 27 | if (isEmpty()) 28 | cout << "Empty Queue" << endl; 29 | else if (length == 1) 30 | { 31 | delete frontPtr; 32 | rearPtr = NULL; 33 | length = 0; 34 | } 35 | else 36 | { 37 | Node *current = frontPtr; 38 | frontPtr = frontPtr->next; 39 | delete current;//free(current) 40 | length--; 41 | } 42 | } 43 | 44 | void enqueue(t item) 45 | { 46 | Node *newNode = new Node; 47 | newNode->item = item; 48 | newNode->next = NULL; 49 | 50 | if (length == 0) 51 | rearPtr = frontPtr = newNode; 52 | else 53 | { 54 | rearPtr->next = newNode; 55 | rearPtr = newNode; 56 | } 57 | length++; 58 | } 59 | 60 | t front() 61 | { 62 | assert(!isEmpty()); 63 | return frontPtr->item; 64 | } 65 | 66 | t rear() 67 | { 68 | assert(!isEmpty()); 69 | return rearPtr->item; 70 | } 71 | 72 | void clearQueue() 73 | { 74 | Node *current; 75 | 76 | while (frontPtr != NULL) 77 | { 78 | current = frontPtr; 79 | frontPtr = frontPtr->next; 80 | delete current; 81 | } 82 | rearPtr = NULL; 83 | length = 0; 84 | } 85 | void display() 86 | { 87 | Node*cur = frontPtr; 88 | cout << "Item in the queue :[ "; 89 | while (cur!=NULL) 90 | { 91 | cout << cur->item<<" "; 92 | cur = cur->next; 93 | }cout << "]" << endl; 94 | } 95 | 96 | void search(t item) 97 | { 98 | Node*cur = frontPtr; 99 | bool flag = true; 100 | while (cur != NULL) 101 | { 102 | if (cur->item == item) 103 | { 104 | cout << "the item :" << item << " found" << endl; 105 | flag = false; 106 | break; 107 | } 108 | cur = cur->next; 109 | } 110 | if(flag) 111 | cout << "the item : " << item << " not found" << endl; 112 | 113 | } 114 | 115 | }; 116 | 117 | int main() 118 | { 119 | linkedQueueq1; 120 | 121 | for (int i = 1; i <= 20; i++) 122 | q1.enqueue(i); 123 | 124 | cout << q1.front() << endl; 125 | cout << q1.rear() << endl; 126 | q1.display(); 127 | return 0; 128 | } 129 | 130 | -------------------------------------------------------------------------------- /Merge Sort.txt: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void merge(int arr[], int l, int m, int r)//m = l + (r - l) / 2; 4 | { 5 | int i, j, k; 6 | int n1 = m - l + 1;// First subarray is arr[l..m] 7 | int n2 = r - m;// Second subarray is arr[m+1..r] 8 | int *L=new int[n1], *R=new int[n2]; 9 | 10 | for (i = 0; i < n1; i++) 11 | L[i] = arr[l + i]; 12 | for (j = 0; j < n2; j++) 13 | R[j] = arr[m + 1 + j]; 14 | 15 | i = j = 0; 16 | k = l; 17 | 18 | while (i < n1 && j < n2) 19 | { 20 | if (L[i] <= R[j]) 21 | { 22 | arr[k] = L[i]; 23 | i++; 24 | } 25 | else 26 | { 27 | arr[k] = R[j]; 28 | j++; 29 | } 30 | k++; 31 | } 32 | 33 | while (i < n1) 34 | { 35 | arr[k] = L[i]; 36 | i++; 37 | k++; 38 | } 39 | 40 | while (j < n2) 41 | { 42 | arr[k] = R[j]; 43 | j++; 44 | k++; 45 | } 46 | } 47 | 48 | 49 | void mergeSort(int arr[], int l, int r) 50 | { 51 | if (l < r) 52 | { 53 | int m = l + (r - l) / 2; 54 | 55 | mergeSort(arr, l, m); 56 | mergeSort(arr, m + 1, r); 57 | 58 | merge(arr, l, m, r); 59 | } 60 | } 61 | 62 | 63 | 64 | void print(int arr[], int n) 65 | { 66 | for (int i = 0; i < n; i++) 67 | { 68 | cout << arr[i] << " "; 69 | } 70 | cout << endl; 71 | 72 | } 73 | int main() 74 | { 75 | 76 | int arr[] = { 60,10,20,5,60,70 }; 77 | int n = sizeof(arr) / sizeof(arr[0]);//24/4=6 78 | 79 | mergeSort(arr, 0, n-1); 80 | print(arr,n); 81 | return 0; 82 | 83 | } 84 | 85 | -------------------------------------------------------------------------------- /Quick Sort.txt: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int partition1(int arr[], int l, int h) 4 | { 5 | int p = arr[l]; 6 | int i = l; 7 | int j = h; 8 | while (i < j) 9 | { 10 | do 11 | { 12 | i++; 13 | } while (arr[i] <= p); 14 | do 15 | { 16 | j--; 17 | } while (arr[j] > p); 18 | 19 | if (i < j) 20 | swap(arr[i], arr[j]); 21 | } 22 | swap(arr[l], arr[j]); 23 | return j; 24 | } 25 | 26 | void quickSort1(int arr[], int l, int h) 27 | { 28 | 29 | if (l < h) { 30 | int piv = partition1(arr, l, h); 31 | quickSort1(arr, l, piv); 32 | quickSort1(arr, piv + 1, h); 33 | } 34 | 35 | } 36 | 37 | 38 | 39 | int partition2(int arr[], int iBegin, int jEnd) { 40 | int i = iBegin; 41 | int j = jEnd; 42 | int pivLoc = i; 43 | while (true) 44 | { 45 | while (arr[pivLoc] <= arr[j] && pivLoc != j) 46 | { 47 | j--; 48 | } 49 | if (pivLoc == j) 50 | break; 51 | else if (arr[pivLoc] > arr[j]) 52 | { 53 | swap(arr[j], arr[pivLoc]); 54 | pivLoc = j; 55 | } 56 | 57 | while (arr[pivLoc] >= arr[i] && pivLoc != i) 58 | { 59 | i++; 60 | } 61 | if (pivLoc == i) 62 | break; 63 | else if (arr[pivLoc] < arr[i]) 64 | { 65 | swap(arr[i], arr[pivLoc]); 66 | pivLoc = i; 67 | } 68 | } 69 | return pivLoc; 70 | } 71 | 72 | 73 | void quickSort2(int arr[], int l, int h) 74 | { 75 | 76 | if (l < h) { 77 | int piv = partition2(arr, l, h); 78 | quickSort2(arr, l, piv - 1); 79 | quickSort2(arr, piv + 1, h); 80 | } 81 | 82 | } 83 | 84 | 85 | int main() 86 | { 87 | int arr[] = { 2,-1,4,7,0 }; 88 | int n = sizeof(arr) / sizeof(arr[0]); 89 | 90 | quickSort2(arr, 0, n - 1); 91 | for (int i = 0; i < n; i++) 92 | { 93 | cout << arr[i] << " "; 94 | } 95 | 96 | return 0; 97 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data Structure source code by Adel Nasim 2 | 3 | ## This source code for course in youtube 4 | 5 | [![N|Solid](https://www.nicepng.com/png/detail/65-653990_youtube-subscribe-us-icon.png)](https://www.youtube.com/watch?v=owCqVRbZlbg&list=PLCInYL3l2AajqOUW_2SwjWeMwf4vL4RSp) 6 | 7 | [![Build Status](https://travis-ci.org/joemccann/dillinger.svg?branch=master)]() 8 | 9 | You must have good knowledge one of 10 | 11 | - C++ 12 | - Java 13 | - Python 14 | - C# 15 | 16 | ## Topic 17 | 18 | - Introduction 19 | - Complexity 20 | - Introduction To Stack 21 | - Stack Implementation 22 | - Stack Using Linked List(Linked Stack)-Part 1 23 | - Stack Implementation 24 | - Stack Using Linked List Implementation 25 | - Balanced Parentheses Using Stack {([ ])} 26 | - Expression Evaluation Using Stack 27 | - and alot 28 | -------------------------------------------------------------------------------- /Selection Sort.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void selectionSort(int arr[], int n) 5 | { 6 | int minIdx; 7 | //0 1 2 3 4 5 8 | for (int i = 0; i < n - 1; i++)//60 40 50 30 10 20 9 | { 10 | minIdx = i;//4 11 | 12 | for (int j = i + 1; j < n; j++) 13 | if (arr[j] > arr[minIdx]) 14 | minIdx = j; 15 | swap(arr[minIdx], arr[i]); 16 | 17 | } 18 | } 19 | /* 20 | //\\//\\//\\//\\//\\Recursive//\\//\\//\\//\\//\\//\\ 21 | int minIndex(int a[], int i, int j) 22 | { 23 | if (i == j) 24 | return i; 25 | 26 | int k = minIndex(a, i + 1, j); 27 | 28 | return (a[i] < a[k]) ? i : k; 29 | } 30 | 31 | 32 | 33 | void recurSelectionSort(int a[], int n, int index = 0) 34 | { 35 | 36 | if (index == n) 37 | return; 38 | 39 | int k = minIndex(a, index, n - 1); 40 | 41 | if (k != index) 42 | swap(a[k], a[index]); 43 | 44 | recurSelectionSort(a, n, index + 1); 45 | } 46 | 47 | */ 48 | 49 | 50 | void print(int arr[], int size) 51 | { 52 | 53 | for (int i = 0; i < size; i++) 54 | cout << arr[i] << " "; 55 | cout << endl; 56 | 57 | } 58 | 59 | int main() 60 | { 61 | int arr[] = { -60, 0, 50, 30, 10,20 }; 62 | int n = sizeof(arr) / sizeof(arr[0]);//6*4=24 / 4 63 | selectionSort(arr, n); 64 | //recurSelectionSort(arr, n); 65 | cout<<"Array After Selection Sort : \n"; 66 | print(arr, n); 67 | 68 | return 0; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /Stack Based Array.txt: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int MAX_SIZE = 3; 4 | template 5 | class stack { 6 | int top; 7 | t item[MAX_SIZE]; 8 | public: 9 | stack() :top(-1) {} 10 | bool isEmpty() 11 | { 12 | return top < 0; 13 | } 14 | 15 | void push(t Element) 16 | { 17 | if (top >= MAX_SIZE-1) 18 | { 19 | cout << "Stack full on push"; 20 | } 21 | else 22 | { 23 | top++; 24 | item[top] = Element; 25 | } 26 | } 27 | void pop() 28 | { 29 | if (isEmpty()) 30 | cout << "stack empty on pop"; 31 | else 32 | top--; 33 | } 34 | void pop(t&Element) 35 | { 36 | if (isEmpty()) 37 | cout << "stack empty on pop"; 38 | else { 39 | Element = item[top]; 40 | top--; 41 | } 42 | } 43 | void getTop(t&stackTop) 44 | { 45 | if (isEmpty()) 46 | cout << "stack empty on getTop"; 47 | else { 48 | stackTop = item[top]; 49 | cout << stackTop << endl; 50 | } 51 | } 52 | void print() { 53 | cout << "[ "; 54 | for (int i = top; i >= 0; i--) 55 | { 56 | cout << item[i] << " "; 57 | } 58 | cout << "]"; 59 | cout << endl; 60 | 61 | } 62 | }; 63 | int main() { 64 | stacks; 65 | s.push(5); 66 | s.push(15); 67 | s.push(20); 68 | s.print(); 69 | } -------------------------------------------------------------------------------- /Stack Using Linked List.txt: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | class Stack { 5 | private: 6 | struct StackNode 7 | { 8 | t item; 9 | StackNode*next; 10 | }; 11 | StackNode *topPtr, *curPtr; 12 | public: 13 | Stack() { 14 | topPtr = NULL; 15 | } 16 | bool isEmpty() 17 | { 18 | return topPtr == NULL; 19 | } 20 | void push(t newItem) 21 | { 22 | StackNode *newPtr = new StackNode; 23 | if (newPtr == NULL) 24 | cout << "Stack push cannot allocate memory"; 25 | else 26 | { 27 | newPtr->item = newItem; 28 | newPtr->next = topPtr; 29 | topPtr = newPtr; 30 | } 31 | } 32 | void pop() { 33 | if (isEmpty()) 34 | cout << "Stack empty on pop"; 35 | else { 36 | StackNode *temp = topPtr; 37 | topPtr = topPtr->next; 38 | temp->next = NULL; 39 | delete temp; 40 | } 41 | } 42 | void pop(t stackTop) 43 | { 44 | if (isEmpty()) 45 | cout << "Stack empty on pop"; 46 | else { 47 | stackTop = topPtr->item; 48 | StackNode *temp = topPtr; 49 | topPtr = topPtr->next; 50 | temp->next = NULL; 51 | delete temp; 52 | } 53 | } 54 | void getTop(t stackTop) 55 | { 56 | if (isEmpty()) 57 | cout << "stack empty on getTop"; 58 | else 59 | stackTop = topPtr->item; 60 | cout << "\nTop Element of the stack is " << stackTop; 61 | cout << endl; 62 | } 63 | void display() 64 | { 65 | curPtr = topPtr; 66 | cout << "\nItems in the stack : "; 67 | cout << "[ "; 68 | while (curPtr != NULL) 69 | { 70 | cout <item; 71 | curPtr = curPtr->next; 72 | } 73 | cout << " ]\n"; 74 | } 75 | }; 76 | int main() 77 | { 78 | Stacks; 79 | s.push(10); 80 | s.display(); 81 | } 82 | 83 | --------------------------------------------------------------------------------