├── README.md ├── PalindromeCheckViaStack.cpp ├── BubbleSort.cpp ├── InsertionSort.cpp ├── SinglyLinkedList.cpp ├── BinarySearch.cpp ├── PostFix.cpp ├── SelectionSort.cpp ├── SequentialSearch.cpp ├── Hashing.cpp ├── MergeSort.cpp ├── Stack.cpp ├── BinaryTree.cpp └── LinkedListDisplayAssignGrade.cpp /README.md: -------------------------------------------------------------------------------- 1 | ## Data Structures 2 | 3 | I'll keep on adding codes as I implement what I learn :) 4 | 5 | -------------------------------------------------------------------------------- /PalindromeCheckViaStack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main(){ 8 | string string; 9 | cout<<"Enter a string: "; 10 | cin>>string; 11 | stack st; 12 | for(int i=0;i 2 | using namespace std; 3 | int main () 4 | { 5 | int i, j,temp,pass=0; 6 | int a[10] = {10,2,0,14,43,25,18,1,5,45}; 7 | cout <<"Input list:\n"; 8 | for(i = 0; i<10; i++) { 9 | cout < 2 | using namespace std; 3 | int main () 4 | { 5 | int myarray[10] = { 12,4,3,1,15,45,33,21,10,2}; 6 | 7 | cout<<"\nInput list is \n"; 8 | for(int i=0;i<10;i++) 9 | { 10 | cout <=0 && temp <= myarray[j]) 17 | { 18 | myarray[j+1] = myarray[j]; 19 | j = j-1; 20 | } 21 | myarray[j+1] = temp; 22 | } 23 | cout<<"\nSorted list is \n"; 24 | for(int i=0;i<10;i++) 25 | { 26 | cout < 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | }; 8 | 9 | struct Node* head = NULL; 10 | 11 | void insert(int new_data) { 12 | struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); 13 | new_node->data = new_data; 14 | new_node->next = head; 15 | head = new_node; 16 | } 17 | 18 | void display() { 19 | struct Node* ptr; 20 | ptr = head; 21 | while (ptr != NULL) { 22 | cout<< ptr->data <<" "; 23 | ptr = ptr->next; 24 | } 25 | } 26 | 27 | int main() { 28 | insert(1); 29 | insert(3); 30 | insert(2); 31 | insert(2); 32 | insert(7); 33 | 34 | cout<<"The linked list is: "; 35 | display(); 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int size,beg,mid,end,i,num; 7 | cout << "Enter the size of array:\n "; 8 | cin >> size; 9 | 10 | int array[size]; 11 | cout << "Enter the elements of a sorted array:\n"; 12 | 13 | for(i = 0; i < size ;i++){ 14 | cin >> array[i];} 15 | 16 | beg = 0; 17 | end = size-1; 18 | 19 | cout << "\n Enter a value to be searched in an array "; 20 | cin >> num; 21 | 22 | while( beg <= end){ 23 | mid = (beg+end)/2; 24 | if(array[mid] == num) { 25 | cout << "\nItem found at position "<< (mid+1); 26 | break;} 27 | else if(num > array[mid]) { 28 | beg=mid+1;} 29 | else if (num < array[mid]) { 30 | end=mid-1;} 31 | else{ 32 | cout << "Number does not found.";} 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /PostFix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int evalPostfix(){ 5 | int stack[100]; 6 | int top = -1; 7 | char postfix[100]; 8 | cout<<"Enter postfix expression: "; 9 | cin>>postfix; 10 | for(int i=0;postfix[i]!='\0';i++){ 11 | if(postfix[i]>='0' && postfix[i]<='9'){ 12 | stack[++top] = postfix[i]-'0'; 13 | } 14 | else{ 15 | int a = stack[top--]; 16 | int b = stack[top--]; 17 | switch(postfix[i]){ 18 | case '+': 19 | stack[++top] = a+b; 20 | break; 21 | case '-': 22 | stack[++top] = a-b; 23 | break; 24 | case '*': 25 | stack[++top] = a*b; 26 | break; 27 | case '/': 28 | stack[++top] = a/b; 29 | break; 30 | } 31 | } 32 | } 33 | return stack[top]; 34 | } 35 | 36 | int main(){ 37 | cout< 2 | using namespace std; 3 | 4 | int findingSmallest (int[],int); 5 | 6 | int findingSmallest(int list[],int i) 7 | { 8 | int small_ele,position,j; 9 | small_ele = list[i]; 10 | position = i; 11 | for(j=i+1;j<10;j++) 12 | { 13 | if(list[j] < small_ele) 14 | { 15 | small_ele = list[j]; 16 | position=j; 17 | } 18 | } 19 | return position; 20 | } 21 | 22 | int main () 23 | { 24 | int list[10] = {19,6,2,30,52,43,24,37,201,29}; 25 | int pos,temp,pass=0; 26 | cout<<"Input list of elements to be sorted: "; 27 | for(int i=0;i<10;i++) 28 | { 29 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int n, position, value, choice,del, found = 0; 6 | cout<<"How many elements do you want to enter in this list? "<>n; 8 | cout<<"\nEnter the elements: "<>list[i]; 12 | } 13 | 14 | cout<< "\nWhat do you want to do? Enter 1 to insert an element, Enter 2 to del an element: "<>choice; 16 | 17 | 18 | if (choice == 1) 19 | { 20 | cout<<"\nEnter the location where you wish to insert an element: "<>position; 22 | cout<<"\nEnter the value to insert: "<>value; 24 | 25 | list[position] = value; 26 | 27 | cout<<"\nResultant array is: "<>del; 36 | 37 | list[del] = '\0'; 38 | 39 | cout<<"\nResultant array is: "< 2 | using namespace std; 3 | 4 | void createHashTable(int *arr, int size) 5 | { 6 | int hashTable[13] = {0}; 7 | for (int i = 0; i < size; i++) 8 | { 9 | int key = arr[i] % 13; 10 | hashTable[key] = arr[i]; 11 | } 12 | cout << "The hash table with collision issue is: " << endl; 13 | for (int i = 0; i < 13; i++) 14 | { 15 | cout << hashTable[i] << " "; 16 | } 17 | cout << endl; 18 | } 19 | 20 | void resolveCollision(int *arr, int size) 21 | { 22 | int hashTable[13] = {0}; 23 | for (int i = 0; i < size; i++) 24 | { 25 | int key = arr[i] % 13; 26 | if (hashTable[key] == 0) 27 | { 28 | hashTable[key] = arr[i]; 29 | } 30 | else 31 | { 32 | int j = 1; 33 | while (hashTable[(key + j) % 13] != 0) 34 | { 35 | j++; 36 | } 37 | hashTable[(key + j) % 13] = arr[i]; 38 | } 39 | } 40 | cout << "The hash table with no collision is: " << endl; 41 | for (int i = 0; i < 13; i++) 42 | { 43 | cout << hashTable[i] << " "; 44 | } 45 | cout << endl; 46 | } 47 | 48 | int main(){ 49 | int arr[8] = {18, 41, 22, 44, 59, 32, 31, 73}; 50 | createHashTable(arr, 8); 51 | resolveCollision(arr, 8); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /MergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void merge(int *,int, int , int ); 5 | 6 | void merge_sort(int *arr, int low, int high) 7 | { 8 | int mid; 9 | if (low < high){ 10 | 11 | mid=(low+high)/2; 12 | merge_sort(arr,low,mid); 13 | merge_sort(arr,mid+1,high); 14 | 15 | merge(arr,low,high,mid); 16 | } 17 | } 18 | 19 | void merge(int *arr, int low, int high, int mid) 20 | { 21 | int i, j, k, c[50]; 22 | i = low;4*/8*4 23 | k = low; 24 | j = mid + 1; 25 | while (i <= mid && j <= high) { 26 | if (arr[i] < arr[j]) { 27 | c[k] = arr[i]; 28 | k++; 29 | i++; 30 | } 31 | else { 32 | c[k] = arr[j]; 33 | k++; 34 | j++; 35 | } 36 | } 37 | while (i <= mid) { 38 | c[k] = arr[i]; 39 | k++; 40 | i++; 41 | } 42 | while (j <= high) { 43 | c[k] = arr[j]; 44 | k++; 45 | j++; 46 | } 47 | for (i = low; i < k; i++) { 48 | arr[i] = c[i]; 49 | } 50 | } 51 | 52 | int main() 53 | { 54 | int myarray[30], num; 55 | cout<<"Enter number of elements to be sorted:"; 56 | cin>>num; 57 | cout<<"Enter "<>myarray[i]; 60 | } 61 | merge_sort(myarray, 0, num-1); 62 | cout<<"Sorted array\n"; 63 | for (int i = 0; i < num; i++) 64 | { 65 | cout< 2 | 3 | using namespace std; 4 | 5 | int MAX_SIZE = 10, top= MAX_SIZE-1, stack[10] = { 1,2,3,4,5,6,7,8,9}; 6 | 7 | void push() { 8 | int value; 9 | if(top > MAX_SIZE){ 10 | cout << "\nStack is Full!!!"; 11 | } 12 | else { 13 | cout << "\nEnter The Value to be pushed : "; 14 | cin>>value; 15 | cout << "\nPosition : " << top << ", Pushed Value :" << value; 16 | stack[top++] = value; 17 | } 18 | } 19 | 20 | void pop() { 21 | if(top == 0) 22 | cout<<"Stack is Empty!!!"<=0) { 31 | cout<<"Stack elements are:"; 32 | for(int i=top; i>=0; i--){ 33 | cout<>ch; 52 | switch(ch) { 53 | case 1: { 54 | push(); 55 | break; 56 | } 57 | case 2: { 58 | pop(); 59 | break; 60 | } 61 | case 3: { 62 | display(); 63 | break; 64 | } 65 | case 4: { 66 | cout<<"\n\t\tExited Program!"< 2 | using namespace std; 3 | 4 | 5 | //Build a binary tree, where N = 11 6 | /* 7 | 1 8 | / \ 9 | 2 3 10 | / \ / \ 11 | 4 5 6 7 12 | / \ / \ 13 | 8 9 10 11 14 | 15 | */ 16 | struct Node{ 17 | int key; 18 | Node* left; 19 | Node* right; 20 | }; 21 | 22 | Node* newNode(int key){ 23 | Node* node = new Node; 24 | node->key = key; 25 | node->left = node->right = NULL; 26 | return node; 27 | } 28 | 29 | Node* buildTree(){ 30 | Node* root = newNode(1); 31 | root->left = newNode(2); 32 | root->right = newNode(3); 33 | root->left->left = newNode(4); 34 | root->left->right = newNode(5); 35 | root->right->left = newNode(6); 36 | root->right->right = newNode(7); 37 | root->left->left->left = newNode(8); 38 | root->left->left->right = newNode(9); 39 | root->left->right->left = newNode(10); 40 | root->left->right->right = newNode(11); 41 | return root; 42 | } 43 | 44 | /*Print inorder*/ 45 | void inorder(Node* root){ 46 | if(root==NULL) return; 47 | inorder(root->left); 48 | cout<key<<" "; 49 | inorder(root->right); 50 | } 51 | 52 | /*Print postorder*/ 53 | void postorder(Node* root){ 54 | if(root==NULL) return; 55 | postorder(root->left); 56 | postorder(root->right); 57 | cout<key<<" "; 58 | } 59 | 60 | /*Print preorder*/ 61 | void preorder(Node* root){ 62 | if(root==NULL) return; 63 | cout<key<<" "; 64 | preorder(root->left); 65 | preorder(root->right); 66 | } 67 | 68 | int main(){ 69 | Node* root = buildTree(); 70 | cout<<"Inorder: "; 71 | inorder(root); 72 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct node 7 | { 8 | char *name; 9 | int marks; 10 | node *next; 11 | }; 12 | 13 | struct node *head=NULL; 14 | 15 | //Function to insert a node 16 | void insertNode(char *name,int marks){ 17 | struct node *newNode=(struct node*)malloc(sizeof(struct node)); 18 | newNode->name = name; 19 | newNode->marks = marks; 20 | newNode->next = head; 21 | head=newNode; 22 | } 23 | 24 | //Function to display all data and assign grades 25 | void SummaryReport(){ 26 | struct node *temp=head; 27 | int a=0,b=0,c=0,d=0,e=0,j=0,k=0,l=0,m=0,f=0, pass; 28 | while(temp!=NULL){ 29 | if(temp->marks>=90){ 30 | cout<<"Name: "<< temp->name<<"\t"<<"Marks: "<marks<<"\t"<<"Grade: A"<marks>=85 && temp->marks<90){ 34 | cout<<"Name: "<< temp->name<<"\t"<<"Marks: "<marks<<"\t"<<"Grade: A-"<marks>=80 && temp->marks<85){ 38 | cout<<"Name: "<< temp->name<<"\t"<<"Marks: "<marks<<"\t"<<"Grade: B+"<marks>=75 && temp->marks<80){ 42 | cout<<"Name: "<< temp->name<<"\t"<<"Marks: "<marks<<"\t"<<"Grade: B"<marks>=70 && temp->marks<75){ 46 | cout<<"Name: "<< temp->name<<"\t"<<"Marks: "<marks<<"\t"<<"Grade: B-"<marks>=65 && temp->marks<70){ 50 | cout<<"Name: "<< temp->name<<"\t"<<"Marks: "<marks<<"\t"<<"Grade: C+"<marks>=60 && temp->marks<65){ 54 | cout<<"Name: "<< temp->name<<"\t"<<"Marks: "<marks<<"\t"<<"Grade: C"<marks>=55 && temp->marks<60){ 58 | cout<<"Name: "<< temp->name<<"\t"<<"Marks: "<marks<<"\t"<<"Grade: C-"<marks>=50 && temp->marks<55){ 62 | cout<<"Name: "<< temp->name<<"\t"<<"Marks: "<marks<<"\t"<<"Grade: D"<name<<"\t"<<"Marks: "<marks<<"\t"<<"Grade: F"<next; 70 | } 71 | 72 | cout<<"\n\n\t\tSummary Report"<