├── .gitignore ├── CONTRIBUTING.MD ├── Data Structures.iml ├── Data-Structures.iml ├── README.md ├── out └── production │ └── Data-Structures │ ├── BinarySearchTree$Node.class │ ├── BinarySearchTree.class │ ├── CircularQueue.class │ ├── DoublyLinkedList.class │ ├── Exceptions │ ├── InvalidPositionException.class │ ├── OverflowException.class │ └── UnderflowException.class │ ├── Hashes.class │ ├── LinkedList.class │ ├── Node.class │ ├── Queue.class │ ├── Stack.class │ └── TrieNode.class ├── src-c ├── arrayops.c ├── binarysearchtree.c ├── breadthfirsttraversal.c ├── circularlinkedlist.c ├── circularqueue.c ├── hashing.c ├── linkedlist.c ├── priorityqueueusinglinkedlist.c ├── priorityqueusingarray.c ├── queueusinglinkedlist.c ├── simplequeue.c ├── stackglobal.c ├── stacklinkedlist.c └── stacklocal.c ├── src-cpp ├── CircularQueue.cpp ├── Queue.cpp ├── Stack.cpp ├── bfs.cpp ├── dfs.cpp ├── dijkstras.cpp ├── knapsack_problem_Niket.cpp ├── linked_list.cpp ├── merge_sort.cpp ├── prims_algo.cpp ├── queue_using_ll.cpp └── stack_using_ll.cpp ├── src-java ├── BinarySearchTree.java ├── CircularLinkedList.java ├── CircularQueue.java ├── DoublyLinkedList.java ├── Exceptions │ ├── InvalidPositionException.java │ ├── OverflowException.java │ └── UnderflowException.java ├── Hashes.java ├── LinkedList.java ├── Node.java ├── Queue.java ├── Stack.java └── TrieNode.java ├── src-javascript ├── LinkedList.js └── binarysearch.js └── src-python └── stack.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .vscode 3 | -------------------------------------------------------------------------------- /CONTRIBUTING.MD: -------------------------------------------------------------------------------- 1 | # How to Contribute 2 | 3 | This project is open to anyone in the community. Developers can contribute by adding code, and non-developers can contribute by improving docs. The main goal of this repository is to support open source with [hacktoberfest](http://hacktoberfest.digitalocean.com). :octocat: 4 | 5 | Follow the steps below to start contributing: 6 | 7 | 1. Fork this repository and clone your fork. 8 | 2. Create a new branch with the name as follows - `/` or any appropriate branch name. 9 | 3. Add all the relevant code and tests.:) 10 | 4. Update the docs if needed (README, gitignore, etc) :sos: 11 | 4. Push your code to your branch. 12 | 5. Finally, open a PR against the `master` branch of this repository. 13 | 14 | Good luck! 15 | -------------------------------------------------------------------------------- /Data Structures.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Data-Structures.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structures 2 | --- 3 | This contains all the programs for data structures that are a part of the syllabus of MAKAUT 2nd year Computer Science and engineering course. 4 | 5 | - [C](https://github.com/diptangsu/Data-Structures/blob/master/src-c) 6 | - [C++](https://github.com/diptangsu/Data-Structures/blob/master/src-cpp) 7 | - [Java](https://github.com/diptangsu/Data-Structures/blob/master/src-java) 8 | - [Javascript](https://github.com/diptangsu/Data-Structures/blob/master/src-javascript) 9 | - [Python](https://github.com/diptangsu/Data-Structures/blob/master/src-python) :snake: 10 | 11 | --- 12 | # Stack 13 | Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). 14 | 15 | ### Main methods 16 | - Push(new item) 17 | - Inserts new element at the "top" of the Stack. 18 | - Need to send the new item as argument. 19 | - Pop() 20 | - Takes out the most top element from the Stack. 21 | - No arguments needed. 22 | 23 | --- 24 | # Linked List 25 | A linked list is a sequence of data structures, which are connected together via links. 26 | Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array. 27 | ### Main methods 28 | - Get 29 | - getFirst() 30 | - Gets the first element of the List. 31 | - getLast() 32 | - Gets the last element of the List. 33 | - getAt(index) 34 | - Gets the element at the index provided or null otherwise. 35 | - Insert 36 | - insertFirst(new_item) 37 | - Inserts the new item in the first index of the List, the previous first takes second place. 38 | - insertLast(new_item) 39 | - Inserts the new item at the new last index (previous last index + 1) of the List. 40 | - insertAt(new_item, index) 41 | - Inserts the new item at the provided index if size_of_list >= index and index > 0. 42 | - Remove 43 | - removeFirst() 44 | - Removes the item on the first index of the List, so the item on the second index takes the first place. 45 | - removeLast() 46 | - Removes the item in the last index of the List 47 | - removeAt(index) 48 | - Removes the item at the provided index if size_of_list >= index and index > 0. 49 | ### Subtypes 50 | #### Double Linked List 51 | It's a subtype of Linked List where each node is not only connected to the next node, but also to the previous node. Everything else is the same as a Linked List. 52 | ##### Circular Linked List 53 | A linked list is a sequence of data structures, which are connected together via links. Linked List is a sequence of links which contains items. Each link contains a connection to another link. The main property of the Circular Linked List, which is also the main difference with the Linked List, is that the last nodes is linked to the first one. 54 | #### Circular Double Linked List 55 | Takes the best of the two previous subtypes, the first and last nodes are linked, and links between the nodes are in both ways. 56 | 57 | --- 58 | # Queue 59 | A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). 60 | ### Main methods 61 | - Enqueue(new_item) 62 | - Inserting the new item at the beginning of the Queue. 63 | - Dequeue() 64 | - Taking out the last item of the Queue. 65 | 66 | --- 67 | # Tree :deciduous_tree: 68 | Tree is a widely used abstract data type that simulates a hierarchical tree structure, with a root value and subtrees of children with a parent node, represented as a set of linked nodes. 69 | ### Main methods 70 | - Insert(new_item) 71 | - Inserts the new item in the correct tree node 72 | - Get 73 | - Pre-Order() 74 | - In-Order() 75 | - Post-Order() 76 | 77 | --- 78 | ## Contributing :octocat: 79 | To start contributing, check out [CONTRIBUTING.md](https://github.com/diptangsu/Data-Structures/blob/master/CONTRIBUTING.MD). New contributors are always welcome to support this project. Check out issues labelled as `Hacktoberfest` if you are up for some grabs! :) 80 | -------------------------------------------------------------------------------- /out/production/Data-Structures/BinarySearchTree$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/BinarySearchTree$Node.class -------------------------------------------------------------------------------- /out/production/Data-Structures/BinarySearchTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/BinarySearchTree.class -------------------------------------------------------------------------------- /out/production/Data-Structures/CircularQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/CircularQueue.class -------------------------------------------------------------------------------- /out/production/Data-Structures/DoublyLinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/DoublyLinkedList.class -------------------------------------------------------------------------------- /out/production/Data-Structures/Exceptions/InvalidPositionException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/Exceptions/InvalidPositionException.class -------------------------------------------------------------------------------- /out/production/Data-Structures/Exceptions/OverflowException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/Exceptions/OverflowException.class -------------------------------------------------------------------------------- /out/production/Data-Structures/Exceptions/UnderflowException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/Exceptions/UnderflowException.class -------------------------------------------------------------------------------- /out/production/Data-Structures/Hashes.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/Hashes.class -------------------------------------------------------------------------------- /out/production/Data-Structures/LinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/LinkedList.class -------------------------------------------------------------------------------- /out/production/Data-Structures/Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/Node.class -------------------------------------------------------------------------------- /out/production/Data-Structures/Queue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/Queue.class -------------------------------------------------------------------------------- /out/production/Data-Structures/Stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/Stack.class -------------------------------------------------------------------------------- /out/production/Data-Structures/TrieNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/diptangsu/Data-Structures/387f52d5a2abf9121c19d75fff82f5a0a48a07fb/out/production/Data-Structures/TrieNode.class -------------------------------------------------------------------------------- /src-c/arrayops.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { int q,a[100],n=0,size=100,inspos,insele,i,delpos,delele,temp,slow,sup,sele,x; 4 | while(1) 5 | { 6 | printf("1.create\n"); 7 | printf("2.insert\n"); 8 | printf("3.delete\n"); 9 | printf("4.sort\n"); 10 | printf("5.search\n"); 11 | printf("6.display\n"); 12 | printf("7.quit\n"); 13 | scanf("%d",&q); 14 | switch(q) 15 | { 16 | case 1: printf("Enter no:of elements to insert"); 17 | scanf("%d",&n); 18 | for(i=0;i=1&&inspos<=(n+2)) 27 | { 28 | for(i=n;i>=inspos-1;i--) 29 | { 30 | a[i+1]=a[i]; 31 | } 32 | a[inspos-1]=insele; 33 | n++; 34 | } 35 | else 36 | printf(("Can't enter\n")); 37 | break; 38 | case 3: printf("Enter position and element\n"); 39 | scanf("%d%d",&delpos,&delele); 40 | if(n!=-1&&delpos>=1&&delpos<=n+1) 41 | { 42 | for(i=delpos-1;ia[i+1]) 55 | { 56 | temp=a[i+1]; 57 | a[i+1]=a[i]; 58 | a[i]=temp; 59 | } 60 | } 61 | break; 62 | 63 | case 5: slow=0; 64 | sup=n; 65 | printf("Enter element to be searched\n"); 66 | scanf("%d",&sele); 67 | x=search(a,n,slow,sup,sele); 68 | printf("%d\n",x); 69 | break; 70 | 71 | case 6: if(n!=0) 72 | { 73 | for(i=0;ia[smid]) 96 | { 97 | slow=smid+1; 98 | x=search(a,n,slow,sup,sele); 99 | } 100 | else if(selesup) 110 | { 111 | return -1; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src-c/binarysearchtree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct btnode 4 | { 5 | int value; 6 | struct btnode *l; 7 | struct btnode *r; 8 | }*root = NULL, *temp = NULL, *t2, *t1; 9 | void delete1(); 10 | void insert(); 11 | void delete(); 12 | void inorder(struct btnode *t); 13 | void create(); 14 | void search(struct btnode *t); 15 | void preorder(struct btnode *t); 16 | void postorder(struct btnode *t); 17 | void search1(struct btnode *t,int data); 18 | int smallest(struct btnode *t); 19 | int largest(struct btnode *t); 20 | int flag = 1; 21 | 22 | 23 | void main() 24 | { 25 | int ch; 26 | printf(" OPERATIONS ---"); 27 | printf(" 1 - Insert an element into tree "); 28 | printf("2 - Delete an element from the tree "); 29 | printf("3 - Inorder Traversal "); 30 | printf("4 - Preorder Traversal "); 31 | printf("5 - Postorder Traversal "); 32 | printf("6 - Exit "); 33 | while(1) 34 | { 35 | printf(" Enter your choice : "); 36 | scanf("%d", &ch); 37 | switch (ch) 38 | { 39 | case 1: 40 | insert(); 41 | break; 42 | case 2: 43 | delete(); 44 | break; 45 | case 3: 46 | inorder(root); 47 | break; 48 | case 4: 49 | preorder(root); 50 | break; 51 | case 5: 52 | postorder(root); 53 | break; 54 | case 6: 55 | exit(0); 56 | default : 57 | printf("Wrong choice, Please enter correct choice "); 58 | break; 59 | } 60 | } 61 | } 62 | /* To insert a node in the tree */ 63 | void insert() 64 | { 65 | create(); 66 | if (root == NULL) 67 | root = temp; 68 | else 69 | search(root); 70 | } 71 | /* To create a node */ 72 | void create() 73 | { 74 | int data; 75 | printf("Enter data of node to be inserted : "); 76 | scanf("%d", &data); 77 | temp = (struct btnode *)malloc(1*sizeof(struct btnode)); 78 | temp->value = data; 79 | temp->l = temp->r = NULL; 80 | } 81 | /* Function to search the appropriate position to insert the new node */ 82 | void search(struct btnode *t) 83 | { 84 | if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert at right */ 85 | search(t->r); 86 | else if ((temp->value > t->value) && (t->r == NULL)) 87 | t->r = temp; 88 | else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */ 89 | search(t->l); 90 | else if ((temp->value < t->value) && (t->l == NULL)) 91 | t->l = temp; 92 | } 93 | /* recursive function to perform inorder traversal of tree */ 94 | void inorder(struct btnode *t) 95 | { 96 | if (root == NULL) 97 | { 98 | printf("No elements in a tree to display"); 99 | return; 100 | } 101 | if (t->l != NULL) 102 | inorder(t->l); 103 | printf("%d -> ", t->value); 104 | if (t->r != NULL) 105 | inorder(t->r); 106 | } 107 | /* To check for the deleted node */ 108 | void delete() 109 | { 110 | int data; 111 | if (root == NULL) 112 | { 113 | printf("No elements in a tree to delete"); 114 | return; 115 | } 116 | printf("Enter the data to be deleted : "); 117 | scanf("%d", &data); 118 | t1 = root; 119 | t2 = root; 120 | search1(root, data); 121 | } 122 | /* To find the preorder traversal */ 123 | void preorder(struct btnode *t) 124 | { 125 | if (root == NULL) 126 | { 127 | printf("No elements in a tree to display"); 128 | return; 129 | } 130 | printf("%d -> ", t->value); 131 | if (t->l != NULL) 132 | preorder(t->l); 133 | if (t->r != NULL) 134 | preorder(t->r); 135 | } 136 | /* To find the postorder traversal */ 137 | void postorder(struct btnode *t) 138 | { 139 | if (root == NULL) 140 | { 141 | printf("No elements in a tree to display "); 142 | return; 143 | } 144 | if (t->l != NULL) 145 | postorder(t->l); 146 | if (t->r != NULL) 147 | postorder(t->r); 148 | printf("%d -> ", t->value); 149 | } 150 | /* Search for the appropriate position to insert the new node */ 151 | void search1(struct btnode *t, int data) 152 | { 153 | if ((data>t->value)) 154 | { 155 | t1 = t; 156 | search1(t->r, data); 157 | } 158 | else if ((data < t->value)) 159 | { 160 | t1 = t; 161 | search1(t->l, data); 162 | } 163 | else if ((data==t->value)) 164 | { 165 | delete1(t); 166 | } 167 | } 168 | /* To delete a node */ 169 | void delete1(struct btnode *t) 170 | { 171 | int k; 172 | /* To delete leaf node */ 173 | if ((t->l == NULL) && (t->r == NULL)) 174 | { 175 | if (t1->l == t) 176 | { 177 | t1->l = NULL; 178 | } 179 | else 180 | { 181 | t1->r = NULL; 182 | } 183 | t = NULL; 184 | free(t); 185 | return; 186 | } 187 | /* To delete node having one left hand child */ 188 | else if ((t->r == NULL)) 189 | { 190 | if (t1 == t) 191 | { 192 | root = t->l; 193 | t1 = root; 194 | } 195 | else if (t1->l == t) 196 | { 197 | t1->l = t->l; 198 | } 199 | else 200 | { 201 | t1->r = t->l; 202 | } 203 | t = NULL; 204 | free(t); 205 | return; 206 | } 207 | /* To delete node having right hand child */ 208 | else if (t->l == NULL) 209 | { 210 | if (t1 == t) 211 | { 212 | root = t->r; 213 | t1 = root; 214 | } 215 | else if (t1->r == t) 216 | t1->r = t->r; 217 | else 218 | t1->l = t->r; 219 | t == NULL; 220 | free(t); 221 | return; 222 | } 223 | /* To delete node having two child */ 224 | else if ((t->l != NULL) && (t->r != NULL)) 225 | { 226 | t2 = root; 227 | if (t->r != NULL) 228 | { 229 | k = smallest(t->r); 230 | flag = 1; 231 | } 232 | else 233 | { 234 | k =largest(t->l); 235 | flag = 2; 236 | } 237 | search1(root, k); 238 | t->value = k; 239 | } 240 | } 241 | /* To find the smallest element in the right sub tree */ 242 | int smallest(struct btnode *t) 243 | { 244 | t2 = t; 245 | if (t->l != NULL) 246 | { 247 | t2 = t; 248 | return(smallest(t->l)); 249 | } 250 | else 251 | return (t->value); 252 | } 253 | int largest(struct btnode *t) 254 | { 255 | if (t->r != NULL) 256 | { 257 | t2 = t; 258 | return(largest(t->r)); 259 | } 260 | else 261 | return(t->value); 262 | } 263 | -------------------------------------------------------------------------------- /src-c/breadthfirsttraversal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 100 4 | #define initial 1 5 | #define waiting 2 6 | #define visited 3 7 | int n; 8 | int adj[MAX][MAX]; 9 | int state[MAX]; 10 | void create_graph(); 11 | void BF_Traversal(); 12 | void BFS(int v); 13 | int queue[MAX], front = -1,rear = -1; 14 | void insert_queue(int vertex); 15 | int delete_queue(); 16 | int isEmpty_queue(); 17 | int main() 18 | { 19 | create_graph(); 20 | BF_Traversal(); 21 | return 0; 22 | } 23 | void BF_Traversal() 24 | { 25 | int v; 26 | for(v=0; v rear) 68 | return 1; 69 | else 70 | return 0; 71 | } 72 | int delete_queue() 73 | { 74 | int delete_item; 75 | if(front == -1 || front > rear) 76 | { 77 | printf("Queue Underflow\n"); 78 | exit(1); 79 | } 80 | delete_item = queue[front]; 81 | front = front+1; 82 | return delete_item; 83 | } 84 | void create_graph() 85 | { 86 | int count,max_edge,origin,destin; 87 | printf("Enter number of vertices : "); 88 | scanf("%d",&n); 89 | max_edge = n*(n-1); 90 | for(count=1; count<=max_edge; count++) 91 | { 92 | printf("Enter edge %d( -1 -1 to quit ) : ",count); 93 | scanf("%d %d",&origin,&destin); 94 | if((origin == -1) && (destin == -1)) 95 | break; 96 | if(origin>=n || destin>=n || origin<0 || destin<0) 97 | { 98 | printf("Invalid edge!\n"); 99 | count--; 100 | } 101 | else 102 | { 103 | adj[origin][destin] = 1; 104 | } 105 | } 106 | } 107 | 108 | -------------------------------------------------------------------------------- /src-c/circularlinkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node* ptr; 7 | }; 8 | struct node *last; 9 | struct node* start=NULL; 10 | struct node *t; 11 | int len; 12 | void insert(void); 13 | void display(void); 14 | void del(void); 15 | void search(void); 16 | void create(void); 17 | void main() 18 | { int ch; 19 | while(1) 20 | { 21 | printf("Circular operations:\n"); 22 | printf("1.create\n"); 23 | printf("2.insertion\n"); 24 | printf("3.deletion\n"); 25 | printf("4.search\n"); 26 | printf("6.display\n"); 27 | printf("8.exit\n"); 28 | scanf("%d",&ch); 29 | 30 | switch(ch) 31 | { 32 | case 1: create(); 33 | break; 34 | case 2: insert(); 35 | break; 36 | case 3: del(); 37 | break; 38 | case 4: search(); 39 | break; 40 | case 6: display(); 41 | break; 42 | case 7: exit(1); 43 | default: printf("Invalid choice\n"); 44 | } 45 | } 46 | return 0; 47 | } 48 | void create() 49 | { int i,q; 50 | printf("Enter no:of elements\n"); 51 | scanf("%d",&q); 52 | for(i=0;idata); 56 | if(start==NULL) 57 | { 58 | start=t; 59 | } 60 | else{ 61 | last->ptr=t; 62 | } 63 | last=t; 64 | } 65 | last->ptr=start; 66 | } 67 | 68 | void insert() 69 | { int pos,ele,i; 70 | struct node *pptr; 71 | printf("Enter position and element\n"); 72 | scanf("%d%d",&pos,&ele); 73 | t=(struct node *)malloc(sizeof(struct node)); 74 | t->data=ele; 75 | if(pos==1) 76 | { 77 | for(last=start;last->ptr!=start;last=last->ptr) 78 | { 79 | t->ptr=start; 80 | start=t; 81 | last=t; 82 | } 83 | } 84 | else 85 | { 86 | pptr=start; 87 | for(i=2;iptr; 90 | } 91 | t->ptr=pptr->ptr; 92 | pptr->ptr=t; 93 | } 94 | } 95 | 96 | void display() 97 | { 98 | if(start!=NULL) 99 | { 100 | for(t=start;t->ptr!=start;t=t->ptr) 101 | { 102 | printf("%d\n",t->data); 103 | } 104 | printf("%d\n",t->data); 105 | } 106 | else 107 | printf("No elements in the array\n"); 108 | } 109 | void search() 110 | { int ele,count=0; 111 | printf("Enter element to be searched\n"); 112 | scanf("%d",&ele); 113 | if(start!=NULL) 114 | { 115 | for(t=start;t->ptr!=start;t=t->ptr) 116 | { count++; 117 | if(t->data==ele) 118 | { 119 | printf("Element found at %d \n",count); 120 | } 121 | } 122 | } 123 | else 124 | printf("No elements in the array to search for\n"); 125 | } 126 | void del() 127 | { int pos,i; 128 | struct node *t1; 129 | printf("Enter position to be deleted \n"); 130 | scanf("%d",&pos); 131 | if(pos==1) 132 | { 133 | t=start; 134 | start=start->ptr; 135 | free(t); 136 | last->ptr=start; 137 | } 138 | else 139 | { 140 | t=start; 141 | for(i=1;iptr!=start;i++) 142 | { 143 | t1=t; 144 | t=t->ptr; 145 | } 146 | t1->ptr=t->ptr; 147 | free(t); 148 | } 149 | } 150 | 151 | -------------------------------------------------------------------------------- /src-c/circularqueue.c: -------------------------------------------------------------------------------- 1 | # include 2 | 3 | 4 | int cqueue_arr[5]; 5 | int front = -1; 6 | int rear = -1; 7 | 8 | 9 | void insert(int item,int max) 10 | { 11 | if((front == 0 && rear == max-1) || (front == rear+1)) 12 | { 13 | printf("Queue Overflow \n"); 14 | return; 15 | } 16 | if (front == -1) 17 | { 18 | front = 0; 19 | rear = 0; 20 | } 21 | else 22 | { 23 | if(rear == max-1) 24 | rear = 0; 25 | else 26 | rear = rear+1; 27 | } 28 | cqueue_arr[rear] = item ; 29 | } 30 | void del(int max) 31 | { 32 | if (front == -1) 33 | { 34 | printf("Queue Underflow\n"); 35 | return ; 36 | } 37 | printf("Element deleted from queue is : %d\n",cqueue_arr[front]); 38 | if(front == rear) 39 | { 40 | front = -1; 41 | rear=-1; 42 | } 43 | else 44 | { 45 | if(front == max-1) 46 | front = 0; 47 | else 48 | front = front+1; 49 | } 50 | } 51 | 52 | void display(int max) 53 | { 54 | int front_pos = front,rear_pos = rear; 55 | if(front == -1) 56 | { 57 | printf("Queue is empty\n"); 58 | return; 59 | } 60 | printf("Queue elements :\n"); 61 | if( front_pos <= rear_pos ) 62 | while(front_pos <= rear_pos) 63 | { 64 | printf("%d ",cqueue_arr[front_pos]); 65 | front_pos++; 66 | } 67 | else 68 | { 69 | while(front_pos <= max-1) 70 | { 71 | printf("%d ",cqueue_arr[front_pos]); 72 | front_pos++; 73 | } 74 | front_pos = 0; 75 | while(front_pos <= rear_pos) 76 | { 77 | printf("%d ",cqueue_arr[front_pos]); 78 | front_pos++; 79 | } 80 | } 81 | printf("\n"); 82 | } 83 | 84 | int main() 85 | { 86 | 87 | 88 | int choice,item; 89 | do 90 | { 91 | printf("1.Insert\n"); 92 | printf("2.Delete\n"); 93 | printf("3.Display\n"); 94 | printf("4.Quit\n"); 95 | 96 | printf("Enter your choice : "); 97 | scanf("%d",&choice); 98 | 99 | switch(choice) 100 | { 101 | case 1 : 102 | printf("Input the element for insertion in queue : "); 103 | scanf("%d", &item); 104 | 105 | insert(item,5); 106 | break; 107 | case 2 : 108 | del(5); 109 | break; 110 | case 3: 111 | display(5); 112 | break; 113 | case 4: 114 | break; 115 | default: 116 | printf("Wrong choice\n"); 117 | } 118 | }while(choice!=4); 119 | 120 | return 0; 121 | } 122 | -------------------------------------------------------------------------------- /src-c/hashing.c: -------------------------------------------------------------------------------- 1 | #include 2 | int arr[10] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; 3 | int f1(int key) 4 | { 5 | return key%10; 6 | } 7 | int f2(int key) 8 | { 9 | return 7-(key%7); 10 | } 11 | int fun(int key, int n) 12 | { 13 | int i; 14 | i = (f1(key)+(n*f2(key)))%10; 15 | return i; 16 | } 17 | void insert() 18 | { 19 | int num,i; 20 | printf("Enter the number: "); 21 | scanf("%d",&num); 22 | i = f1(num); 23 | if(arr[i] == -1) 24 | arr[i] = num; 25 | else 26 | { 27 | int i,ind; 28 | for(i=1; i<=10; i++) 29 | { 30 | ind = fun(num,i); 31 | if(arr[ind] == -1) 32 | { 33 | arr[ind] = num; 34 | break; 35 | } 36 | } 37 | if(i == 11) 38 | printf("Element cannot be inserted\n"); 39 | } 40 | } 41 | void display() 42 | { 43 | int i; 44 | for(i=0; i<10; i++) 45 | { 46 | if(arr[i] == -1) 47 | printf("\n%d -> \n",i); 48 | else 49 | printf("%d -> %d\n",i,arr[i]); 50 | } 51 | } 52 | 53 | 54 | void main() 55 | { 56 | int che; 57 | do{ 58 | printf("\nEnter your choice\n1.Insert\n2.Display\n0.Exit\nEnter element:\t "); 59 | scanf("%d",&che); 60 | switch(che) 61 | { 62 | case 1 : insert(); 63 | break; 64 | case 2 : display(); 65 | break; 66 | case 3 : printf("Exiting"); 67 | break; 68 | } 69 | }while(che != 3); 70 | } 71 | 72 | -------------------------------------------------------------------------------- /src-c/linkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node* link; 7 | }; 8 | struct node *last; 9 | struct node* root=NULL; 10 | int len; 11 | void append(void); 12 | void insert(void); 13 | int length(void); 14 | void display(void); 15 | void del(void); 16 | void search(void); 17 | void sort(void); 18 | void main() 19 | { int ch; 20 | while(1) 21 | { 22 | printf("Single operations:\n"); 23 | printf("1.app\n"); 24 | printf("2.insertion\n"); 25 | printf("3.deletion\n"); 26 | printf("4.search\n"); 27 | printf("5.sort\n"); 28 | printf("6.display\n"); 29 | printf("7.length\n"); 30 | printf("8.exit\n"); 31 | scanf("%d",&ch); 32 | 33 | switch(ch) 34 | { 35 | case 1: append(); 36 | break; 37 | case 2: insert(); 38 | break; 39 | case 3: del(); 40 | break; 41 | case 4: search(); 42 | break; 43 | case 8: len=length(); 44 | printf("%d\n",len); 45 | break; 46 | case 5: sort(); 47 | break; 48 | case 6: display(); 49 | break; 50 | case 7: exit(1); 51 | default: printf("Invalid choice\n"); 52 | } 53 | } 54 | } 55 | void append() 56 | { int i,n; 57 | struct node* temp; 58 | temp=(struct node*)malloc(sizeof(struct node)); 59 | printf("Enter node data\n"); 60 | scanf("%d",&temp->data); 61 | temp->link=NULL; 62 | if(root==NULL) 63 | { 64 | root=temp; 65 | } 66 | else 67 | { 68 | struct node* p; 69 | p=root; 70 | while(p->link!=NULL) 71 | { 72 | p=p->link; 73 | } 74 | p->link=temp; 75 | } 76 | } 77 | int length() 78 | { 79 | int count=0; 80 | struct node* temp; 81 | temp=root; 82 | while(temp!=NULL) 83 | { 84 | count++; 85 | temp=temp->link; 86 | } 87 | return count; 88 | } 89 | void display() 90 | { 91 | 92 | struct node* temp; 93 | temp=root; 94 | if(temp==NULL) 95 | { 96 | printf("list empty\n"); 97 | } 98 | else 99 | { 100 | while(temp!=NULL) 101 | { 102 | printf("%d\n",temp->data); 103 | temp=temp->link; 104 | } 105 | } 106 | printf("\n\n"); 107 | } 108 | void insert() 109 | { int ele,pos,i; 110 | struct node *temp,*pptr; 111 | printf("Enter element as position\n"); 112 | scanf("%d%d",&ele,&pos); 113 | temp=(struct node*)malloc(sizeof(struct node)); 114 | temp->data=ele; 115 | if(pos==1) 116 | { 117 | temp->link=root; 118 | root=temp; 119 | } 120 | else 121 | { 122 | pptr=root; 123 | for(i=2;ilink!=NULL);i++) 124 | { 125 | pptr=pptr->link; 126 | } 127 | temp->link=pptr->link; 128 | pptr->link=temp; 129 | 130 | } 131 | } 132 | void del() 133 | { int pos,i; 134 | struct node *temp,*pptr; 135 | printf("Enter the position to be deleted from\n"); 136 | scanf("%d",&pos); 137 | if(pos==1) 138 | { 139 | temp=root; 140 | root=temp->link; 141 | } 142 | else 143 | { 144 | pptr=NULL; 145 | temp=root; 146 | for(i=1;ilink!=NULL);i++) 147 | { 148 | pptr=temp; 149 | temp=temp->link; 150 | } 151 | pptr->link=temp->link; 152 | } 153 | free(temp); 154 | } 155 | 156 | void search() 157 | { int a,count=0; 158 | struct node *temp; 159 | temp=root; 160 | printf("Enter data to be searched\n"); 161 | scanf("%d",&a); 162 | while(temp!=NULL) 163 | { count++; 164 | if(temp->data==a) 165 | { 166 | printf("Found at %d \n",count); 167 | } 168 | temp=temp->link; 169 | } 170 | } 171 | 172 | void sort() 173 | { int q; 174 | struct node *temp,*pptr; 175 | for(pptr=root;pptr->link!=NULL;pptr=pptr->link) 176 | { 177 | for(temp=pptr->link;temp!=NULL;temp=temp->link) 178 | { 179 | if((pptr->data)>(temp->data)) 180 | { 181 | q=pptr->data; 182 | pptr->data=temp->data; 183 | temp->data=q; 184 | } 185 | } 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src-c/priorityqueueusinglinkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct node 4 | { 5 | int data; 6 | int pri; 7 | struct node *ptr; 8 | }; 9 | struct node *start=NULL; 10 | struct node *t1,*t2; 11 | 12 | void insert(int ele,int pri) 13 | { struct node *t; 14 | t=(struct node*)malloc(sizeof(struct node)); 15 | if(t==NULL) 16 | printf("Overflow"); 17 | else if(start==NULL||start->pri>pri) 18 | { 19 | t->data=ele; 20 | t->pri=pri; 21 | t->ptr=start; 22 | start=t; 23 | } 24 | else 25 | { 26 | t1=start; 27 | t2=start; 28 | for(t2=start;t2->pri<=pri&&t2!=NULL;t2=t2->ptr) 29 | {t1=t2;} 30 | t->ptr=t1->ptr; 31 | t1->ptr=t; 32 | t->data=ele; 33 | t->ptr=pri; 34 | } 35 | } 36 | void deletion() 37 | { 38 | printf("%d\n",start->data); 39 | start=start->ptr; 40 | } 41 | void display() 42 | { 43 | struct node *t; 44 | t=start; 45 | for(t=start;t!=NULL;t=t->ptr) 46 | { 47 | printf("%d\n",t->data); 48 | } 49 | } 50 | void main() 51 | { int q,ele,prio; 52 | while(1) 53 | { 54 | printf("1.Insert\n2.Delete\n3.display\n4.exit\n"); 55 | scanf("%d",&q); 56 | switch(q) 57 | { 58 | case 1: printf("Enter element\n"); 59 | scanf("%d",&ele); 60 | printf("Enter priority\n"); 61 | scanf("%d",&prio); 62 | insert(ele,prio); 63 | break; 64 | case 2: deletion(); 65 | break; 66 | case 4: exit(1); 67 | break; 68 | case 3: display(); 69 | break; 70 | default: 71 | printf("Invalid entry\n"); 72 | break; 73 | 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src-c/priorityqueusingarray.c: -------------------------------------------------------------------------------- 1 | # include 2 | int q[3][5]; 3 | int fr[3][2]={-1,-1,-1,-1,-1,-1}; 4 | 5 | int row = 3; 6 | int size = 5; 7 | 8 | insertionPQ(int ele,int prio) 9 | { 10 | if(fr[prio][0]==(fr[prio][1]+1)%size) 11 | printf("Overflow\n"); 12 | else if(fr[prio][0]==-1) 13 | { 14 | fr[prio][0]=fr[prio][1]=0; 15 | q[prio][0] = ele; 16 | } 17 | else 18 | { 19 | fr[prio][1] = (fr[prio][1]+1)%size; 20 | q[prio][fr[prio][1]] = ele; 21 | } 22 | } 23 | 24 | deletionPQ(int prio) 25 | { 26 | int i; 27 | for( i=0;i 2 | struct node 3 | { 4 | int data; 5 | struct node *ptr; 6 | }; 7 | struct node *front=NULL; 8 | struct node *rear=NULL; 9 | 10 | void insert(int ele) 11 | { struct node *t; 12 | t=(struct node *)malloc(sizeof(struct node)); 13 | if(t==NULL) 14 | printf("overflow\n"); 15 | else if(rear==NULL) 16 | { 17 | rear=t; 18 | front=t; 19 | t->data=ele; 20 | t->ptr=NULL; 21 | } 22 | else 23 | { 24 | rear->ptr=t; 25 | rear=t; 26 | t->data=ele; 27 | t->ptr=NULL; 28 | } 29 | } 30 | 31 | void del() 32 | { struct node *t; 33 | if(front==NULL) 34 | printf("Underflow\n"); 35 | else if(front==rear) 36 | { 37 | printf("%d\n",front->data); 38 | free(front); 39 | front=NULL; 40 | rear=NULL; 41 | } 42 | else 43 | { 44 | printf("%d\n",front->data); 45 | t=front; 46 | front=front->ptr; 47 | free(t); 48 | } 49 | } 50 | void print() 51 | { struct node *t; 52 | for(t=front;t!=NULL;t=t->ptr) 53 | { 54 | printf("%d\n",t->data); 55 | } 56 | } 57 | 58 | void main() 59 | { int q,ele,prio; 60 | while(1) 61 | { 62 | printf("1.Insert\n2.Delete\n3.display\n4.exit\n"); 63 | scanf("%d",&q); 64 | switch(q) 65 | { 66 | case 1: printf("Enter element\n"); 67 | scanf("%d",&ele); 68 | insert(ele); 69 | break; 70 | case 2: del(); 71 | break; 72 | case 4: exit(1); 73 | break; 74 | case 3: print(); 75 | break; 76 | default: 77 | printf("Invalid entry\n"); 78 | break; 79 | 80 | } 81 | } 82 | } 83 | 84 | -------------------------------------------------------------------------------- /src-c/simplequeue.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX 50 4 | 5 | void insert(); 6 | void delete(); 7 | void display(); 8 | int queue_array[MAX]; 9 | int rear = - 1; 10 | int front = - 1; 11 | main() 12 | { 13 | int choice; 14 | while (1) 15 | { 16 | printf("1.Insert element to queue \n"); 17 | printf("2.Delete element from queue \n"); 18 | printf("3.Display all elements of queue \n"); 19 | printf("4.Quit \n"); 20 | printf("Enter your choice : "); 21 | scanf("%d", &choice); 22 | switch (choice) 23 | { 24 | case 1: 25 | insert(); 26 | break; 27 | case 2: 28 | delete(); 29 | break; 30 | case 3: 31 | display(); 32 | break; 33 | case 4: 34 | exit(1); 35 | default: 36 | printf("Wrong choice \n"); 37 | } 38 | } 39 | } 40 | 41 | void insert() 42 | { 43 | int add_item; 44 | if (rear == MAX - 1) 45 | printf("Queue Overflow \n"); 46 | else 47 | { 48 | if (front == - 1) 49 | front = 0; 50 | printf("Inset the element in queue : "); 51 | scanf("%d", &add_item); 52 | rear = rear + 1; 53 | queue_array[rear] = add_item; 54 | } 55 | } 56 | 57 | void delete() 58 | { 59 | if (front == - 1 || front > rear) 60 | { 61 | printf("Queue Underflow \n"); 62 | return ; 63 | } 64 | else 65 | { 66 | printf("Element deleted from queue is : %d\n", queue_array[front]); 67 | front = front + 1; 68 | } 69 | } 70 | 71 | void display() 72 | { 73 | int i; 74 | if (front == - 1) 75 | printf("Queue is empty \n"); 76 | else 77 | { 78 | printf("Queue is : \n"); 79 | for (i = front; i <= rear; i++) 80 | printf("%d ", queue_array[i]); 81 | printf("\n"); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src-c/stackglobal.c: -------------------------------------------------------------------------------- 1 | #include 2 | int stack[100]; 3 | top=-1; 4 | size=100; 5 | 6 | int main() 7 | { int q,x,ele; 8 | while(1) 9 | { 10 | printf("1.pop\n2.push\n"); 11 | scanf("%d",&q); 12 | switch(q) 13 | { 14 | case 1: x=pop(); 15 | printf("%d has been popped\n",x); 16 | display(stack,top); 17 | break; 18 | case 2: printf("Enter element to be pushed\n"); 19 | scanf("%d",&ele); 20 | push(ele); 21 | display(stack,top); 22 | break; 23 | default: 24 | printf("invalid entry\n"); 25 | break; 26 | } 27 | } 28 | 29 | } 30 | 31 | void push(int ele) 32 | { 33 | if(top==size-1) 34 | { 35 | printf("Overflow\n"); 36 | } 37 | else 38 | { 39 | stack[++top]=ele; 40 | } 41 | } 42 | 43 | int pop() 44 | { 45 | if(top==-1) 46 | { 47 | printf("Underflow\n"); 48 | } 49 | else 50 | { 51 | return stack[top--]; 52 | } 53 | 54 | } 55 | void display() 56 | { int i; 57 | for(i=top;i>=0;i--) 58 | { 59 | printf("%d\n",stack[i]); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src-c/stacklinkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *ptr; 8 | }; 9 | 10 | struct node* push(struct node *top,int ele) 11 | { 12 | struct node *t; 13 | t=(struct node *)malloc(sizeof(struct node)); 14 | if(t==NULL) 15 | { 16 | printf("Memory overflow\n"); 17 | } 18 | else 19 | { 20 | t->data=ele; 21 | t->ptr=top; 22 | top=t; 23 | } 24 | return top; 25 | } 26 | struct node* pop(struct node *top) 27 | { int ele; 28 | struct node *t; 29 | if(top==NULL) 30 | { 31 | printf("Underflow\n"); 32 | } 33 | else 34 | { 35 | t=top; 36 | top = top->ptr; 37 | ele =t->data; 38 | free(t); 39 | return top; 40 | } 41 | } 42 | void display(struct node *top) 43 | { 44 | for(top;top!=NULL;top=top->ptr) 45 | { 46 | printf("%d\n",top->data); 47 | } 48 | } 49 | int main() 50 | { 51 | struct node *top=NULL; 52 | int q,x,ele; 53 | while(1) 54 | { 55 | printf("1.pop\n2.push\n"); 56 | scanf("%d",&q); 57 | switch(q) 58 | { 59 | case 1: top=pop(top); 60 | display(top); 61 | break; 62 | case 2: printf("Enter element to be pushed\n"); 63 | scanf("%d",&ele); 64 | top=push(top,ele); 65 | display(top); 66 | break; 67 | default: 68 | printf("invalid entry\n"); 69 | break; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src-c/stacklocal.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main() 5 | { int q,x,ele; 6 | int stack[100]; 7 | int top=-1; 8 | int size=100; 9 | while(1) 10 | { 11 | printf("1.pop\n2.push\n"); 12 | scanf("%d",&q); 13 | switch(q) 14 | { 15 | case 1: x=pop(&stack,&top); 16 | printf("%d has been popped\n",x); 17 | display(&stack,&top); 18 | break; 19 | case 2: printf("Enter element to be pushed\n"); 20 | scanf("%d",&ele); 21 | push(&stack,size,&top,ele); 22 | display(&stack,&top); 23 | break; 24 | default: 25 | printf("invalid entry\n"); 26 | break; 27 | } 28 | } 29 | 30 | } 31 | 32 | void push(int *stack,int size,int *top,int ele) 33 | { 34 | if(*top==size-1) 35 | { 36 | printf("Overflow\n"); 37 | } 38 | else 39 | { 40 | *(stack+(++*top))=ele; 41 | } 42 | } 43 | 44 | int pop(int *stack,int *top) 45 | { 46 | if(*top==-1) 47 | { 48 | printf("Underflow\n"); 49 | } 50 | else 51 | { 52 | return stack[(*(top))--]; 53 | } 54 | 55 | } 56 | void display(int *stack,int *top) 57 | { int i; 58 | for(i=*top;i>=0;i--) 59 | { 60 | printf("%d\n",*(stack+i)); 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /src-cpp/CircularQueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Queue 5 | { 6 | // Initialize front and rear 7 | int rear, front; 8 | 9 | // Circular Queue 10 | int size; 11 | int *arr; 12 | 13 | Queue(int s) 14 | { 15 | front = rear = -1; 16 | size = s; 17 | arr = new int[s]; 18 | } 19 | 20 | void enQueue(int value); 21 | int deQueue(); 22 | void displayQueue(); 23 | }; 24 | 25 | 26 | /* Function to create Circular queue */ 27 | void Queue::enQueue(int value) 28 | { 29 | if ((front == 0 && rear == size-1) || 30 | (rear == (front-1)%(size-1))) 31 | { 32 | printf("\nQueue is Full"); 33 | return; 34 | } 35 | 36 | else if (front == -1) /* Insert First Element */ 37 | { 38 | front = rear = 0; 39 | arr[rear] = value; 40 | } 41 | 42 | else if (rear == size-1 && front != 0) 43 | { 44 | rear = 0; 45 | arr[rear] = value; 46 | } 47 | 48 | else 49 | { 50 | rear++; 51 | arr[rear] = value; 52 | } 53 | } 54 | 55 | // Function to delete element from Circular Queue 56 | int Queue::deQueue() 57 | { 58 | if (front == -1) 59 | { 60 | printf("\nQueue is Empty"); 61 | return INT_MIN; 62 | } 63 | 64 | int data = arr[front]; 65 | arr[front] = -1; 66 | if (front == rear) 67 | { 68 | front = -1; 69 | rear = -1; 70 | } 71 | else if (front == size-1) 72 | front = 0; 73 | else 74 | front++; 75 | 76 | return data; 77 | } 78 | 79 | // Function displaying the elements 80 | // of Circular Queue 81 | void Queue::displayQueue() 82 | { 83 | if (front == -1) 84 | { 85 | printf("\nQueue is Empty"); 86 | return; 87 | } 88 | printf("\nElements in Circular Queue are: "); 89 | if (rear >= front) 90 | { 91 | for (int i = front; i <= rear; i++) 92 | printf("%d ",arr[i]); 93 | } 94 | else 95 | { 96 | for (int i = front; i < size; i++) 97 | printf("%d ", arr[i]); 98 | 99 | for (int i = 0; i <= rear; i++) 100 | printf("%d ", arr[i]); 101 | } 102 | } 103 | 104 | int main() 105 | { 106 | Queue q(5); 107 | 108 | q.enQueue(14); 109 | q.enQueue(22); 110 | q.enQueue(13); 111 | q.enQueue(-6); 112 | 113 | 114 | q.displayQueue(); 115 | 116 | 117 | printf("\nDeleted value = %d", q.deQueue()); 118 | printf("\nDeleted value = %d", q.deQueue()); 119 | 120 | q.displayQueue(); 121 | 122 | q.enQueue(9); 123 | q.enQueue(20); 124 | q.enQueue(5); 125 | 126 | q.displayQueue(); 127 | 128 | q.enQueue(20); 129 | return 0; 130 | } 131 | -------------------------------------------------------------------------------- /src-cpp/Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int queue[100], n = 100, front = - 1, rear = - 1; 4 | void Insert() { 5 | int val; 6 | if (rear == n - 1) 7 | cout<<"Queue Overflow"<>val; 13 | rear++; 14 | queue[rear] = val; 15 | } 16 | } 17 | void Delete() { 18 | if (front == - 1 || front > rear) { 19 | cout<<"Queue Underflow "; 20 | return ; 21 | } else { 22 | cout<<"Element deleted from queue is : "<< queue[front] < 4 | 5 | using namespace std; 6 | 7 | #define MAX 5000 8 | 9 | class Stack { 10 | int top; 11 | 12 | public: 13 | int a[MAX]; 14 | 15 | Stack() { 16 | top = -1; 17 | 18 | } 19 | void push(int x); 20 | int pop(); 21 | int peek(); 22 | bool isEmpty(); 23 | }; 24 | 25 | void Stack::push(int x) 26 | { 27 | if (top >= (MAX - 1)) { 28 | cout << "Stack Overflow"; 29 | return; 30 | } 31 | else { 32 | a[++top] = x; 33 | cout << x << " pushed into stack\n"; 34 | } 35 | } 36 | 37 | int Stack::pop() 38 | { 39 | if (top < 0) { 40 | cout << "Stack Underflow"; 41 | return 0; 42 | } 43 | else { 44 | int x = a[top--]; 45 | return x; 46 | } 47 | } 48 | int Stack::peek() 49 | { 50 | if (top < 0) { 51 | cout << "Stack is Empty"; 52 | return 0; 53 | } 54 | else { 55 | int x = a[top]; 56 | return x; 57 | } 58 | } 59 | 60 | bool Stack::isEmpty() 61 | { 62 | if(top < 0) return true; 63 | else return false; 64 | } 65 | 66 | // main fuction 67 | int main() 68 | { 69 | class Stack st; 70 | st.push(5); 71 | st.push(15); 72 | st.push(45); 73 | st.push(55); 74 | cout << st.pop() << " Popped from stack\n"; 75 | 76 | return 0; 77 | } -------------------------------------------------------------------------------- /src-cpp/bfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX 100 5 | 6 | #define initial 1 7 | #define waiting 2 8 | #define visited 3 9 | 10 | int n; 11 | int adj[MAX][MAX]; 12 | int state[MAX]; 13 | void create_graph(); 14 | void BF_Traversal(); 15 | void BFS(int v); 16 | 17 | int queue[MAX], front = -1,rear = -1; 18 | void insert_queue(int vertex); 19 | int delete_queue(); 20 | int isEmpty_queue(); 21 | 22 | 23 | 24 | int main() 25 | { 26 | create_graph(); 27 | BF_Traversal(); 28 | return 0; 29 | } 30 | 31 | void BF_Traversal() 32 | { 33 | int v; 34 | for(v=0; v rear) 79 | return 1; 80 | else 81 | return 0; 82 | } 83 | 84 | int delete_queue() 85 | { 86 | int delete_item; 87 | if(front == -1 || front > rear) 88 | { 89 | printf("Queue Underflow\n"); 90 | exit(1); 91 | } 92 | delete_item = queue[front]; 93 | front = front+1; 94 | return delete_item; 95 | } 96 | 97 | void create_graph() 98 | { 99 | int count,max_edge,origin,destin; 100 | 101 | printf("Enter number of vertices : "); 102 | scanf("%d",&n); 103 | max_edge = n*(n-1); 104 | 105 | for(count=1; count<=max_edge; count++) 106 | { 107 | printf("Enter edge %d( -1 -1 to quit ) : ",count); 108 | scanf("%d %d",&origin,&destin); 109 | 110 | if((origin == -1) && (destin == -1)) 111 | break; 112 | 113 | if(origin>=n || destin>=n || origin<0 || destin<0) 114 | { 115 | printf("Invalid edge!\n"); 116 | count--; 117 | } 118 | else 119 | { 120 | adj[origin][destin] = 1; 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src-cpp/dfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void DFS(int); 3 | int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10] 4 | int main() 5 | { 6 | int i,j; 7 | printf("Enter number of vertices:"); 8 | 9 | scanf("%d",&n); 10 | //read the adjecency matrix 11 | printf("\nEnter adjecency matrix of the graph:"); 12 | 13 | for(i=0;i 2 | #define INFINITY 9999 3 | #define MAX 10 4 | 5 | void dijkstra(int G[MAX][MAX],int n,int startnode); 6 | 7 | int main() 8 | { 9 | int G[MAX][MAX],i,j,n,u; 10 | printf("Enter no. of vertices:"); 11 | scanf("%d",&n); 12 | printf("\nEnter the adjacency matrix:\n"); 13 | for(i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int w[]={6,10,3,5,1,3}; 7 | int v[]={6,2,1,8,3,5}; 8 | double vw[6]; 9 | for(int i=0;i<6;i++) 10 | { 11 | vw[i]=(double)(v[i])/w[i]; 12 | } 13 | double max=0; 14 | int m1; 15 | printf("Enter the capacity : "); 16 | scanf("%d",&m1); 17 | int ci=0; 18 | double tv=0; 19 | int tw=0; 20 | do 21 | { 22 | max=0; 23 | for(int i=0;i<6;i++) 24 | { 25 | if(vw[i]>max) 26 | { 27 | max=vw[i]; 28 | ci=i; 29 | } 30 | } 31 | if(max!=0) 32 | { 33 | if(m1-(tw+w[ci])>=0) 34 | { 35 | tv=tv+v[ci]; 36 | tw=tw+w[ci]; 37 | cout<<"i"<<(ci+1)<<" : 1"< 2 | using namespace std; 3 | 4 | 5 | class node{ 6 | public: 7 | int data; 8 | node*next; 9 | 10 | node(int d){ 11 | data = d; 12 | next = NULL; 13 | } 14 | ~node(){ 15 | int temp = data; 16 | if(next!=NULL){ 17 | delete next; 18 | next = NULL; 19 | } 20 | cout<<"Deleting node with data "<next = head; 27 | head = n; 28 | } 29 | 30 | void takeInput(node *&head){ 31 | int d; 32 | cin>>d; 33 | while(d!=-1){ 34 | insertAtHead(head,d); 35 | cin>>d; 36 | } 37 | } 38 | void print(node*head){ 39 | while(head!=NULL){ 40 | cout<data<<"-->"; 41 | head = head->next; 42 | } 43 | cout<<"NULL"<next; 50 | } 51 | return l; 52 | } 53 | void insertInMiddle(node*&head,int d,int p){ 54 | 55 | if(p==0){ 56 | insertAtHead(head,d); 57 | return; 58 | } 59 | int jump=1; 60 | node*temp = head; 61 | while(jump<=p-1){ 62 | jump++; 63 | temp = temp->next; 64 | } 65 | 66 | node*n = new node(d); 67 | n->next = temp->next; 68 | temp->next = n; 69 | 70 | } 71 | 72 | istream& operator>>(istream&is, node*&head){ 73 | takeInput(head); 74 | return is; 75 | } 76 | ostream& operator<<(ostream&os, node*head){ 77 | print(head); 78 | return os; 79 | } 80 | 81 | void deleteNode(node*&head,int d){ 82 | node* prev = NULL; 83 | node* current = head; 84 | 85 | while(current!=NULL){ 86 | if(current->data == d){ 87 | if(prev==NULL){ 88 | ///Head Node 89 | node*temp = head; 90 | head = temp->next; 91 | temp->next = NULL; 92 | delete temp; 93 | current = head; 94 | } 95 | else{ 96 | ///Some Middle Node 97 | node*temp = current; 98 | prev->next = current->next; 99 | //temp->next = NULL; 100 | temp->next = NULL; 101 | delete temp; 102 | 103 | } 104 | 105 | } 106 | prev = current; 107 | current = current->next; 108 | } 109 | 110 | } 111 | 112 | int main(){ 113 | node*head=NULL; 114 | cin>>head; 115 | cout<>d>>p; 121 | insertInMiddle(head,d,p); 122 | cout<>d; 127 | deleteNode(head,d); 128 | cout< 2 | #include 3 | #include 4 | #define max 100000 5 | 6 | int a[max]; 7 | int b[max]; 8 | 9 | void merging(int low, int mid, int high) 10 | { 11 | int l1, l2, i; 12 | 13 | for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) { 14 | if(a[l1] <= a[l2]) 15 | b[i] = a[l1++]; 16 | else 17 | b[i] = a[l2++]; 18 | } 19 | 20 | while(l1 <= mid) 21 | b[i++] = a[l1++]; 22 | 23 | while(l2 <= high) 24 | b[i++] = a[l2++]; 25 | 26 | for(i = low; i <= high; i++) 27 | a[i] = b[i]; 28 | } 29 | 30 | void sort(int low, int high) { 31 | int mid; 32 | 33 | if(low < high) { 34 | mid = (low + high) / 2; 35 | sort(low, mid); 36 | sort(mid+1, high); 37 | merging(low, mid, high); 38 | } else { 39 | return; 40 | } 41 | } 42 | 43 | int main() { 44 | int i; 45 | 46 | for(i = 0; i < max; i++) 47 | a[i]=(rand()%100)+1; 48 | 49 | double time_spent=0.0; 50 | clock_t begin=clock(); 51 | 52 | sort(0, max); 53 | 54 | clock_t end=clock(); 55 | time_spent=(double)(end-begin)/CLOCKS_PER_SEC; 56 | printf("%d", CLOCKS_PER_SEC); 57 | printf("\nTime spent:%ld", time_spent); 58 | return 0; 59 | } 60 | 61 | -------------------------------------------------------------------------------- /src-cpp/prims_algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define infinity 9999 5 | #define MAX 20 6 | 7 | int G[MAX][MAX],spanning[MAX][MAX],n;//Maximum vertices allowed = MAX. Can be taken as an input as well 8 | 9 | int prims(); 10 | 11 | int main() 12 | { 13 | int i,j,total_cost; 14 | printf("Enter no. of vertices:"); 15 | scanf("%d",&n); 16 | printf("\nEnter the adjacency matrix:\n"); 17 | for(i=0;i0) 61 | { 62 | //find the vertex at minimum distance from the tree 63 | min_distance=infinity; 64 | for(i=1;i 2 | using namespace std; 3 | 4 | struct node { 5 | int data; 6 | struct node *next; 7 | }; 8 | 9 | struct node* front = NULL, *rear = NULL; 10 | 11 | void enqueue() 12 | { 13 | struct node* temp = (struct node* )malloc(sizeof(struct node)); 14 | cout<<"Enter data: "; 15 | cin>>temp->data; 16 | temp->next = NULL; 17 | 18 | if (front == NULL) 19 | { 20 | front = rear = temp; 21 | return; 22 | } 23 | 24 | rear->next = temp; 25 | rear = temp; 26 | } 27 | 28 | void dequeue() 29 | { 30 | if (front == NULL) 31 | { 32 | cout<<"Empty!\n"; 33 | return; 34 | } 35 | struct node* temp = front; 36 | cout<<"data: \n"<data<next; 41 | free(temp); 42 | } 43 | 44 | void atFront() 45 | { 46 | if (front == NULL) 47 | { 48 | cout<<"Empty!\n"; 49 | return; 50 | } 51 | cout<<"At the top: \n"<data<data<<" "; 59 | temp=temp->next; 60 | } 61 | cout<<"\n"; 62 | } 63 | 64 | 65 | int main() 66 | { 67 | int choice; 68 | 69 | while(1) 70 | { 71 | cout<<"1. Enqueue\n2. Dequeue\n3. At front\n4. travers\n5. Exit\n\nEnter Choice: "; 72 | cin>>choice; 73 | switch(choice) 74 | { 75 | case 1: 76 | enqueue(); 77 | break; 78 | case 2: 79 | dequeue(); 80 | break; 81 | case 3: 82 | atFront(); 83 | break; 84 | case 4: 85 | travers(); 86 | break; 87 | default: 88 | exit(0); 89 | } 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /src-cpp/stack_using_ll.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct node 4 | { 5 | int data; 6 | node* next; 7 | }; 8 | node* head; 9 | void push(int x) 10 | { 11 | node* temp=new node(); 12 | temp->data=x; 13 | temp->next=NULL; 14 | if(head==NULL){ 15 | head=temp; 16 | return; 17 | } 18 | 19 | temp->next=head; 20 | head=temp; 21 | } 22 | void top() 23 | { 24 | node* temp=head; 25 | cout<<"Top elements is: "<data<data<<" "; 31 | print(p->next); 32 | } 33 | int main() 34 | { 35 | head=NULL; 36 | int n,x; 37 | cout<<"How many numbers:"; 38 | cin>>n; 39 | for (int i = 0; i < n; ++i) 40 | { 41 | cout<<"Enter numbers you want to push into stack: "; 42 | cin>>x; 43 | push(x); 44 | } 45 | print(head); 46 | cout<<""< size) 60 | { 61 | return -1; 62 | } 63 | Node n = head; 64 | while (index - 1 != 0) 65 | { 66 | n = n.next; 67 | index--; 68 | } 69 | 70 | return n.data; 71 | } 72 | 73 | public void print() { 74 | 75 | System.out.print("CircularLinked List:"); 76 | Node temp = head; 77 | 78 | if (size <= 0) 79 | { 80 | System.out.print("List is empty"); 81 | } 82 | else 83 | { 84 | do 85 | { 86 | System.out.print(" " + temp.data); 87 | temp = temp.next; 88 | } while (temp != head); 89 | } 90 | System.out.print("\n"); 91 | } 92 | 93 | public int getSize() { 94 | return size; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src-java/CircularQueue.java: -------------------------------------------------------------------------------- 1 | import Exceptions.OverflowException; 2 | import Exceptions.UnderflowException; 3 | 4 | import java.util.Scanner; 5 | 6 | public class CircularQueue { 7 | private int[] queue; 8 | private int size; 9 | private int front; 10 | private int rear; 11 | 12 | public CircularQueue() { 13 | queue = null; 14 | size = 0; 15 | front = rear = 0; 16 | } 17 | 18 | public CircularQueue(int size) { 19 | this.size = size; 20 | front = rear = -1; 21 | queue = new int[size]; 22 | } 23 | 24 | public static void main(String[] args) { 25 | int ch, x; 26 | Scanner sc = new Scanner(System.in); 27 | System.out.print("Enter the size of the queue : "); 28 | int n = sc.nextInt(); 29 | n = Math.abs(n); 30 | CircularQueue q = new CircularQueue(n); 31 | 32 | loop: 33 | for (; ; ) { 34 | System.out.print("1. Enqueue\n" + 35 | "2. Dequeue\n" + 36 | "3. Display\n" + 37 | "0. Exit\n" + 38 | "Enter your choice : "); 39 | ch = sc.nextInt(); 40 | switch (ch) { 41 | case 0: 42 | break loop; 43 | case 1: 44 | System.out.print("Enter the number you want to enqueue : "); 45 | x = sc.nextInt(); 46 | try { 47 | q.enqueue(x); 48 | } catch (OverflowException e) { 49 | System.out.println(e.getMessage()); 50 | } 51 | break; 52 | case 2: 53 | try { 54 | x = q.dequeue(); 55 | System.out.println(x + " has been removed"); 56 | } catch (UnderflowException e) { 57 | System.out.println(e.getMessage()); 58 | } 59 | break; 60 | case 3: 61 | q.display(); 62 | break; 63 | default: 64 | System.out.println("Dude, seriously?\nI'm sure you can do better than that.\nTry again."); 65 | } 66 | System.out.println(); 67 | } 68 | } 69 | 70 | private boolean isFull() { 71 | return (rear + 1) % size == front; 72 | } 73 | 74 | private boolean isEmpty() { 75 | return front == rear; 76 | } 77 | 78 | public void enqueue(int x) throws OverflowException { 79 | if (!isFull()) { 80 | rear = (rear + 1) % size; 81 | queue[rear] = x; 82 | } else 83 | throw new OverflowException("Cannot enqueue. Queue is full"); 84 | } 85 | 86 | public int dequeue() throws UnderflowException { 87 | if (!isEmpty()) { 88 | front = (front + 1) % size; 89 | return queue[front]; 90 | } else 91 | throw new UnderflowException("Cannot dequeue. Queue is empty"); 92 | } 93 | 94 | public void display() { 95 | if (!isEmpty()) { 96 | int i; 97 | System.out.print("["); 98 | for (i = (front + 1) % size; i != rear; i = (i + 1) % size) 99 | System.out.print(queue[i] + ", "); 100 | System.out.println(queue[i] + "]"); 101 | } else 102 | System.out.println("[]"); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src-java/DoublyLinkedList.java: -------------------------------------------------------------------------------- 1 | import Exceptions.InvalidPositionException; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DoublyLinkedList { 6 | 7 | Node head; 8 | 9 | public DoublyLinkedList() { 10 | this.head = null; 11 | } 12 | 13 | public static void main(String[] args) { 14 | Scanner sc = new Scanner(System.in); 15 | DoublyLinkedList doublyLinkedList = new DoublyLinkedList(); 16 | int ch, x, pos, del; 17 | 18 | loop: 19 | for (; ; ) { 20 | System.out.print("1. Insert at Front\n" + 21 | "2. Insert at End\n" + 22 | "3. Delete from Front\n" + 23 | "4. Delete from End\n" + 24 | "5. Display\n" + 25 | "6. Reverse\n" + 26 | "7. Size\n" + 27 | "0. Exit\n" + 28 | "Enter your choice: "); 29 | ch = sc.nextInt(); 30 | 31 | switch (ch) { 32 | case 0: 33 | break loop; 34 | case 1: 35 | System.out.print("Enter the value you want to insert : "); 36 | x = sc.nextInt(); 37 | doublyLinkedList.insertAtFront(x); 38 | break; 39 | case 2: 40 | System.out.print("Enter the value you want to insert : "); 41 | x = sc.nextInt(); 42 | doublyLinkedList.insertAtEnd(x); 43 | break; 44 | case 3: 45 | del = doublyLinkedList.deleteFromFront(); 46 | if (del == -999) 47 | System.out.println("Linked List is empty"); 48 | else 49 | System.out.println(del + " has been removed from the Linked List"); 50 | break; 51 | case 4: 52 | del = doublyLinkedList.deleteFromEnd(); 53 | if (del == -999) 54 | System.out.println("Linked List is empty"); 55 | else 56 | System.out.println(del + " has been removed from the Linked List"); 57 | break; 58 | case 5: 59 | System.out.println("Linked List : "); 60 | doublyLinkedList.display(); 61 | break; 62 | case 6: 63 | doublyLinkedList.reverse(); 64 | System.out.println("The Linked List has been reversed"); 65 | break; 66 | case 7: 67 | System.out.println("The size is : " + doublyLinkedList.size()); 68 | break; 69 | 70 | default: 71 | System.out.println("Enter a valid choice you blind fuck!"); 72 | } 73 | System.out.println(); 74 | } 75 | } 76 | 77 | private int size() { 78 | if (isEmpty()) 79 | return 0; 80 | 81 | int counter = 1; 82 | Node temp = head; 83 | while (temp.next != null) { 84 | temp = temp.next; 85 | counter++; 86 | } 87 | return counter; 88 | } 89 | 90 | private void reverse() { 91 | Node temp = null; 92 | Node currentNode = head; 93 | 94 | while (currentNode != null) { 95 | temp = currentNode.prev; 96 | currentNode.prev = currentNode.next; 97 | currentNode.next = temp; 98 | currentNode = currentNode.prev; 99 | } 100 | 101 | if (temp != null) { 102 | head = temp.prev; 103 | } 104 | } 105 | 106 | private int deleteFromEnd() { 107 | if (isEmpty()) 108 | throw new InvalidPositionException("Cannot delete from end since DoublyLinkedList is empty"); 109 | 110 | Node temp = head; 111 | while (temp.next != null) { 112 | temp = temp.next; 113 | } 114 | int value = temp.data; 115 | if (temp.prev != null) { 116 | temp.prev.next = null; 117 | } else { 118 | head = null; 119 | } 120 | return value; 121 | } 122 | 123 | private int deleteFromFront() { 124 | if (isEmpty()) 125 | throw new InvalidPositionException("Cannot delete from front since DoublyLinkedList is empty"); 126 | 127 | Node temp = head; 128 | int value = temp.data; 129 | head = temp.next; 130 | if (temp.next != null) 131 | temp.next.prev = null; 132 | return value; 133 | } 134 | 135 | private boolean isEmpty() { 136 | return head == null; 137 | } 138 | 139 | private void insertAtFront(int value) { 140 | Node newNode = new Node(value); 141 | 142 | newNode.next = head; 143 | newNode.prev = null; 144 | 145 | if (head != null) 146 | head.prev = newNode; 147 | 148 | head = newNode; 149 | } 150 | 151 | private void insertAtEnd(int value) { 152 | Node newNode = new Node(value); 153 | 154 | if (isEmpty()) { 155 | head = newNode; 156 | return; 157 | } 158 | 159 | Node temp = head; 160 | while (temp.next != null) { 161 | temp = temp.next; 162 | } 163 | temp.next = newNode; 164 | newNode.prev = temp; 165 | } 166 | 167 | private void display() { 168 | Node t; 169 | for (t = head; t != null; t = t.next) { 170 | System.out.print(t.data + " "); 171 | } 172 | System.out.println(); 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src-java/Exceptions/InvalidPositionException.java: -------------------------------------------------------------------------------- 1 | package Exceptions; 2 | 3 | public class InvalidPositionException extends RuntimeException { 4 | 5 | public InvalidPositionException(final String message) { 6 | super(message); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src-java/Exceptions/OverflowException.java: -------------------------------------------------------------------------------- 1 | package Exceptions; 2 | 3 | public class OverflowException extends Exception { 4 | public OverflowException() { 5 | } 6 | 7 | public OverflowException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src-java/Exceptions/UnderflowException.java: -------------------------------------------------------------------------------- 1 | package Exceptions; 2 | 3 | public class UnderflowException extends Exception { 4 | public UnderflowException() { 5 | } 6 | 7 | public UnderflowException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src-java/Hashes.java: -------------------------------------------------------------------------------- 1 | // Java program to illustrate 2 | // Java.util.HashMap 3 | 4 | import java.util.*; 5 | 6 | public class Hashes { 7 | public static void main(String[] args) { 8 | 9 | HashMap map = new HashMap<>(); 10 | System.out.println(map); 11 | map.put("First", 10); 12 | map.put("Second", 30); 13 | map.put("Third", 20); 14 | System.out.println("Size of map is:- " + map.size()); 15 | System.out.println(map); 16 | if (map.containsKey("vishal")) { 17 | Integer a = map.get("vishal"); 18 | System.out.println("value for key" + " \"vishal\" is:- " + a); 19 | } 20 | sortByKey(map); 21 | sortByValue(map); 22 | map.clear(); 23 | System.out.println(map); 24 | } 25 | 26 | public static void print(Map map) { 27 | if (map.isEmpty()) { 28 | System.out.println("map is empty"); 29 | } else { 30 | System.out.println(map); 31 | } 32 | } 33 | 34 | public static void sortByKey(Map map) { 35 | // TreeMap to store values of HashMap 36 | TreeMap sorted = new TreeMap<>(); 37 | 38 | // Copy all data from hashMap into TreeMap 39 | sorted.putAll(map); 40 | System.out.println("After sorting by key"); 41 | // Display the TreeMap which is naturally sorted 42 | for (Map.Entry entry : sorted.entrySet()) 43 | System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 44 | } 45 | 46 | public static void sortByValue(HashMap map) { 47 | List mapKeys = new ArrayList<>(map.keySet()); 48 | List mapValues = new ArrayList<>(map.values()); 49 | Collections.sort(mapValues); 50 | Collections.sort(mapKeys); 51 | 52 | LinkedHashMap sortedMap = new LinkedHashMap<>(); 53 | 54 | Iterator valueIt = mapValues.iterator(); 55 | while (valueIt.hasNext()) { 56 | Integer val = valueIt.next(); 57 | Iterator keyIt = mapKeys.iterator(); 58 | while (keyIt.hasNext()) { 59 | String key = keyIt.next(); 60 | Integer comp1 = map.get(key); 61 | Integer comp2 = val; 62 | if (comp1.equals(comp2)) { 63 | keyIt.remove(); 64 | sortedMap.put(key, val); 65 | break; 66 | } 67 | } 68 | } 69 | System.out.println("Map Sorted By Value"); 70 | System.out.println(sortedMap); 71 | } 72 | } -------------------------------------------------------------------------------- /src-java/LinkedList.java: -------------------------------------------------------------------------------- 1 | import Exceptions.InvalidPositionException; 2 | 3 | import java.util.Scanner; 4 | 5 | public class LinkedList { 6 | Node start; 7 | 8 | public LinkedList() { 9 | start = null; 10 | } 11 | 12 | public static void main(String[] args) { 13 | Scanner sc = new Scanner(System.in); 14 | LinkedList linkedList = new LinkedList(); 15 | int ch, x, pos, del; 16 | 17 | loop: 18 | for (; ; ) { 19 | System.out.print("1. Insert at Front\n" + 20 | "2. Insert at End\n" + 21 | "3. Insert at Position\n" + 22 | "4. Delete at Front\n" + 23 | "5. Delete at End\n" + 24 | "6. Delete at Position\n" + 25 | "7. Display\n" + 26 | "8. Reverse\n" + 27 | "9. Recursive Reverse\n" + 28 | "10. Size\n" + 29 | "11. Reverse in groups\n" + 30 | "12. Check if the Linked List is a palindrome\n" + 31 | "0. Exit\n" + 32 | "Enter your choice: "); 33 | ch = sc.nextInt(); 34 | 35 | switch (ch) { 36 | case 0: 37 | break loop; 38 | case 1: 39 | System.out.print("Enter the value you want to insert : "); 40 | x = sc.nextInt(); 41 | linkedList.insertAtFront(x); 42 | break; 43 | case 2: 44 | System.out.print("Enter the value you want to insert : "); 45 | x = sc.nextInt(); 46 | linkedList.insertAtEnd(x); 47 | break; 48 | case 3: 49 | System.out.print("Enter the value you want to insert : "); 50 | x = sc.nextInt(); 51 | System.out.print("Enter the position where you want to enter it : "); 52 | pos = sc.nextInt(); 53 | linkedList.insertAtPos(x, pos); 54 | break; 55 | case 4: 56 | del = linkedList.deleteAtFront(); 57 | if (del == -999) 58 | System.out.println("Linked List is empty"); 59 | else 60 | System.out.println(del + " has been removed from the Linked List"); 61 | break; 62 | case 5: 63 | del = linkedList.deleteAtEnd(); 64 | if (del == -999) 65 | System.out.println("Linked List is empty"); 66 | else 67 | System.out.println(del + " has been removed from the Linked List"); 68 | break; 69 | case 6: 70 | System.out.print("Enter the position which you want to remove : "); 71 | pos = sc.nextInt(); 72 | del = linkedList.deleteAtPos(pos); 73 | if (del == -999) 74 | System.out.println("Position is invalid"); 75 | else 76 | System.out.println(del + " has been removed from the Linked List"); 77 | break; 78 | case 7: 79 | System.out.println("Linked List : "); 80 | linkedList.display(); 81 | break; 82 | case 8: 83 | linkedList.reverse(); 84 | System.out.println("The Linked List has been reversed"); 85 | break; 86 | case 9: 87 | linkedList.recursiveReverse(linkedList.start, null); 88 | System.out.println("The Linked List has been reversed"); 89 | break; 90 | case 10: 91 | System.out.println(linkedList.size()); 92 | break; 93 | case 11: 94 | System.out.print("Enter the size of the group you want to reverse : "); 95 | x = sc.nextInt(); 96 | linkedList.start = linkedList.reverseGroup(linkedList.start, x); 97 | System.out.println("The Linked ist has been reversed in groups of " + x); 98 | break; 99 | case 12: 100 | if (linkedList.isPalindromeList()) 101 | System.out.println("The Linked List is a palindrome"); 102 | else 103 | System.out.println("The Linked List is not a palindrome"); 104 | break; 105 | default: 106 | System.out.println("Enter a valid choice you blind fuck!"); 107 | } 108 | System.out.println(); 109 | } 110 | } 111 | 112 | private void insertAtEnd(int x) { 113 | Node t, n; 114 | n = new Node(x); 115 | if (start == null) { 116 | start = n; 117 | return; 118 | } 119 | for (t = start; t.next != null; t = t.next) ; 120 | t.next = n; 121 | } 122 | 123 | private void insertAtFront(int x) { 124 | Node n = new Node(x); 125 | n.next = start; 126 | start = n; 127 | } 128 | 129 | private void insertAtPos(int x, int pos) { 130 | Node n = new Node(x); 131 | Node t; 132 | if (start == null) { 133 | start = n; 134 | return; 135 | } 136 | int i = 1; 137 | for (t = start; i < pos - 1; i++, t = t.next) ; 138 | n.next = t.next; 139 | t.next = n; 140 | } 141 | 142 | private int deleteAtEnd() { 143 | if (start == null) 144 | throw new InvalidPositionException("Cannot delete at end since the LinkedList is empty"); 145 | Node t; 146 | for (t = start; t.next.next != null; t = t.next) ; 147 | int del = t.next.data; 148 | t.next = null; 149 | return del; 150 | } 151 | 152 | private int deleteAtFront() { 153 | if (start == null) 154 | throw new InvalidPositionException("Cannot delete at front since the LinkedList is empty"); 155 | int del = start.data; 156 | start = start.next; 157 | return del; 158 | } 159 | 160 | private int deleteAtPos(int pos) { 161 | int del; 162 | if (pos == 1) 163 | return deleteAtFront(); 164 | if (pos == size()) 165 | return deleteAtEnd(); 166 | if (pos < 0 || pos > size()) 167 | throw new InvalidPositionException("Cannot delete at invalid position" + pos); 168 | Node t; 169 | int i = 0; 170 | for (t = start; i < pos - 2; i++, t = t.next) ; 171 | del = t.next.data; 172 | t.next = t.next.next; 173 | return del; 174 | } 175 | 176 | private int size() { 177 | Node t; 178 | int len = 0; 179 | for (t = start; t != null; t = t.next, len++) ; 180 | return len; 181 | } 182 | 183 | private void display() { 184 | Node t; 185 | for (t = start; t != null; t = t.next) { 186 | System.out.print(t.data + " "); 187 | } 188 | System.out.println(); 189 | } 190 | 191 | private void reverse() { 192 | Node prev, cur, next; 193 | prev = null; 194 | cur = start; 195 | for (; cur != null; ) { 196 | next = cur.next; 197 | cur.next = prev; 198 | prev = cur; 199 | cur = next; 200 | } 201 | start = prev; 202 | } 203 | 204 | /** 205 | * @param cur holds the reference to the current Node. pass root Node while calling. 206 | * @param prev holds the reference to the previous Node. pass null while calling. 207 | */ 208 | private void recursiveReverse(Node cur, Node prev) { 209 | //last node reached 210 | if (cur.next == null) { 211 | start = cur; 212 | cur.next = prev; 213 | return; 214 | } 215 | 216 | Node next = cur.next; 217 | cur.next = prev; 218 | 219 | recursiveReverse(next, cur); 220 | } 221 | 222 | /** 223 | * @param b holds the reference to the beginning Node. 224 | * @param g size of the group by which the Linked List will be reversed. 225 | */ 226 | private Node reverseGroup(Node b, int g) { 227 | Node prev, cur, next; 228 | prev = null; 229 | cur = b; 230 | next = null; 231 | for (int i = 1; i <= g && cur != null; ++i) { 232 | next = cur.next; 233 | cur.next = prev; 234 | prev = cur; 235 | cur = next; 236 | } 237 | if (cur != null) 238 | b.next = reverseGroup(next, g); 239 | return prev; 240 | } 241 | 242 | private boolean isPalindromeList() { 243 | int l = size(); 244 | LinkedList temp = new LinkedList(); 245 | Node t = start; 246 | for (int i = 0; i < l / 2; i++) { 247 | temp.insertAtFront(t.data); 248 | t = t.next; 249 | } 250 | if (l % 2 != 0) 251 | t = t.next; 252 | for (int i = 0; i < l / 2; i++, t = t.next) { 253 | if (temp.deleteAtFront() != t.data) 254 | return false; 255 | } 256 | return true; 257 | } 258 | 259 | /*private boolean isPalindromeRecursive(Node x, Node t, int i, int n) { 260 | if (n % 2 != 0 && i == n / 2 + 1) 261 | return isPalindromeRecursive(x, x.next, i + 1, n); 262 | if (i > n / 2) 263 | return x.data == t.data; 264 | else 265 | }*/ 266 | } 267 | -------------------------------------------------------------------------------- /src-java/Node.java: -------------------------------------------------------------------------------- 1 | public class Node { 2 | int data; 3 | Node next, prev; 4 | 5 | public Node() { 6 | data = 0; 7 | next = prev = null; 8 | } 9 | 10 | public Node(int data) { 11 | this.data = data; 12 | next = prev = null; 13 | } 14 | 15 | public Node(Node x) { 16 | data = x.data; 17 | next = x.next; 18 | prev = x.prev; 19 | } 20 | 21 | @Override 22 | public int hashCode() { 23 | return data; 24 | } 25 | 26 | @Override 27 | public boolean equals(Object obj) { 28 | if (obj instanceof Node) { 29 | Node n = (Node) obj; 30 | return data == n.data; 31 | } else 32 | return false; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src-java/Queue.java: -------------------------------------------------------------------------------- 1 | import Exceptions.OverflowException; 2 | import Exceptions.UnderflowException; 3 | 4 | import java.util.Scanner; 5 | 6 | public class Queue { 7 | private int[] queue; 8 | private int size; 9 | private int front; 10 | private int rear; 11 | 12 | public Queue() { 13 | queue = null; 14 | size = 0; 15 | front = rear = -1; 16 | } 17 | 18 | public Queue(int size) { 19 | this.size = size; 20 | front = rear = -1; 21 | queue = new int[size]; 22 | } 23 | 24 | public static void main(String[] args) { 25 | int ch, x; 26 | Scanner sc = new Scanner(System.in); 27 | System.out.print("Enter the size of the queue : "); 28 | int n = sc.nextInt(); 29 | Queue q = new Queue(n); 30 | 31 | loop: 32 | for (; ; ) { 33 | System.out.print("1. Enqueue\n" + 34 | "2. Dequeue\n" + 35 | "3. Display\n" + 36 | "0. Exit\n" + 37 | "Enter your choice : "); 38 | ch = sc.nextInt(); 39 | switch (ch) { 40 | case 0: 41 | break loop; 42 | case 1: 43 | System.out.print("Enter the number you want to enqueue : "); 44 | x = sc.nextInt(); 45 | try { 46 | q.enqueue(x); 47 | } catch (OverflowException e) { 48 | System.out.println(e.getMessage()); 49 | } 50 | break; 51 | case 2: 52 | try { 53 | x = q.dequeue(); 54 | System.out.println(x + " has been removed"); 55 | } catch (UnderflowException e) { 56 | System.out.println(e.getMessage()); 57 | } 58 | break; 59 | case 3: 60 | q.display(); 61 | break; 62 | default: 63 | System.out.println("Dude, seriously?\nI'm sure you can do better than that.\nTry again."); 64 | } 65 | System.out.println(); 66 | } 67 | } 68 | 69 | private boolean isFull() { 70 | return rear >= size - 1; 71 | } 72 | 73 | private boolean isEmpty() { 74 | return front > rear; 75 | } 76 | 77 | public void enqueue(int x) throws OverflowException { 78 | if (front == -1) 79 | ++front; 80 | if (!isFull()) 81 | queue[++rear] = x; 82 | else 83 | throw new OverflowException("Queue is full. Unable to enqueue"); 84 | } 85 | 86 | public int dequeue() throws UnderflowException { 87 | if (isEmpty()) { 88 | throw new UnderflowException("Queue is empty. Unable to dequeue"); 89 | } 90 | return queue[front++]; 91 | } 92 | 93 | public void display() { 94 | if (isEmpty()) { 95 | System.out.println("Queue is empty"); 96 | return; 97 | } 98 | System.out.print("["); 99 | for (int i = front; i < rear; i++) 100 | System.out.print(queue[i] + ", "); 101 | System.out.println(queue[rear] + "]"); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src-java/Stack.java: -------------------------------------------------------------------------------- 1 | import Exceptions.OverflowException; 2 | import Exceptions.UnderflowException; 3 | 4 | import java.util.Scanner; 5 | 6 | public class Stack { 7 | private int[] stack; 8 | private int size, top; 9 | 10 | Stack() { 11 | stack = null; 12 | size = 0; 13 | top = -1; 14 | } 15 | 16 | Stack(int size) { 17 | this.size = size; 18 | top = -1; 19 | stack = new int[size]; 20 | } 21 | 22 | public static void main(String[] args) { 23 | int ch, x; 24 | Scanner sc = new Scanner(System.in); 25 | System.out.print("Enter the size of the stack : "); 26 | int n = sc.nextInt(); 27 | Stack s = new Stack(n); 28 | 29 | loop: 30 | for (; ; ) { 31 | System.out.print("1. Push\n" + 32 | "2. Pop\n" + 33 | "3. Display\n" + 34 | "0. Exit\n" + 35 | "Enter your choice : "); 36 | ch = sc.nextInt(); 37 | switch (ch) { 38 | case 0: 39 | break loop; 40 | case 1: 41 | System.out.print("Enter the number you want to push : "); 42 | x = sc.nextInt(); 43 | try { 44 | s.push(x); 45 | } catch (OverflowException e) { 46 | System.out.println(e.getMessage()); 47 | } 48 | break; 49 | case 2: 50 | try { 51 | x = s.pop(); 52 | System.out.println(x + " has been popped"); 53 | } catch (UnderflowException e) { 54 | System.out.println(e.getMessage()); 55 | } 56 | break; 57 | case 3: 58 | s.display(); 59 | break; 60 | default: 61 | System.out.println("Dude, seriously?\nI'm sure you can do better than that.\nTry again."); 62 | } 63 | System.out.println(); 64 | } 65 | } 66 | 67 | private boolean isFull() { 68 | return top >= size - 1; 69 | } 70 | 71 | private boolean isEmpty() { 72 | return top == -1; 73 | } 74 | 75 | private void push(int x) throws OverflowException { 76 | if (!isFull()) 77 | stack[++top] = x; 78 | else 79 | throw new OverflowException("Stack is full"); 80 | } 81 | 82 | private int pop() throws UnderflowException { 83 | if (!isEmpty()) 84 | return stack[top--]; 85 | else 86 | throw new UnderflowException("Stack is empty"); 87 | } 88 | 89 | private void display() { 90 | if (isEmpty()) { 91 | System.out.println("[]"); 92 | return; 93 | } 94 | int i; 95 | System.out.print("["); 96 | for (i = 0; i < top; i++) 97 | System.out.print(stack[i] + ", "); 98 | System.out.println(stack[i] + "]"); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src-java/TrieNode.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | 3 | class TrieNode { 4 | char data; 5 | boolean isEnd; 6 | int count; 7 | LinkedList childList; 8 | 9 | public TrieNode(char c) { 10 | childList = new LinkedList(); 11 | isEnd = false; 12 | data = c; 13 | count = 0; 14 | } 15 | 16 | public TrieNode getChild(char c) { 17 | if (childList != null) { 18 | for (TrieNode eachChild : childList) { 19 | if (eachChild.data == c) { 20 | return eachChild; 21 | } 22 | } 23 | } 24 | 25 | return null; 26 | } 27 | } -------------------------------------------------------------------------------- /src-javascript/LinkedList.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(data, next = null) { 3 | this.data = data; 4 | this.next = next; 5 | } 6 | } 7 | 8 | class LinkedList { 9 | 10 | constructor() { 11 | this.head = null; 12 | } 13 | 14 | size() { 15 | let counter = 0; 16 | let node = this.getFirst(); 17 | while(node) { 18 | counter++; 19 | node = node.next; 20 | } 21 | return counter; 22 | } 23 | 24 | clear() { 25 | this.head = null; 26 | } 27 | 28 | getAt(index) { 29 | if(!this.head) return null; 30 | 31 | let counter = 0; 32 | let node = this.getFirst(); 33 | 34 | while(node) { 35 | if(counter == index) return node; 36 | counter++; 37 | node = node.next; 38 | } 39 | return null; 40 | } 41 | 42 | removeAt(index) { 43 | if(!this.head) return; 44 | if(index == 0) this.head = this.head.next; 45 | 46 | const previous = this.getAt(index-1); 47 | 48 | if(!previous || !previous.next) return; 49 | previous.next = previous.next.next; 50 | } 51 | 52 | insertAt(data, index) { 53 | if(!this.head) { 54 | this.head = new Node(data); 55 | return; 56 | } 57 | if(index === 0) { 58 | this.insertFirst(data); 59 | return; 60 | } 61 | 62 | const previous = this.getAt(index-1) || this.getLast(); 63 | previous.next = new Node(data, previous.next); 64 | } 65 | 66 | getFirst() { 67 | return this.head; 68 | } 69 | 70 | insertFirst(data) { 71 | this.head = new Node(data, this.getFirst()); 72 | } 73 | 74 | getLast() { 75 | let node = this.getFirst(); 76 | while(node.next) { 77 | node = node.next; 78 | } 79 | return node; 80 | } 81 | 82 | insertLast(data) { 83 | let node = this.getFirst(); 84 | if(!node) { 85 | this.head = new Node(data); 86 | return; 87 | } 88 | while(node.next) { 89 | node = node.next; 90 | } 91 | node.next = new Node(data); 92 | } 93 | 94 | removeFirst() { 95 | this.head = this.head.next; 96 | } 97 | 98 | removeLast() { 99 | let node = this.getFirst(); 100 | 101 | if (!node) { 102 | return; 103 | } 104 | 105 | if (!node.next) { 106 | this.head = null; 107 | return; 108 | } 109 | 110 | while(node.next.next) { 111 | node = node.next; 112 | } 113 | node.next = null; 114 | } 115 | 116 | forEach(callback) { 117 | let node = this.getFirst(); 118 | let counter = 0; 119 | while(node) { 120 | callback(node, counter); 121 | node = node.next; 122 | counter++; 123 | } 124 | } 125 | 126 | *[Symbol.iterator]() { 127 | let node = this.head; 128 | while(node) { 129 | yield node; 130 | node = node.next; 131 | } 132 | } 133 | 134 | } 135 | 136 | module.exports = { Node, LinkedList }; 137 | -------------------------------------------------------------------------------- /src-javascript/binarysearch.js: -------------------------------------------------------------------------------- 1 | function binarySearch(arr, el){ 2 | var beg = 0; 3 | var end = arr.length - 1; 4 | while(end >= beg){ 5 | var mid = Math.floor((end + beg) / 2); 6 | if(arr[mid] === el) return mid; 7 | else if(arr[mid] < el) beg = mid + 1; 8 | else end = mid - 1; 9 | } 10 | return -1; 11 | } 12 | 13 | var sortedArray = [2,3,5,6,7,8,10,20,28,30,49,56,67,80]; 14 | console.log("Pos of 10: " + binarySearch(sortedArray, 10)); 15 | -------------------------------------------------------------------------------- /src-python/stack.py: -------------------------------------------------------------------------------- 1 | class Stack(): 2 | def __init__(self): 3 | self.l = [] 4 | def push(self,val): 5 | self.l.append(val) 6 | def pop(self): 7 | self.l.pop() 8 | def show(self): 9 | for i in self.l[::-1]: 10 | if i<10: 11 | print('| ',i,' |') 12 | else: 13 | print('| ',i,' |') 14 | print('|_______|') 15 | 16 | # To test this module 17 | # s = Stack() 18 | # s.push() 19 | # s.pop() 20 | # s.show() 21 | --------------------------------------------------------------------------------