diff --git a/Datastructures/Binary Search Tree.cpp b/Datastructures/Binary Search Tree.cpp new file mode 100644 index 000000000..5b83263bc --- /dev/null +++ b/Datastructures/Binary Search Tree.cpp @@ -0,0 +1,232 @@ +#include +using namespace std; + + +struct node +{ + int val; + node *left; + node *right; +}; + +struct queue +{ + node *t[100]; + int front; + int rear; +}; + +queue q; + + +void enqueue(node *n) +{ + q.t[q.rear++]=n; +} + +node * dequeue() +{ + return (q.t[q.front++]); +} + + + +void Insert(node *n, int x) +{ + if (xval) + { + if (n->left==NULL) + { + node *temp=new node; + temp->val=x; + temp->left=NULL; + temp->right=NULL; + n->left=temp; + } + else + { + Insert(n->left, x); + } + } + else + { + if (n->right==NULL) + { + node *temp=new node; + temp->val=x; + temp->left=NULL; + temp->right=NULL; + n->left=temp; + } + else + { + Insert(n->right, x); + } + } +} + + + + +int findMaxInLeftST(node *n) +{ + while(n->right!=NULL) + { + n=n->right; + } + return n->val; +} + +void Remove(node *p, node *n, int x) +{ + if (n->val==x) + { + if (n->right==NULL && n->left==NULL) + { + if (xval) + { + p->right=NULL; + } + else + { + p->left=NULL; + } + } + else if (n->right==NULL) + { + if (xval) + { + p->right=n->left; + } + else + { + p->left=n->left; + } + } + else if (n->left==NULL) + { + if (xval) + { + p->right=n->right; + } + else + { + p->left=n->right; + } + } + else + { + int y=findMaxInLeftST(n->left); + n->val=y; + Remove(n, n->right, y); + } + } + else if (xval) + { + Remove(n, n->left, x); + } + else + { + Remove(n, n->right, x); + } +} + + + + +void BFT(node *n) +{ + if(n!=NULL) + { + cout<val<<" "; + enqueue(n->left); + enqueue(n->right); + BFT(dequeue()); + } +} + +void Pre(node *n) +{ + if (n!=NULL) + { + cout<val<<" "; + Pre(n->left); + Pre(n->right); + } +} + +void In(node *n) +{ + if (n!=NULL) + { + In(n->left); + cout<val<<" "; + In(n->right); + } +} + + +void Post(node *n) +{ + if (n!=NULL) + { + Post(n->left); + Post(n->right); + cout<val<<" "; + } +} + + + +int main() +{ + q.front=0; + q.rear=0; + int value; + int ch; + node *root=new node; + cout<<"\nEnter the value of root node :"; + cin>>value; + root->val=value; + root->left=NULL; + root->right=NULL; + do + { + cout<<"\n1. Insert"; + cout<<"\n2. Delete"; + cout<<"\n3. Breadth First"; + cout<<"\n4. Preorder Depth First"; + cout<<"\n5. Inorder Depth First"; + cout<<"\n6. Postorder Depth First"; + + cout<<"\nEnter Your Choice : "; + cin>>ch; + int x; + switch(ch) + { + case 1: + cout<<"\nEnter the value to be Inserted : "; + cin>>x; + Insert(root, x); + break; + case 2: + cout<<"\nEnter the value to be Deleted : "; + cin>>x; + Remove(root, root, x); + break; + case 3: + BFT(root); + break; + case 4: + Pre(root); + break; + case 5: + In(root); + break; + case 6: + Post(root); + break; + } + } + while(ch!=0); +} diff --git a/Datastructures/Doubly Linked List.cpp b/Datastructures/Doubly Linked List.cpp new file mode 100644 index 000000000..db550d422 --- /dev/null +++ b/Datastructures/Doubly Linked List.cpp @@ -0,0 +1,125 @@ +#include +using namespace std; + +struct node +{ + int val; + node *prev; + node *next; +}; + +node *start; + +void insert(int x) +{ + node *t=start; + if (start!=NULL) + { + while(t->next!=NULL) + { + t=t->next; + } + node *n= new node; + t->next=n; + n->prev=t; + n->val=x; + n->next=NULL; + } + else + { + node *n= new node; + n->val=x; + n->prev=NULL; + n->next=NULL; + start=n; + } +} + +void remove(int x) +{ + node *t=start; + while(t->val!=x) + { + t=t->next; + } + t->prev->next=t->next; + t->next->prev=t->prev; + delete t; +} + +void search(int x) +{ + node *t= start; + int found =0; + while(t!=NULL) + { + if(t->val==x) + { + cout<<"\nFound"; + found=1; + break; + } + t=t->next; + } + if(found==0) + { + cout<<"\nNot Found"; + } +} + +void show() +{ + node *t=start; + while(t!=NULL) + { + cout<val<<"\t"; + t=t->next; + } + +} + +void reverseShow() +{ + node *t=start; + while(t->next!=NULL) + { + t=t->next; + } + while(t!=NULL) + { + cout<val<<"\t"; + t=t->prev; + } +} + +int main() +{ + int choice, x; + do + { + cout<<"\n1. Insert"; + cout<<"\n2. Delete"; + cout<<"\n3. Search"; + cout<<"\n4. Forward print"; + cout<<"\n5. Reverse print"; + cout<<"\n\nEnter you choice : "; + cin>>choice; + switch (choice) + { + case 1 : cout<<"\nEnter the element to be inserted : "; + cin>>x;; + insert(x); break; + case 2 : cout<<"\nEnter the element to be removed : "; + cin>>x; + remove(x); break; + case 3 : cout<<"\nEnter the element to be searched : "; + cin>>x; + search(x); break; + case 4 : show(); break; + case 5 : reverseShow(); break; + } + } + while(choice!=0); + + return 0; +} diff --git a/Datastructures/Linked List.cpp b/Datastructures/Linked List.cpp new file mode 100644 index 000000000..9319249fd --- /dev/null +++ b/Datastructures/Linked List.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +struct node +{ + int val; + node *next; +}; + +node *start; + +void insert(int x) +{ + node *t=start; + if (start!=NULL) + { + while(t->next!=NULL) + { + t=t->next; + } + node *n= new node; + t->next=n; + n->val=x; + n->next=NULL; + } + else + { + node *n= new node; + n->val=x; + n->next=NULL; + start=n; + } +} + +void remove(int x) +{ + node *t=start; + node *p; + while(t->val!=x) + { + p=t; + t=t->next; + } + p->next=t->next; + delete t; +} + +void search(int x) +{ + node *t= start; + int found =0; + while(t!=NULL) + { + if(t->val==x) + { + cout<<"\nFound"; + found=1; + break; + } + t=t->next; + } + if(found==0) + { + cout<<"\nNot Found"; + } +} + +void show() +{ + node *t=start; + while(t!=NULL) + { + cout<val<<"\t"; + t=t->next; + } + +} + +int main() +{ + int choice, x; + do + { + cout<<"\n1. Insert"; + cout<<"\n2. Delete"; + cout<<"\n3. Search"; + cout<<"\n4. Print"; + cout<<"\n\nEnter you choice : "; + cin>>choice; + switch (choice) + { + case 1 : cout<<"\nEnter the element to be inserted : "; + cin>>x;; + insert(x); break; + case 2 : cout<<"\nEnter the element to be removed : "; + cin>>x; + remove(x); break; + case 3 : cout<<"\nEnter the element to be searched : "; + cin>>x; + search(x); break; + case 4 : show(); break; + } + } + while(choice!=0); + + return 0; +} diff --git a/Datastructures/List Array.cpp b/Datastructures/List Array.cpp new file mode 100644 index 000000000..21f5c6ae5 --- /dev/null +++ b/Datastructures/List Array.cpp @@ -0,0 +1,185 @@ +#include +using namespace std; + +struct list +{ + int data[50]; + int top=0; + bool isSorted=false; + + int BinarySearch(int *array, int first, int last, int x) + { + if(lastarray[mid]) + return (BinarySearch(array, mid+1, last, x)); + } + + int LinarSearch(int *array, int x) + { + for (int i = 0; i < top; i++) + { + if (array[i]==x) + { + return i; + } + } + + return -1; + } + + int Search(int x) + { + int pos=-1; + + if (isSorted) + { + pos=BinarySearch(data, 0, top-1, x); + } + + else + { + pos=LinarSearch(data, x); + } + + if (pos!=-1) + { + cout<<"\nElement found at position : "< pos; i--) + { + data[i]=data[i-1]; + } + top++; + data[pos]=x; + } + } + + void Remove(int x) + { + int pos=Search(x); + cout<<"\n"<>choice; + switch (choice) + { + case 1: cout<<"\nEnter the element to be inserted : "; + cin>>x; + L.insert(x); + break; + case 2: cout<<"\nEnter the element to be removed : "; + cin>>x; + L.Remove(x); + break; + case 3: cout<<"\nEnter the element to be searched : "; + cin>>x; + L.Search(x); + break; + case 4: L.Sort(); + break; + case 5: L.Show(); + break; + } + } + while(choice!=0); + return 0; +} diff --git a/Datastructures/Queue Using Array.cpp b/Datastructures/Queue Using Array.cpp new file mode 100644 index 000000000..7b5816f84 --- /dev/null +++ b/Datastructures/Queue Using Array.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +int queue[10]; +int front=0; +int rear=0; + +void Enque(int x) +{ + if(rear==10) + { + cout<<"\nOverflow"; + } + else + { + queue[rear++]=x; + } +} + +void Deque() +{ + if (front==rear) + { + cout<<"\nUnderflow"; + } + + else + { + cout<<"\n"<>ch; + if (ch==1) + { + cout<<"\nInsert : "; + cin>>x; + Enque(x); + } + else if (ch==2) + { + Deque(); + } + else if (ch==3) + { + show(); + } + } + while(ch!=0); + + return 0; +} + diff --git a/Datastructures/Queue Using Linked List.cpp b/Datastructures/Queue Using Linked List.cpp new file mode 100644 index 000000000..acf32ebd5 --- /dev/null +++ b/Datastructures/Queue Using Linked List.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + +struct node +{ + int val; + node *next; +}; + + +node *front, *rear; + + +void Enque(int x) +{ + if (rear==NULL) + { + node *n= new node; + n->val=x; + n->next=NULL; + rear=n; + front=n; + } + + else + { + + node *n = new node; + n->val=x; + n->next=NULL; + rear->next=n; + rear=n; + } +} + +void Deque() +{ + if (rear==front) + { + cout<<"\nUnderflow"; + } + else + { + node *t = front; + cout<<"\n"<val<<" deleted"; + front=front->next; + delete t; + } +} + +void show() +{ + node *t=front; + while(t!=NULL) + { + cout<val<<"\t"; + t=t->next; + } +} + +int main() +{ + int ch, x; + do + { + cout<<"\n1. Enque"; + cout<<"\n2. Deque"; + cout<<"\n3. Print"; + cout<<"\nEnter Your Choice : "; + cin>>ch; + if (ch==1) + { + cout<<"\nInsert : "; + cin>>x; + Enque(x); + } + else if (ch==2) + { + Deque(); + } + else if (ch==3) + { + show(); + } + } + while(ch!=0); + + return 0; +} + diff --git a/Datastructures/Stack Using Array.cpp b/Datastructures/Stack Using Array.cpp new file mode 100644 index 000000000..944258ed4 --- /dev/null +++ b/Datastructures/Stack Using Array.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; + +int stack[10]; +int top=0; + +void push(int x) +{ + if(top==10) + { + cout<<"\nOverflow"; + } + else + { + stack[top++]=x; + } +} + +void pop() +{ + if (top==0) + { + cout<<"\nUnderflow"; + } + else + { + cout<<"\n"<>ch; + if (ch==1) + { + cout<<"\nInsert : "; + cin>>x; + push(x); + } + else if (ch==2) + { + pop(); + } + else if (ch==3) + { + show(); + } + else if(ch==4) + { + topmost(); + } + } + while(ch!=0); + + return 0; +} + diff --git a/Datastructures/Stack Using Linked List.cpp b/Datastructures/Stack Using Linked List.cpp new file mode 100644 index 000000000..753fda8ed --- /dev/null +++ b/Datastructures/Stack Using Linked List.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; + +struct node +{ + int val; + node *next; +}; + + +node *top; + +void push(int x) +{ + node *n = new node; + n->val=x; + n->next=top; + top=n; +} + +void pop() +{ + if (top==NULL) + { + cout<<"\nUnderflow"; + } + else + { + node *t = top; + cout<<"\n"<val<<" deleted"; + top=top->next; + delete t; + } +} + +void show() +{ + node *t=top; + while(t!=NULL) + { + cout<val<<"\n"; + t=t->next; + } +} + +int main() +{ + int ch, x; + do + { + cout<<"\n1. Push"; + cout<<"\n2. Pop"; + cout<<"\n3. Print"; + cout<<"\nEnter Your Choice : "; + cin>>ch; + if (ch==1) + { + cout<<"\nInsert : "; + cin>>x; + push(x); + } + else if (ch==2) + { + pop(); + } + else if (ch==3) + { + show(); + } + } + while(ch!=0); + + return 0; +} + diff --git a/Datastructures/Tree.cpp b/Datastructures/Tree.cpp new file mode 100644 index 000000000..963779abe --- /dev/null +++ b/Datastructures/Tree.cpp @@ -0,0 +1,160 @@ +#include +using namespace std; + + +struct node +{ + int val; + node *left; + node *right; +}; + +struct queue +{ + node *t[100]; + int front; + int rear; +}; + +queue q; + + +void enqueue(node *n) +{ + q.t[q.rear++]=n; +} + +node * dequeue() +{ + return (q.t[q.front++]); +} + + + +void CreateTree(node *curr, node *n, int x, char pos) +{ + if(n!=NULL) + { + char ch; + cout<<"\nLeft or Right of "<val<<" : "; + cin>>ch; + if(ch=='l') + CreateTree(n, n->left, x, ch); + else if(ch=='r') + CreateTree(n, n->right, x, ch); + } + + else + { + node *t=new node; + t->val=x; + t->left=NULL; + t->right=NULL; + if (pos=='l') + { + curr->left=t; + } + else if(pos=='r') + { + curr->right=t; + } + } +} + + +void BFT(node *n) +{ + if(n!=NULL) + { + cout<val<<" "; + enqueue(n->left); + enqueue(n->right); + BFT(dequeue()); + } +} + +void Pre(node *n) +{ + if (n!=NULL) + { + cout<val<<" "; + Pre(n->left); + Pre(n->right); + } +} + +void In(node *n) +{ + if (n!=NULL) + { + In(n->left); + cout<val<<" "; + In(n->right); + } +} + + +void Post(node *n) +{ + if (n!=NULL) + { + Post(n->left); + Post(n->right); + cout<val<<" "; + } +} + + + +int main() +{ + q.front=0; + q.rear=0; + int value; + int ch; + node *root=new node; + cout<<"\nEnter the value of root node :"; + cin>>value; + root->val=value; + root->left=NULL; + root->right=NULL; + do + { + cout<<"\n1. Insert : "; + cout<<"\n2. Breadth First"; + cout<<"\n3. Preorder Depth First"; + cout<<"\n4. Inorder Depth First"; + cout<<"\n5. Postorder Depth First"; + + cout<<"\nEnter Your Choice : "; + cin>>ch; + switch(ch) + { + case 1: + int x; + char pos; + cout<<"\nEnter the value to be Inserted : "; + cin>>x; + cout<<"\nLeft or Right of Root : "; + cin>>pos; + if(pos=='l') + CreateTree(root, root->left, x, pos); + else if(pos=='r') + CreateTree(root, root->right, x, pos); + break; + case 2: + BFT(root); + break; + case 3: + Pre(root); + break; + case 4: + In(root); + break; + case 5: + Post(root); + break; + } + } + while(ch!=0); +} diff --git a/Operations on Datastructures/Array Left Rotation.cpp b/Operations on Datastructures/Array Left Rotation.cpp new file mode 100644 index 000000000..177dada01 --- /dev/null +++ b/Operations on Datastructures/Array Left Rotation.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +int main(){ + int n,k; + cout<<"Enter size of array=\t"; + cin>>n; + cout<<"Enter Number of indeces u want to rotate the array to left=\t"; + cin>>k; + int a[n]; + cout<<"Enter elements of array=\t"; + for(int i=0;i>a[i]; + } + int temp=0; + for(int i=0;i +using namespace std; +int main(){ + int n,k; + cout<<"Enter size of array=\t"; + cin>>n; + cout<<"Enter Number of indices u want to rotate the array to right=\t"; +cin>>k; +int a[n]; +cout<<"Enter elements of array=\t"; + for(int i=0;i>a[i]; + int temp=0; + for(int i=0;i=0;j--){ + if(j==0){ + a[j]=temp; + } + else{ + a[j]=a[j-1];} + + } + + } + cout<<"Your rotated array is=\t"; + for(int i=0;i +using namespace std; + +struct node +{ + int val; + node *next; +}; + +node *start; + +void insert(int x) +{ + node *t=start; + + if (start!=NULL) + { + while(t->next!=start) + { + t=t->next; + } + node *n= new node; + t->next=n; + n->val=x; + n->next=start; + } + else + { + node *n= new node; + n->val=x; + start=n; + n->next=start; + } +} + +void remove(int x) +{ + node *t=start; + node *p; + while(t->val!=x) + { + p=t; + t=t->next; + } + p->next=t->next; + delete t; +} + +void search(int x) +{ + node *t= start; + int found =0; + while(t->next!=start) + { + if(t->val==x) + { + cout<<"\nFound"; + found=1; + break; + } + t=t->next; + } + if(found==0) + { + cout<<"\nNot Found"; + } +} + +void show() +{ + node *t=start; + do + { + cout<val<<"\t"; + t=t->next; + } + while(t!=start); + +} + +int main() +{ + int choice, x; + do + { + cout<<"\n1. Insert"; + cout<<"\n2. Delete"; + cout<<"\n3. Search"; + cout<<"\n4. Print"; + cout<<"\n\nEnter you choice : "; + cin>>choice; + switch (choice) + { + case 1 : cout<<"\nEnter the element to be inserted : "; + cin>>x; + insert(x); break; + case 2 : cout<<"\nEnter the element to be removed : "; + cin>>x; + remove(x); break; + case 3 : cout<<"\nEnter the element to be searched : "; + cin>>x; + search(x); break; + case 4 : show(); break; + } + } + while(choice!=0); + + return 0; +} diff --git a/Operations on Datastructures/Circular Queue Using Array.cpp b/Operations on Datastructures/Circular Queue Using Array.cpp new file mode 100644 index 000000000..d91f31af3 --- /dev/null +++ b/Operations on Datastructures/Circular Queue Using Array.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; + +int queue[10]; +int front=0; +int rear=0; +int count=0; + +void Enque(int x) +{ + if(count==10) + { + cout<<"\nOverflow"; + } + else + { + queue[rear]=x; + rear=(rear+1)%10; + count++; + } +} + +void Deque() +{ + if (front==rear) + { + cout<<"\nUnderflow"; + } + + else + { + cout<<"\n"<>ch; + if (ch==1) + { + cout<<"\nInsert : "; + cin>>x; + Enque(x); + } + else if (ch==2) + { + Deque(); + } + else if (ch==3) + { + show(); + } + } + while(ch!=0); + + return 0; +} + diff --git a/Operations on Datastructures/Intersection_of_2_arrays.cpp b/Operations on Datastructures/Intersection_of_2_arrays.cpp new file mode 100644 index 000000000..e2c064e1b --- /dev/null +++ b/Operations on Datastructures/Intersection_of_2_arrays.cpp @@ -0,0 +1,30 @@ +#include +int main() +{ + int i,j,m,n; + cout <<"Enter size of array 1:"; + cin >> m; + cout <<"Enter size of array 2:"; + cin >> n; + int a[m]; + int b[n]; + cout <<"Enter elements of array 1:"; + for(i=0;i> a[i]; + for(i=0;i> b[i]; + i=0;j=0; + while((ib[j]) + j++; + else + { + cout << a[i++]<<" "; + j++; + } + } + return 0; +} diff --git a/Operations on Datastructures/Reverse a Linked List using Recusion.cpp b/Operations on Datastructures/Reverse a Linked List using Recusion.cpp new file mode 100644 index 000000000..daefa18e8 --- /dev/null +++ b/Operations on Datastructures/Reverse a Linked List using Recusion.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + +struct node +{ + int val; + node *next; +}; + +node *start; + +void insert(int x) +{ + node *t=start; + if (start!=NULL) + { + while(t->next!=NULL) + { + t=t->next; + } + node *n= new node; + t->next=n; + n->val=x; + n->next=NULL; + } + else + { + node *n= new node; + n->val=x; + n->next=NULL; + start=n; + } +} + +void reverse(node *p, node *q) +{ + if (q->next == NULL) + { + q->next=p; + p->next=NULL; + start=q; + return; + } + else + { + reverse(q, q->next); + q->next=p; + p->next=NULL; + } + +} + + +void show() +{ + node *t=start; + while(t!=NULL) + { + cout<val<<"\t"; + t=t->next; + } + +} + +int main() +{ + insert(1); + insert(2); + insert(3); + insert(4); + insert(5); + insert(6); + + reverse(start, start->next); + + show(); + + + return 0; +} diff --git a/Operations on Datastructures/Union_of_2_arrays.cpp b/Operations on Datastructures/Union_of_2_arrays.cpp new file mode 100644 index 000000000..4046e1ae8 --- /dev/null +++ b/Operations on Datastructures/Union_of_2_arrays.cpp @@ -0,0 +1,33 @@ +#include +int main() +{ + int m,n,i=0,j=0; + cout << "Enter size of both arrays:"; + cin >> m >> n; + int a[m]; + int b[n]; + cout << "Enter elements of array 1:"; + for(i=0;i>a[i]; + cout << "Enter elements of array 2:"; + for(i=0;i> b[i]; + i=0;j=0; + while((ib[j]) + cout << b[j++] <<" "; + else + { + cout << a[i++]; + j++; + } + } + while(i +using namespace std; +int main() +{ + int n,t; + cin >> t; + while(t--) + { + cin >> n; + if((n%7==0)||(n%10==7)) + cout << n << " is a buzz number" << endl; + else + cout << n << " is not a buzz number" << endl; + } + return 0; +} diff --git a/Others/Decimal To Binary.cpp b/Others/Decimal To Binary.cpp new file mode 100644 index 000000000..3794d2a96 --- /dev/null +++ b/Others/Decimal To Binary.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +int main() +{ + int number; + cin>>number; + int remainder,binary=0,var=1; + +do{ + + remainder=number%2; + number=number/2; + binary=binary+(remainder*var); + var=var*10; + + +} +while(number>0); + cout< + +using namespace std; + +void main(void) +{ + int valueToConvert = 0; //Holds user input + int hexArray[8]; //Contains hex values backwards + int i = 0; //counter + int lValue = 0; //Last Value of Hex result + + cout << "Enter a Decimal Value" << endl; //Displays request to stdout + cin >> valueToConvert; //Stores value into valueToConvert via user input + + while (valueToConvert > 0) //Dec to Hex Algorithm + { + lValue = valueToConvert % 16; //Gets remainder + valueToConvert = valueToConvert / 16; + hexArray[i] = lValue; //Stores converted values into an array + i++; + } + cout << "Hex Value: "; + while (i > 0) + { + //Displays Hex Letters to stdout + switch (hexArray[i - 1]) { + case 10: + cout << "A"; + break; + case 11: + cout << "B"; + break; + case 12: + cout << "C"; + break; + case 13: + cout << "D"; + break; + case 14: + cout << "E"; + break; + case 15: + cout << "F"; + break; + default: + cout << hexArray[i - 1]; //if not an int 10 - 15, displays int value + } + i--; + } + cout << endl; +} diff --git a/Others/GCD_of_n_numbers.cpp b/Others/GCD_of_n_numbers.cpp new file mode 100644 index 000000000..3e9e9ce17 --- /dev/null +++ b/Others/GCD_of_n_numbers.cpp @@ -0,0 +1,23 @@ +//This program aims at calculating the GCD of n numbers by division method +#include +using namepsace std; +int main() +{ + cout <<"Enter value of n:"<> n; + int a[n]; + int i,j,gcd; + cout << "Enter the n numbers:" << endl; + for(i=0;i> a[i]; + j=1; //to access all elements of the array starting from 1 + gcd=a[0]; + while(j +using namespace std; +int main() +{ + int n,k,s=0,d; + cout << "Enter a number:"; + cin >> n; + s=0;k=n; + while(k>9) + { + while(k!=0) + { + d=k%10; + s+=d; + k/=10; + } + k=s; + s=0; + } + if(k==1) + cout << n << " is a happy number" << endl; + else + cout << n << " is not a happy number" << endl; +} diff --git a/Others/Paranthesis Matching.cpp b/Others/Paranthesis Matching.cpp new file mode 100644 index 000000000..5d37a161e --- /dev/null +++ b/Others/Paranthesis Matching.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +using namespace std; + +char stack[100]; +int top=0; + +void push(char ch) +{ + stack[top++]=ch; +} + +char pop() +{ + return stack[--top]; +} + +bool check(char x, char y) +{ + if ((x=='(' && y==')') || (x=='{' && y=='}') || (x=='[' && y==']') || (x=='<' && y=='>')) + { + return true; + } + else + { + return false; + } +} + + + +int main() +{ + char exp[100]; + cout<<"Enter The Expression : "; + gets(exp); + for (int i = 0; i < strlen(exp); i++) + { + if (exp[i]=='(' || exp[i]=='{' || exp[i]=='[' || exp[i]=='<') + { + push(exp[i]); + } + else if (exp[i]==')' || exp[i]=='}' || exp[i]==']' || exp[i]=='>') + { + if(!check(pop(), exp[i])) + { + cout<<"\nWrong Expression"; + exit(0); + } + } + } + + if(top==0) + { + cout<<"Correct Expression"; + } + else + { + cout<<"\nWrong Expression"; + } + + return 0; +} diff --git a/Others/Sparse matrix.cpp b/Others/Sparse matrix.cpp new file mode 100644 index 000000000..19452dfee --- /dev/null +++ b/Others/Sparse matrix.cpp @@ -0,0 +1,28 @@ +/*A sparse matrix is a matrix which has number of zeroes greater than (m*n)/2, +where m and n are the dimensions of the matrix.*/ +#include +int main() +{ + int m,n,i,j,c=0; + cout << "Enter dimensions of matrix:"; + cin >> m >> n; + int a[m][n]; + cout << "Enter matrix elements:"; + for(i=0;> a[i][j]; + } + for(i=0;i((m*n)/2)) //Checking for sparse matrix + cout << "Sparse matrix"; + else + cout << "Not a sparse matrix"; +} diff --git a/Others/Strassen Matrix Multiplication.cpp b/Others/Strassen Matrix Multiplication.cpp new file mode 100644 index 000000000..1cb398a62 --- /dev/null +++ b/Others/Strassen Matrix Multiplication.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +Multiply(int A[][], int B[][], int n) +{ + if (n==2) + { + int p1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]); + int p2= (a[1][0]+a[1][1])*b[0][0]; + int p3= a[0][0]*(b[0][1]-b[1][1]); + int p4= a[1][1]*(b[1][0]-b[0][0]); + int p5= (a[0][0]+a[0][1])*b[1][1]; + int p6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]); + int p7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]); + + + int c[n][n]; + c[0][0]=p1+p4-p5+p7; + c[0][1]=p3+p5; + c[1][0]=p2+p4; + c[1][1]=p1-p2+p3+p6; + + return c[][]; + } + else + { + + } + +} + +int main() +{ + int p,q,r,s; + cout<<"Enter the dimensions of Matrices"; + cin>>n; + int A[n][n],; + int B[n][n],; + cout<<"Enter the elements of Matrix A"; + for (int i = 0; i < n; i++) + { + for (int j = 0; j >A[i][j]; + } + } + + + cout<<"Enter the elements of Matrix B"; + for (int i = 0; i < n; i++) + { + for (int j = 0; j >B[i][j]; + } + } + + Multiply(A, B, n); + return 0; +} \ No newline at end of file diff --git a/Others/Tower of Hanoi.cpp b/Others/Tower of Hanoi.cpp new file mode 100644 index 000000000..f9b363784 --- /dev/null +++ b/Others/Tower of Hanoi.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + +struct tower +{ + int values[10]; + int top; +}F, U, T; + + + +void show() +{ + cout<<"\n\n\tF : "; + for(int i=0; i> no; + + for (int i = no; i >0; i--) + { + F.values[F.top++]=i; + }; + + + + + + show(); + TH(no, F, U, T); + + + + + + return 0; +} diff --git a/Others/fibonacci.cpp b/Others/fibonacci.cpp new file mode 100644 index 000000000..64c346873 --- /dev/null +++ b/Others/fibonacci.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + + +/* + Efficient method for finding nth term in a fibonacci number sequence. + Uses Dvide and conquer approach + Enter Values from 1 + Eg- + 1st term = 0; + 2nd term = 1; + 3rd term = 1; + . + . + . + . +*/ +int a[2][2] = {{1,1},{1,0}};//fibonacci matrix +int ans[2][2] = {{1,1},{1,0}};//final ans matrix +/* + Working Principal: + + [F(k+1) F(k)] [1 1]^k + | | = | | + [F(k) F(k-1)] [1 0] + + where F(k) is the kth term of the Fibonacci Sequence. + + +*/ + +void product(int b[][2],int k[][2]) +{ + /* + Function for computing product of the two matrices b and k + and storing them into the variable ans. + + Implementation : + Simple matrix multiplication of two (2X2) matrices. + */ + int c[2][2];//temporary stores the answer + c[0][0] = b[0][0]*k[0][0]+b[0][1]*k[1][0]; + c[0][1] = b[0][0]*k[0][1]+b[0][1]*k[1][1]; + c[1][0] = b[1][0]*k[0][0]+b[1][1]*k[1][0]; + c[1][1] = b[1][0]*k[0][1]+b[1][1]*k[1][1]; + ans[0][0] = c[0][0]; + ans[0][1] = c[0][1]; + ans[1][0] = c[1][0]; + ans[1][1] = c[1][1]; + +} + +void power_rec(int n) +{ + + /* + Function for calculating A^n(exponent) in a recursive fashion + + Implementation: + A^n = { A^(n/2)*A^(n/2) if n is even + { A^((n-1)/2)*A^((n-1)/2)*A if n is odd + */ + if((n == 1)||(n==0)) + return; + else + { + if((n%2) == 0) + { + power_rec(n/2); + product(ans,ans); + + } + else + { + power_rec((n-1)/2); + product(ans,ans); + product(ans,a); + + } + } +} + +int main() +{ + //Main Function + cout <<"Enter the value of n\n"; + int n; + cin >>n; + if(n == 1) + { + cout<<"Ans: 0"< +using namespace std; + +#define MAX 10000000 + +int primes[MAX]; + + +/* + * This is the function that finds the primes and eliminates + * the multiples. + */ +void sieve(int N) +{ + primes[0] = 1; + primes[1] = 1; + for(int i=2;i<=N;i++) + { + if(primes[i] == 1) continue; + for(int j=i+i;j<=N;j+=i) + primes[j] = 1; + } +} + +/* + * This function prints out the primes to STDOUT + */ +void print(int N) +{ + for(int i=0;i<=N;i++) + if(primes[i] == 0) + cout << i << ' '; + cout << '\n'; +} + +/* + * NOTE: This function is important for the + * initialization of the array. + */ +void init() +{ + for(int i=0;i +using namespace std; +int binary_search(int a[],int l,int r,int key){ + while(l<=r){ + int m = l+(r-l)/2; + if(key==a[m]) + return m; + else if(key>n; + cout<<"Enter array elements: "; + int a[n]; + for (int i = 0; i < n; ++i) + { + cin>>a[i]; + } + cout<<"Enter search key: "; + cin>>key; + int res = binary_search(a,0,n-1,key); + if(res != -1) + cout< +using namespace std; + +int LinearSearch(int *array, int key) +{ + for (int i = 0; i < 10; ++i) + { + if (array[i]==key) + { + return key; + } + } + + return -1; +} + + +int main() +{ + int array[10]; + int key; + + //Input array + cout<<"\nEnter the Array of 10 numbers : "; + for (int i = 0; i < 10; i++) + { + cin>>array[i]; + } + cout<<"\nEnter the number to be searched : "; + cin>>key; + + int index=LinearSearch(array, key); + if (index!=-1) + { + cout<<"\nNumber found at index : "< +#include +#include + +using namespace std; +char paragraph; + +int main() +{ + string paragraph; + cout << "Please enter your paragraph: \n"; + getline (cin,paragraph); + cout << "\nHello, your paragraph is:\n " << paragraph << "!\n"; + cout << "\nThe size of your paragraph = " << paragraph.size() << " characters. \n\n"; + + if (paragraph.empty()) + { + cout << "\nThe paragraph is empty" << endl; + } + else + { + while (true) { + string word; + cout << "Please enter the word you are searching for: "; + getline (cin,word); + cout << "Hello, your word is " << word << "!\n"; + if (paragraph.find(word) == string::npos) + { + cout << word << " does not exist in the sentence" << endl; + } + else + { + cout << "The word " << word << " is now found at location " << paragraph.find(word) << endl << endl; + } + system("pause"); + } + + } +} diff --git a/Search/ternary_search.cpp b/Search/ternary_search.cpp new file mode 100644 index 000000000..4d126f09d --- /dev/null +++ b/Search/ternary_search.cpp @@ -0,0 +1,116 @@ +/* + * This is a divide and conquer algorithm. + * It does this by dividing the search space by 3 parts and + * using its property (usually monotonic property) to find + * the desired index. + * + * Time Complexity : O(log3 n) + * Space Complexity : O(1) (without the array) + */ + +#include +using namespace std; + +/* + * The absolutePrecision can be modified to fit preference but + * it is recommended to not go lower than 10 due to errors that + * may occur. + * + * The value of _target should be decided or can be decided later + * by using the variable of the function. + */ + +#define _target 10 +#define absolutePrecision 10 +#define MAX 10000000 + +int N = 21; +int A[MAX] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,10}; + +/* + * get_input function is to receive input from standard IO + */ +void get_input() +{ + // TODO: Get input from STDIO or write input to memory as done above. +} + + +/* + * This is the iterative method of the ternary search which returns the index of the element. + */ +int it_ternary_search(int left, int right, int A[],int target) +{ + while (1) + { + if(left A[twoThird]) left = twoThird+1; + else if(target < A[oneThird]) right = oneThird-1; + + else left = oneThird+1, right = twoThird-1; + } + else return -1; + } +} + +/* + * This is the recursive method of the ternary search which returns the index of the element. + */ +int rec_ternary_search(int left, int right, int A[],int target) +{ + if(left A[twoThird]) return rec_ternary_search(twoThird+1, right, A, target); + + return rec_ternary_search(oneThird+1, twoThird-1, A, target); + } + else return -1; +} + +/* + * ternary_search is a template function + * You could either use it_ternary_search or rec_ternary_search according to preference. + */ +void ternary_search(int N,int A[],int target) +{ + cout << it_ternary_search(0,N-1,A,target) << '\t'; + cout << rec_ternary_search(0,N-1,A,target) << '\t'; + cout << '\n'; +} + +int main() +{ + get_input(); + ternary_search(N,A,_target); + return 0; +}