├── BUBBLE_SORT.cpp ├── Bellman_Ford.cpp ├── Breadth_First_Search.cpp ├── Depth_First_Search.cpp ├── avl_tree.cpp ├── binary_search_tree.cpp ├── circ_doubly_ll.cpp ├── circ_queue.cpp ├── circ_singly_ll.cpp ├── dequeue.cpp ├── dijkstra.cpp ├── disjoint_set.cpp ├── doubly_ll.cpp ├── evaluate_postfix.cpp ├── ford_fulkerson.cpp ├── hash_table.cpp ├── header_ll.cpp ├── kruskal.cpp ├── parentheses_checker.cpp ├── priority_queue.cpp ├── queue.cpp ├── queue_ll.cpp ├── right_threaded _bst.cpp ├── singly_ll.cpp ├── stack.cpp └── stack_ll.cpp /BUBBLE_SORT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void bubblesort(int*,int); 4 | void swaps(int*,int*); 5 | int main() 6 | { 7 | int n; 8 | cout<<"\nenter the number of elements\n"; 9 | cin>>n; 10 | int arr[n]; 11 | cout<<"\nenter the values:\n"; 12 | for(int i=0;i> arr[i]; 14 | bubblesort(arr,n); 15 | cout<<"\nthe sorted array is:\n"; 16 | for(int i=0;iarr[i+1]) swaps(&arr[i],&arr[i+1]); 28 | } 29 | 30 | 31 | 32 | } 33 | 34 | 35 | void swaps(int *a,int *b) 36 | { 37 | int temp; 38 | temp=*a; 39 | *a=*b; 40 | *b=temp; 41 | } 42 | 43 | 44 | 45 | /* 46 | 47 | 8 6 10 5 7 9 11 48 | 6 8 5 7 9 10 11 49 | 6 5 7 8 9 10 11 50 | 5 6 7 8 9 10 11 51 | 52 | */ 53 | -------------------------------------------------------------------------------- /Bellman_Ford.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct Edge 9 | { 10 | // This structure is equal to an edge. Edge contains two end points. These edges are directed edges so they 11 | //contain source and destination and some weight. These 3 are elements in this structure 12 | int source, destination, weight; 13 | }; 14 | 15 | // a structure to represent a connected, directed and weighted graph 16 | struct Graph 17 | { 18 | int V, E; 19 | // V is number of vertices and E is number of edges 20 | 21 | struct Edge* edge; 22 | // This structure contain another structure which we already created edge. 23 | }; 24 | 25 | struct Graph* createGraph(int V, int E) 26 | { 27 | struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph)); 28 | //Allocating space to structure graph 29 | 30 | graph->V = V; //assigning values to structure elements that taken form user. 31 | 32 | graph->E = E; 33 | 34 | graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) ); 35 | //Creating "Edge" type structures inside "Graph" structure, the number of edge type structures are equal to number of edges 36 | 37 | return graph; 38 | } 39 | 40 | void FinalSolution(int dist[], int n) 41 | { 42 | // This function prints the final solution 43 | cout<<"\nVertex\tDistance from Source Vertex\n"; 44 | int i; 45 | 46 | for (i = 0; i < n; ++i){ 47 | cout<V; 54 | 55 | int E = graph->E; 56 | 57 | int StoreDistance[V]; 58 | 59 | int i,j; 60 | 61 | // This is initial step that we know , we initialize all distance to infinity except source. 62 | // We assign source distance as 0(zero) 63 | 64 | for (i = 0; i < V; i++) 65 | StoreDistance[i] = INT_MAX; 66 | 67 | StoreDistance[source] = 0; 68 | 69 | //The shortest path of graph that contain V vertices, never contain "V-1" edges. So we do here "V-1" relaxations 70 | for (i = 1; i <= V-1; i++) 71 | { 72 | for (j = 0; j < E; j++) 73 | { 74 | int u = graph->edge[j].source; 75 | 76 | int v = graph->edge[j].destination; 77 | 78 | int weight = graph->edge[j].weight; 79 | 80 | if (StoreDistance[u] + weight < StoreDistance[v]) 81 | StoreDistance[v] = StoreDistance[u] + weight; 82 | } 83 | } 84 | 85 | // Actually upto now shortest path found. But BellmanFord checks for negative edge cycle. In this step we check for that 86 | // shortest distances if graph doesn't contain negative weight cycle. 87 | 88 | // If we get a shorter path, then there is a negative edge cycle. 89 | for (i = 0; i < E; i++) 90 | { 91 | int u = graph->edge[i].source; 92 | 93 | int v = graph->edge[i].destination; 94 | 95 | int weight = graph->edge[i].weight; 96 | 97 | if (StoreDistance[u] + weight < StoreDistance[v]) 98 | cout<<"\nThis graph contains negative edge cycle\n"; 99 | } 100 | 101 | FinalSolution(StoreDistance, V); 102 | 103 | return; 104 | } 105 | 106 | int main() 107 | { 108 | int V,E,S; //V = no.of Vertices, E = no.of Edges, S is source vertex 109 | 110 | cout<<"Enter number of vertices in graph\n"; 111 | cin>>V; 112 | 113 | cout<<"Enter number of edges in graph\n"; 114 | cin>>E; 115 | 116 | cout<<"Enter your source vertex number\n"; 117 | cin>>S; 118 | 119 | struct Graph* graph = createGraph(V, E); //calling the function to allocate space to these many vertices and edges 120 | 121 | int i; 122 | for(i=0;i>graph->edge[i].source; 125 | cin>>graph->edge[i].destination; 126 | cin>>graph->edge[i].weight; 127 | } 128 | 129 | BellmanFord(graph, S); 130 | //passing created graph and source vertex to BellmanFord Algorithm function 131 | 132 | return 0; 133 | } -------------------------------------------------------------------------------- /Breadth_First_Search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Graph 8 | { 9 | int numberVertex; 10 | vector *adjacency; 11 | 12 | public: 13 | // Constructor to initialise graph 14 | Graph(int numberVertex) 15 | { 16 | this->numberVertex = numberVertex; 17 | adjacency = new vector [numberVertex]; 18 | } 19 | 20 | // Function to add edge between source and destination 21 | void addEdge(int source, int destination) 22 | { 23 | adjacency[source].push_back(destination); 24 | } 25 | 26 | // Function to perform Breadth First Search 27 | void bfs(int starting); 28 | }; 29 | 30 | void Graph::bfs(int starting) 31 | { 32 | bool visited[numberVertex]; 33 | 34 | for (int i = 0; i < numberVertex; i++) 35 | visited[i] = false; 36 | 37 | queue queue_vertex; 38 | 39 | visited[starting] = true; 40 | queue_vertex.push(starting); 41 | 42 | while (!queue_vertex.empty()) 43 | { 44 | starting = queue_vertex.front(); 45 | cout << starting << " "; 46 | queue_vertex.pop(); 47 | 48 | for (vector :: iterator it = adjacency[starting].begin(); it != adjacency[starting].end(); ++it) 49 | { 50 | if(!visited[*it]) 51 | { 52 | visited[*it] = true; 53 | queue_vertex.push(*it); 54 | } 55 | } 56 | } 57 | } 58 | 59 | int main() 60 | { 61 | // Number of vertices is 8 62 | Graph graph(8); 63 | 64 | // Create edges between vertices 65 | graph.addEdge(0, 1); 66 | graph.addEdge(0, 2); 67 | graph.addEdge(1, 2); 68 | graph.addEdge(1, 4); 69 | graph.addEdge(2, 0); 70 | graph.addEdge(2, 3); 71 | graph.addEdge(3, 3); 72 | graph.addEdge(3, 6); 73 | graph.addEdge(4, 0); 74 | graph.addEdge(4, 5); 75 | graph.addEdge(5, 6); 76 | graph.addEdge(5, 7); 77 | graph.addEdge(6, 2); 78 | graph.addEdge(7, 3); 79 | 80 | cout << "Breadth First Traversal is : "; 81 | graph.bfs(0); 82 | 83 | return 0; 84 | } 85 | 86 | 87 | /* Output 88 | 89 | Breadth First Traversal is : 0 1 2 4 3 5 6 7 90 | 91 | */ 92 | -------------------------------------------------------------------------------- /Depth_First_Search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Graph 8 | { 9 | int numberVertex; 10 | vector *adjacency; 11 | 12 | public: 13 | // Constructor to initialise graph 14 | Graph(int numberVertex) 15 | { 16 | this->numberVertex = numberVertex; 17 | adjacency = new vector [numberVertex]; 18 | } 19 | 20 | // Function to add edge between source and destination 21 | void addEdge(int source, int destination) 22 | { 23 | adjacency[source].push_back(destination); 24 | } 25 | 26 | // Function to perform Depth First Search 27 | void dfs(int starting); 28 | }; 29 | 30 | void Graph::dfs(int starting) 31 | { 32 | bool visited[numberVertex]; 33 | 34 | for(int i = 0; i < numberVertex; i++) 35 | visited[i] = false; 36 | 37 | stack stack_vertex; 38 | 39 | visited[starting] = true; 40 | stack_vertex.push(starting); 41 | 42 | vector :: iterator it; 43 | 44 | while(!stack_vertex.empty()) 45 | { 46 | starting = stack_vertex.top(); 47 | cout << starting << " "; 48 | stack_vertex.pop(); 49 | 50 | for(it = adjacency[starting].begin(); it != adjacency[starting].end(); ++it) 51 | { 52 | if(!visited[*it]) 53 | { 54 | visited[*it] = true; 55 | stack_vertex.push(*it); 56 | } 57 | } 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | // Number of vertices is 8 64 | Graph graph(8); 65 | 66 | // Create edges between vertices 67 | graph.addEdge(0, 1); 68 | graph.addEdge(0, 2); 69 | graph.addEdge(1, 2); 70 | graph.addEdge(1, 4); 71 | graph.addEdge(2, 0); 72 | graph.addEdge(2, 3); 73 | graph.addEdge(3, 3); 74 | graph.addEdge(3, 6); 75 | graph.addEdge(4, 0); 76 | graph.addEdge(4, 5); 77 | graph.addEdge(5, 6); 78 | graph.addEdge(5, 7); 79 | graph.addEdge(6, 2); 80 | graph.addEdge(7, 3); 81 | 82 | cout << "Depth First Traversal is : "; 83 | graph.dfs(0); 84 | 85 | return 0; 86 | } 87 | 88 | 89 | /* Output 90 | 91 | Depth First Traversal is : 0 2 3 6 1 4 5 7 92 | 93 | */ 94 | -------------------------------------------------------------------------------- /avl_tree.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Souvik Biswas 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | #include 22 | using namespace std; 23 | 24 | // Functions for inserting an element in the tree 25 | // and creating the tree by calling insert method repetadly 26 | struct node *insert(node *, int ); 27 | struct node *create(node *); 28 | 29 | // Function for RR rotation 30 | struct node *RRrotation(node *); 31 | 32 | // Function for LL rotation 33 | struct node *LLrotation(node *); 34 | 35 | // Function for traversing the tree 36 | void inorderTraversal(node *); 37 | 38 | // Function for finding height of the tree 39 | int avlHeight(node *); 40 | 41 | // Function for finding the maximum value 42 | int max(int , int); 43 | 44 | // Functon for getting the Balance Factor of the avl tree 45 | // i.e., left height - right height 46 | int getBalanceFactor(node *); 47 | 48 | 49 | /* 50 | 51 | Declaring a structure called node 52 | having four members: 53 | 54 | 1) left block (which stores the address of the left node) 55 | 2) right block (which stores the address of the right node) 56 | 3) data block (which stores the value) 57 | 4) height block (which stores the height of the tree) 58 | 59 | */ 60 | 61 | struct node { 62 | node *left; 63 | node *right; 64 | int data; 65 | int height; 66 | }; 67 | 68 | 69 | // Function for finding the height of the tree 70 | // If tree is empty return 0, otherwise return height of the tree 71 | int avlHeight(node *tree) { 72 | if(tree == NULL) { 73 | return 0; 74 | } 75 | else { 76 | return tree->height; 77 | } 78 | } 79 | 80 | 81 | // Function for finding the max value 82 | int max(int a, int b) { 83 | return (a > b) ? a : b; 84 | } 85 | 86 | // Function to calculate the balance factor of a node 87 | // Balance factor = left height - right height 88 | int getBalanceFactor(node *tree) { 89 | if(tree == NULL) { 90 | return 0; 91 | } 92 | 93 | return avlHeight(tree->left) - avlHeight(tree->right); 94 | } 95 | 96 | // ALGORITHM FOR LL ROTATION 97 | node *LLrotation(node *x) { 98 | node *y = x->right; 99 | node *T2 = y->left; 100 | 101 | y->left = x; 102 | x->right = T2; 103 | 104 | x->height = max(avlHeight(x->left), avlHeight(x->right)) + 1; 105 | y->height = max(avlHeight(y->left), avlHeight(y->right)) + 1; 106 | 107 | return y; 108 | } 109 | 110 | // ALGORITHM FOR RR ROTATION 111 | node *RRrotation(node *y) { 112 | node *x = y->left; 113 | node *T2 = x->right; 114 | 115 | x->right = y; 116 | y->left = T2; 117 | 118 | x->height = max(avlHeight(x->left), avlHeight(x->right)) + 1; 119 | y->height = max(avlHeight(y->left), avlHeight(y->right)) + 1; 120 | 121 | return x; 122 | } 123 | 124 | 125 | /* 126 | 127 | ALGORITHM TO INSERT AN ELEMENT IN THE TREE: 128 | 129 | 1) IF AVAIL = NULL 130 | WRITE Overflow 131 | EXIT 132 | 133 | 2) NEW_NODE = AVAIL 134 | 3) AVAIL = AVAIL -> LEFT 135 | 4) NEW_NODE -> DATA = VALUE 136 | 5) NEW_NODE -> LEFT = NULL 137 | 6) NEW_NODE -> RIGHT = NULL 138 | 7) NEW_NODE -> HEIGHT = 1 139 | 8) IF TREE = NULL 140 | RETRUN NEW_NODE 141 | 142 | 9) IF VALUE < TREE -> DATA 143 | TREE -> LEFT = RECURSIVE CALL PASSING TREE -> LEFT & VALUE 144 | 145 | 10) ELSE IF VALUE > TREE -> DATA 146 | TREE -> RIGHT = RECURSIVE CALL PASSING TREE -> RIGHT & VALUE 147 | 148 | 11) ELSE 149 | RETURN TREE 150 | 151 | 12) TREE -> HEIGHT = MAX(HEIGHT OF TREE -> LEFT, HEIGHT OF TREE -> RIGHT) + 1 152 | 13) BALANCE_FACTOR = GET BALANCE FACTOR PASSING TREE 153 | 14) IF BALANCE_FACTOR > 1 && VALUE < TREE -> LEFT -> DATA 154 | RETURN RRrotation(TREE); 155 | 156 | 15) IF BALANCE_FACTOR < -1 && VALUE > TREE -> RIGHT -> DATA 157 | RETURN LLrotation(TREE); 158 | 159 | 16) IF BALANCE_FACTOR > 1 && VALUE > TREE -> LEFT -> DATA 160 | TREE -> LEFT = LLrotation(TREE -> LEFT) 161 | RETURN RRrotation(TREE); 162 | 163 | 17) IF BALANCE_FACTOR < -1 && VALUE < TREE -> RIGHT -> DATA 164 | TREE -> RIGHT = RRrotation(TREE -> RIGHT) 165 | RETURN LLrotation(TREE); 166 | 167 | 18) RETURN TREE 168 | 19) EXIT 169 | 170 | */ 171 | 172 | node *insert(node *tree, int value) { 173 | // Alocating space for a new node 174 | node *new_node = new node; 175 | 176 | // Storing data in new node 177 | new_node->data = value; 178 | new_node->left = NULL; 179 | new_node->right = NULL; 180 | new_node->height = 1; 181 | 182 | // When node is empty 183 | if(tree == NULL) { 184 | return new_node; 185 | } 186 | 187 | // When the value to be inserted is less then tree -> data 188 | if(value < tree->data) { 189 | tree->left = insert(tree->left, value); 190 | } 191 | // When the value to be inserted is greater then tree -> data 192 | else if(value > tree->data) { 193 | tree->right = insert(tree->right, value); 194 | } 195 | // When the value to be inserted is equal to tree -> data 196 | else { 197 | return tree; 198 | } 199 | 200 | // Calculating the height of the tree 201 | tree->height = max(avlHeight(tree->left), avlHeight(tree->right)) + 1; 202 | 203 | // Calculating the balance factor of the tree 204 | int balanceFactor = getBalanceFactor(tree); 205 | 206 | // For RR rotation 207 | if(balanceFactor > 1 && value < tree->left->data) { 208 | return RRrotation(tree); 209 | } 210 | 211 | // For LL rotation 212 | if(balanceFactor < -1 && value > tree->right->data) { 213 | return LLrotation(tree); 214 | } 215 | 216 | // For LR rotation 217 | if(balanceFactor > 1 && value > tree->left->data) { 218 | tree->left = LLrotation(tree->left); 219 | return RRrotation(tree); 220 | } 221 | 222 | // For RL rotation 223 | if(balanceFactor < -1 && value < tree->right->data) { 224 | tree->right = RRrotation(tree->right); 225 | return LLrotation(tree); 226 | } 227 | 228 | return tree; 229 | } 230 | 231 | 232 | // Creating the AVL tree by calling insert method repetadly 233 | node *create(node *tree) { 234 | int value; 235 | 236 | cout<<"Enter the value to be inserted: "; 237 | cin>>value; 238 | 239 | while(value != -1) { 240 | tree = insert(tree, value); 241 | 242 | cout<<"Enter the value to be inserted: "; 243 | cin>>value; 244 | } 245 | 246 | return tree; 247 | } 248 | 249 | void inorderTraversal(node *tree) { 250 | if(tree != NULL) { 251 | inorderTraversal(tree->left); 252 | cout<data<<" "; 253 | inorderTraversal(tree->right); 254 | } 255 | } 256 | 257 | 258 | // MAIN FUNCTION 259 | int main() { 260 | node *root = NULL; 261 | 262 | int option, value; 263 | 264 | do { 265 | cout<<"\n******* MENU *******\n" 266 | <<"1. Create\n" 267 | <<"2. Insert\n" 268 | <<"3. Inorder Traversal\n" 269 | <<"4. Height\n" 270 | <<"5. Exit\n"; 271 | 272 | cout<<"Enter your option: "; 273 | cin>>option; 274 | 275 | switch(option) { 276 | case 1: root = create(root); 277 | break; 278 | 279 | case 2: cout<<"Enter the value to be inserted: "; 280 | cin>>value; 281 | 282 | root = insert(root, value); 283 | break; 284 | 285 | case 3: inorderTraversal(root); 286 | cout< 22 | using namespace std; 23 | 24 | // Functions for inserting an element in the tree 25 | // and creating the tree by calling insert method repetadly 26 | struct node *insert(node *, int ); 27 | struct node *create(node *); 28 | 29 | // Functions for finding smallest and largest element of the tree 30 | struct node *findSmallestElement(node *); 31 | struct node *findLargestElement(node *); 32 | 33 | // Functions for traversing the tree 34 | void preorderTraversal(node *); 35 | void inorderTraversal(node *); 36 | void postorderTraversal(node *); 37 | 38 | // Functions for finding the number of nodes 39 | int totalNodes(node *); 40 | int totalExternalNodes(node *); 41 | int totalInternalNodes(node *); 42 | 43 | // Function for calculating the height of the tree 44 | int height(node *); 45 | 46 | // Function for forming the mirror image of the tree 47 | void mirrorImage(node *); 48 | 49 | // Function for deleting the whole tree 50 | void deleteTree(node *); 51 | 52 | // Function for deleting an element of the tree 53 | struct node *deleteElement(node *, int ); 54 | 55 | 56 | /* 57 | 58 | Declaring a structure called node 59 | having three members: 60 | 61 | 1) left block (which stores the address of the left node) 62 | 2) data block (which stores the value) 63 | 3) right block (which stores the address of the right node) 64 | 65 | */ 66 | 67 | struct node { 68 | node *left; 69 | int data; 70 | node *right; 71 | }; 72 | 73 | 74 | /* 75 | 76 | ALGORITHM FOR INSERTING A NODE IN THE TREE: 77 | 78 | 1) IF AVAIL = NULL 79 | WRITE Overflow 80 | EXIT 81 | 82 | 2) NEW_NODE = AVAIL 83 | 3) AVAIL = AVAIL -> LEFT 84 | 4) NEW_NODE -> DATA = VALUE 85 | 5) NEW_NODE -> LEFT = NULL 86 | 6) NEW_NODE -> RIGHT = NULL 87 | 7) IF TREE = NULL 88 | TREE = NEW_NODE 89 | 90 | 8) ELSE 91 | 9) PAR = NULL 92 | 10) CUR = TREE 93 | 11) WHILE CUR != NULL 94 | 12) PAR = CUR 95 | 13) IF VALUE < CUR -> data 96 | CUR = CUR -> LEFT 97 | 98 | 14) ELSE 99 | CUR = CUR -> RIGHT 100 | [END OF IF] 101 | [END OF WHILE] 102 | 103 | 15) IF VALUE < PAR -> DATA 104 | PAR -> LEFT = NEW_NODE 105 | 106 | 16) ELSE 107 | PAR -> RIGHT = NEW_NODE 108 | 109 | 17) EXIT 110 | 111 | */ 112 | 113 | node *insert(node *tree, int value) { 114 | // Creating a new node 115 | node *new_node = new node; 116 | // Storing values in all blocks of new_node 117 | new_node->data = value; 118 | new_node->left = NULL; 119 | new_node->right = NULL; 120 | 121 | // When tree is null, just store the new_node in tree 122 | if(tree == NULL) { 123 | tree = new_node; 124 | } 125 | else { 126 | // Setting parent node to null 127 | node *parentNode = NULL; 128 | // Storing tree in current node 129 | node *currentNode = tree; 130 | 131 | // Repeat when current node is null 132 | while(currentNode != NULL) { 133 | // Storing current node in parent node 134 | parentNode = currentNode; 135 | 136 | // When the value to be inserted is less than the current node, 137 | // we have to go to the left branch and otherwise right 138 | if(value < currentNode->data) { 139 | currentNode = currentNode->left; 140 | } 141 | else { 142 | currentNode = currentNode->right; 143 | } 144 | } 145 | 146 | // When the value is less than the parent node, 147 | // we have to go to the left branch and otherwise right 148 | if(value < parentNode->data) { 149 | parentNode->left = new_node; 150 | } 151 | else { 152 | parentNode->right = new_node; 153 | } 154 | } 155 | 156 | return tree; 157 | } 158 | 159 | // Creating the tree by calling insert method repetadly 160 | node *create(node *tree) { 161 | int value; 162 | 163 | // Taking the value to be inserted as input 164 | cout<<"Enter the value to be inserted: "; 165 | cin>>value; 166 | 167 | // When the value is -1, then stop storing more values 168 | while(value != -1) { 169 | tree = insert(tree, value); 170 | 171 | // Taking the value to be inserted as input 172 | cout<<"Enter the value to be inserted: "; 173 | cin>>value; 174 | } 175 | 176 | return tree; 177 | } 178 | 179 | 180 | /* 181 | 182 | ALGORITHM FOR FINDING THE SMALLEST ELEMENT OF THE TREE: 183 | 184 | 1) IF TREE = NULL OR TREE -> LEFT = NULL 185 | RETURN TREE 186 | 2) ELSE 187 | RETURN ( RECURSIVELY CALLING FIND_SMALLEST_ELEMENT PASSING TREE -> LEFT) 188 | 189 | 3) EXIT 190 | 191 | */ 192 | 193 | node *findSmallestElement(node *tree) { 194 | if((tree == NULL) || (tree->left == NULL)) { 195 | return tree; 196 | } 197 | else { 198 | return findSmallestElement(tree->left); 199 | } 200 | } 201 | 202 | 203 | /* 204 | 205 | ALGORITHM FOR FINDING THE LARGEST ELEMENT OF THE TREE: 206 | 207 | 1) IF TREE = NULL OR TREE -> RIGHT = NULL 208 | RETURN TREE 209 | 2) ELSE 210 | RETURN ( RECURSIVELY CALLING FIND_SMALLEST_ELEMENT PASSING TREE -> RIGHT) 211 | 212 | 3) EXIT 213 | 214 | */ 215 | 216 | node *findLargestElement(node *tree) { 217 | if((tree == NULL) || (tree->right == NULL)) { 218 | return tree; 219 | } 220 | else { 221 | return findLargestElement(tree->right); 222 | } 223 | } 224 | 225 | 226 | /* 227 | 228 | ALGORITHM FOR PREORDER TRAVERSAL: 229 | 230 | 1) IF TREE != NULL 231 | 2) PRINT TREE -> DATA 232 | 3) RECURSIVELY CALL PREORDER_TRAVERSAL PASSING TREE -> LEFT 233 | 4) RECURSIVELY CALL PREORDER_TRAVERSAL PASSING TREE -> RIGHT 234 | [END OF IF] 235 | 236 | 5) EXIT 237 | 238 | */ 239 | 240 | void preorderTraversal(node *tree) { 241 | if(tree != NULL) { 242 | cout<data<<" "; 243 | preorderTraversal(tree->left); 244 | preorderTraversal(tree->right); 245 | } 246 | } 247 | 248 | 249 | /* 250 | 251 | ALGORITHM FOR INORDER TRAVERSAL: 252 | 253 | 1) IF TREE != NULL 254 | 2) RECURSIVELY CALL INORDER_TRAVERSAL PASSING TREE -> LEFT 255 | 3) PRINT TREE -> DATA 256 | 4) RECURSIVELY CALL INORDER_TRAVERSAL PASSING TREE -> RIGHT 257 | [END OF IF] 258 | 259 | 5) EXIT 260 | 261 | */ 262 | 263 | void inorderTraversal(node *tree) { 264 | if(tree != NULL) { 265 | inorderTraversal(tree->left); 266 | cout<data<<" "; 267 | inorderTraversal(tree->right); 268 | } 269 | } 270 | 271 | 272 | /* 273 | 274 | ALGORITHM FOR POSTORDER TRAVERSAL: 275 | 276 | 1) IF TREE != NULL 277 | 2) RECURSIVELY CALL POSTORDER_TRAVERSAL PASSING TREE -> LEFT 278 | 3) RECURSIVELY CALL POSTORDER_TRAVERSAL PASSING TREE -> RIGHT 279 | 4) PRINT TREE -> DATA 280 | [END OF IF] 281 | 282 | 5) EXIT 283 | 284 | */ 285 | 286 | void postorderTraversal(node *tree) { 287 | if(tree != NULL) { 288 | postorderTraversal(tree->left); 289 | postorderTraversal(tree->right); 290 | cout<data<<" "; 291 | } 292 | } 293 | 294 | 295 | /* 296 | 297 | ALGORITHM FOR COUNTING THE TOTAL NUMBER OF NODES: 298 | 299 | 1) IF TREE = NULL 300 | RETURN 0 301 | 302 | 2) ELSE 303 | RETURN (RECURSIVE CALL PASSING TREE -> LEFT 304 | + RECURSIVE CALL PASSING TREE -> RIGHT + 1) 305 | 306 | [END OF IT] 307 | 308 | 3) EXIT 309 | 310 | */ 311 | 312 | int totalNodes(node *tree) { 313 | if(tree == NULL) { 314 | return 0; 315 | } 316 | else { 317 | return (totalNodes(tree->left) + totalNodes(tree->right) + 1); 318 | } 319 | } 320 | 321 | 322 | /* 323 | 324 | ALGORITHM FOR COUNTING THE TOTAL NUMBER OF EXTERNAL NODES: 325 | 326 | 1) IF TREE = NULL 327 | RETURN 0 328 | 329 | 2) ELSE IF TREE -> LEFT = NULL && TREE -> RIGHT = NULL 330 | RETRUN 1 331 | 332 | 3) ELSE 333 | RETRUN (RECURSIVE CALL PASSING TREE -> LEFT 334 | + RECURSIVE CALL PASSING TREE -> RIGHT) 335 | [END OF IF] 336 | 337 | 4) EXIT 338 | 339 | */ 340 | 341 | int totalExternalNodes(node *tree) { 342 | if(tree == NULL) { 343 | return 0; 344 | } 345 | else if(tree->left == NULL && tree->right == NULL) { 346 | return 1; 347 | } 348 | else { 349 | return (totalExternalNodes(tree->left) + totalExternalNodes(tree->right)); 350 | } 351 | } 352 | 353 | 354 | /* 355 | 356 | ALGORITHM FOR COUNTING THE TOTAL NUMBER OF INTERNAL NODES: 357 | 358 | 1) IF TREE = NULL OR (TREE -> LEFT = NULL && TREE -> RIGHT = NULL) 359 | RETURN 0 360 | 361 | 2) ELSE 362 | RETRUN (RECURSIVE CALL PASSING TREE -> LEFT 363 | + RECURSIVE CALL PASSING TREE -> RIGHT + 1) 364 | [END OF IF] 365 | 366 | 3) EXIT 367 | 368 | */ 369 | 370 | int totalInternalNodes(node *tree) { 371 | if(tree == NULL || (tree->left == NULL && tree->right == NULL)) { 372 | return 0; 373 | } 374 | else { 375 | return (totalInternalNodes(tree->left) + totalInternalNodes(tree->right) + 1); 376 | } 377 | } 378 | 379 | 380 | /* 381 | 382 | ALGORITHM FOR CALCULATING THE HEIGHT OF THE TREE: 383 | 384 | 1) IF TREE = NULL 385 | RETURN 0 386 | 387 | 2) ELSE 388 | LEFT_HEIGHT = RECURSIVE CALL PASSING TREE -> LEFT 389 | RIGHT_HEGHT = RECURSIVE CALL PASSING TREE -> RIGHT 390 | 391 | IF LEFT_HEIGHT > RIGHT_HEGHT 392 | RETURN LEFT_HEIGHT + 1 393 | 394 | ELSE 395 | RETURN RIGHT_HEGHT + 1 396 | [END OF IF] 397 | [END OF IF] 398 | 399 | 3) EXIT 400 | 401 | */ 402 | 403 | int height(node *tree) { 404 | if(tree == NULL) { 405 | return 0; 406 | } 407 | else { 408 | int leftHeight = height(tree->left); 409 | int rightHeight = height(tree->right); 410 | 411 | if(leftHeight > rightHeight) { 412 | return (leftHeight + 1); 413 | } 414 | else { 415 | return (rightHeight + 1); 416 | } 417 | } 418 | } 419 | 420 | 421 | /* 422 | 423 | ALGORITHM FOR CREATING MIRROR IMAGE OF THE TREE: 424 | 425 | 1) IF TREE != NULL 426 | 2) RECURSIVE CALL PASSING TREE -> LEFT 427 | 3) RECURSIVE CALL PASSING TREE -> RIGHT 428 | 4) PTR = TREE -> LEFT 429 | 5) TREE -> LEFT = TREE -> RIGHT 430 | 6) TREE -> RIGHT = PTR 431 | [END OF IF] 432 | 7) EXIT 433 | 434 | */ 435 | 436 | void mirrorImage(node *tree) { 437 | if(tree != NULL) { 438 | mirrorImage(tree->left); 439 | mirrorImage(tree->right); 440 | 441 | node *ptr = tree->left; 442 | tree->left = tree->right; 443 | tree->right = ptr; 444 | } 445 | } 446 | 447 | 448 | /* 449 | 450 | ALGORITHM FOR DELETING THE WHOLE TREE: 451 | 452 | 1) IF TREE != NULL 453 | 2) RECURSIVE CALL PASSING TREE -> LEFT 454 | 3) RECURSIVE CALL PASSING TREE -> RIGHT 455 | 4) DELETE TREE 456 | [END OF IF] 457 | 5) EXIT 458 | 459 | */ 460 | void deleteTree(node *tree) { 461 | if(tree != NULL) { 462 | deleteTree(tree->left); 463 | deleteTree(tree->right); 464 | delete tree; 465 | } 466 | } 467 | 468 | 469 | /* 470 | 471 | ALGORITHM FOR DELETING AN ELEMENT OF THE TREE: 472 | 473 | 1) IF TREE = NULL 474 | RETURN TREE 475 | 476 | 2) IF VALUE < TREE -> DATA 477 | TREE -> LEFT = RECURSIVE CALL PASSING TREE -> LEFT AND VALUE 478 | 479 | 3) ELSE IF VALUE > TREE -> DATA 480 | TREE -> RIGHT = RECURSIVE CALL PASSING TREE -> RIGHT AND VALUE 481 | 482 | 4) ELSE 483 | IF TREE -> LEFT = NULL 484 | TEMP = TREE -> RIGHT 485 | DELETE TREE 486 | RETURN TEMP 487 | 488 | ELSE IF TREE -> RIGHT = NULL 489 | TEMP = TREE -> LEFT 490 | DELETE TREE 491 | RETURN TEMP 492 | 493 | TEMP = FIND THE SMALLEST ELEMENT IN THE RIGHT BRANCH OF TREE 494 | TREE -> DATA = TEMP -> DATA 495 | TREE -> RIGHT = RECURSIVE CALL PASSING TREE -> RIGHT AND TEMP -> DATA 496 | [END OF IF] 497 | 498 | 5) EXIT 499 | 500 | */ 501 | 502 | node *deleteElement(node *tree, int value) { 503 | if(tree == NULL) { 504 | return tree; 505 | } 506 | 507 | if(value < tree->data) { 508 | tree->left = deleteElement(tree->left, value); 509 | } 510 | else if(value > tree->data) { 511 | tree->right = deleteElement(tree->right, value); 512 | } 513 | else { 514 | if(tree->left == NULL) { 515 | node *temp = tree->right; 516 | delete tree; 517 | return temp; 518 | } 519 | else if(tree->right == NULL) { 520 | node *temp = tree->left; 521 | delete tree; 522 | return temp; 523 | } 524 | 525 | node *temp = findSmallestElement(tree->right); 526 | 527 | tree->data = temp->data; 528 | 529 | tree->right = deleteElement(tree->right, temp->data); 530 | } 531 | return tree; 532 | } 533 | 534 | 535 | // MAIN FUNCTION 536 | int main() { 537 | // Setting the root node to null, initially 538 | node *root = NULL; 539 | node *ptr; 540 | 541 | int option, value; 542 | 543 | do { 544 | cout<<"\n***** MENU *****\n" 545 | <<"1. Create\n" 546 | <<"2. Insert\n" 547 | <<"3. Search for the smallest element\n" 548 | <<"4. Search for the largest element\n" 549 | <<"5. Preorder Traversal\n" 550 | <<"6. Inorder Traversal\n" 551 | <<"7. Postorder Traversal\n" 552 | <<"8. Total number of nodes\n" 553 | <<"9. Total number of external nodes\n" 554 | <<"10. Total number of internal nodes\n" 555 | <<"11. Height of the tree\n" 556 | <<"12. Create mirror image of the tree\n" 557 | <<"13. Delete the whole tree\n" 558 | <<"14. Delete an element of the tree\n" 559 | <<"15. Exit\n"; 560 | 561 | cout<<"Enter your option: "; 562 | cin>>option; 563 | 564 | switch(option) { 565 | case 1: root = create(root); 566 | break; 567 | 568 | case 2: cout<<"Enter the value to be inserted: "; 569 | cin>>value; 570 | 571 | root = insert(root, value); 572 | break; 573 | 574 | case 3: ptr = findSmallestElement(root); 575 | cout<<"The smallest element is "<data<data<>value; 621 | 622 | root = deleteElement(root, value); 623 | cout<<"The deleted value is "< 22 | using namespace std; 23 | 24 | 25 | // Function prototypes 26 | struct node *insert_beg(node *, int ); 27 | struct node *insert_end(node *, int ); 28 | struct node *insert_after(node *, int ); 29 | struct node *insert_before(node *, int ); 30 | 31 | struct node *create(node *); 32 | 33 | struct node *del_beg(node *); 34 | struct node *del_end(node *); 35 | struct node *del_after(node *); 36 | struct node *del_before(node *); 37 | struct node *del_element(node *); 38 | 39 | void count(node *); 40 | void display(node *); 41 | 42 | 43 | /* 44 | 45 | Declaring a structure called node 46 | having three members: 47 | 48 | 1) prev block (which stores the address of the previous node) 49 | 2) data block 50 | 3) next block (which stores the address of the next node) 51 | 52 | */ 53 | 54 | struct node { 55 | node *prev; 56 | int data; 57 | node *next; 58 | }; 59 | 60 | 61 | /* 62 | 63 | ALGORITHM FOR INSERTING AT THE BEGINNING: 64 | 65 | 1) IF AVAIL = NULL 66 | Write Overflow 67 | EXIT 68 | 69 | 2) NEW_NODE = AVAIL 70 | 3) AVAIL = AVAIL -> NEXT 71 | 4) NEW_NODE -> DATA = VALUE 72 | 5) NEW_NODE -> PREV = START -> PREV 73 | 6) NEW_NODE -> NEXT = START 74 | 7) START -> PREV -> NEXT = NEW_NODE 75 | 8) START -> PREV = NEW_NODE 76 | 9) START = NEW_NODE 77 | 10) EXIT 78 | 79 | */ 80 | 81 | node *insert_beg(node *start, int value) { 82 | // Creating a new node 83 | node *new_node = new node; 84 | 85 | // Storing the data and assigning 86 | // which address to keep in the next block and prev block 87 | new_node->data = value; 88 | new_node->prev = start->prev; 89 | new_node->next = start; 90 | 91 | // Updating the next block of last node 92 | start->prev->next = new_node; 93 | 94 | // Updating the prev block of start to store the address 95 | start->prev = new_node; 96 | 97 | // making the start point to be first element of the linked list 98 | start = new_node; 99 | 100 | return start; 101 | } 102 | 103 | 104 | /* 105 | 106 | ALGORITHM FOR INSERTING AT THE END: 107 | 108 | 1) IF AVAIL = NULL 109 | Write Overflow 110 | EXIT 111 | 112 | 2) NEW_NODE = AVAIL 113 | 3) AVAIL = AVAIL -> NEXT 114 | 4) NEW_NODE -> DATA = VALUE 115 | 5) NEW_NODE -> PREV = START -> PREV 116 | 6) NEW_NODE -> NEXT = START 117 | 7) START -> PREV -> NEXT = NEW_NODE 118 | 8) START -> PREV = NEW_NODE 119 | 9) EXIT 120 | 121 | */ 122 | 123 | node *insert_end(node *start, int value) { 124 | // Creating a new node 125 | node *new_node = new node; 126 | 127 | // Storing the data and assigning 128 | // which address to keep in the next block 129 | new_node->data = value; 130 | new_node->prev = start->prev; 131 | new_node->next = start; 132 | 133 | // Accommodating the new node at the end 134 | start->prev->next = new_node; 135 | start->prev = new_node; 136 | 137 | return start; 138 | } 139 | 140 | 141 | /* 142 | 143 | ALGORITHM FOR INSERTING AFTER A GIVEN NODE: 144 | 145 | 1) IF AVAIL = NULL 146 | Write Overflow 147 | EXIT 148 | 149 | 2) NEW_NODE = AVAIL 150 | 3) AVAIL = AVAIL -> NEXT 151 | 4) NEW_NODE -> DATA = VALUE 152 | 5) PTR = START 153 | 6) WHILE ptr -> DATA != NUM 154 | PTR = PTR -> NEXT 155 | 156 | 7) NEW_NODE -> NEXT = PTR -> NEXT 157 | 8) NEW_NODE -> PREV = PTR 158 | 9) PTR -> NEXT -> PREV = NEW_NODE 159 | 10) PTR -> NEXT = NEW_NODE 160 | 11) EXIT 161 | 162 | */ 163 | 164 | node *insert_after(node *start, int value) { 165 | int num; 166 | 167 | // Number after which to insert 168 | cout<<"Enter the number after which to insert: "; 169 | cin>>num; 170 | 171 | // Creating a new node 172 | node *new_node = new node; 173 | 174 | // Storing the data 175 | new_node->data = value; 176 | 177 | // Storing the value of start to ptr 178 | node *ptr = start; 179 | 180 | // Incrementing till the number is found 181 | while(ptr->data != num) { 182 | ptr = ptr->next; 183 | } 184 | 185 | // Adding the new node 186 | new_node->next = ptr->next; 187 | new_node->prev = ptr; 188 | ptr->next->prev = new_node; 189 | ptr->next = new_node; 190 | 191 | return start; 192 | } 193 | 194 | 195 | /* 196 | 197 | ALGORITHM FOR INSERTING BEFORE A GIVEN NODE: 198 | 199 | 1) IF AVAIL = NULL 200 | Write Overflow 201 | EXIT 202 | 203 | 2) NEW_NODE = AVAIL 204 | 3) AVAIL = AVAIL -> NEXT 205 | 4) NEW_NODE -> DATA = VALUE 206 | 5) PTR = START 207 | 6) WHILE PTR -> NEXT != NUM 208 | PREPTR = PTR 209 | PTR = PTR -> NEXT 210 | 211 | 7) NEW_NODE -> NEXT = PTR 212 | 8) NEW_NODE -> PREV = PTR -> PREV 213 | 9) PTR -> PREV -> NEXT = NEW_NODE 214 | 10) PTR -> PREV = NEW_NODE 215 | 11) EXIT 216 | 217 | */ 218 | 219 | node *insert_before(node *start, int value) { 220 | int num; 221 | 222 | // Number after which to insert 223 | cout<<"Enter the number before which to insert: "; 224 | cin>>num; 225 | 226 | // Solving edge case #1 227 | if(start->data == num) { 228 | // If the element is present is to be inserted before a number, 229 | // which is in the beginning, then use the insert_beg method 230 | // to insert the new value. 231 | start = insert_beg(start, value); 232 | } 233 | else { 234 | // Creating a new node 235 | node *new_node = new node; 236 | 237 | // Storing the data 238 | new_node->data = value; 239 | 240 | // Storing the value of start to ptr 241 | node *ptr = start; 242 | 243 | // Incrementing till the number is found 244 | while(ptr->data != num) { 245 | ptr = ptr->next; 246 | } 247 | 248 | // Adding the new node 249 | new_node->next = ptr; 250 | new_node->prev = ptr->prev; 251 | ptr->prev->next = new_node; 252 | ptr->prev = new_node; 253 | } 254 | 255 | return start; 256 | } 257 | 258 | 259 | // For creating the entire linked list 260 | node *create(node *start) { 261 | int value; 262 | 263 | // Storing the value to be inserted 264 | cout<<"Enter -1 to stop\n"; 265 | cout<<"Enter the value to be stored: "; 266 | cin>>value; 267 | 268 | while(value != -1){ 269 | if(start == NULL) { 270 | node *new_node = new node; 271 | new_node->data = value; 272 | 273 | start = new_node; 274 | 275 | new_node->prev = start; 276 | new_node->next = start; 277 | } 278 | else { 279 | // Elements are inserted at the end 280 | insert_end(start, value); 281 | } 282 | 283 | cout<<"Enter the value to be stored: "; 284 | cin>>value; 285 | } 286 | 287 | return start; 288 | } 289 | 290 | 291 | /* 292 | 293 | ALGORITHM FOR DELETING FROM THE BEGINNING: 294 | 295 | 1) IF START = NULL 296 | Write Underflow 297 | EXIT 298 | 299 | 2) PTR = START 300 | 3) PTR -> PREV -> NEXT = START -> NEXT 301 | 4) PTR -> NEXT -> PREV = START -> PREV 302 | 4) DELETE START 303 | 5) START = PTR -> NEXT 304 | 6) EXIT 305 | 306 | */ 307 | 308 | node *del_beg(node *start) { 309 | // Checking if the linked list is empty 310 | if(start == NULL) { 311 | cout<<"The linked list is empty !\n"; 312 | } 313 | else { 314 | // Storing start to ptr 315 | node *ptr = start; 316 | 317 | ptr->prev->next = start->next; 318 | ptr->next->prev = start->prev; 319 | 320 | // Printing the value to be deleted 321 | cout<<"The deleted value is: "<data<next; 327 | } 328 | 329 | return start; 330 | } 331 | 332 | 333 | /* 334 | 335 | ALGORITHM FOR DELETING FROM THE END: 336 | 337 | 1) IF START = NULL 338 | Write Underflow 339 | EXIT 340 | 341 | 2) PTR = START -> PREV 342 | 3) PTR -> PREV -> NEXT = START; 343 | 4) START -> PREV = PTR -> PREV 344 | 5) DELETE PTR 345 | 6) EXIT 346 | 347 | */ 348 | 349 | node *del_end(node *start) { 350 | // Checking if the linked list is empty 351 | if(start == NULL) { 352 | cout<<"The linked list is empty !\n"; 353 | } 354 | else { 355 | // Storing start to ptr 356 | node *ptr = start->prev; 357 | 358 | ptr->prev->next = start; 359 | start->prev = ptr->prev; 360 | 361 | // Printing the value to be deleted 362 | cout<<"The deleted value is: "<data< DATA != NUM 382 | PTR = PTR -> NEXT 383 | 384 | 4) TEMP = PTR -> NEXT 385 | 5) TEMP -> NEXT -> PREV = PTR 386 | 5) PTR -> NEXT = TEMP -> NEXT 387 | 6) DELETE TEMP 388 | 7) EXIT 389 | 390 | */ 391 | 392 | node *del_after(node *start) { 393 | // Checking if the linked list is empty 394 | if(start == NULL) { 395 | cout<<"The linked list is empty !\n"; 396 | } 397 | else { 398 | int num; 399 | 400 | // Storing the element, after which to be deleted 401 | cout<<"Enter the element after which to be deleted: "; 402 | cin>>num; 403 | 404 | // Storing start to ptr 405 | node *ptr = start; 406 | 407 | // Incrementing till num is found 408 | while(ptr->data != num) { 409 | ptr = ptr->next; 410 | } 411 | 412 | // Storing the node which is to be deleted 413 | node *temp = ptr->next; 414 | 415 | // Updating the stored addresses in blocks 416 | temp->next->prev = ptr; 417 | ptr->next = temp->next; 418 | 419 | // Printing the value to be deleted 420 | cout<<"The deleted value is: "<data< DATA != NUM 440 | PTR = PTR -> NEXT 441 | 442 | 4) TEMP = PTR -> PREV 443 | 5) TEMP -> PREV -> NEXT = PTR 444 | 6) PTR -> PREV = TEMP -> PREV 445 | 7) DELETE TEMP 446 | 8) EXIT 447 | 448 | */ 449 | 450 | node *del_before(node *start) { 451 | // Checking if the linked list is empty 452 | if(start == NULL) { 453 | cout<<"The linked list is empty !\n"; 454 | } 455 | else { 456 | int num; 457 | 458 | // Storing the element, before which to be deleted 459 | cout<<"Enter the element before which to be deleted: "; 460 | cin>>num; 461 | 462 | // Storing start to ptr 463 | node *ptr = start; 464 | 465 | // Incrementing till num is found 466 | while(ptr->data != num) { 467 | ptr = ptr->next; 468 | } 469 | 470 | // Storing the node which is to be deleted 471 | node *temp = ptr->prev; 472 | 473 | // Updating the stored addresses in blocks 474 | temp->prev->next = ptr; 475 | ptr->prev = temp->prev; 476 | 477 | // Printing the value to be deleted 478 | cout<<"The deleted value is: "<data< DATA != NUM 498 | PTR = PTR -> NEXT 499 | 500 | 4) TEMP = PTR -> NEXT 501 | 5) PTR -> PREV -> NEXT = TEMP 502 | 6) TEMP -> PREV = PTR -> PREV 503 | 6) DELETE PTR 504 | 7) EXIT 505 | 506 | */ 507 | 508 | node *del_element(node *start) { 509 | // Checking if the linked list is empty 510 | if(start == NULL) { 511 | cout<<"The linked list is empty !\n"; 512 | } 513 | else { 514 | int num; 515 | 516 | // Storing the element, which to be deleted 517 | cout<<"Enter the element which to be deleted: "; 518 | cin>>num; 519 | 520 | // Storing start to ptr 521 | node *ptr = start; 522 | 523 | // Incrementing till num is found 524 | while(ptr->data != num) { 525 | ptr = ptr->next; 526 | } 527 | 528 | // Storing a node in temp 529 | node *temp = ptr->next; 530 | 531 | // Updating the stored addresses in blocks 532 | ptr->prev->next = temp; 533 | temp->prev = ptr->prev; 534 | 535 | // Printing the value to be deleted 536 | cout<<"The deleted value is: "<data< NEXT 559 | 560 | 5) WRITE COUNT 561 | 6) EXIT 562 | 563 | */ 564 | 565 | void count(node *start) { 566 | int count = 0; 567 | 568 | // Checking if the linked list is empty 569 | if(start == NULL) { 570 | cout<<"The linked list is empty !\n"; 571 | } 572 | else { 573 | count = 1; 574 | 575 | // Storing start in ptr 576 | node *ptr = start; 577 | 578 | // Incrementing till the end 579 | while(ptr->next != start) { 580 | count++; 581 | ptr = ptr->next; 582 | } 583 | } 584 | // Printing the number of elements 585 | cout<<"The number of elements present in the linked list are: " 586 | < DATA 602 | PTR = PTR -> NEXT 603 | 4) EXIT 604 | 605 | */ 606 | 607 | void display(node *start) { 608 | // Checking if the linked list is empty 609 | if(start == NULL) { 610 | cout<<"The linked list is empty !\n"; 611 | } 612 | else { 613 | // Storing start in ptr 614 | node *ptr = start; 615 | 616 | // Incrementing till the end 617 | while(ptr->next != start) { 618 | // Printing the elements 619 | cout<data<<" "; 620 | ptr = ptr->next; 621 | 622 | } 623 | cout<data<>option; 654 | 655 | switch(option) { 656 | 657 | case 1: start = create(start); 658 | break; 659 | 660 | case 2: // Storing the value to be inserted 661 | cout<<"Enter the value to be stored: "; 662 | cin>>value; 663 | 664 | start = insert_beg(start, value); 665 | break; 666 | 667 | case 3: // Storing the value to be inserted 668 | cout<<"Enter the value to be stored: "; 669 | cin>>value; 670 | 671 | start = insert_end(start, value); 672 | break; 673 | 674 | case 4: // Storing the value to be inserted 675 | cout<<"Enter the value to be stored: "; 676 | cin>>value; 677 | 678 | start = insert_after(start, value); 679 | break; 680 | 681 | case 5: // Storing the value to be inserted 682 | cout<<"Enter the value to be stored: "; 683 | cin>>value; 684 | 685 | start = insert_before(start, value); 686 | break; 687 | 688 | case 6: start = del_beg(start); 689 | break; 690 | 691 | case 7: start = del_end(start); 692 | break; 693 | 694 | case 8: start = del_after(start); 695 | break; 696 | 697 | case 9: start = del_before(start); 698 | break; 699 | 700 | case 10: start = del_element(start); 701 | break; 702 | 703 | case 11: count(start); 704 | break; 705 | 706 | case 12: display(start); 707 | break; 708 | 709 | case 13: break; 710 | 711 | default: cout<<"Wrong option is selecred !!\n"; 712 | break; 713 | } 714 | } while(option != 13); 715 | 716 | // Freeing the space occupied by start 717 | if(option == 13) { 718 | cout<< "\nTHANK YOU for using the program !\n" 719 | <<"Have a good day.\n\n"; 720 | 721 | delete start; 722 | } 723 | } 724 | -------------------------------------------------------------------------------- /circ_queue.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Souvik Biswas 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | #include 22 | using namespace std; 23 | 24 | // Defining the maximum size of the queue to be 10 25 | #define MAX 10 26 | 27 | // Defining a class to store the member variables 28 | // and member functions required 29 | class Queue { 30 | int queue[MAX], front, rear; 31 | 32 | public: 33 | Queue(): front(-1), rear(-1) {} 34 | void insert(int ); 35 | void del(); 36 | void display(); 37 | }; 38 | 39 | 40 | /* 41 | 42 | ALGORITHM FOR INSERTING AN ELEMENT IN THE QUEUE: 43 | 44 | 1) IF (FRONT = REAR+1) OR (FRONT = 0 AND REAR = MAX-1) 45 | WRITE Overflow 46 | EXIT 47 | 48 | 2) IF FRONT = -1 & REAR = -1 49 | FRONT = 0 50 | 51 | 3) REAR = (REAR + 1) % MAX 52 | 3) QUEUE[REAR] = VALUE 53 | 4) EXIT 54 | 55 | */ 56 | 57 | void Queue :: insert(int value) { 58 | // When both front and rear is equal to -1, then the queue is empty 59 | if((front == rear+1) || (front == 0 && rear == MAX-1)) { 60 | cout<<"The Queue is full !\n"; 61 | } 62 | else { 63 | // For inserting when the queue is empty 64 | if(front == -1 && rear == -1) { 65 | front = 0; 66 | } 67 | // Using modular arithmatic to calculate the rear 68 | rear = (rear+1) % MAX; 69 | 70 | // Storing the value in rear position of queue 71 | queue[rear] = value; 72 | } 73 | } 74 | 75 | 76 | /* 77 | 78 | ALGORITHM FOR DELETING AN ELEMENT FROM THE QUEUE: 79 | 80 | 1) IF FRONT = -1 AND REAR = -1 81 | WRITE Underflow 82 | EXIT 83 | 84 | 2) ITEM = QUEUE[FRONT] 85 | 3) IF FRONT = REAR 86 | FRONT = -1 AND REAR = -1 87 | 88 | 4) ELSE 89 | FRONT = (FRONT + 1) % MAX 90 | 91 | 5) EXIT 92 | 93 | */ 94 | 95 | void Queue :: del() { 96 | // When both front and rear is equal to -1, then the queue is empty 97 | if(front == -1 && rear == -1) { 98 | cout<<"The Queue is empty !\n"; 99 | } 100 | else { 101 | cout<<"The deleted element is "<>option; 146 | 147 | switch(option) { 148 | case 1: cout<<"Enter the value to be inserted: "; 149 | cin>>value; 150 | 151 | q.insert(value); 152 | break; 153 | 154 | case 2: q.del(); 155 | break; 156 | 157 | case 3: q.display(); 158 | break; 159 | 160 | case 4: break; 161 | 162 | default: cout<<"Wrong option !\n"; 163 | break; 164 | } 165 | } while(option != 4); 166 | 167 | if(option == 4) { 168 | cout<< "\nTHANK YOU for using the program !\n" 169 | <<"Have a good day.\n\n"; 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /circ_singly_ll.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Souvik Biswas 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | #include 22 | using namespace std; 23 | 24 | 25 | // Function prototypes 26 | struct node *insert_beg(node *, int ); 27 | struct node *insert_end(node *, int ); 28 | struct node *insert_after(node *, int ); 29 | struct node *insert_before(node *, int ); 30 | 31 | struct node *create(node *); 32 | 33 | struct node *del_beg(node *); 34 | struct node *del_end(node *); 35 | struct node *del_after(node *); 36 | struct node *del_before(node *); 37 | struct node *del_element(node *); 38 | 39 | void count(node *); 40 | void display(node *); 41 | 42 | 43 | /* 44 | 45 | Declaring a structure called node 46 | having two members: 47 | 48 | 1) data block 49 | 2) next block (which stores the address of the next node) 50 | 51 | */ 52 | 53 | struct node { 54 | int data; 55 | node *next; 56 | }; 57 | 58 | 59 | /* 60 | 61 | ALGORITHM FOR INSERTING AT THE BEGINNING: 62 | 63 | 1) IF AVAIL = NULL 64 | Write Overflow 65 | EXIT 66 | 67 | 2) NEW_NODE = AVAIL 68 | 3) AVAIL = AVAIL -> NEXT 69 | 4) NEW_NODE -> DATA = VALUE 70 | 5) NEW_NODE -> NEXT = START 71 | 6) PTR = START 72 | 7) WHILE PTR -> NEXT != START 73 | PTR = PTR -> NEXT 74 | 75 | 8) PTR -> NEXT = NEW_NODE 76 | 9) START = NEW_NODE 77 | 10) EXIT 78 | 79 | */ 80 | 81 | node *insert_beg(node *start, int value) { 82 | // Creating a new node 83 | node *new_node = new node; 84 | 85 | // Storing the data and assigning 86 | // which address to keep in the next block 87 | new_node->data = value; 88 | new_node->next = start; 89 | 90 | // Storing start in ptr 91 | node *ptr = start; 92 | 93 | // Incrementing till the last element 94 | while(ptr->next != start) { 95 | ptr = ptr->next; 96 | } 97 | 98 | // Updating the next block of the last node to store the address 99 | // of new node 100 | ptr->next = new_node; 101 | 102 | // making the start point to be first element of the linked list 103 | start = new_node; 104 | 105 | return start; 106 | } 107 | 108 | 109 | /* 110 | 111 | ALGORITHM FOR INSERTING AT THE END: 112 | 113 | 1) IF AVAIL = NULL 114 | Write Overflow 115 | EXIT 116 | 117 | 2) NEW_NODE = AVAIL 118 | 3) AVAIL = AVAIL -> NEXT 119 | 4) NEW_NODE -> DATA = VALUE 120 | 5) NEW_NODE -> NEXT = START 121 | 6) PTR = START 122 | 7) WHILE PTR -> NEXT != START 123 | PTR = PTR -> next 124 | 125 | 8) PTR -> NEXT = NEW_NODE 126 | 7) EXIT 127 | 128 | */ 129 | 130 | node *insert_end(node *start, int value) { 131 | // Storing the value of start to ptr 132 | node *ptr = start; 133 | 134 | // Creating a new node 135 | node *new_node = new node; 136 | 137 | // Storing the data and assigning 138 | // which address to keep in the next block 139 | new_node->data = value; 140 | new_node->next = start; 141 | 142 | // Searching for the last element after which we should insert 143 | while(ptr->next != start) { 144 | // Incrementing 145 | ptr = ptr->next; 146 | } 147 | 148 | // Storing the address of new node to the next pointer of ptr 149 | ptr->next = new_node; 150 | 151 | return start; 152 | } 153 | 154 | 155 | /* 156 | 157 | ALGORITHM FOR INSERTING AFTER A GIVEN NODE: 158 | 159 | 1) IF AVAIL = NULL 160 | Write Overflow 161 | EXIT 162 | 163 | 2) NEW_NODE = AVAIL 164 | 3) AVAIL = AVAIL -> NEXT 165 | 4) NEW_NODE -> DATA = VALUE 166 | 5) PTR = START 167 | 6) PREPTR = PTR 168 | 7) WHILE PREPTR -> NEXT != NUM 169 | PREPTR = PTR 170 | PTR = PTR -> NEXT 171 | 172 | 8) PREPTR -> NEXT = NEW_NODE 173 | 9) NEW_NODE -> NEXT = PTR 174 | 10) EXIT 175 | 176 | */ 177 | 178 | node *insert_after(node *start, int value) { 179 | int num; 180 | 181 | // Storing the value of start to ptr 182 | node *ptr = start; 183 | 184 | // Number after which to insert 185 | cout<<"Enter the number after which to insert: "; 186 | cin>>num; 187 | 188 | // Creating a new node 189 | node *new_node = new node; 190 | 191 | // Storing the data 192 | new_node->data = value; 193 | 194 | // Taking another node to store the location of 195 | // the element before ptr 196 | node *preptr = ptr; 197 | 198 | // Incrementing till the number is found 199 | while(preptr->data != num) { 200 | preptr = ptr; 201 | ptr = ptr->next; 202 | } 203 | 204 | // Adding the new node 205 | preptr->next = new_node; 206 | new_node->next = ptr; 207 | 208 | return start; 209 | } 210 | 211 | 212 | /* 213 | 214 | ALGORITHM FOR INSERTING BEFORE A GIVEN NODE: 215 | 216 | 1) IF AVAIL = NULL 217 | Write Overflow 218 | EXIT 219 | 220 | 2) NEW_NODE = AVAIL 221 | 3) AVAIL = AVAIL -> NEXT 222 | 4) NEW_NODE -> DATA = VALUE 223 | 5) PTR = START 224 | 6) PREPTR = PTR 225 | 7) WHILE PTR -> NEXT != NUM 226 | PREPTR = PTR 227 | PTR = PTR -> NEXT 228 | 229 | 8) PREPTR -> NEXT = NEW_NODE 230 | 9) NEW_NODE -> NEXT = PTR 231 | 10) EXIT 232 | 233 | */ 234 | 235 | node *insert_before(node *start, int value) { 236 | int num; 237 | 238 | // Storing the value of start to ptr 239 | node *ptr = start; 240 | 241 | // Number after which to insert 242 | cout<<"Enter the number before which to insert: "; 243 | cin>>num; 244 | 245 | // Creating a new node 246 | node *new_node = new node; 247 | 248 | // Storing the data 249 | new_node->data = value; 250 | 251 | // Taking another node to store the location of 252 | // the element before ptr 253 | node *preptr = ptr; 254 | 255 | // Incrementing till the number is found 256 | while(ptr->data != num) { 257 | preptr = ptr; 258 | ptr = ptr->next; 259 | } 260 | 261 | // Adding the new node 262 | preptr->next = new_node; 263 | new_node->next = ptr; 264 | 265 | return start; 266 | } 267 | 268 | 269 | // For creating the entire linked list 270 | node *create(node *start) { 271 | int value; 272 | 273 | // Storing the value to be inserted 274 | cout<<"Enter -1 to stop\n"; 275 | cout<<"Enter the value to be stored: "; 276 | cin>>value; 277 | 278 | while(value != -1){ 279 | if(start == NULL) { 280 | node *new_node = new node; 281 | new_node->data = value; 282 | start = new_node; 283 | new_node->next = start; 284 | } 285 | else { 286 | // Elements are inserted at the end 287 | insert_end(start, value); 288 | } 289 | 290 | cout<<"Enter the value to be stored: "; 291 | cin>>value; 292 | } 293 | 294 | return start; 295 | } 296 | 297 | 298 | /* 299 | 300 | ALGORITHM FOR DELETING FROM THE BEGINNING: 301 | 302 | 1) IF START = NULL 303 | Write Underflow 304 | EXIT 305 | 306 | 2) PTR = START 307 | 3) WHILE PTR -> NEXT != START 308 | PTR = PTR -> NEXT 309 | 310 | 4) PTR -> NEXT = START -> NEXT 311 | 5) DELETE START 312 | 6) START = PTR-> NEXT 313 | 7) EXIT 314 | 315 | */ 316 | 317 | node *del_beg(node *start) { 318 | // Checking if the linked list is empty 319 | if(start == NULL) { 320 | cout<<"The linked list is empty !\n"; 321 | } 322 | else { 323 | // Storing start to ptr 324 | node *ptr = start; 325 | 326 | // Incrementing till the last position 327 | while(ptr->next != start) { 328 | ptr = ptr->next; 329 | } 330 | 331 | // Updating the next block of ptr 332 | ptr->next = start->next; 333 | 334 | // Printing the value to be deleted 335 | cout<<"The deleted value is: "<data<next; 342 | } 343 | 344 | return start; 345 | } 346 | 347 | 348 | /* 349 | 350 | ALGORITHM FOR DELETING FROM THE END: 351 | 352 | 1) IF START = NULL 353 | Write Underflow 354 | EXIT 355 | 356 | 2) PTR = START 357 | 3) PREPTR = PTR 358 | 4) WHILE PTR -> NEXT != START 359 | PREPTR = PTR 360 | PTR = PTR -> NEXT 361 | 362 | 5) PREPTR -> NEXT = START 363 | 6) DELETE PTR 364 | 7) EXIT 365 | 366 | */ 367 | 368 | node *del_end(node *start) { 369 | // Checking if the linked list is empty 370 | if(start == NULL) { 371 | cout<<"The linked list is empty !\n"; 372 | } 373 | else { 374 | // Storing start to ptr 375 | node *ptr = start; 376 | 377 | // Defining another pointer for storing 378 | // the previous element of ptr 379 | node *preptr = ptr; 380 | 381 | // Incrementing till the last position 382 | while(ptr->next != start) { 383 | preptr = ptr; 384 | ptr = ptr->next; 385 | } 386 | 387 | // Storing the address of start in the NEXT block as the 388 | // the last element is to be deleted 389 | preptr->next = start; 390 | 391 | // Printing the value to be deleted 392 | cout<<"The deleted value is: "<data< DATA != NUM 413 | PREPTR = PTR 414 | PTR = PTR -> NEXT 415 | 416 | 5) PREPTR -> NEXT = PTR -> NEXT 417 | 6) DELETE PTR 418 | 7) EXIT 419 | 420 | */ 421 | 422 | node *del_after(node *start) { 423 | // Checking if the linked list is empty 424 | if(start == NULL) { 425 | cout<<"The linked list is empty !\n"; 426 | } 427 | else { 428 | int num; 429 | 430 | // Storing the element, after which to be deleted 431 | cout<<"Enter the element after which to be deleted: "; 432 | cin>>num; 433 | 434 | // Storing start to ptr 435 | node *ptr = start; 436 | 437 | // Defining another pointer for storing 438 | // the previous element of ptr 439 | node *preptr = ptr; 440 | 441 | // Incrementing till num is found 442 | while(preptr->data != num) { 443 | preptr = ptr; 444 | ptr = ptr->next; 445 | } 446 | 447 | // Storing the address of element leaving one block, 448 | // which is to be deleted 449 | preptr->next = ptr->next; 450 | 451 | // Printing the value to be deleted 452 | cout<<"The deleted value is: "<data< NEXT -> DATA != NUM 473 | PREPTR = PTR 474 | PTR = PTR -> NEXT 475 | 476 | 5) PREPTR -> NEXT = PTR -> NEXT 477 | 6) DELETE PTR 478 | 7) EXIT 479 | 480 | */ 481 | 482 | node *del_before(node *start) { 483 | // Checking if the linked list is empty 484 | if(start == NULL) { 485 | cout<<"The linked list is empty !\n"; 486 | } 487 | else { 488 | int num; 489 | 490 | // Storing the element, before which to be deleted 491 | cout<<"Enter the element before which to be deleted: "; 492 | cin>>num; 493 | 494 | // Storing start to ptr 495 | node *ptr = start; 496 | 497 | // Defining another pointer for storing 498 | // the previous element of ptr 499 | node *preptr = ptr; 500 | 501 | // Incrementing till num is found 502 | while(ptr->next->data != num) { 503 | preptr = ptr; 504 | ptr = ptr->next; 505 | } 506 | 507 | // Storing the address of element leaving one block, 508 | // which is to be deleted 509 | preptr->next = ptr->next; 510 | 511 | // Printing the value to be deleted 512 | cout<<"The deleted value is: "<data< DATA != NUM 533 | PREPTR = PTR 534 | PTR = PTR -> NEXT 535 | 536 | 5) PREPTR -> NEXT = PTR -> NEXT 537 | 6) DELETE PTR 538 | 7) EXIT 539 | 540 | */ 541 | 542 | node *del_element(node *start) { 543 | // Checking if the linked list is empty 544 | if(start == NULL) { 545 | cout<<"The linked list is empty !\n"; 546 | } 547 | else { 548 | int num; 549 | 550 | // Storing the element, which to be deleted 551 | cout<<"Enter the element which to be deleted: "; 552 | cin>>num; 553 | 554 | // Storing start to ptr 555 | node *ptr = start; 556 | 557 | // Defining another pointer for storing 558 | // the previous element of ptr 559 | node *preptr = ptr; 560 | 561 | // Incrementing till num is found 562 | while(ptr->data != num) { 563 | preptr = ptr; 564 | ptr = ptr->next; 565 | } 566 | 567 | // Storing the address of element leaving one block, 568 | // which is to be deleted 569 | preptr->next = ptr->next; 570 | 571 | // Printing the value to be deleted 572 | cout<<"The deleted value is: "<data< NEXT 595 | 596 | 5) WRITE COUNT 597 | 6) EXIT 598 | 599 | */ 600 | 601 | void count(node *start) { 602 | int count = 0; 603 | 604 | // Checking if the linked list is empty 605 | if(start == NULL) { 606 | cout<<"The linked list is empty !\n"; 607 | } 608 | else { 609 | count = 1; 610 | 611 | // Storing start in ptr 612 | node *ptr = start; 613 | 614 | // Incrementing till the end 615 | while(ptr->next != start) { 616 | count++; 617 | ptr = ptr->next; 618 | } 619 | } 620 | // Printing the number of elements 621 | cout<<"The number of elements present in the linked list are: " 622 | < DATA 638 | PTR = PTR -> NEXT 639 | 4) EXIT 640 | 641 | */ 642 | 643 | void display(node *start) { 644 | // Checking if the linked list is empty 645 | if(start == NULL) { 646 | cout<<"The linked list is empty !\n"; 647 | } 648 | else { 649 | // Storing start in ptr 650 | node *ptr = start; 651 | 652 | // Incrementing till the end 653 | while(ptr->next != start) { 654 | // Printing the elements 655 | cout<data<<" "; 656 | ptr = ptr->next; 657 | 658 | } 659 | cout<data<>option; 690 | 691 | switch(option) { 692 | 693 | case 1: start = create(start); 694 | break; 695 | 696 | case 2: // Storing the value to be inserted 697 | cout<<"Enter the value to be stored: "; 698 | cin>>value; 699 | 700 | start = insert_beg(start, value); 701 | break; 702 | 703 | case 3: // Storing the value to be inserted 704 | cout<<"Enter the value to be stored: "; 705 | cin>>value; 706 | 707 | start = insert_end(start, value); 708 | break; 709 | 710 | case 4: // Storing the value to be inserted 711 | cout<<"Enter the value to be stored: "; 712 | cin>>value; 713 | 714 | start = insert_after(start, value); 715 | break; 716 | 717 | case 5: // Storing the value to be inserted 718 | cout<<"Enter the value to be stored: "; 719 | cin>>value; 720 | 721 | start = insert_before(start, value); 722 | break; 723 | 724 | case 6: start = del_beg(start); 725 | break; 726 | 727 | case 7: start = del_end(start); 728 | break; 729 | 730 | case 8: start = del_after(start); 731 | break; 732 | 733 | case 9: start = del_before(start); 734 | break; 735 | 736 | case 10: start = del_element(start); 737 | break; 738 | 739 | case 11: count(start); 740 | break; 741 | 742 | case 12: display(start); 743 | break; 744 | 745 | case 13: break; 746 | 747 | default: cout<<"Wrong option is selecred !!\n"; 748 | break; 749 | } 750 | } while(option != 13); 751 | 752 | // Freeing the space occupied by start 753 | if(option == 13) { 754 | cout<< "\nTHANK YOU for using the program !\n" 755 | <<"Have a good day.\n\n"; 756 | 757 | delete start; 758 | } 759 | } 760 | -------------------------------------------------------------------------------- /dequeue.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Souvik Biswas 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | #include 22 | using namespace std; 23 | 24 | // Defining the maximum size of the queue to be 10 25 | #define MAX 10 26 | 27 | void input_dequeue(); 28 | void output_dequeue(); 29 | 30 | 31 | /* 32 | 33 | Declaring a class called Dequeue having: 34 | 35 | three private members: 36 | dequeue[MAX] 37 | left 38 | right 39 | 40 | three public member functions: 41 | 42 | Constructor defining left to -1 and right to -1 43 | 44 | insert right 45 | insert left 46 | delete right 47 | delete left 48 | display 49 | 50 | */ 51 | 52 | class Dequeue { 53 | int dequeue[MAX], left, right; 54 | 55 | public: 56 | Dequeue(): left(-1), right(-1) {} 57 | void insert_right(int ); 58 | void insert_left(int ); 59 | void del_right(); 60 | void del_left(); 61 | void display(); 62 | }; 63 | 64 | void Dequeue :: insert_right(int value) { 65 | if((left == right + 1) || (left == 0 && right == MAX-1)) { 66 | cout<<"The Queue is full !\n"; 67 | return; 68 | } 69 | if(left == -1) { 70 | left = 0; 71 | right = 0; 72 | } 73 | else { 74 | if(right == MAX-1) { 75 | right = 0; 76 | } 77 | else { 78 | right++; 79 | } 80 | } 81 | dequeue[right] = value; 82 | } 83 | 84 | void Dequeue :: insert_left(int value) { 85 | if((left == right + 1) || (left == 0 && right == MAX-1)) { 86 | cout<<"The queue is full !\n"; 87 | return; 88 | } 89 | if(left == -1) { 90 | left = 0; 91 | right = 0; 92 | } 93 | else { 94 | if(left == 0) { 95 | left = MAX-1; 96 | } 97 | else { 98 | left--; 99 | } 100 | } 101 | dequeue[left] = value; 102 | } 103 | 104 | void Dequeue :: del_right() { 105 | if(left == -1) { 106 | cout<<"The queue is empty !\n"; 107 | return; 108 | } 109 | 110 | cout<<"The deleted value is: "<>option; 190 | 191 | switch(option) { 192 | case 1: cout<<"Enter the value to be inserted: "; 193 | cin>>value; 194 | 195 | dq.insert_right(value); 196 | break; 197 | 198 | case 2: dq.del_right(); 199 | break; 200 | 201 | case 3: dq.del_left(); 202 | break; 203 | 204 | case 4: dq.display() ; 205 | break; 206 | 207 | case 5: break; 208 | 209 | default: cout<<"Wrong option !\n"; 210 | break; 211 | } 212 | } while(option != 5); 213 | } 214 | 215 | void output_dequeue() { 216 | int option, value; 217 | Dequeue dq; 218 | 219 | do { 220 | cout<<"\n****** MENU ******\n" 221 | <<"1. Insert right\n" 222 | <<"2. Insert left\n" 223 | <<"3. Delete left\n" 224 | <<"4. Display\n" 225 | <<"5. Exit to Main Menu\n"; 226 | 227 | cout<<"Enter your option: "; 228 | cin>>option; 229 | 230 | switch(option) { 231 | case 1: cout<<"Enter the value to be inserted: "; 232 | cin>>value; 233 | 234 | dq.insert_right(value); 235 | break; 236 | 237 | case 2: cout<<"Enter the value to be inserted: "; 238 | cin>>value; 239 | 240 | dq.insert_left(value); 241 | break; 242 | 243 | case 3: dq.del_left(); 244 | break; 245 | 246 | case 4: dq.display() ; 247 | break; 248 | 249 | case 5: break; 250 | 251 | default: cout<<"Wrong option !\n"; 252 | break; 253 | } 254 | } while(option != 5); 255 | } 256 | 257 | int main() { 258 | int option, value; 259 | Dequeue dq; 260 | 261 | do { 262 | cout<<"\n******* MAIN MENU *******\n" 263 | <<"1. Input Restricted Dequeue\n" 264 | <<"2. Output Restricted Dequeue\n" 265 | <<"3. Exit\n"; 266 | 267 | cout<<"Enter your option: "; 268 | cin>>option; 269 | 270 | switch(option) { 271 | case 1: input_dequeue(); 272 | break; 273 | 274 | case 2: output_dequeue(); 275 | break; 276 | 277 | case 3: break; 278 | 279 | default: cout<<"Wrong option !\n"; 280 | break; 281 | } 282 | } while(option != 3); 283 | 284 | if(option == 3) { 285 | cout<< "\nTHANK YOU for using the program !\n" 286 | <<"Have a good day.\n\n"; 287 | } 288 | } 289 | -------------------------------------------------------------------------------- /dijkstra.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void djk(vector> graph[],int nodes,int src,int end) 4 | { 5 | bool visit[nodes]; // Initializing visited array 6 | 7 | memset(visit,false,sizeof(visit)); // filling visited array with FALSE 8 | 9 | vector dist(nodes); // initializing distance vector 10 | fill(dist.begin(),dist.end(),INT_MAX); 11 | dist[src]=0; // distance of source node is 0 12 | 13 | 14 | multiset> m; // using multiset as queue 15 | 16 | 17 | m.insert({0,src}); // Insertint the source node 18 | while(!m.empty()) 19 | { 20 | pair p=*m.begin(); 21 | m.erase(*m.begin()); 22 | int v=p.second; 23 | int w=p.first; 24 | if(visit[v]==false){ // If the nodes are unvisited then we visit it 25 | visit[v]=true; 26 | 27 | for(int i=0;i>n>>m>>src>>end; 50 | vector> graph[n]; // Here we initialize the adjacency list using a 2D vector 51 | for(int i=0;i>x>>y>>ti; 54 | x--; // Here we take input for each vertex and edges 55 | y--; 56 | graph[x].push_back(make_pair(y,ti)); 57 | graph[y].push_back(make_pair(x,ti)); 58 | } 59 | djk(graph,n,src-1,end-1); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /disjoint_set.cpp: -------------------------------------------------------------------------------- 1 | // Disjoint Set Union: optimised complexity O(1) by using 'union by height' and 'path compression' 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class dsu { 8 | private: 9 | vector parent, height; 10 | // parent[] stores parent of each node & height[] stores heights of root node 11 | public: 12 | dsu(int n); 13 | int findSet(int a); // returns the root of that component 14 | bool isSameSet(int a, int b); // returns if they are in same set or not 15 | void unionSet(int a, int b); // merges two components into one 16 | void reset(); 17 | }; 18 | 19 | dsu::dsu(int n) { 20 | parent.assign(n, 0); 21 | height.assign(n, 0); 22 | for (int i = 0;i < parent.size();i++) 23 | parent[i] = i; // Initialize each node being the root of itself 24 | } 25 | 26 | int dsu::findSet(int a) { 27 | return parent[a] == a ? a : parent[a] = findSet(parent[a]); // storing root/parent of the set and then returning the value : 'path compression' 28 | } 29 | 30 | bool dsu::isSameSet(int a, int b) { 31 | return findSet(a) == findSet(b); 32 | } 33 | 34 | void dsu::unionSet(int a, int b) { 35 | int x = findSet(a), y = findSet(b); 36 | height[x] < height[y] ? parent[x] = y : parent[y] = x; // The root having more height becomes parent of the other root 37 | if (height[x] == height[y]) 38 | height[x]++; // If both have same height => choose any one of them & increase the height of that root by 1 39 | } 40 | 41 | void dsu::reset() { 42 | for (int i = 0;i < parent.size();i++) { 43 | parent[i] = i; 44 | height[i] = 0; 45 | } 46 | } 47 | 48 | int main() 49 | { 50 | // Usage of dsu class is given below 51 | dsu a(5); // creates 5 disjoint sets i.e {{0}, {1}, {2}, {3}, {4}} 52 | a.unionSet(1, 4); // now the structue becomes {{0}, {1,4}, {2}, {3}} 53 | a.unionSet(3, 4); // now the structue becomes {{0}, {1,4,3}, {2}} 54 | cout << a.isSameSet(2, 0) << "\n"; // returns false 55 | cout << a.isSameSet(4, 1) << "\n"; // returns true 56 | a.reset(); // the structure resets to {{0}, {1}, {2}, {3}, {4}} 57 | cout << a.isSameSet(4, 1) << "\n"; // returns false 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /doubly_ll.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Souvik Biswas 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | #include 22 | using namespace std; 23 | 24 | 25 | // Function prototypes 26 | struct node *insert_beg(node *, int ); 27 | struct node *insert_end(node *, int ); 28 | struct node *insert_after(node *, int ); 29 | struct node *insert_before(node *, int ); 30 | 31 | struct node *create(node *); 32 | 33 | struct node *del_beg(node *); 34 | struct node *del_end(node *); 35 | struct node *del_after(node *); 36 | struct node *del_before(node *); 37 | struct node *del_element(node *); 38 | 39 | void count(node *); 40 | void display(node *); 41 | 42 | 43 | /* 44 | 45 | Declaring a structure called node 46 | having three members: 47 | 48 | 1) prev block (which stores the address of the previous node) 49 | 2) data block 50 | 3) next block (which stores the address of the next node) 51 | 52 | */ 53 | 54 | struct node { 55 | node *prev; 56 | int data; 57 | node *next; 58 | }; 59 | 60 | 61 | /* 62 | 63 | ALGORITHM FOR INSERTING AT THE BEGINNING: 64 | 65 | 1) IF AVAIL = NULL 66 | Write Overflow 67 | EXIT 68 | 69 | 2) NEW_NODE = AVAIL 70 | 3) AVAIL = AVAIL -> NEXT 71 | 4) NEW_NODE -> DATA = VALUE 72 | 5) NEW_NODE -> PREV = NULL 73 | 6) NEW_NODE -> NEXT = START 74 | 7) START -> PREV = NEW_NODE 75 | 8) START = NEW_NODE 76 | 9) EXIT 77 | 78 | */ 79 | 80 | node *insert_beg(node *start, int value) { 81 | // Creating a new node 82 | node *new_node = new node; 83 | 84 | // Storing the data and assigning 85 | // which address to keep in the next block and prev block 86 | new_node->data = value; 87 | new_node->prev = NULL; 88 | new_node->next = start; 89 | 90 | // Updating the previous block of start, 91 | // to store the address of new node 92 | start->prev = new_node; 93 | 94 | // making the start point to be first element of the linked list 95 | start = new_node; 96 | 97 | return start; 98 | } 99 | 100 | 101 | /* 102 | 103 | ALGORITHM FOR INSERTING AT THE END: 104 | 105 | 1) IF AVAIL = NULL 106 | Write Overflow 107 | EXIT 108 | 109 | 2) NEW_NODE = AVAIL 110 | 3) AVAIL = AVAIL -> NEXT 111 | 4) NEW_NODE -> DATA = VALUE 112 | 5) NEW_NODE -> NEXT = NULL 113 | 6) PTR = START 114 | 7) WHILE PTR -> NEXT != NULL 115 | PTR = PTR -> next 116 | 117 | 8) PTR -> NEXT = NEW_NODE 118 | 9) NEW_NODE -> PREV = PTR 119 | 10) EXIT 120 | 121 | */ 122 | 123 | node *insert_end(node *start, int value) { 124 | // Storing the value of start to ptr 125 | node *ptr = start; 126 | 127 | // Creating a new node 128 | node *new_node = new node; 129 | 130 | // Storing the data and assigning 131 | // which address to keep in the next block 132 | new_node->data = value; 133 | new_node->next = NULL; 134 | 135 | // Searching for the last element after which we should insert 136 | while(ptr->next != NULL) { 137 | // Incrementing 138 | ptr = ptr->next; 139 | } 140 | 141 | // Storing the address of new node to the next pointer of ptr 142 | ptr->next = new_node; 143 | 144 | // Storing the address of the previous node in prev block of new node 145 | new_node->prev = ptr; 146 | 147 | return start; 148 | } 149 | 150 | 151 | /* 152 | 153 | ALGORITHM FOR INSERTING AFTER A GIVEN NODE: 154 | 155 | 1) IF AVAIL = NULL 156 | Write Overflow 157 | EXIT 158 | 159 | 2) NEW_NODE = AVAIL 160 | 3) AVAIL = AVAIL -> NEXT 161 | 4) NEW_NODE -> DATA = VALUE 162 | 5) PTR = START 163 | 6) PREPTR = PTR 164 | 7) WHILE PREPTR -> NEXT != NUM 165 | PREPTR = PTR 166 | PTR = PTR -> NEXT 167 | 168 | 8) PREPTR -> NEXT = NEW_NODE 169 | 9) NEW_NODE -> PREV = PREPTR 170 | 10) NEW_NODE -> NEXT = PTR 171 | 11) PTR -> PREV = NEW_NODE 172 | 12) EXIT 173 | 174 | */ 175 | 176 | node *insert_after(node *start, int value) { 177 | int num; 178 | 179 | // Storing the value of start to ptr 180 | node *ptr = start; 181 | 182 | // Number after which to insert 183 | cout<<"Enter the number after which to insert: "; 184 | cin>>num; 185 | 186 | // Creating a new node 187 | node *new_node = new node; 188 | 189 | // Storing the data 190 | new_node->data = value; 191 | 192 | // Taking another node to store the location of 193 | // the element before ptr 194 | node *preptr = ptr; 195 | 196 | // Incrementing till the number is found 197 | while(preptr->data != num) { 198 | preptr = ptr; 199 | ptr = ptr->next; 200 | } 201 | 202 | // Adding the new node 203 | preptr->next = new_node; 204 | new_node->prev = preptr; 205 | new_node->next = ptr; 206 | ptr->prev = new_node; 207 | 208 | return start; 209 | } 210 | 211 | 212 | /* 213 | 214 | ALGORITHM FOR INSERTING BEFORE A GIVEN NODE: 215 | 216 | 1) IF AVAIL = NULL 217 | Write Overflow 218 | EXIT 219 | 220 | 2) NEW_NODE = AVAIL 221 | 3) AVAIL = AVAIL -> NEXT 222 | 4) NEW_NODE -> DATA = VALUE 223 | 5) PTR = START 224 | 6) PREPTR = PTR 225 | 7) WHILE PTR -> NEXT != NUM 226 | PREPTR = PTR 227 | PTR = PTR -> NEXT 228 | 229 | 8) PREPTR -> NEXT = NEW_NODE 230 | 9) NEW_NODE -> PREV = PREPTR 231 | 10) NEW_NODE -> NEXT = PTR 232 | 11) PTR -> PREV = NEW_NODE 233 | 12) EXIT 234 | 235 | */ 236 | 237 | node *insert_before(node *start, int value) { 238 | int num; 239 | 240 | // Storing the value of start to ptr 241 | node *ptr = start; 242 | 243 | // Number after which to insert 244 | cout<<"Enter the number before which to insert: "; 245 | cin>>num; 246 | 247 | // Creating a new node 248 | node *new_node = new node; 249 | 250 | // Storing the data 251 | new_node->data = value; 252 | 253 | // Taking another node to store the location of 254 | // the element before ptr 255 | node *preptr = ptr; 256 | 257 | // Incrementing till the number is found 258 | while(ptr->data != num) { 259 | preptr = ptr; 260 | ptr = ptr->next; 261 | } 262 | 263 | // Adding the new node 264 | preptr->next = new_node; 265 | new_node->prev = preptr; 266 | new_node->next = ptr; 267 | ptr->prev = new_node; 268 | 269 | return start; 270 | } 271 | 272 | 273 | // For creating the entire linked list 274 | node *create(node *start) { 275 | int value; 276 | 277 | // Storing the value to be inserted 278 | cout<<"Enter -1 to stop\n"; 279 | cout<<"Enter the value to be stored: "; 280 | cin>>value; 281 | 282 | while(value != -1){ 283 | if(start == NULL) { 284 | node *new_node = new node; 285 | new_node->data = value; 286 | new_node->prev = NULL; 287 | new_node->next = NULL; 288 | 289 | start = new_node; 290 | } 291 | else { 292 | // Elements are inserted at the end 293 | insert_end(start, value); 294 | } 295 | 296 | cout<<"Enter the value to be stored: "; 297 | cin>>value; 298 | } 299 | 300 | return start; 301 | } 302 | 303 | 304 | /* 305 | 306 | ALGORITHM FOR DELETING FROM THE BEGINNING: 307 | 308 | 1) IF START = NULL 309 | Write Underflow 310 | EXIT 311 | 312 | 2) PTR = START 313 | 3) START = START -> NEXT 314 | 4) START -> PREV = NULL 315 | 4) DELETE PTR 316 | 5) EXIT 317 | 318 | */ 319 | 320 | node *del_beg(node *start) { 321 | // Checking if the linked list is empty 322 | if(start == NULL) { 323 | cout<<"The linked list is empty !\n"; 324 | } 325 | else { 326 | // Storing start to ptr 327 | node *ptr = start; 328 | 329 | // Incrementing start to one position 330 | start = start->next; 331 | 332 | // Updating the prev block to null, 333 | // as it does not have any previous node any more 334 | start->prev = NULL; 335 | 336 | // Printing the value to be deleted 337 | cout<<"The deleted value is: "<data< NEXT != NULL 357 | PTR = PTR -> NEXT 358 | 359 | 4) PTR -> PREV -> NEXT = NULL 360 | 5) DELETE PTR 361 | 6) EXIT 362 | 363 | */ 364 | 365 | node *del_end(node *start) { 366 | // Checking if the linked list is empty 367 | if(start == NULL) { 368 | cout<<"The linked list is empty !\n"; 369 | } 370 | else { 371 | // Storing start to ptr 372 | node *ptr = start; 373 | 374 | // Incrementing till the last position 375 | while(ptr->next != NULL) { 376 | ptr = ptr->next; 377 | } 378 | 379 | // Storing null in the NEXT block as there 380 | // is no element after this 381 | ptr->prev->next = NULL; 382 | 383 | // Printing the value to be deleted 384 | cout<<"The deleted value is: "<data< DATA != NUM 404 | PTR = PTR -> NEXT 405 | 406 | 4) TEMP = PTR -> NEXT 407 | 5) TEMP -> NEXT -> PREV = PTR 408 | 5) PTR -> NEXT = TEMP -> NEXT 409 | 6) DELETE TEMP 410 | 7) EXIT 411 | 412 | */ 413 | 414 | node *del_after(node *start) { 415 | // Checking if the linked list is empty 416 | if(start == NULL) { 417 | cout<<"The linked list is empty !\n"; 418 | } 419 | else { 420 | int num; 421 | 422 | // Storing the element, after which to be deleted 423 | cout<<"Enter the element after which to be deleted: "; 424 | cin>>num; 425 | 426 | // Storing start to ptr 427 | node *ptr = start; 428 | 429 | // Incrementing till num is found 430 | while(ptr->data != num) { 431 | ptr = ptr->next; 432 | } 433 | 434 | // Storing the node which is to be deleted 435 | node *temp = ptr->next; 436 | 437 | // Updating the stored addresses in blocks 438 | temp->next->prev = ptr; 439 | ptr->next = temp->next; 440 | 441 | // Printing the value to be deleted 442 | cout<<"The deleted value is: "<data< DATA != NUM 462 | PTR = PTR -> NEXT 463 | 464 | 4) TEMP = PTR -> PREV 465 | 5) TEMP -> PREV -> NEXT = PTR 466 | 6) PTR -> PREV = TEMP -> PREV 467 | 7) DELETE TEMP 468 | 8) EXIT 469 | 470 | */ 471 | 472 | node *del_before(node *start) { 473 | // Checking if the linked list is empty 474 | if(start == NULL) { 475 | cout<<"The linked list is empty !\n"; 476 | } 477 | else { 478 | int num; 479 | 480 | // Storing the element, before which to be deleted 481 | cout<<"Enter the element before which to be deleted: "; 482 | cin>>num; 483 | 484 | // Storing start to ptr 485 | node *ptr = start; 486 | 487 | // Incrementing till num is found 488 | while(ptr->data != num) { 489 | ptr = ptr->next; 490 | } 491 | 492 | // Storing the node which is to be deleted 493 | node *temp = ptr->prev; 494 | 495 | // Updating the stored addresses in blocks 496 | temp->prev->next = ptr; 497 | ptr->prev = temp->prev; 498 | 499 | // Printing the value to be deleted 500 | cout<<"The deleted value is: "<data< DATA != NUM 520 | PTR = PTR -> NEXT 521 | 522 | 4) TEMP = PTR -> NEXT 523 | 5) PTR -> PREV -> NEXT = TEMP 524 | 6) TEMP -> PREV = PTR -> PREV 525 | 6) DELETE PTR 526 | 7) EXIT 527 | 528 | */ 529 | 530 | node *del_element(node *start) { 531 | // Checking if the linked list is empty 532 | if(start == NULL) { 533 | cout<<"The linked list is empty !\n"; 534 | } 535 | else { 536 | int num; 537 | 538 | // Storing the element, which to be deleted 539 | cout<<"Enter the element which to be deleted: "; 540 | cin>>num; 541 | 542 | // Storing start to ptr 543 | node *ptr = start; 544 | 545 | // Incrementing till num is found 546 | while(ptr->data != num) { 547 | ptr = ptr->next; 548 | } 549 | 550 | // Storing a node in temp 551 | node *temp = ptr->next; 552 | 553 | // Updating the stored addresses in blocks 554 | ptr->prev->next = temp; 555 | temp->prev = ptr->prev; 556 | 557 | // Printing the value to be deleted 558 | cout<<"The deleted value is: "<data< NEXT 581 | 582 | 5) WRITE COUNT 583 | 6) EXIT 584 | 585 | */ 586 | 587 | void count(node *start) { 588 | int count = 0; 589 | 590 | // Checking if the linked list is empty 591 | if(start == NULL) { 592 | cout<<"The linked list is empty !\n"; 593 | } 594 | else { 595 | // Storing start in ptr 596 | node *ptr = start; 597 | 598 | // Incrementing till the end 599 | while(ptr != NULL) { 600 | count++; 601 | ptr = ptr->next; 602 | } 603 | } 604 | // Printing the number of elements 605 | cout<<"The number of elements present in the linked list are: " 606 | < DATA 622 | PTR = PTR -> NEXT 623 | 4) EXIT 624 | 625 | */ 626 | 627 | void display(node *start) { 628 | // Checking if the linked list is empty 629 | if(start == NULL) { 630 | cout<<"The linked list is empty !\n"; 631 | } 632 | else { 633 | // Storing start in ptr 634 | node *ptr = start; 635 | 636 | // Incrementing till the end 637 | while(ptr != NULL) { 638 | // Printing the elements 639 | cout<data<<" "; 640 | ptr = ptr->next; 641 | 642 | } cout<>option; 673 | 674 | switch(option) { 675 | 676 | case 1: start = create(start); 677 | break; 678 | 679 | case 2: // Storing the value to be inserted 680 | cout<<"Enter the value to be stored: "; 681 | cin>>value; 682 | 683 | start = insert_beg(start, value); 684 | break; 685 | 686 | case 3: // Storing the value to be inserted 687 | cout<<"Enter the value to be stored: "; 688 | cin>>value; 689 | 690 | start = insert_end(start, value); 691 | break; 692 | 693 | case 4: // Storing the value to be inserted 694 | cout<<"Enter the value to be stored: "; 695 | cin>>value; 696 | 697 | start = insert_after(start, value); 698 | break; 699 | 700 | case 5: // Storing the value to be inserted 701 | cout<<"Enter the value to be stored: "; 702 | cin>>value; 703 | 704 | start = insert_before(start, value); 705 | break; 706 | 707 | case 6: start = del_beg(start); 708 | break; 709 | 710 | case 7: start = del_end(start); 711 | break; 712 | 713 | case 8: start = del_after(start); 714 | break; 715 | 716 | case 9: start = del_before(start); 717 | break; 718 | 719 | case 10: start = del_element(start); 720 | break; 721 | 722 | case 11: count(start); 723 | break; 724 | 725 | case 12: display(start); 726 | break; 727 | 728 | case 13: break; 729 | 730 | default: cout<<"Wrong option is selecred !!\n"; 731 | break; 732 | } 733 | } while(option != 13); 734 | 735 | // Freeing the space occupied by start 736 | if(option == 13) { 737 | cout<< "\nTHANK YOU for using the program !\n" 738 | <<"Have a good day.\n\n"; 739 | 740 | delete start; 741 | } 742 | } 743 | -------------------------------------------------------------------------------- /evaluate_postfix.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Souvik Biswas 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | #include 22 | using namespace std; 23 | 24 | // Defining the maximum size of the stack to be 10 25 | #define MAX 10 26 | 27 | float evaluatePostfix(); 28 | 29 | // Defining a class to store the member variables 30 | // and member functions required 31 | class Stack { 32 | float stk[MAX]; 33 | 34 | public: 35 | int top; 36 | Stack(): top(-1) {} 37 | void push(float ); 38 | float pop(); 39 | void display(); 40 | }; 41 | 42 | 43 | /* 44 | 45 | ALGORITHM FOR PUSHING AN ELEMENT IN THE STACK: 46 | 47 | 1) IF TOP = MAX-1 48 | WRITE Overflow 49 | EXIT 50 | 51 | 2) TOP = TOP + 1 52 | 3) STACK[TOP] = VALUE 53 | 4) EXIT 54 | 55 | */ 56 | 57 | void Stack :: push(float value) { 58 | // When the size of stack has reached maximum, 59 | // we cannot insert any element 60 | if(top == MAX-1) { 61 | cout<<"The Stack is full !\n"; 62 | } 63 | else { 64 | // Incrementing top 65 | top++; 66 | 67 | // Store the value in the stack at top position 68 | stk[top] = value; 69 | } 70 | } 71 | 72 | 73 | /* 74 | 75 | ALGORITHM FOR POPPING AN ELEMENT FROM THE STACK: 76 | 77 | 1) IF TOP = -1 78 | WRITE Underflow 79 | RETURN -1 80 | EXIT 81 | 82 | 2) VALUE = STK[TOP] 83 | 3) TOP = TOP - 1 84 | 4) RETURN VALUE 85 | 5) EXIT 86 | 87 | */ 88 | 89 | float Stack :: pop() { 90 | // When the size of stack is -1, the stack is empty 91 | if(top == -1) { 92 | return -1; 93 | } 94 | else { 95 | // Store the top value 96 | float value = stk[top]; 97 | 98 | // Decrement top 99 | top --; 100 | 101 | return value; 102 | } 103 | } 104 | 105 | 106 | float evaluatePostfix() { 107 | Stack s; 108 | string exp; 109 | int i=0; 110 | float op1, op2, value; 111 | 112 | cout<<"Enter the experssion: "; 113 | getline(cin, exp); 114 | 115 | while(exp[i] != '\0') { 116 | if(isdigit(exp[i])) { 117 | s.push((float)exp[i]-'0'); 118 | i++; 119 | } 120 | else { 121 | op2 = s.pop(); 122 | op1 = s.pop(); 123 | 124 | switch(exp[i]) { 125 | case '+': value = op1 + op2; 126 | break; 127 | 128 | case '-': value = op1 - op2; 129 | break; 130 | 131 | case '*': value = op1 * op2; 132 | break; 133 | 134 | case '/': value = op1 / op2; 135 | break; 136 | 137 | case '%': value = (int)op1 % (int)op2; 138 | break; 139 | } 140 | s.push(value); 141 | } 142 | i++; 143 | } 144 | return s.pop(); 145 | } 146 | 147 | int main() { 148 | int option; 149 | float result; 150 | 151 | result = evaluatePostfix(); 152 | cout<<"The result is "< 8 | #include 9 | #include 10 | #include 11 | #define v 8 // total number of vertex update accn to your graph 12 | 13 | using namespace std; 14 | 15 | 16 | // function which can search a suitable path which reach from source to sink vertex and return true if reached else return false and also store the parents of each node which come in the path 17 | 18 | 19 | int bfs(int rgraph[v][v],int s,int t,int parent[v]) 20 | { 21 | bool visited[v]; 22 | memset(visited,0,sizeof(visited)); // initialised all 23 | 24 | queue Queue; // Creating queue which is integer type and named as Queue 25 | Queue.push(s); 26 | visited[s]=true; // if we reach a vertex then mark it visited sothat we not come to it again 27 | parent[s]=-1; // parent of source vertex is alway -1 28 | //BFS loop 29 | 30 | while(!Queue.empty()) // doing BFS to searching path and update parents accordingly 31 | { 32 | int u=Queue.front(); 33 | Queue.pop(); 34 | 35 | for(int k=0;k0) 38 | { 39 | Queue.push(k); 40 | parent[k]=u; 41 | visited[k]=true; 42 | } 43 | } 44 | } 45 | //if we reach the sink element in graph 46 | return visited[t]==true; 47 | } 48 | 49 | // ######################################################################################################################################################################### 50 | 51 | 52 | // main function which find out the maximum flow in the graph 53 | 54 | 55 | int ford_fulkerson(int graph[v][v],int s,int t) // s is the source vertex and t is the destination vertex 56 | { 57 | int rgraph[v][v],u,x,y; // residual graph which store the graph after using some path 58 | 59 | int parent[v]; 60 | int max_flow=0; // store the max flow 61 | for(int i=0;i 22 | #include 23 | #include 24 | using namespace std; 25 | 26 | #define STARTING_SIZE 101 // The initial size of the hash table 27 | #define EXPAND_FACTOR 0.7 // The factor to indicates when it's time to expand the hash table 28 | // The two defines above are used to have a more static hash table which also allows more optimization 29 | 30 | // Base class for the Hash Table 31 | class HashTable { 32 | public: 33 | HashTable() { 34 | this->elements = 0; 35 | this->table_size = STARTING_SIZE; 36 | this->hash_table = new int[STARTING_SIZE]; 37 | memset(this->hash_table, 0, sizeof(int) * STARTING_SIZE); 38 | } 39 | ~HashTable() { 40 | if (this->hash_table) { 41 | delete[] this->hash_table; 42 | } 43 | } 44 | void insert(int value) { 45 | int position = this->find_position(value); 46 | if (position == -1) { 47 | cout<<"The table is full and can't be expanded !"<hash_table[position] != value) { 51 | // The position returned is a empty slot 52 | this->hash_table[position] = value; 53 | this->elements++; 54 | } 55 | // Verify if it's time to expand the hash_table yet 56 | if((float)(this->elements/this->table_size) > EXPAND_FACTOR) { 57 | this->expand(); 58 | } 59 | } 60 | bool find(int value) { 61 | int position = this->find_position(value); 62 | if (position == -1) { //Value not present and table is full 63 | return false; 64 | } 65 | if (this->hash_table[position] == value) { //Value present 66 | return true; 67 | } 68 | return false; //Value not present and table has space 69 | } 70 | bool del(int value) { 71 | int position = this->find_position(value); 72 | if (this->hash_table[position] == value) { 73 | // Element found on the position, so let's remove it and decrease the counter 74 | this->hash_table[position] = 0; 75 | this->elements--; 76 | //Resizing and rehashing is expensive, that's why we will not resize it if we are removing elements 77 | return true; 78 | } 79 | return false; 80 | } 81 | void display() { 82 | for(int i = 0; i < this->table_size; i++) { 83 | if (hash_table[i] != 0) { 84 | cout<table_size); 99 | } 100 | 101 | // This function returns the position of a value or the first empty slot starting from the 102 | // hash of the value or -1 if the value is not present and the table is full 103 | int find_position(int value) { 104 | int hash = this->hash(value); 105 | // Search the inputed value starting from hash position 106 | for(int i = 0; i < this->table_size; i++) { 107 | if (this->hash_table[hash] == value) { 108 | return hash; 109 | } 110 | if (this->hash_table[hash] == 0) { 111 | return hash; 112 | } 113 | if (hash++ == this->table_size) { 114 | hash = 0; 115 | } 116 | } 117 | return -1; 118 | } 119 | 120 | // This function expands(double its size) the hash table 121 | void expand() { 122 | // Creates a new table 123 | int *new_table = new int[this->table_size * 2]; 124 | if (!new_table) { 125 | cout<<"The can't be expanded !"<hash_table; 130 | int old_table_size = this->table_size; 131 | this->hash_table = new_table; 132 | this->table_size = this->table_size * 2; 133 | memset(this->hash_table, 0, sizeof(int) * STARTING_SIZE); 134 | // Insert all values again, now the position(hash) could have changed because of the new table size 135 | for(int i = 0; i < old_table_size; i++) { 136 | if (old_table[i] != 0) { 137 | this->insert(old_table[i]); 138 | } 139 | } 140 | delete[] old_table; 141 | } 142 | }; 143 | 144 | 145 | // Main Function 146 | int main() { 147 | int option, value; 148 | HashTable hashtable; 149 | 150 | do { 151 | cout<<"\n**** MENU ****\n" 152 | <<"1. Insert\n" 153 | <<"2. Search\n" 154 | <<"3. Delete\n" 155 | <<"4. Display\n" 156 | <<"5. Exit\n"; 157 | 158 | cout<<"Enter your option: "; 159 | cin>>option; 160 | 161 | switch(option) { 162 | case 1: cout<<"Enter the value to be inserted: "; 163 | cin>>value; 164 | 165 | hashtable.insert(value); 166 | break; 167 | 168 | case 2: cout<<"Enter the value to be searched: "; 169 | cin>>value; 170 | 171 | if(hashtable.find(value)) { 172 | cout<<"The element was found !"<>value; 180 | if(hashtable.del(value)) { 181 | cout<<"The deleted was deleted !"< 22 | using namespace std; 23 | 24 | 25 | // Function prototypes 26 | struct node *insert_beg(node *, int ); 27 | struct node *insert_end(node *, int ); 28 | struct node *insert_after(node *, int ); 29 | struct node *insert_before(node *, int ); 30 | 31 | struct node *create(node *); 32 | 33 | struct node *del_beg(node *); 34 | struct node *del_end(node *); 35 | struct node *del_after(node *); 36 | struct node *del_before(node *); 37 | struct node *del_element(node *); 38 | 39 | void count(node *); 40 | void display(node *); 41 | 42 | 43 | /* 44 | 45 | Declaring a structure called node 46 | having two members: 47 | 48 | 1) data block 49 | 2) next block (which stores the address of the next node) 50 | 51 | */ 52 | 53 | struct node { 54 | int data; 55 | node *next; 56 | }; 57 | 58 | 59 | /* 60 | 61 | ALGORITHM FOR INSERTING AT THE BEGINNING: 62 | 63 | 1) IF AVAIL = NULL 64 | Write Overflow 65 | EXIT 66 | 67 | 2) NEW_NODE = AVAIL 68 | 3) AVAIL = AVAIL -> NEXT 69 | 4) NEW_NODE -> DATA = VALUE 70 | 5) NEW_NODE -> NEXT = START -> NEXT 71 | 6) START -> NEXT = NEW_NODE 72 | 7) EXIT 73 | 74 | */ 75 | 76 | node *insert_beg(node *start, int value) { 77 | // Creating a new node 78 | node *new_node = new node; 79 | 80 | // Storing the data and assigning 81 | // which address to keep in the next block 82 | new_node->data = value; 83 | new_node->next = start->next; 84 | 85 | // Updating the next block of start 86 | start->next = new_node; 87 | 88 | return start; 89 | } 90 | 91 | 92 | /* 93 | 94 | ALGORITHM FOR INSERTING AT THE END: 95 | 96 | 1) IF AVAIL = NULL 97 | Write Overflow 98 | EXIT 99 | 100 | 2) NEW_NODE = AVAIL 101 | 3) AVAIL = AVAIL -> NEXT 102 | 4) NEW_NODE -> DATA = VALUE 103 | 5) NEW_NODE -> NEXT = START 104 | 6) PTR = START -> NEXT 105 | 7) WHILE PTR -> NEXT != START 106 | PTR = PTR -> next 107 | 108 | 8) PTR -> NEXT = NEW_NODE 109 | 7) EXIT 110 | 111 | */ 112 | 113 | node *insert_end(node *start, int value) { 114 | // Storing the value of start to ptr 115 | node *ptr = start->next; 116 | 117 | // Creating a new node 118 | node *new_node = new node; 119 | 120 | // Storing the data and assigning 121 | // which address to keep in the next block 122 | new_node->data = value; 123 | new_node->next = start; 124 | 125 | // Searching for the last element after which we should insert 126 | while(ptr->next != start) { 127 | // Incrementing 128 | ptr = ptr->next; 129 | } 130 | 131 | // Storing the address of new node to the next pointer of ptr 132 | ptr->next = new_node; 133 | 134 | return start; 135 | } 136 | 137 | 138 | /* 139 | 140 | ALGORITHM FOR INSERTING AFTER A GIVEN NODE: 141 | 142 | 1) IF AVAIL = NULL 143 | Write Overflow 144 | EXIT 145 | 146 | 2) NEW_NODE = AVAIL 147 | 3) AVAIL = AVAIL -> NEXT 148 | 4) NEW_NODE -> DATA = VALUE 149 | 5) PTR = START -> NEXT 150 | 6) PREPTR = PTR 151 | 7) WHILE PREPTR -> NEXT != NUM 152 | PREPTR = PTR 153 | PTR = PTR -> NEXT 154 | 155 | 8) PREPTR -> NEXT = NEW_NODE 156 | 9) NEW_NODE -> NEXT = PTR 157 | 10) EXIT 158 | 159 | */ 160 | 161 | node *insert_after(node *start, int value) { 162 | int num; 163 | 164 | // Storing the value of start to ptr 165 | node *ptr = start->next; 166 | 167 | // Number after which to insert 168 | cout<<"Enter the number after which to insert: "; 169 | cin>>num; 170 | 171 | // Creating a new node 172 | node *new_node = new node; 173 | 174 | // Storing the data 175 | new_node->data = value; 176 | 177 | // Taking another node to store the location of 178 | // the element before ptr 179 | node *preptr = ptr; 180 | 181 | // Incrementing till the number is found 182 | while(preptr->data != num) { 183 | preptr = ptr; 184 | ptr = ptr->next; 185 | } 186 | 187 | // Adding the new node 188 | preptr->next = new_node; 189 | new_node->next = ptr; 190 | 191 | return start; 192 | } 193 | 194 | 195 | /* 196 | 197 | ALGORITHM FOR INSERTING BEFORE A GIVEN NODE: 198 | 199 | 1) IF AVAIL = NULL 200 | Write Overflow 201 | EXIT 202 | 203 | 2) NEW_NODE = AVAIL 204 | 3) AVAIL = AVAIL -> NEXT 205 | 4) NEW_NODE -> DATA = VALUE 206 | 5) PTR = START -> NEXT 207 | 6) PREPTR = PTR 208 | 7) WHILE PTR -> NEXT != NUM 209 | PREPTR = PTR 210 | PTR = PTR -> NEXT 211 | 212 | 8) PREPTR -> NEXT = NEW_NODE 213 | 9) NEW_NODE -> NEXT = PTR 214 | 10) EXIT 215 | 216 | */ 217 | 218 | node *insert_before(node *start, int value) { 219 | int num; 220 | 221 | // Number after which to insert 222 | cout<<"Enter the number before which to insert: "; 223 | cin>>num; 224 | 225 | // Creating a new node 226 | node *new_node = new node; 227 | 228 | // Storing the data 229 | new_node->data = value; 230 | 231 | // Storing the value of start to ptr 232 | node *ptr = start->next; 233 | 234 | // Taking another node to store the location of 235 | // the element before ptr 236 | node *preptr = ptr; 237 | 238 | // Incrementing till the number is found 239 | while(ptr->data != num) { 240 | preptr = ptr; 241 | ptr = ptr->next; 242 | } 243 | 244 | // Adding the new node 245 | preptr->next = new_node; 246 | new_node->next = ptr; 247 | 248 | return start; 249 | } 250 | 251 | 252 | /* 253 | 254 | ALGORITHM FOR CREATING THE LINKED LIST: 255 | 256 | 1) IF START = NULL 257 | 2) IF AVAIL = NULL 258 | Write Overflow 259 | EXIT 260 | 261 | 3) NEW_NODE = AVAIL 262 | 4) AVAIL = AVAIL -> NEXT 263 | 5) NEW_NODE -> DATA = VALUE 264 | 6) START -> NEXT = NEW_NODE 265 | 7) NEW_NODE -> NEXT = START 266 | [ END OF IF ] 267 | 268 | 8) ELSE 269 | INSERT AT THE END 270 | 271 | 9) EXIT 272 | 273 | */ 274 | 275 | node *create(node *start) { 276 | int value; 277 | 278 | // Storing the value to be inserted 279 | cout<<"Enter -1 to stop\n"; 280 | cout<<"Enter the value to be stored: "; 281 | cin>>value; 282 | 283 | while(value != -1){ 284 | if(start == NULL) { 285 | node *new_node = new node; 286 | new_node->data = value; 287 | 288 | start = new node; 289 | start->next = new_node; 290 | new_node->next = start; 291 | } 292 | else { 293 | // Elements are inserted at the end 294 | insert_end(start, value); 295 | } 296 | 297 | cout<<"Enter the value to be stored: "; 298 | cin>>value; 299 | } 300 | 301 | return start; 302 | } 303 | 304 | 305 | /* 306 | 307 | ALGORITHM FOR DELETING FROM THE BEGINNING: 308 | 309 | 1) IF START = NULL 310 | Write Underflow 311 | EXIT 312 | 313 | 2) PTR = START -> NEXT 314 | 3) START -> NEXT = PTR -> NEXT 315 | 4) DELETE PTR 316 | 5) EXIT 317 | 318 | */ 319 | 320 | node *del_beg(node *start) { 321 | // Checking if the linked list is empty 322 | if(start == NULL) { 323 | cout<<"The linked list is empty !\n"; 324 | } 325 | else { 326 | // Storing start to ptr 327 | node *ptr = start->next; 328 | 329 | // Updating the next block of ptr 330 | start->next = ptr->next; 331 | 332 | // Printing the value to be deleted 333 | cout<<"The deleted value is: "<data< NEXT != START 354 | PREPTR = PTR 355 | PTR = PTR -> NEXT 356 | 357 | 5) PREPTR -> NEXT = START 358 | 6) DELETE PTR 359 | 7) EXIT 360 | 361 | */ 362 | 363 | node *del_end(node *start) { 364 | // Checking if the linked list is empty 365 | if(start == NULL) { 366 | cout<<"The linked list is empty !\n"; 367 | } 368 | else { 369 | // Storing start to ptr 370 | node *ptr = start->next; 371 | 372 | // Defining another pointer for storing 373 | // the previous element of ptr 374 | node *preptr = ptr; 375 | 376 | // Incrementing till the last position 377 | while(ptr->next != start) { 378 | preptr = ptr; 379 | ptr = ptr->next; 380 | } 381 | 382 | // Storing the address of start in the NEXT block as the 383 | // the last element is to be deleted 384 | preptr->next = start; 385 | 386 | // Printing the value to be deleted 387 | cout<<"The deleted value is: "<data< NEXT 406 | 3) PREPTR = PTR 407 | 4) WHILE PREPTR -> DATA != NUM 408 | PREPTR = PTR 409 | PTR = PTR -> NEXT 410 | 411 | 5) PREPTR -> NEXT = PTR -> NEXT 412 | 6) DELETE PTR 413 | 7) EXIT 414 | 415 | */ 416 | 417 | node *del_after(node *start) { 418 | // Checking if the linked list is empty 419 | if(start == NULL) { 420 | cout<<"The linked list is empty !\n"; 421 | } 422 | else { 423 | int num; 424 | 425 | // Storing the element, after which to be deleted 426 | cout<<"Enter the element after which to be deleted: "; 427 | cin>>num; 428 | 429 | // Storing start to ptr 430 | node *ptr = start->next; 431 | 432 | // Defining another pointer for storing 433 | // the previous element of ptr 434 | node *preptr = ptr; 435 | 436 | // Incrementing till num is found 437 | while(preptr->data != num) { 438 | preptr = ptr; 439 | ptr = ptr->next; 440 | } 441 | 442 | // Storing the address of element leaving one block, 443 | // which is to be deleted 444 | preptr->next = ptr->next; 445 | 446 | // Printing the value to be deleted 447 | cout<<"The deleted value is: "<data< NEXT 466 | 3) PREPTR = PTR 467 | 4) WHILE PTR -> NEXT -> DATA != NUM 468 | PREPTR = PTR 469 | PTR = PTR -> NEXT 470 | 471 | 5) PREPTR -> NEXT = PTR -> NEXT 472 | 6) DELETE PTR 473 | 7) EXIT 474 | 475 | */ 476 | 477 | node *del_before(node *start) { 478 | // Checking if the linked list is empty 479 | if(start == NULL) { 480 | cout<<"The linked list is empty !\n"; 481 | } 482 | else { 483 | int num; 484 | 485 | // Storing the element, before which to be deleted 486 | cout<<"Enter the element before which to be deleted: "; 487 | cin>>num; 488 | 489 | // Storing start to ptr 490 | node *ptr = start -> next; 491 | 492 | // Defining another pointer for storing 493 | // the previous element of ptr 494 | node *preptr = ptr; 495 | 496 | // Incrementing till num is found 497 | while(ptr->next->data != num) { 498 | preptr = ptr; 499 | ptr = ptr->next; 500 | } 501 | 502 | // Storing the address of element leaving one block, 503 | // which is to be deleted 504 | preptr->next = ptr->next; 505 | 506 | // Printing the value to be deleted 507 | cout<<"The deleted value is: "<data< NEXT 526 | 3) PREPTR = PTR 527 | 4) WHILE PTR -> DATA != NUM 528 | PREPTR = PTR 529 | PTR = PTR -> NEXT 530 | 531 | 5) PREPTR -> NEXT = PTR -> NEXT 532 | 6) DELETE PTR 533 | 7) EXIT 534 | 535 | */ 536 | 537 | node *del_element(node *start) { 538 | // Checking if the linked list is empty 539 | if(start == NULL) { 540 | cout<<"The linked list is empty !\n"; 541 | } 542 | else { 543 | int num; 544 | 545 | // Storing the element, which to be deleted 546 | cout<<"Enter the element which to be deleted: "; 547 | cin>>num; 548 | 549 | // Storing start to ptr 550 | node *ptr = start -> next; 551 | 552 | // Defining another pointer for storing 553 | // the previous element of ptr 554 | node *preptr = ptr; 555 | 556 | // Incrementing till num is found 557 | while(ptr->data != num) { 558 | preptr = ptr; 559 | ptr = ptr->next; 560 | } 561 | 562 | // Storing the address of element leaving one block, 563 | // which is to be deleted 564 | preptr->next = ptr->next; 565 | 566 | // Printing the value to be deleted 567 | cout<<"The deleted value is: "<data< NEXT 590 | 591 | 5) WRITE COUNT 592 | 6) EXIT 593 | 594 | */ 595 | 596 | void count(node *start) { 597 | int count = 0; 598 | 599 | // Checking if the linked list is empty 600 | if(start == NULL) { 601 | cout<<"The linked list is empty !\n"; 602 | } 603 | else { 604 | count = 1; 605 | 606 | // Storing start in ptr 607 | node *ptr = start->next; 608 | 609 | // Incrementing till the end 610 | while(ptr->next != start) { 611 | count++; 612 | ptr = ptr->next; 613 | } 614 | } 615 | // Printing the number of elements 616 | cout<<"The number of elements present in the linked list are: " 617 | < DATA 633 | PTR = PTR -> NEXT 634 | 4) EXIT 635 | 636 | */ 637 | 638 | void display(node *start) { 639 | // Checking if the linked list is empty 640 | if(start == NULL) { 641 | cout<<"The linked list is empty !\n"; 642 | } 643 | else { 644 | // Storing start in ptr 645 | node *ptr = start->next; 646 | 647 | // Incrementing till the end 648 | while(ptr->next != start) { 649 | // Printing the elements 650 | cout<data<<" "; 651 | ptr = ptr->next; 652 | 653 | } 654 | cout<data<>option; 685 | 686 | switch(option) { 687 | 688 | case 1: start = create(start); 689 | break; 690 | 691 | case 2: // Storing the value to be inserted 692 | cout<<"Enter the value to be stored: "; 693 | cin>>value; 694 | 695 | start = insert_beg(start, value); 696 | break; 697 | 698 | case 3: // Storing the value to be inserted 699 | cout<<"Enter the value to be stored: "; 700 | cin>>value; 701 | 702 | start = insert_end(start, value); 703 | break; 704 | 705 | case 4: // Storing the value to be inserted 706 | cout<<"Enter the value to be stored: "; 707 | cin>>value; 708 | 709 | start = insert_after(start, value); 710 | break; 711 | 712 | case 5: // Storing the value to be inserted 713 | cout<<"Enter the value to be stored: "; 714 | cin>>value; 715 | 716 | start = insert_before(start, value); 717 | break; 718 | 719 | case 6: start = del_beg(start); 720 | break; 721 | 722 | case 7: start = del_end(start); 723 | break; 724 | 725 | case 8: start = del_after(start); 726 | break; 727 | 728 | case 9: start = del_before(start); 729 | break; 730 | 731 | case 10: start = del_element(start); 732 | break; 733 | 734 | case 11: count(start); 735 | break; 736 | 737 | case 12: display(start); 738 | break; 739 | 740 | case 13: break; 741 | 742 | default: cout<<"Wrong option is selecred !!\n"; 743 | break; 744 | } 745 | } while(option != 13); 746 | 747 | // Freeing the space occupied by start 748 | if(option == 13) { 749 | cout<< "\nTHANK YOU for using the program !\n" 750 | <<"Have a good day.\n\n"; 751 | 752 | delete start; 753 | } 754 | } 755 | -------------------------------------------------------------------------------- /kruskal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define edge pair 7 | 8 | class Graph { 9 | private: 10 | vector> G; // graph 11 | vector> T; // mst 12 | int *parent; 13 | int V; // number of vertices/nodes in graph 14 | public: 15 | Graph(int V); 16 | void AddWeightedEdge(int u, int v, int w); 17 | int find_set(int i); 18 | void union_set(int u, int v); 19 | void kruskal(); 20 | void print(); 21 | }; 22 | Graph::Graph(int V) { 23 | parent = new int[V]; 24 | 25 | //i 0 1 2 3 4 5 26 | //parent[i] 0 1 2 3 4 5 27 | for (int i = 0; i < V; i++) 28 | parent[i] = i; 29 | 30 | G.clear(); 31 | T.clear(); 32 | } 33 | void Graph::AddWeightedEdge(int u, int v, int w) { 34 | G.push_back(make_pair(w, edge(u, v))); 35 | } 36 | int Graph::find_set(int i) { 37 | // If i is the parent of itself 38 | if (i == parent[i]) 39 | return i; 40 | else 41 | // Else if i is not the parent of itself 42 | // Then i is not the representative of his set, 43 | // so we recursively call Find on its parent 44 | return find_set(parent[i]); 45 | } 46 | 47 | void Graph::union_set(int u, int v) { 48 | parent[u] = parent[v]; 49 | } 50 | void Graph::kruskal() { 51 | int i, uRep, vRep; 52 | sort(G.begin(), G.end()); // increasing weight 53 | for (i = 0; i < G.size(); i++) { 54 | uRep = find_set(G[i].second.first); 55 | vRep = find_set(G[i].second.second); 56 | if (uRep != vRep) { 57 | T.push_back(G[i]); // add to tree 58 | union_set(uRep, vRep); 59 | } 60 | } 61 | } 62 | void Graph::print() { 63 | cout << "Edge :" << " Weight" << endl; 64 | for (int i = 0; i < T.size(); i++) { 65 | cout << T[i].second.first << " - " << T[i].second.second << " : " 66 | << T[i].first; 67 | cout << endl; 68 | } 69 | } 70 | int main() { 71 | int V,E; 72 | cout<<"Enter the number of vertex and edges :- "<>V>>E; 74 | 75 | Graph g(V); 76 | cout<<"Enter the edge and weight(a,b,weight) : - "<>a>>b>>w; 80 | g.AddWeightedEdge(a,b,w); 81 | } 82 | g.kruskal(); 83 | g.print(); 84 | return 0; 85 | } -------------------------------------------------------------------------------- /parentheses_checker.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Souvik Biswas 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | #include 22 | using namespace std; 23 | 24 | // Defining the maximum size of the stack to be 10 25 | #define MAX 10 26 | 27 | // Defining a class to store the member variables 28 | // and member functions required 29 | class Stack { 30 | int stk[MAX]; 31 | 32 | public: 33 | int top; 34 | Stack(): top(-1) {} 35 | void push(char ); 36 | char pop(); 37 | void display(); 38 | }; 39 | 40 | 41 | /* 42 | 43 | ALGORITHM FOR PUSHING AN ELEMENT IN THE STACK: 44 | 45 | 1) IF TOP = MAX-1 46 | WRITE Overflow 47 | EXIT 48 | 49 | 2) TOP = TOP + 1 50 | 3) STACK[TOP] = VALUE 51 | 4) EXIT 52 | 53 | */ 54 | 55 | void Stack :: push(char value) { 56 | // When the size of stack has reached maximum, 57 | // we cannot insert any element 58 | if(top == MAX-1) { 59 | cout<<"The Stack is full !\n"; 60 | } 61 | else { 62 | // Incrementing top 63 | top++; 64 | 65 | // Store the value in the stack at top position 66 | stk[top] = value; 67 | } 68 | } 69 | 70 | 71 | /* 72 | 73 | ALGORITHM FOR POPPING AN ELEMENT FROM THE STACK: 74 | 75 | 1) IF TOP = -1 76 | WRITE Underflow 77 | RETURN -1 78 | EXIT 79 | 80 | 2) VALUE = STK[TOP] 81 | 3) TOP = TOP - 1 82 | 4) RETURN VALUE 83 | 5) EXIT 84 | 85 | */ 86 | 87 | char Stack :: pop() { 88 | // When the size of stack is -1, the stack is empty 89 | if(top == -1) { 90 | cout<<"The Stack is empty !\n"; 91 | return -1; 92 | } 93 | else { 94 | // Store the top value 95 | char value = stk[top]; 96 | 97 | // Decrement top 98 | top --; 99 | 100 | return value; 101 | } 102 | } 103 | 104 | 105 | int main() { 106 | Stack s; 107 | string exp; 108 | char temp; 109 | int flag = 1; 110 | 111 | cout<<"Enter the expression: "; 112 | getline(cin, exp); 113 | 114 | for(int i=0; i < exp.length(); i++) { 115 | if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[') { 116 | s.push(exp[i]); 117 | } 118 | if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']') { 119 | if(s.top == -1) { 120 | flag = 0; 121 | } 122 | else { 123 | temp = s.pop(); 124 | if((exp[i] == ')') && (temp == '{' || temp == '[')) { 125 | flag = 0; 126 | } 127 | if((exp[i] == '}') && (temp == '(' || temp == ']')) { 128 | flag = 0; 129 | } 130 | if((exp[i] == ']') && (temp == '(' || temp == '{')) { 131 | flag = 0; 132 | } 133 | } 134 | } 135 | } 136 | 137 | if(s.top >= 0) { 138 | flag = 0; 139 | } 140 | 141 | if(flag == 1) { 142 | cout<<"The expression is valid.\n"; 143 | } 144 | else { 145 | cout<<"The expression is not valid.\n"; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /priority_queue.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Souvik Biswas 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | #include 22 | using namespace std; 23 | 24 | struct node *insert(node *); 25 | struct node *del(node *); 26 | void display(node *); 27 | 28 | /* 29 | 30 | Declaring a structure called node 31 | having two members: 32 | 33 | 1) data block 34 | 2) priority block (to store the priority of the queue) 35 | 3) next block (which stores the address of the next node) 36 | 37 | */ 38 | 39 | struct node { 40 | int data; 41 | int priority; 42 | node *next; 43 | }; 44 | 45 | node *insert(node *start) { 46 | int value, priority; 47 | 48 | cout<<"Enter the value to be inserted: "; 49 | cin>>value; 50 | 51 | cout<<"Enter the priority: "; 52 | cin>>priority; 53 | 54 | node *new_node = new node; 55 | new_node->data = value; 56 | new_node->priority = priority; 57 | 58 | if(start == NULL || priority < start->priority) { 59 | new_node->next = start; 60 | start = new_node; 61 | } 62 | else { 63 | node *ptr = start; 64 | 65 | if(ptr->next != NULL && ptr->next->priority <= priority) { 66 | ptr = ptr->next; 67 | } 68 | new_node->next = ptr->next; 69 | ptr->next = new_node; 70 | } 71 | 72 | return start; 73 | } 74 | 75 | node *del(node *start) { 76 | if(start == NULL) { 77 | cout<<"The Queue is empty !\n"; 78 | } 79 | else { 80 | node *ptr = start; 81 | 82 | cout<<"The deleted node is "<data<next; 85 | delete ptr; 86 | } 87 | return start; 88 | } 89 | 90 | void display(node *start) { 91 | if(start == NULL) { 92 | cout<<"The Queue is empty !\n"; 93 | } 94 | else { 95 | node *ptr = start; 96 | 97 | while(ptr != NULL) { 98 | cout<data<<" [Priority: "<priority<<"]"<next; 100 | } 101 | } 102 | } 103 | 104 | int main() { 105 | node *start = NULL; 106 | 107 | int option; 108 | 109 | do { 110 | cout<<"\n***** MENU *****\n" 111 | <<"1. Insert\n" 112 | <<"2. Delete\n" 113 | <<"3. Display\n" 114 | <<"4. Exit\n"; 115 | 116 | cout<<"Enter your option: "; 117 | cin>>option; 118 | 119 | switch(option) { 120 | case 1: start = insert(start); 121 | break; 122 | 123 | case 2: start = del(start); 124 | break; 125 | 126 | case 3: cout<<"The Queue elements are:\n"; 127 | display(start); 128 | break; 129 | 130 | case 4: break; 131 | 132 | default: cout<<"Wrong option !\n"; 133 | break; 134 | } 135 | } while(option != 4); 136 | 137 | if(option == 4) { 138 | cout<< "\nTHANK YOU for using the program !\n" 139 | <<"Have a good day.\n\n"; 140 | 141 | delete start; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /queue.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Souvik Biswas 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | #include 22 | using namespace std; 23 | 24 | // Defining the maximum size of the queue to be 10 25 | #define MAX 10 26 | 27 | // Defining a class to store the member variables 28 | // and member functions required 29 | class Queue { 30 | int queue[MAX], front, rear; 31 | 32 | public: 33 | Queue(): front(-1), rear(-1) {} 34 | void insert(int ); 35 | int del(); 36 | void display(); 37 | }; 38 | 39 | 40 | /* 41 | 42 | ALGORITHM FOR INSERTING AN ELEMENT IN THE QUEUE: 43 | 44 | 1) IF REAR = MAX-1 45 | WRITE Overflow 46 | EXIT 47 | 48 | 2) REAR = REAR + 1 49 | 3) QUEUE[REAR] = VALUE 50 | 4) EXIT 51 | 52 | */ 53 | 54 | void Queue :: insert(int value) { 55 | if(rear == MAX-1) { 56 | cout<<"The Queue is full !\n"; 57 | } 58 | else { 59 | rear++; 60 | queue[rear] = value; 61 | } 62 | } 63 | 64 | 65 | /* 66 | 67 | ALGORITHM FOR DELETING AN ELEMENT FROM THE QUEUE: 68 | 69 | 1) IF FRONT = REAR 70 | WRITE Underflow 71 | RETURN -1 72 | EXIT 73 | 74 | 2) FRONT = FRONT + 1 75 | 3) VALUE = QUEUE[FRONT] 76 | 4) RETURN VALUE 77 | 5) EXIT 78 | 79 | */ 80 | 81 | int Queue :: del() { 82 | // When front is equal to rear, then the queue is empty 83 | if(front == rear) { 84 | cout<<"The Queue is empty !\n"; 85 | return -1; 86 | } 87 | else { 88 | // Incrementing front 89 | front++; 90 | 91 | // Storing the element in front position in value 92 | int value = queue[front]; 93 | 94 | return value; 95 | } 96 | } 97 | 98 | // Display the elements of the Queue by incrementing 99 | void Queue :: display() { 100 | // When front is equal to rear, then the queue is empty 101 | if(front == rear) { 102 | cout<<"The Queue is empty !\n"; 103 | } 104 | else { 105 | // Printing each element 106 | for(int i=front+1; i<=rear; i++) { 107 | cout<>option; 127 | 128 | switch(option) { 129 | case 1: cout<<"Enter the value to be inserted: "; 130 | cin>>value; 131 | 132 | q.insert(value); 133 | break; 134 | 135 | case 2: value = q.del(); 136 | if(value != -1) { 137 | cout<<"The deleted element is: "< 22 | using namespace std; 23 | 24 | /* 25 | 26 | Declaring a structure called node 27 | having two members: 28 | 29 | 1) data block 30 | 2) next block (which stores the address of the next node) 31 | 32 | */ 33 | 34 | struct node { 35 | int data; 36 | node *next; 37 | }; 38 | 39 | 40 | /* 41 | 42 | Declaring a class called Queue having: 43 | 44 | two private member: 45 | front 46 | rear 47 | 48 | three public member functions: 49 | insert 50 | delete 51 | display 52 | 53 | The top is set to NULL initially 54 | 55 | */ 56 | 57 | class Queue { 58 | node *front, *rear; 59 | 60 | public: 61 | Queue(): front(NULL), rear(NULL) {} 62 | void insert(int ); 63 | void del(); 64 | void display(); 65 | }; 66 | 67 | 68 | /* 69 | 70 | ALGORITHM FOR INSERTING AN ELEMENT IN THE QUEUE: 71 | 72 | 1) IF AVAIL = NULL 73 | WRITE Overflow 74 | EXIT 75 | 76 | 2) NEW_NODE = AVAIL 77 | 3) AVAIL = AVAIL -> NEXT 78 | 4) NEW_NODE -> DATA = VALUE 79 | 5) NEW_NODE -> NEXT = NULL 80 | 6) IF REAR = NULL 81 | REAR = NEW_NODE 82 | FRONT = NEW_NODE 83 | 84 | 6) ELSE 85 | REAR -> NEXT = NEW_NODE 86 | REAR = NEW_NODE 87 | 88 | 7) EXIT 89 | 90 | */ 91 | 92 | void Queue :: insert(int value) { 93 | node *new_node = new node; 94 | new_node->data = value; 95 | new_node->next = NULL; 96 | 97 | if(rear == NULL) { 98 | rear = new_node; 99 | front = new_node; 100 | } 101 | else { 102 | rear->next = new_node; 103 | rear = new_node; 104 | } 105 | } 106 | 107 | 108 | /* 109 | 110 | ALGORITHM TO DELETE A NODE FROM QUEUE: 111 | 112 | 1) IF FRONT = NULL 113 | WRITE Underflow 114 | EXIT 115 | 116 | 2) PTR = FRONT; 117 | 3) FRONT = FRONT -> NEXT 118 | 4) DELETE PTR 119 | 5) EXIT 120 | 121 | */ 122 | 123 | void Queue :: del() { 124 | // Checking if the queue is empty 125 | if(front == NULL) { 126 | cout<<"The Queue is empty !\n"; 127 | } 128 | else { 129 | // Storing the front node in ptr 130 | node *ptr = front; 131 | 132 | // Incrementing front 133 | front = front->next; 134 | 135 | // Printing the element to be deleted 136 | cout<<"The deleted node is: "<data< NEXT 160 | 161 | 4) EXIT 162 | 163 | */ 164 | 165 | void Queue :: display() { 166 | if(front == NULL) { 167 | cout<<"The Queue is empty !\n"; 168 | } 169 | else { 170 | node *ptr = front; 171 | 172 | while(ptr != NULL) { 173 | cout<data<<" "; 174 | ptr = ptr->next; 175 | } cout<>option; 194 | 195 | switch(option) { 196 | case 1: cout<<"Enter the value to be inserted: "; 197 | cin>>value; 198 | 199 | q.insert(value); 200 | break; 201 | 202 | case 2: q.del(); 203 | break; 204 | 205 | case 3: q.display(); 206 | break; 207 | 208 | case 4: break; 209 | 210 | default: cout<<"Wrong option !\n"; 211 | break; 212 | } 213 | } while(option != 4); 214 | 215 | if(option == 4) { 216 | cout<< "\nTHANK YOU for using the program !\n" 217 | <<"Have a good day.\n\n"; 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /right_threaded _bst.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Souvik Biswas 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | #include 22 | using namespace std; 23 | 24 | // Functions for inserting an element in the tree 25 | // and creating the tree by calling insert method repetadly 26 | struct node *insert(node *, int ); 27 | struct node *create(node *); 28 | 29 | // Functions for traversing the tree 30 | void inorderTraversal(node *); 31 | 32 | /* 33 | 34 | Declaring a structure called node 35 | having four members: 36 | 37 | 1) left block (which stores the address of the left node) 38 | 2) data block (which stores the value) 39 | 3) right block (which stores the address of the right node) 40 | 4) rightThread block (which stores the a boolean, determing whether 41 | the node has a right branch or not) 42 | 43 | */ 44 | 45 | struct node { 46 | node *left; 47 | int data; 48 | node *right; 49 | bool rightThread; 50 | }; 51 | 52 | 53 | /* 54 | 55 | ALGORITHM FOR INSERTING A NODE IN THE TREE: 56 | 57 | 1) IF AVAIL = NULL 58 | WRITE Overflow 59 | EXIT 60 | 61 | 2) NEW_NODE = AVAIL 62 | 3) AVAIL = AVAIL -> LEFT 63 | 4) NEW_NODE -> DATA = VALUE 64 | 5) NEW_NODE -> LEFT = NULL 65 | 6) NEW_NODE -> RIGHT_THREAD = 1 66 | 7) IF TREE = NULL 67 | TREE = NEW_NODE 68 | NEW_NODE -> RIGHT = NULL 69 | NEW_NODE -> RIGHT_THREAD = 0 70 | 71 | 8) ELSE 72 | 9) PAR = NULL 73 | 10) CUR = TREE 74 | 11) WHILE CUR != NULL 75 | 12) PAR = CUR 76 | 13) IF VALUE < CUR -> data 77 | CUR = CUR -> LEFT 78 | 79 | 14) ELSE 80 | IF RIGHT_THREAD = 1 81 | CUR = NULL 82 | ELSE 83 | CUR = CUR -> RIGHT 84 | [END OF IF] 85 | [END OF IF] 86 | [END OF WHILE] 87 | 88 | 15) IF VALUE < PAR -> DATA 89 | PAR -> LEFT = NEW_NODE 90 | NEW_NODE -> RIGHT = PAR 91 | NEW_NODE -> RIGHT_THREAD = 1 92 | 93 | 16) ELSE 94 | IF PAR -> RIGHT_THREAD = 1 95 | PAR -> RIGHT_THREAD = 0 96 | NEW_NODE -> RIGHT_THREAD = 1 97 | NEW_NODE -> RIGHT = PAR -> RIGHT 98 | PAR -> RIGHT = NEW_NODE 99 | 100 | ELSE 101 | NEW_NODE -> RIGHT_THREAD = 0 102 | NEW_NODE -> RIGHT = NULL 103 | PAR -> RIGHT = NEW_NODE 104 | 105 | [END OF IF] 106 | [END OF IF] 107 | 108 | 17) EXIT 109 | 110 | */ 111 | 112 | node *insert(node *tree, int value) { 113 | // Creating a new node 114 | node *new_node = new node; 115 | // Storing values in all blocks of new_node 116 | new_node->data = value; 117 | new_node->left = NULL; 118 | new_node->rightThread = true; 119 | 120 | // When tree is null, just store the new_node in tree 121 | if(tree == NULL) { 122 | tree = new_node; 123 | new_node->right = NULL; 124 | new_node->rightThread = false; 125 | } 126 | else { 127 | // Setting parent node to null 128 | node *parentNode = NULL; 129 | // Storing tree in current node 130 | node *currentNode = tree; 131 | 132 | // Repeat when current node is null 133 | while(currentNode != NULL) { 134 | // Storing current node in parent node 135 | parentNode = currentNode; 136 | 137 | // When the value to be inserted is less than the current node, 138 | // we have to go to the left branch and otherwise 139 | // check if the currentNode has any right thread 140 | 141 | // 1) if it has then, assign null to currentNode, else 142 | // 2) Traverse currentNode's right branch by one palce 143 | if(value < currentNode->data) { 144 | currentNode = currentNode->left; 145 | } 146 | else { 147 | if(currentNode->rightThread) { 148 | currentNode = NULL; 149 | } 150 | else { 151 | currentNode = currentNode->right; 152 | } 153 | } 154 | } 155 | 156 | // When the value to be inserted is less than the parentNode value 157 | if(value < parentNode->data) { 158 | parentNode->left = new_node; 159 | new_node->right = parentNode; 160 | new_node->rightThread = true; 161 | } 162 | // When the value to be inserted is greater than the parentNode value 163 | else { 164 | // When parentNode's right thread is present 165 | if(parentNode->rightThread) { 166 | parentNode->rightThread = false; 167 | new_node->rightThread = true; 168 | new_node->right = parentNode->right; 169 | parentNode->right = new_node; 170 | } 171 | else { 172 | new_node->rightThread = false; 173 | new_node->right = NULL; 174 | parentNode->right = new_node; 175 | } 176 | } 177 | } 178 | return tree; 179 | } 180 | 181 | // Creating the tree by calling insert method repetadly 182 | node *create(node *tree) { 183 | int value; 184 | 185 | // Taking the value to be inserted as input 186 | cout<<"Enter the value to be inserted: "; 187 | cin>>value; 188 | 189 | // When the value is -1, then stop storing more values 190 | while(value != -1) { 191 | tree = insert(tree, value); 192 | 193 | // Taking the value to be inserted as input 194 | cout<<"Enter the value to be inserted: "; 195 | cin>>value; 196 | } 197 | 198 | return tree; 199 | } 200 | 201 | 202 | /* 203 | 204 | ALGORITHM FOR INORDER TRAVERSAL: 205 | 206 | 1) CUR = TREE 207 | 2) DO 208 | { 209 | 2.1) PAR = NULL 210 | 211 | 2.2) WHILE CUR != NULL 212 | PAR = CUR 213 | CUR = CUR ->LEFT 214 | [END OF WHILE] 215 | 216 | 2.3) IF PAR != NULL 217 | PRINT PAR -> DATA 218 | CUR = PAR -> RIGHT 219 | 220 | WHILE PAR -> RIGHT_THREAD = 1 && CUR != NULL 221 | PRINT CUR -> DATA 222 | PAR = CUR 223 | CUR = CUR -> RIGHT 224 | [END OF WHILE] 225 | [END OF IF] 226 | 227 | } WHILE CUR != NULL 228 | 229 | [END OF DO-WHILE] 230 | 231 | 3) EXIT 232 | */ 233 | 234 | void inorderTraversal(node *tree) { 235 | // Storing the tree node in currentNode 236 | node *currentNode = tree; 237 | 238 | do { 239 | // Assigning parentNode to be null initially 240 | node *parentNode = NULL; 241 | 242 | // When current is not null traverse the left branch of the 243 | // current node 244 | while(currentNode != NULL) { 245 | parentNode = currentNode; 246 | currentNode = currentNode->left; 247 | } 248 | 249 | if(parentNode != NULL) { 250 | // Printing thr parentNode value 251 | cout<data<<" "; 252 | 253 | // Storing parentNode -> right in currentNode 254 | currentNode = parentNode->right; 255 | 256 | // When parentNode has rightThread and currentNode is not null 257 | while(parentNode->rightThread && currentNode != NULL) { 258 | // Print the currentNode value 259 | cout<data<<" "; 260 | 261 | // Store currentNode in parentNode 262 | parentNode = currentNode; 263 | 264 | // traverse the left branch of the current node by one place 265 | currentNode = currentNode->right; 266 | } 267 | } 268 | } while(currentNode != NULL); 269 | } 270 | 271 | 272 | // MAIN FUNCTION 273 | int main() { 274 | // Setting the root node to null, initially 275 | node *root = NULL; 276 | 277 | int option, value; 278 | 279 | do { 280 | cout<<"\n***** MENU *****\n" 281 | <<"1. Create\n" 282 | <<"2. Insert\n" 283 | <<"3. Inorder Traversal\n" 284 | <<"4. Exit\n"; 285 | 286 | cout<<"Enter your option: "; 287 | cin>>option; 288 | 289 | switch(option) { 290 | case 1: root = create(root); 291 | break; 292 | 293 | case 2: cout<<"Enter the value to be inserted: "; 294 | cin>>value; 295 | 296 | root = insert(root, value); 297 | break; 298 | 299 | case 3: inorderTraversal(root); 300 | cout< 22 | using namespace std; 23 | 24 | 25 | // Function prototypes 26 | struct node *insert_beg(node *, int ); 27 | struct node *insert_end(node *, int ); 28 | struct node *insert_after(node *, int ); 29 | struct node *insert_before(node *, int ); 30 | 31 | struct node *create(node *); 32 | 33 | struct node *del_beg(node *); 34 | struct node *del_end(node *); 35 | struct node *del_after(node *); 36 | struct node *del_before(node *); 37 | struct node *del_element(node *); 38 | 39 | void count(node *); 40 | void display(node *); 41 | 42 | 43 | /* 44 | 45 | Declaring a structure called node 46 | having two members: 47 | 48 | 1) data block 49 | 2) next block (which stores the address of the next node) 50 | 51 | */ 52 | 53 | struct node { 54 | int data; 55 | node *next; 56 | }; 57 | 58 | 59 | /* 60 | 61 | ALGORITHM FOR INSERTING AT THE BEGINNING: 62 | 63 | 1) IF AVAIL = NULL 64 | Write Overflow 65 | EXIT 66 | 67 | 2) NEW_NODE = AVAIL 68 | 3) AVAIL = AVAIL -> NEXT 69 | 4) NEW_NODE -> DATA = VALUE 70 | 5) NEW_NODE -> NEXT = START 71 | 6) START = NEW_NODE 72 | 7) EXIT 73 | 74 | */ 75 | 76 | node *insert_beg(node *start, int value) { 77 | // Creating a new node 78 | node *new_node = new node; 79 | 80 | // Storing the data and assigning 81 | // which address to keep in the next block 82 | new_node->data = value; 83 | new_node->next = start; 84 | 85 | // making the start point to be first element of the linked list 86 | start = new_node; 87 | 88 | return start; 89 | } 90 | 91 | 92 | /* 93 | 94 | ALGORITHM FOR INSERTING AT THE END: 95 | 96 | 1) IF AVAIL = NULL 97 | Write Overflow 98 | EXIT 99 | 100 | 2) NEW_NODE = AVAIL 101 | 3) AVAIL = AVAIL -> NEXT 102 | 4) NEW_NODE -> DATA = VALUE 103 | 5) NEW_NODE -> NEXT = NULL 104 | 6) PTR = START 105 | 7) WHILE PTR -> NEXT != NULL 106 | PTR = PTR -> next 107 | 108 | 8) PTR -> NEXT = NEW_NODE 109 | 7) EXIT 110 | 111 | */ 112 | 113 | node *insert_end(node *start, int value) { 114 | // Storing the value of start to ptr 115 | node *ptr = start; 116 | 117 | // Creating a new node 118 | node *new_node = new node; 119 | 120 | // Storing the data and assigning 121 | // which address to keep in the next block 122 | new_node->data = value; 123 | new_node->next = NULL; 124 | 125 | // Searching for the last element after which we should insert 126 | while(ptr->next != NULL) { 127 | // Incrementing 128 | ptr = ptr->next; 129 | } 130 | 131 | // Storing the address of new node to the next pointer of ptr 132 | ptr->next = new_node; 133 | 134 | return start; 135 | } 136 | 137 | 138 | /* 139 | 140 | ALGORITHM FOR INSERTING AFTER A GIVEN NODE: 141 | 142 | 1) IF AVAIL = NULL 143 | Write Overflow 144 | EXIT 145 | 146 | 2) NEW_NODE = AVAIL 147 | 3) AVAIL = AVAIL -> NEXT 148 | 4) NEW_NODE -> DATA = VALUE 149 | 5) PTR = START 150 | 6) PREPTR = PTR 151 | 7) WHILE PREPTR -> NEXT != NUM 152 | PREPTR = PTR 153 | PTR = PTR -> NEXT 154 | 155 | 8) PREPTR -> NEXT = NEW_NODE 156 | 9) NEW_NODE -> NEXT = PTR 157 | 10) EXIT 158 | 159 | */ 160 | 161 | node *insert_after(node *start, int value) { 162 | int num; 163 | 164 | // Storing the value of start to ptr 165 | node *ptr = start; 166 | 167 | // Number after which to insert 168 | cout<<"Enter the number after which to insert: "; 169 | cin>>num; 170 | 171 | // Creating a new node 172 | node *new_node = new node; 173 | 174 | // Storing the data 175 | new_node->data = value; 176 | 177 | // Taking another node to store the location of 178 | // the element before ptr 179 | node *preptr = ptr; 180 | 181 | // Incrementing till the number is found 182 | while(preptr->data != num) { 183 | preptr = ptr; 184 | ptr = ptr->next; 185 | } 186 | 187 | // Adding the new node 188 | preptr->next = new_node; 189 | new_node->next = ptr; 190 | 191 | return start; 192 | } 193 | 194 | 195 | /* 196 | 197 | ALGORITHM FOR INSERTING BEFORE A GIVEN NODE: 198 | 199 | 1) IF AVAIL = NULL 200 | Write Overflow 201 | EXIT 202 | 203 | 2) NEW_NODE = AVAIL 204 | 3) AVAIL = AVAIL -> NEXT 205 | 4) NEW_NODE -> DATA = VALUE 206 | 5) PTR = START 207 | 6) PREPTR = PTR 208 | 7) WHILE PTR -> NEXT != NUM 209 | PREPTR = PTR 210 | PTR = PTR -> NEXT 211 | 212 | 8) PREPTR -> NEXT = NEW_NODE 213 | 9) NEW_NODE -> NEXT = PTR 214 | 10) EXIT 215 | 216 | */ 217 | 218 | node *insert_before(node *start, int value) { 219 | int num; 220 | 221 | // Storing the value of start to ptr 222 | node *ptr = start; 223 | 224 | // Number after which to insert 225 | cout<<"Enter the number before which to insert: "; 226 | cin>>num; 227 | 228 | // Creating a new node 229 | node *new_node = new node; 230 | 231 | // Storing the data 232 | new_node->data = value; 233 | 234 | // Taking another node to store the location of 235 | // the element before ptr 236 | node *preptr = ptr; 237 | 238 | // Incrementing till the number is found 239 | while(ptr->data != num) { 240 | preptr = ptr; 241 | ptr = ptr->next; 242 | } 243 | 244 | // Adding the new node 245 | preptr->next = new_node; 246 | new_node->next = ptr; 247 | 248 | return start; 249 | } 250 | 251 | // For creating the entire linked list 252 | node *create(node *start) { 253 | int value; 254 | 255 | // Storing the value to be inserted 256 | cout<<"Enter -1 to stop\n"; 257 | cout<<"Enter the value to be stored: "; 258 | cin>>value; 259 | 260 | while(value != -1){ 261 | if(start == NULL) { 262 | node *new_node = new node; 263 | new_node->data = value; 264 | new_node->next = NULL; 265 | 266 | start = new_node; 267 | } 268 | else { 269 | // Elements are inserted at the end 270 | insert_end(start, value); 271 | } 272 | 273 | cout<<"Enter the value to be stored: "; 274 | cin>>value; 275 | } 276 | 277 | return start; 278 | } 279 | 280 | 281 | /* 282 | 283 | ALGORITHM FOR DELETING FROM THE BEGINNING: 284 | 285 | 1) IF START = NULL 286 | Write Underflow 287 | EXIT 288 | 289 | 2) PTR = START 290 | 3) START = START -> NEXT 291 | 4) DELETE PTR 292 | 5) EXIT 293 | 294 | */ 295 | 296 | node *del_beg(node *start) { 297 | // Checking if the linked list is empty 298 | if(start == NULL) { 299 | cout<<"The linked list is empty !\n"; 300 | } 301 | else { 302 | // Storing start to ptr 303 | node *ptr = start; 304 | 305 | // Incrementing start to one position 306 | start = start->next; 307 | 308 | // Printing the value to be deleted 309 | cout<<"The deleted value is: "<data< NEXT != NULL 330 | PREPTR = PTR 331 | PTR = PTR -> NEXT 332 | 333 | 5) PREPTR -> NEXT = NULL 334 | 6) DELETE PTR 335 | 7) EXIT 336 | 337 | */ 338 | 339 | node *del_end(node *start) { 340 | // Checking if the linked list is empty 341 | if(start == NULL) { 342 | cout<<"The linked list is empty !\n"; 343 | } 344 | else { 345 | // Storing start to ptr 346 | node *ptr = start; 347 | 348 | // Defining another pointer for storing 349 | // the previous element of ptr 350 | node *preptr = ptr; 351 | 352 | // Incrementing till the last position 353 | while(ptr->next != NULL) { 354 | preptr = ptr; 355 | ptr = ptr->next; 356 | } 357 | 358 | // Storing null in the NEXT block as there 359 | // is no element after this 360 | preptr->next = NULL; 361 | 362 | // Printing the value to be deleted 363 | cout<<"The deleted value is: "<data< DATA != NUM 384 | PREPTR = PTR 385 | PTR = PTR -> NEXT 386 | 387 | 5) PREPTR -> NEXT = PTR -> NEXT 388 | 6) DELETE PTR 389 | 7) EXIT 390 | 391 | */ 392 | 393 | node *del_after(node *start) { 394 | // Checking if the linked list is empty 395 | if(start == NULL) { 396 | cout<<"The linked list is empty !\n"; 397 | } 398 | else { 399 | int num; 400 | 401 | // Storing the element, after which to be deleted 402 | cout<<"Enter the element after which to be deleted: "; 403 | cin>>num; 404 | 405 | // Storing start to ptr 406 | node *ptr = start; 407 | 408 | // Defining another pointer for storing 409 | // the previous element of ptr 410 | node *preptr = ptr; 411 | 412 | // Incrementing till num is found 413 | while(preptr->data != num) { 414 | preptr = ptr; 415 | ptr = ptr->next; 416 | } 417 | 418 | // Storing the address of element leaving one block, 419 | // which is to be deleted 420 | preptr->next = ptr->next; 421 | 422 | // Printing the value to be deleted 423 | cout<<"The deleted value is: "<data< NEXT -> DATA != NUM 444 | PREPTR = PTR 445 | PTR = PTR -> NEXT 446 | 447 | 5) PREPTR -> NEXT = PTR -> NEXT 448 | 6) DELETE PTR 449 | 7) EXIT 450 | 451 | */ 452 | 453 | node *del_before(node *start) { 454 | // Checking if the linked list is empty 455 | if(start == NULL) { 456 | cout<<"The linked list is empty !\n"; 457 | } 458 | else { 459 | int num; 460 | 461 | // Storing the element, before which to be deleted 462 | cout<<"Enter the element before which to be deleted: "; 463 | cin>>num; 464 | 465 | // Storing start to ptr 466 | node *ptr = start; 467 | 468 | // Defining another pointer for storing 469 | // the previous element of ptr 470 | node *preptr = ptr; 471 | 472 | // Incrementing till num is found 473 | while(ptr->next->data != num) { 474 | preptr = ptr; 475 | ptr = ptr->next; 476 | } 477 | 478 | // Storing the address of element leaving one block, 479 | // which is to be deleted 480 | preptr->next = ptr->next; 481 | 482 | // Printing the value to be deleted 483 | cout<<"The deleted value is: "<data< DATA != NUM 504 | PREPTR = PTR 505 | PTR = PTR -> NEXT 506 | 507 | 5) PREPTR -> NEXT = PTR -> NEXT 508 | 6) DELETE PTR 509 | 7) EXIT 510 | 511 | */ 512 | 513 | node *del_element(node *start) { 514 | // Checking if the linked list is empty 515 | if(start == NULL) { 516 | cout<<"The linked list is empty !\n"; 517 | } 518 | else { 519 | int num; 520 | 521 | // Storing the element, which to be deleted 522 | cout<<"Enter the element which to be deleted: "; 523 | cin>>num; 524 | 525 | // Storing start to ptr 526 | node *ptr = start; 527 | 528 | // Defining another pointer for storing 529 | // the previous element of ptr 530 | node *preptr = ptr; 531 | 532 | // Incrementing till num is found 533 | while(ptr->data != num) { 534 | preptr = ptr; 535 | ptr = ptr->next; 536 | } 537 | 538 | // Storing the address of element leaving one block, 539 | // which is to be deleted 540 | preptr->next = ptr->next; 541 | 542 | // Printing the value to be deleted 543 | cout<<"The deleted value is: "<data< NEXT 566 | 567 | 5) WRITE COUNT 568 | 6) EXIT 569 | 570 | */ 571 | 572 | void count(node *start) { 573 | int count = 0; 574 | 575 | // Checking if the linked list is empty 576 | if(start == NULL) { 577 | cout<<"The linked list is empty !\n"; 578 | } 579 | else { 580 | // Storing start in ptr 581 | node *ptr = start; 582 | 583 | // Incrementing till the end 584 | while(ptr != NULL) { 585 | count++; 586 | ptr = ptr->next; 587 | } 588 | } 589 | // Printing the number of elements 590 | cout<<"The number of elements present in the linked list are: " 591 | < DATA 607 | PTR = PTR -> NEXT 608 | 4) EXIT 609 | 610 | */ 611 | 612 | void display(node *start) { 613 | // Checking if the linked list is empty 614 | if(start == NULL) { 615 | cout<<"The linked list is empty !\n"; 616 | } 617 | else { 618 | // Storing start in ptr 619 | node *ptr = start; 620 | 621 | // Incrementing till the end 622 | while(ptr != NULL) { 623 | // Printing the elements 624 | cout<data<<" "; 625 | ptr = ptr->next; 626 | 627 | } cout<>option; 657 | 658 | switch(option) { 659 | 660 | case 1: start = create(start); 661 | break; 662 | 663 | case 2: // Storing the value to be inserted 664 | cout<<"Enter the value to be stored: "; 665 | cin>>value; 666 | 667 | start = insert_beg(start, value); 668 | break; 669 | 670 | case 3: // Storing the value to be inserted 671 | cout<<"Enter the value to be stored: "; 672 | cin>>value; 673 | 674 | start = insert_end(start, value); 675 | break; 676 | 677 | case 4: // Storing the value to be inserted 678 | cout<<"Enter the value to be stored: "; 679 | cin>>value; 680 | 681 | start = insert_after(start, value); 682 | break; 683 | 684 | case 5: // Storing the value to be inserted 685 | cout<<"Enter the value to be stored: "; 686 | cin>>value; 687 | 688 | start = insert_before(start, value); 689 | break; 690 | 691 | case 6: start = del_beg(start); 692 | break; 693 | 694 | case 7: start = del_end(start); 695 | break; 696 | 697 | case 8: start = del_after(start); 698 | break; 699 | 700 | case 9: start = del_before(start); 701 | break; 702 | 703 | case 10: start = del_element(start); 704 | break; 705 | 706 | case 11: count(start); 707 | break; 708 | 709 | case 12: display(start); 710 | break; 711 | 712 | case 13: break; 713 | 714 | default: cout<<"Wrong option is selecred !!\n"; 715 | break; 716 | } 717 | } while(option != 13); 718 | 719 | // Freeing the space occupied by start 720 | if(option == 13) { 721 | cout<< "\nTHANK YOU for using the program !\n" 722 | <<"Have a good day.\n\n"; 723 | 724 | delete start; 725 | } 726 | } 727 | -------------------------------------------------------------------------------- /stack.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Souvik Biswas 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | #include 22 | using namespace std; 23 | 24 | // Defining the maximum size of the stack to be 10 25 | #define MAX 10 26 | 27 | // Defining a class to store the member variables 28 | // and member functions required 29 | class Stack { 30 | int stk[MAX], top; 31 | 32 | public: 33 | Stack(): top(-1) {} 34 | void push(int ); 35 | int pop(); 36 | void display(); 37 | }; 38 | 39 | 40 | /* 41 | 42 | ALGORITHM FOR PUSHING AN ELEMENT IN THE STACK: 43 | 44 | 1) IF TOP = MAX-1 45 | WRITE Overflow 46 | EXIT 47 | 48 | 2) TOP = TOP + 1 49 | 3) STACK[TOP] = VALUE 50 | 4) EXIT 51 | 52 | */ 53 | 54 | void Stack :: push(int value) { 55 | // When the size of stack has reached maximum, 56 | // we cannot insert any element 57 | if(top == MAX-1) { 58 | cout<<"The Stack is full !\n"; 59 | } 60 | else { 61 | // Incrementing top 62 | top++; 63 | 64 | // Store the value in the stack at top position 65 | stk[top] = value; 66 | } 67 | } 68 | 69 | 70 | /* 71 | 72 | ALGORITHM FOR POPPING AN ELEMENT FROM THE STACK: 73 | 74 | 1) IF TOP = -1 75 | WRITE Underflow 76 | RETURN -1 77 | EXIT 78 | 79 | 2) VALUE = STK[TOP] 80 | 3) TOP = TOP - 1 81 | 4) RETURN VALUE 82 | 5) EXIT 83 | 84 | */ 85 | 86 | int Stack :: pop() { 87 | // When the size of stack is -1, the stack is empty 88 | if(top == -1) { 89 | cout<<"The Stack is empty !\n"; 90 | return -1; 91 | } 92 | else { 93 | // Store the top value 94 | int value = stk[top]; 95 | 96 | // Decrement top 97 | top --; 98 | 99 | return value; 100 | } 101 | } 102 | 103 | 104 | // Display the elements of the Stack by decrementing starting from top 105 | void Stack :: display() { 106 | // When the size of stack is -1, the stack is empty 107 | if(top == -1) { 108 | cout<<"The Stack is empty !\n"; 109 | } 110 | else { 111 | // Decrementing stack starting from top position 112 | for(int i=top; i>=0; i--) { 113 | // Printing each stack element 114 | cout<>option; 134 | 135 | switch(option) { 136 | case 1: cout<<"Enter the value to be pushed: "; 137 | cin>>value; 138 | 139 | s.push(value); 140 | break; 141 | 142 | case 2: value = s.pop(); 143 | if(value != -1) { 144 | cout<<"The popped element is: "< 22 | using namespace std; 23 | 24 | 25 | /* 26 | 27 | Declaring a structure called node 28 | having two members: 29 | 30 | 1) data block 31 | 2) next block (which stores the address of the next node) 32 | 33 | */ 34 | 35 | struct node { 36 | int data; 37 | node *next; 38 | }; 39 | 40 | 41 | /* 42 | 43 | Declaring a class called Stack having: 44 | 45 | one private member: 46 | top 47 | 48 | three public member functions: 49 | push 50 | pop 51 | display 52 | 53 | The top is set to NULL initially 54 | 55 | */ 56 | 57 | class Stack { 58 | node *top; 59 | 60 | public: 61 | Stack(): top(NULL) {} 62 | void push(int ); 63 | void pop(); 64 | void display(); 65 | }; 66 | 67 | 68 | /* 69 | 70 | ALGORITHM FOR PUSHING AN ELEMENT IN THE STACK: 71 | 72 | 1) IF AVAIL = NULL 73 | WRITE Overflow 74 | EXIT 75 | 76 | 2) NEW_NODE = AVAIL 77 | 3) AVAIL = AVAIL -> NEXT 78 | 4) NEW_NODE -> DATA = VALUE 79 | 5) IF TOP = NULL 80 | NEW_NODE -> NEXT = NULL 81 | TOP = NEW_NODE 82 | 83 | 6) ELSE 84 | NEW_NODE -> NEXT = TOP 85 | TOP = NEW_NODE 86 | 87 | 7) EXIT 88 | 89 | */ 90 | 91 | void Stack :: push(int value) { 92 | node *new_node = new node; 93 | new_node->data = value; 94 | 95 | if(top == NULL) { 96 | new_node->next = NULL; 97 | top = new_node; 98 | } 99 | else { 100 | new_node->next = top; 101 | top = new_node; 102 | } 103 | } 104 | 105 | 106 | /* 107 | 108 | ALGORITHM TO POP A NODE FROM STACK: 109 | 110 | 1) IF TOP = NULL 111 | WRITE Underflow 112 | EXIT 113 | 114 | 2) PTR = TOP; 115 | 3) TOP = TOP -> NEXT 116 | 4) DELETE PTR 117 | 5) EXIT 118 | 119 | */ 120 | 121 | void Stack :: pop() { 122 | // Checking if the stack is empty 123 | if(top == NULL) { 124 | cout<<"The Stack is empty !\n"; 125 | } 126 | else { 127 | node *ptr = top; 128 | top = top->next; 129 | 130 | cout<<"The popped node is: "<data< NEXT 148 | 149 | 4) EXIT 150 | 151 | */ 152 | 153 | void Stack :: display() { 154 | if(top == NULL) { 155 | cout<<"The Stack is empty !\n"; 156 | } 157 | else { 158 | node *ptr = top; 159 | 160 | while(ptr != NULL) { 161 | cout<data<<" "; 162 | ptr = ptr->next; 163 | } 164 | } 165 | } 166 | 167 | 168 | // Main Function 169 | int main() { 170 | int option, value; 171 | Stack s; 172 | 173 | do { 174 | cout<<"\n**** MENU ****\n" 175 | <<"1. Push\n" 176 | <<"2. Pop\n" 177 | <<"3. Display\n" 178 | <<"4. Exit\n"; 179 | 180 | cout<<"Enter your option: "; 181 | cin>>option; 182 | 183 | switch(option) { 184 | case 1: cout<<"Enter the value to be inserted: "; 185 | cin>>value; 186 | 187 | s.push(value); 188 | break; 189 | 190 | case 2: s.pop(); 191 | break; 192 | 193 | case 3: s.display(); 194 | break; 195 | 196 | case 4: break; 197 | 198 | default: cout<<"Wrong option !\n"; 199 | break; 200 | } 201 | } while(option != 4); 202 | 203 | if(option == 4) { 204 | cout<< "\nTHANK YOU for using the program !\n" 205 | <<"Have a good day.\n\n"; 206 | } 207 | } 208 | --------------------------------------------------------------------------------