diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 568ce6ed..00000000 Binary files a/.DS_Store and /dev/null differ diff --git a/AVL_Tree/AVL_Tree.c b/AVL_Tree/AVL_Tree.c deleted file mode 100644 index 0878e0a2..00000000 --- a/AVL_Tree/AVL_Tree.c +++ /dev/null @@ -1,301 +0,0 @@ -#include -#include - -struct AVLnode{ - int Data; - int Hight; - struct AVLnode* LeftNode; - struct AVLnode* RightNode; -}; -int GetHight(struct AVLnode* Node){ - if(Node == NULL) return -1; - if(Node->LeftNode == NULL && Node->RightNode == NULL) return 0; - int Hight,LeftChild_Hight,RightChild_Hight; - LeftChild_Hight = GetHight(Node->LeftNode); - RightChild_Hight = GetHight(Node->RightNode); - if(LeftChild_Hight > RightChild_Hight) - return LeftChild_Hight+1; - else - return RightChild_Hight+1; -} -int GetBalance(struct AVLnode * Node){ - if(Node == NULL) return 0; - return GetHight(Node->LeftNode)-GetHight(Node->RightNode); -} - - -struct AVLnode *SingleRightRotation(struct AVLnode *Parent){ - struct AVLnode* LeftChild; - LeftChild = Parent->LeftNode; - Parent->LeftNode = LeftChild->RightNode; - LeftChild->RightNode = Parent; - Parent->Hight =GetHight(Parent); - LeftChild->Hight = GetHight(LeftChild); - return LeftChild; -} -struct AVLnode *SingleLeftRotation(struct AVLnode *Parent){ - struct AVLnode* RightChild; - RightChild = Parent->RightNode; - Parent->RightNode = RightChild->LeftNode; - RightChild->LeftNode = Parent; - Parent->Hight =GetHight(Parent); - RightChild->Hight = GetHight(RightChild); - return RightChild; -} -struct AVLnode *DobleLeftRightRotation(struct AVLnode *Parent){ -struct AVLnode *LeftChild;; - LeftChild = Parent->LeftNode; - Parent->LeftNode = SingleLeftRotation(Parent->LeftNode); - Parent = SingleRightRotation(Parent); - return Parent; -} -struct AVLnode *DobleRightLeftRotation(struct AVLnode *Parent){ - Parent->RightNode = SingleRightRotation(Parent->RightNode); - Parent = SingleLeftRotation(Parent); - return Parent; -} - -struct AVLnode* InsertNode(struct AVLnode* Root,int val){ - if(Root == NULL){ - struct AVLnode *NewNode; - NewNode = (struct AVLnode*)malloc(sizeof(struct AVLnode)); - if(NewNode == NULL){ - printf(" Sorry! malloc is fail\n"); - return Root; - } - NewNode->Data = val; - NewNode->Hight = 0; - NewNode->LeftNode = NULL; - NewNode->RightNode = NULL; - return NewNode; - } - if(val < Root->Data){ - Root->LeftNode = InsertNode(Root->LeftNode,val); - if(GetBalance(Root) == 2){ - // heavy Left Sub-tree - if(val < (Root->LeftNode)->Data){ - // left-left case - Root = SingleRightRotation(Root); - }else{ - // left-Right case - Root = DobleLeftRightRotation(Root); - } - } - }else{ - Root->RightNode = InsertNode(Root->RightNode,val); - if(GetBalance(Root) == -2){ - // right-sub tree heavy - if(val > (Root->RightNode)->Data){ - // right - right case - Root= SingleLeftRotation(Root); - }else{ - // right - left case - Root = DobleRightLeftRotation(Root); - } - } - } - - Root->Hight = GetHight(Root); - return Root; -} - -struct AVLnode* DeleteNode(struct AVLnode* Root,int val){ - struct AVLnode *temp; - int Balance; - if(Root == NULL) return Root; - else if(val < Root->Data) - Root->LeftNode = DeleteNode(Root->LeftNode,val); - else if(val > Root->Data) - Root->RightNode = DeleteNode(Root->RightNode,val); - else{ - // Root->Data == val - if(Root->LeftNode == NULL && Root->RightNode == NULL){ - // Node with 0 childe - free(Root); - return NULL; - }else if(Root->LeftNode == NULL){ - // Node with only Right child - temp = Root->RightNode; - free(Root); - return temp; - }else if(Root->RightNode == NULL ){ - // Node with only Left Child - temp = Root->LeftNode; - free(Root); - return temp; - }else{ - // Node with 2 child - temp = Root->RightNode; - while(temp->LeftNode != NULL){ - temp = temp->LeftNode; - } - printf("%d\n",temp->Data); - Root->Data = temp->Data; - Root->RightNode = DeleteNode(Root->RightNode,temp->Data); - } - if(GetBalance(Root)== 2){ - if(GetBalance(Root->LeftNode)>0){ - //left-left case - return SingleRightRotation(Root); - }else{ - // left-right case - return DobleLeftRightRotation(Root); - } - if(GetBalance(Root)== -2){ - if(GetBalance(Root->RightNode)<0){ - // Right-right case - return SingleRightRotation(Root); - }else{ - //Right-left case - return DobleLeftRightRotation(Root); - } - } - } - } - return Root; -} -void INORDER_Traversal(struct AVLnode *Root){ - if(Root == NULL) - return; - INORDER_Traversal(Root->LeftNode); - printf("%d ",Root->Data); - INORDER_Traversal(Root->RightNode); -} -void PREORDER_Traversal(struct AVLnode *Root){ - if(Root == NULL) - return; - printf("%d ",Root->Data); - PREORDER_Traversal(Root->LeftNode); - PREORDER_Traversal(Root->RightNode); -} - - void POSTORDER_Traversal(struct AVLnode *Root){ - if(Root == NULL) - return; - POSTORDER_Traversal(Root->LeftNode); - POSTORDER_Traversal(Root->RightNode); - printf("%d ",Root->Data); -} -int front=-1,rear=-1; -#define MaxSize 100 -struct AVLnode* Q[MaxSize]; - -void InQueue(struct AVLnode* Data){ - if(rear == MaxSize){ - printf("Sorry Queue is Full\n"); - }else{ - if(front == -1 ) front++; - Q[++rear] = Data; - } -} - -struct AVLnode* Dequeue(){ - //if(front == -1) return - if(front == rear){ - struct AVLnode* r = Q[front]; - front = -1; - rear = -1; - return r; - }else{ - return Q[front++]; - } -} - -void LEVELORDER_Traversal(struct AVLnode *Root){ - InQueue(Root); - struct AVLnode *Temp; - while (front != -1) { - Temp = Dequeue(); - if( Temp != NULL ){ - InQueue(Temp->LeftNode); - InQueue(Temp->RightNode); - printf("%d ",Temp->Data); - } - } - printf("\n"); - } - int Search(struct AVLnode* Root,int Data){ - if(Root == NULL) return 0; - if(Root->Data == Data) return 1; - else if(Root->Data < Data) return Search(Root->RightNode,Data); - else return Search(Root->LeftNode,Data); - - /* while(Data != Root->Data && Root != NULL){ - - if(Data > Root->Data) - Root = Root->RightNode; - else - Root = Root->LeftNode; - } - if(Root == NULL){ - printf("Node Not Avalable.....\n"); - return 0; - }else{ - return 1; - }*/ - - } - - -int main(){ - int c,n,i; - struct AVLnode *Root= NULL; - printf("1) InsertNode\n2) Hight of Tree\n3) Search Data\n4) INORDER_Traversal\n5) PREORDER_Traversal\n6) POSTORDER_Traversal\n7) LEVELORDER_Traversal\n8) DeleteNode\n9) Exit\n\n"); - while(c != 9) { - printf("Enter Your choice :"); - scanf("%d",&c); - switch (c) { - case 1: - printf("Enter Data :"); - scanf("%d",&n); - Root = InsertNode(Root,n); - break; - case 2: - i = GetHight(Root); - printf("Hight Of tree is %d\n",i); - break; - case 3: - printf("Find Data in Tree :"); - scanf("%d",&n); - i= Search(Root,n); - if(i == 1){ - printf("Data found\n"); - }else{ - printf("Data Not found\n"); - } - break; - case 4: - printf("INORDER_Traversal of Tree is...."); - INORDER_Traversal(Root); - printf("\n"); - break; - case 5: - printf("PREORDER_Traversal of Tree is...."); - PREORDER_Traversal(Root); - printf("\n"); - break; - case 6: - printf("POSTORDER_Traversal of Tree is...."); - POSTORDER_Traversal(Root); - printf("\n"); - break; - case 7: - printf("LEVELORDER_Traversal of Tree is...."); - LEVELORDER_Traversal(Root); - break; - case 8: - printf("which Node you want to delete :"); - scanf("%d",&n); - DeleteNode(Root,n); - break; - case 9: - exit(0); - break; - default: - printf("Enter valid choice between 1-10\n"); - break; - } - - } - return 0; -} diff --git a/AVL_Tree/AVL_Tree.txt b/AVL_Tree/AVL_Tree.txt deleted file mode 100644 index a2927666..00000000 --- a/AVL_Tree/AVL_Tree.txt +++ /dev/null @@ -1,69 +0,0 @@ -1) InsertNode -2) Hight of Tree -3) Search Data -4) INORDER_Traversal -5) PREORDER_Traversal -6) POSTORDER_Traversal -7) LEVELORDER_Traversal -8) DeleteNode -9) Exit - -Enter Your choice :1 -Enter Data :50 -Enter Your choice :1 -Enter Data :60 -Enter Your choice :1 -Enter Data :40 -Enter Your choice :1 -Enter Data :70 -Enter Your choice :1 -Enter Data :80 -Enter Your choice :7 -LEVELORDER_Traversal of Tree is....50 40 70 60 80 -Enter Your choice :1 -Enter Data :90 -Enter Your choice :7 -LEVELORDER_Traversal of Tree is....70 50 80 40 60 90 -Enter Your choice :1 -Enter Data :65 -Enter Your choice :7 -LEVELORDER_Traversal of Tree is....70 50 80 40 60 90 65 -Enter Your choice :1 -Enter Data :64 -Enter Your choice :7 -LEVELORDER_Traversal of Tree is....70 50 80 40 64 90 60 65 -Enter Your choice :4 -INORDER_Traversal of Tree is....40 50 60 64 65 70 80 90 -Enter Your choice :5 -PREORDER_Traversal of Tree is....70 50 40 64 60 65 80 90 -Enter Your choice :6 -POSTORDER_Traversal of Tree is....40 60 65 64 50 90 80 70 -Enter Your choice :2 -Hight Of tree is 3 -Enter Your choice :8 -which Node you want to delete :40 -Enter Your choice :7 -LEVELORDER_Traversal of Tree is....70 50 80 64 90 60 65 -Enter Your choice :8 -which Node you want to delete :90 -Enter Your choice :7 -LEVELORDER_Traversal of Tree is....70 50 80 64 60 65 -Enter Your choice :4 -INORDER_Traversal of Tree is....50 60 64 65 70 80 -Enter Your choice :5 -PREORDER_Traversal of Tree is....70 50 64 60 65 80 -Enter Your choice :6 -POSTORDER_Traversal of Tree is....60 65 64 50 80 70 -Enter Your choice :8 -which Node you want to delete :60 -Enter Your choice :4 -INORDER_Traversal of Tree is....50 64 65 70 80 -Enter Your choice :7 -LEVELORDER_Traversal of Tree is....70 50 80 64 65 -Enter Your choice :8 -which Node you want to delete :80 -Enter Your choice :7 -LEVELORDER_Traversal of Tree is....70 50 64 65 -Enter Your choice :9 - -Press any key to continue . . . diff --git a/Algorithms/Dynamic Programming/Travelling_Salesman_Problem.cpp b/Algorithms/Dynamic Programming/Travelling_Salesman_Problem.cpp deleted file mode 100644 index 6d341942..00000000 --- a/Algorithms/Dynamic Programming/Travelling_Salesman_Problem.cpp +++ /dev/null @@ -1,85 +0,0 @@ -#include -using namespace std; - -int costMatrix[5][5]; -int vistedCities[5]; -int numCity = 5; -int cost = 0; - -int tsp(int); -void minCost(int); - -//Main functions -int main() -{ - - int i; - int j; - cout << "\n\nEnter distance between cities into matrix...\n"; - for (i = 0; i < numCity; i++) - { - cout << "\nEnter " << numCity << " elements in Row[" << i + 1 << "]\n"; - for (j = 0; j < numCity; j++) - cin >> costMatrix[i][j]; - } - cout << "\nDistances entered into cost matrix:\n"; - for (i = 0; i < numCity; i++) - { - cout << endl; - for (j = 0; j < numCity; j++) - { - cout << costMatrix[i][j] << " "; - } - } - - //Display results - cout << "\n\n Optimum Path: \t "; - minCost(0); - cout << "\n Minimum Cost: \t"; - cout << cost; - return 0; -} - -//Function to determine minimum cost -void minCost(int city) -{ - int nearestCity; - vistedCities[city] = 1; - - cout << city + 1; - nearestCity = tsp(city); - - if (nearestCity == 999) - { - nearestCity = 0; - cout << nearestCity + 1; - cost = cost + costMatrix[city][nearestCity]; - return; - } - minCost(nearestCity); -} - -int tsp(int city1) -{ - int counter; - int nearestCity = 999; - int mini = 999; - int temp; - - for (counter = 0; counter < numCity; counter++) - { - if ((costMatrix[city1][counter] != 0) && (vistedCities[counter] == 0)) - { - if (costMatrix[city1][counter] < mini) - { - mini = costMatrix[counter][0] + costMatrix[city1][counter]; - } - temp = costMatrix[city1][counter]; - nearestCity = counter; - } - } - if (mini != 999) - cost = cost + temp; - - return nearestCity; -} diff --git a/Algorithms/Dynamic Programming/chained-matrix-multiplication.c b/Algorithms/Dynamic Programming/chained-matrix-multiplication.c deleted file mode 100644 index c775d947..00000000 --- a/Algorithms/Dynamic Programming/chained-matrix-multiplication.c +++ /dev/null @@ -1,67 +0,0 @@ -#include - -int infinity=32500; - -struct x{ - int used,weight; -}; - -int main() -{ - int graph[9][9]={ - { 0, 4, 0, 0, 0, 0, 0, 8, 0 }, - { 4, 0, 8, 0, 0, 0, 0, 11, 0 }, - { 0, 8, 0, 7, 0, 4, 0, 0, 2 }, - { 0, 0, 7, 0, 9, 14, 0, 0, 0 }, - { 0, 0, 0, 9, 0, 10, 0, 0, 0 }, - { 0, 0, 4, 14, 10, 0, 2, 0, 0 }, - { 0, 0, 0, 0, 0, 2, 0, 1, 6 }, - { 8, 11, 0, 0, 0, 0, 1, 0, 7 }, - { 0, 0, 2, 0, 0, 0, 6, 7, 0 } - }; - - struct x arr[9];//this array traces value is filled or not - int w[9];//Stores the temporary weight - - //lets start from index 0 as default - w[0]=0; - arr[0].weight=0; - // arr[0].used=1; - for(int i=1;i<9;i++){ - w[i]=infinity; - arr[i].weight=infinity; - arr[i].used=0; - } - int count=0; - while(count<9){ - int temp=infinity,//temporary min weight - ti=-1;//temporary min index - - //find the minimum unused index - for(int i=0;i<9;i++){ - if(w[i]replacer+arr[ti].weight) && arr[i].used!=1) - w[i]=replacer+arr[ti].weight; - } - count++; - } - for(int i=0;i<9;i++) - printf("%d\t",arr[i].weight); - return 0; -} diff --git a/Algorithms/Dynamic Programming/coin_change.cpp b/Algorithms/Dynamic Programming/coin_change.cpp deleted file mode 100644 index d1c3b5db..00000000 --- a/Algorithms/Dynamic Programming/coin_change.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include -using namespace std; - -int dp[10000][10000]; - -int countMemo(int a[],int n,int val) -{ - if(val==0) - return 1; - if(val<0) - return 0; - if(n<=0 && val>=1) - return 0; - if(dp[n][val]==-1) - dp[n][val]=countMemo(a,n-1,val)+countMemo(a,n,val-a[n-1]); - return dp[n][val]; -} - -int countTabulation(int a[],int n,int val) -{ - int table[val+1][n]; - int i,j; - for(i=0;i=0?table[i-a[j]][j]:0; - int y=j>=1?table[i][j-1]:0; - table[i][j]=x+y; - } - } - return table[val][n-1]; -} - -int main() -{ - int t; cin>>t; - while(t--) - { - int n,i; cin>>n; - int arr[n]; - for(i=0;i>arr[i]; - int val; cin>>val; - memset(dp,-1,sizeof(dp)); - cout< -using namespace std; - -void editDist(string s1,string s2) -{ - // for printing the minimum number of operations required - int n=s1.length(),m=s2.length(); - int dp[n+1][m+1]; - int i,j; - for(i=0;i<=n;i++) - { - for(j=0;j<=m;j++) - { - if(i==0) - dp[i][j]=j; - else if(j==0) - dp[i][j]=i; - else - { - if(s1[i-1]==s2[j-1]) - dp[i][j]=dp[i-1][j-1]; - else - dp[i][j]=1+min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]); - } - } - } - cout<>s1>>s2; - editDist(s1,s2); - return 0; -} \ No newline at end of file diff --git a/Algorithms/Dynamic Programming/eggdrop.cpp b/Algorithms/Dynamic Programming/eggdrop.cpp deleted file mode 100644 index b119ca2b..00000000 --- a/Algorithms/Dynamic Programming/eggdrop.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include -using namespace std; - -int minAttempts(int n,int k) -{ - int dp[n+1][k+1]; - int i,j,p; - for(i=1;i<=n;i++) - { - dp[i][1]=1; - dp[i][0]=0; - } - for(i=1;i<=k;i++) - dp[1][i]=i; - - for(i=2;i<=n;i++) - { - for(j=2;j<=k;j++) - { - dp[i][j]=INT_MAX; - for(p=1;p<=j;p++) - { - int res=1+max(dp[i-1][p-1],dp[i][j-p]); - dp[i][j]=min(dp[i][j],res); - } - } - } - return dp[n][k]; -} - -int main() -{ - int t; cin>>t; - while(t--) - { - int n,k; cin>>n>>k; - cout<b) - return a; - return b; - } - - public static void main(String args[]) { - int values[]=new int[] {10,15,40}; - int weights[]=new int[] {1,2,3}; - int total=6; - int arr[][]=new int[weights.length+1][total+1]; - for(int i=0;i=0) { - //max value from - //1.same Column above value - //2.i-1 th value + arr[i-1][j-weights[i-1]] - arr[i][j]=getMax(arr[i-1][j],values[i-1]+arr[i-1][j-weights[i-1]]); - } - else { - arr[i][j]=arr[i-1][j]; - } - System.out.print(arr[i][j] + " "); - }System.out.println(""); - } - System.out.println(arr[weights.length][total]); - } - -} - - -//Output: -/*0 0 0 0 0 0 0 -0 10 10 10 10 10 10 -0 10 15 25 25 25 25 -0 10 15 40 50 55 65 -65*/ diff --git a/Algorithms/Dynamic Programming/largest_area.cpp b/Algorithms/Dynamic Programming/largest_area.cpp deleted file mode 100644 index 9a45c46b..00000000 --- a/Algorithms/Dynamic Programming/largest_area.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include -using namespace std; - -int matrix[100][100]; - -int largest_square_of_1(int n,int m) -{ - int dp[n][m],i,j,ma=0; - for(i=0;i0) - matrix[i][j]=1+matrix[i-1][j]; - } - } - for(i=0;i()); - int ma=0; - for(i=0;i>n>>m; - int i,j; - for(i=0;i>matrix[i][j]; - } - } - cout< -using namespace std; - -void lengthOfLCS(string s1,string s2) -{ - - // for calculating the length of lcs - int n=s1.length(),m=s2.length(); - int dp[n+1][m+1]; - memset(dp,0,sizeof(dp)); - int i,j; - for(i=1;i<=n;i++) - { - for(j=1;j<=m;j++) - { - if(s1[i-1]==s2[j-1]) - dp[i][j]=1+dp[i-1][j-1]; - else - dp[i][j]=max(dp[i-1][j],dp[i][j-1]); - } - } - cout<>s1>>s2; - lengthOfLCS(s1,s2); -} \ No newline at end of file diff --git a/Algorithms/Dynamic Programming/lis.cpp b/Algorithms/Dynamic Programming/lis.cpp deleted file mode 100644 index b8c4501a..00000000 --- a/Algorithms/Dynamic Programming/lis.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include -using namespace std; - -int lis_N2(int a[],int n) -{ - if(n==0) - return 0; - int lis[n]; - lis[0]=1; - int ma=1; - for(int i=1;i1) - { - int m=l+(r-l)/2; - if(tail[m]>=key) - r=m; - else - l=m; - } - return r; -} - -int lis_NlogN(int a[],int n) -{ - if(n==0) - return 0; - int tail[n]={0}; - tail[0]=a[0]; int length=1; - for(int i=1;itail[length-1]) - tail[length++]=a[i]; - else - tail[ceilIndex(tail,-1,length-1,a[i])]=a[i]; - } - return length; -} - -int main() -{ - int n; cin>>n; - int a[n],i; - for (int i = 0; i < n; ++i) - { - cin>>a[i]; - } - cout< -using namespace std; - -void longestPalindromicSubsequence(string s) -{ - int n=s.length(); - int dp[n][n]; - int i,j; - int ma=0,index=0; - for(i=0;i>s; - longestPalindromicSubsequence(s); -} \ No newline at end of file diff --git a/Algorithms/Dynamic Programming/longest_palindromic_substring.cpp b/Algorithms/Dynamic Programming/longest_palindromic_substring.cpp deleted file mode 100644 index 70791bce..00000000 --- a/Algorithms/Dynamic Programming/longest_palindromic_substring.cpp +++ /dev/null @@ -1,85 +0,0 @@ -#include -using namespace std; - -void longestPalindromicSubstring1(string s) // time complexity:O(N^2) space complexity:O(N^2) -{ - int n=s.length(); - int dp[n][n]; - int i,j; - int ma=0,index=0; - for(i=0;i=0 && highmaxLength) - { - start=low; - maxLength=high-low+1; - } - ++high; - --low; - } - - // all odd length palindromes - low=i-1; - high=i+1; - while(low>=0 && highmaxLength) - { - start=low; - maxLength=high-low+1; - } - ++high; - --low; - } - } - cout<>s; - longestPalindromicSubstring1(s); - cout< -using namespace std; - -int maxFromCell(int i,int j,int *matrix,int *dp,int n) -{ - if(i<0 || i>=n || j<0 || j>=n) - return 0; - if(*(dp+i*n+j)!=-1) - return *(dp+i*n+j); - if (j0 && (*(matrix+i*n+j) +1 == *(matrix+i*n+j-1))) - return *(dp+i*n+j) = 1 + maxFromCell(i,j-1,matrix,dp,n); - - if (i>0 && (*(matrix+i*n+j) +1 == *(matrix+(i-1)*n+j))) - return *(dp+i*n+j) = 1 + maxFromCell(i-1,j,matrix,dp,n); - - if (i>n; - int matrix[n][n],i,j; - for(i=0;i>matrix[i][j]; - } - int dp[n][n]; - memset(dp,-1,sizeof(dp)); - int res=1; - for(i=0;i -using namespace std; - -int solve(string str,int n) -{ - int dp[n+1][n+1]; - memset(dp,0,sizeof(dp)); - int i,j; - for(i=1;i<=n;i++) - { - for(j=1;j<=n;j++) - { - if(str[i-1]==str[j-1] && i!=j) - dp[i][j]=1+dp[i-1][j-1]; - else - dp[i][j]=max(dp[i][j-1],dp[i-1][j]); - } - } - return dp[n][n]; -} - -int main() -{ - int t; cin>>t; - while(t--) - { - int n; cin>>n; - string str; cin>>str; - cout<l[j] and (LIS[i]l[j] and (LIS[i] -using namespace std; - -int minCoinsDP(int a[],int n,int val) -{ - int table[val+1]; - - table[0]=0; // if value is zero then zero coins required - - for (int i = 1; i <=val; ++i) - { - table[i]=INT_MAX; - for (int j = 0; j < n; ++j) - { - if(i>=a[j]) - { - int subres=table[i-a[j]]; - if (subres!=INT_MAX && subres+1 q; - q.push(val); - int v[n+1]={0}; - int d=0; - while(q.size()) - { - int s=q.size(); - while(s--) - { - int p=q.front(); - if(p==0) - return d; - q.pop(); - if(v[p] || p<0) - continue; - v[p]=1; - for (int i = 0; i < n; i++) - q.push(p - a[i]); - - } - d++; - } - return -1; -} - -int main(int argc, char const *argv[]) -{ - int n; cin>>n; - int a[n],i,val; - for(i=0;i>a[i]; - cin>>val; - - cout< -using namespace std; - -int dp[101][101][2]; - -int sol(int i,int j,int f,int a[]) -{ - if(i==j) - return dp[i][j][f%2]=0; - if(f%2==0) - { - if(dp[i+1][j][1]==-1) - dp[i+1][j][1]=sol(i+1,j,f+1,a); - if(dp[i][j-1][1]==-1) - dp[i][j-1][1]=sol(i,j-1,f+1,a); - return dp[i][j][0]=max(a[i]+dp[i+1][j][1],a[j]+dp[i][j-1][1]); - } - else - { - if(a[i]>a[j]) - { - if(dp[i+1][j][0]==-1) - dp[i+1][j][0]=sol(i+1,j,f+1,a); - return dp[i][j][1]=dp[i+1][j][0]; - } - else - { - if(dp[i][j-1][0]==-1) - dp[i][j-1][0]=sol(i,j-1,f+1,a); - return dp[i][j][1]=dp[i][j-1][0]; - } - } -} - -int main() -{ - int t,i; cin>>t; - while(t--) - { - int n; cin>>n; - int a[n]; - for(i=0;i>a[i]; - } - memset(dp,-1,sizeof(dp)); - cout<0: - lower_bound = j-1 - upper_bound = j+1 - - min_cost = min(arr2[i][lower_bound:upper_bound+1]) - for k in range(lower_bound, upper_bound+1): - if arr2[i][k] == min_cost: - min_index[i] = k - - -path = [] -for i in range(0, rows): - path.append((i+1, min_index[i]+1)) -print("Minimum cost path is: ") -print(path) \ No newline at end of file diff --git a/Algorithms/Dynamic Programming/n lis.cpp b/Algorithms/Dynamic Programming/n lis.cpp deleted file mode 100644 index 809ed760..00000000 --- a/Algorithms/Dynamic Programming/n lis.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - -The problem is variation of Longest Increasing Subsequence. -The numbers which are already a part of LIS need not to be changed. -So minimum elements to change is difference of size of array and number of elements in LIS. -Note that we also need to make sure that the numbers are integers. -So while making LIS, we do not consider those elements as part of LIS that cannot form strictly -increasing by inserting elements in middle. - -Example { 1, 2, 5, 3, 4 }, we consider length of LIS as three {1, 2, 5}, not as {1, 2, 3, 4} -because we cannot make a strictly increasing array of integers with this LIS. - -*/ -#include -using namespace std; - -int maxi(int a[],int n) -{ - int g=a[0],i; - for(i=1;ig) - g=a[i]; - } - return g; -} - -int main() -{ - int t; - cin>>t; - while(t--) - { - int n,i,j; - cin>>n; - int a[n]; - for(i=0;i>a[i]; - - int ans[n]; - ans[0]=1; - for(i=1;ia[j] && ans[i] -using namespace std; - -int partitionRec(int a[],int index,int cSum,int totSum) -{ - if(index==-1) - return abs((2*cSum)-totSum); - - return min(partitionRec(a,index-1,cSum+a[index],totSum),partitionRec(a,index-1,cSum,totSum)); -} - -int partitionTab(int a[],int n,int sum) -{ - int dp[n+1][sum+1]; - int i,j; - for(i=0;i<=n;i++) - dp[i][0]=1; - for(i=1;i<=sum;i++) - dp[0][i]=0; - for(i=1;i<=n;i++) - { - for(j=1;j<=sum;j++) - { - if(a[i-1]<=j) // if the item can be included - dp[i][j]=dp[i-1][j] || dp[i-1][j-a[i-1]]; - - else // if the element cannot be included - dp[i][j]=dp[i-1][j]; - } - } - int diff=INT_MAX; - for(i=sum/2;i>=0;i--) - { - if(dp[n][i]) - { - diff=sum-2*i; - break; - } - } - return diff; -} - -int main() -{ - int n; cin>>n; - int a[n],i,totSum=0; - for(i=0;i>a[i]; - totSum+=a[i]; - } - cout< -using namespace std; - -int dp[100][100]; -int parent[100][100]; -int ans[100]; int k; -int a[100]; - -int rec(int l,int r) -{ - if(l+1>=r) // 0 or 1 element - return 0; - - int &ret=dp[l][r]; - if(ret!=-1) - return ret; - - ret=INT_MAX; - int bestIndex; - for(int i=l+1;i=r) - return; - - ans[k++]=a[parent[l][r]]; - back(l,parent[l][r]); - back(parent[l][r],r); -} - -int main() -{ - int n,i; cin>>n; - int m; cin>>m; - int a[m+2]; - a[0]=0; - a[m+1]=n; - for(i=1;i<=m;i++) - cin>>a[i]; - memset(dp,-1,sizeof(dp)); - memset(parent,-1,sizeof(parent)); - int best=rec(0,n-1); - k=0; - back(0,n-1); - for(i=0;i -using namespace std; - -bool sol(int a[],int n,int sum) -{ - if(sum%2==1) - return false; - sum=sum/2; - bool dp[n+1][sum+1]; - int i,j; - for(i=0;i<=n;i++) - dp[i][0]=true; - for(i=1;i<=sum;i++) - dp[0][i]=false; - for(i=1;i<=n;i++) - { - for(j=1;j<=sum;j++) - { - if(j>t; - while(t--) - { - int n,i; cin>>n; - int a[n]; - int sum=0; - for(i=0;i>a[i]; - sum+=a[i]; - } - if(sol(a,n,sum)) - cout<<"YES\n"; - else - cout<<"NO\n"; - } - return 0; -} \ No newline at end of file diff --git a/Algorithms/Graph Algorithms/BFS.cpp b/Algorithms/Graph Algorithms/BFS.cpp deleted file mode 100644 index d3677a47..00000000 --- a/Algorithms/Graph Algorithms/BFS.cpp +++ /dev/null @@ -1,95 +0,0 @@ - - //Bfs for undirected and unweighted graph - -#include -#include -#include -#include -#include -#define rep(i,a,b) for(int i=a;i> edges; - -void BFS(char root) -{ - cout << "\nBFS : " ; - queue visitRank ; - map visited ; - - char currNode = root ; - visitRank.push(currNode); - visited[currNode]=true; - while(!visitRank.empty()) - { - currNode = visitRank.front(); - for(auto c : edges[currNode]) - if(!visited[c]) - visitRank.push(c),visited[c]=true; - cout << currNode << " "; - visitRank.pop(); - } -} - - -void DFS(char root) -{ - cout << "\nDFS : " ; - stack visitRank; - map visited ; - char currNode = root ; - visitRank.push(currNode); - visited[currNode]=true; - while(!visitRank.empty()) - { - cout << currNode << " "; - currNode = visitRank.top(); - for(auto c : edges[currNode]) - if(!visited[c]) - visitRank.push(c),visited[c]=true,currNode=c; - visitRank.pop(); - } -} - -int main() -{ - int totalNodes,totalEdges ; - - cout << "Enter Total Nodes : " ; - cin >> totalNodes ; - cout << "Enter total number of Edges : "; - cin >> totalEdges ; - - - char u,v ; - - rep(i,0,totalEdges) - { - cin >> u >> v ; - edges[u].pb(v); - edges[v].pb(u); - } - char root ; - cout << "Enter Root : " ; - cin >> root ; - while(edges[root].size()==0) - { - cout << "Not Valid Root Node, " << "Enter Again : " ; - cin >> root ; - } - BFS(root); - DFS(root); -} - -/* -A B -A C -B D -B E -D E -E F -C H -C G -G H -*/ - diff --git a/Algorithms/Graph Algorithms/BFS.dart b/Algorithms/Graph Algorithms/BFS.dart deleted file mode 100644 index ee675dba..00000000 --- a/Algorithms/Graph Algorithms/BFS.dart +++ /dev/null @@ -1,50 +0,0 @@ -import 'dart:collection'; - -void main(){ - bfs b=bfs(4); - b.addEdge(0, 1); - b.addEdge(0, 2); - b.addEdge(1, 2); - b.addEdge(2, 0); - b.addEdge(2, 3); - b.addEdge(3, 3); - b.traverse(0); -} - -class bfs{ - List>address; - Set visited= {}; - Queue queue = Queue(); - int vertices; - - bfs(this.vertices){ - address=List>(this.vertices); - for(int index=0;index(); - } - } - - void addEdge(int start, int end){ - address[start].add(end); - } - - void traverse(int start){ - visited.add(start); - queue.add(start); - while(queue.isNotEmpty){ - int visit=queue.removeFirst(); - print(visit); - - for(int index=0;index adj[]; //Adjacency List - - Graph(int v) //Parameterized Constructor - { - num_of_vertices = v; - adj = new LinkedList[v]; - for (int i=0; i queue = new LinkedList(); //a queue for BFS - - visited[s]=true; //Mark current node as visited by setting it to true. - queue.add(s); // enqueue the visited node. - - while (queue.size() != 0) - { - s = queue.poll(); // Dequeue a vertex from queue - System.out.print(s+" "); //print that vertex. - - // If a adjacent vertex has not been visited, then mark it as visited and enqueue it from queue. - Iterator i = adj[s].listIterator(); - while (i.hasNext()) - { - int n = i.next(); - if (!visited[n]) - { - visited[n] = true; - queue.add(n); - } - } - } - } - - public static void main(String args[]) - { - Graph g = new Graph(5); - - g.addEdge(0, 1); - g.addEdge(0, 2); - g.addEdge(1, 2); - g.addEdge(1, 4); - g.addEdge(1, 3); - g.addEdge(2, 4); - g.addEdge(3, 4); - - System.out.println("Breadth First Traversal starting from vertex 0 : "); - - g.BFS(0); - } -} \ No newline at end of file diff --git a/Algorithms/Graph Algorithms/DFS.dart b/Algorithms/Graph Algorithms/DFS.dart deleted file mode 100644 index 4e51ad81..00000000 --- a/Algorithms/Graph Algorithms/DFS.dart +++ /dev/null @@ -1,40 +0,0 @@ -void main(){ - dfs graph=dfs(4); - graph.newEdge(0,1); - graph.newEdge(0,2); - graph.newEdge(1,2); - graph.newEdge(2,0); - graph.newEdge(2,3); - graph.newEdge(3,3); - graph.traverse(0); -} - -class dfs{ - int vertices; - static List> address; - static Set visited={}; - - dfs(this.vertices){ - address = List>(this.vertices); - for(int index=0;index(); - } - - } - - void newEdge(int start,int end){ - address[start].add(end); - } - - void traverse(int vertex){ - print(vertex); - visited.add(vertex); - int length = address[vertex].length; - for(int index=0;indexy.rank: - y.p=x - return y - else: - x.p=y - if x.rank==y.rank: - y.rank+=1 - return x - def findSet(x): - if x!=x.p: - x.p=DisjointSet.findSet(x.p) - return x.p - def sameComponent(x,y): - if DisjointSet.findSet(x)==DisjointSet.findSet(y): - return True - return False - def union(x,y): - DisjointSet.link(DisjointSet.findSet(x),DisjointSet.findSet(y)) - -def parseFile(filename): - with open(filename,'r') as al: - content = al.readlines() - adj_list = [x.strip().split() for x in content] - edges=[]#contains edges - list of tuple - vertices=[]#contains vertices - list of int - for a_list in adj_list: - vertices.append(int(a_list[0])) - for i in range(1,len(a_list)): - edges.append((int(a_list[0]),int(a_list[i]))) - - - #to remove duplicates - vertices=list(set(vertices)) - edges=list(set(edges)) - #to remove (b,a) if (a.b) exists - for edge in range(len(edges)-1,-1,-1): - if (edges[edge][1],edges[edge][0]) in edges: - print("[INFO]:removing ",edges[edge]) - del edges[edge] - #to remove self-loops - elif edges[edge][0]==edges[edge][1]: - print("[INFO]:removing ",edges[edge]) - del edges[edge] - #print(edges) - return vertices,edges -def main(vertices,edges): - min_cut=len(vertices) - for i in range(100): - e=edges[:] - V=vertices[:] - DSForest=dict() - for vertex in V: - DSForest.update({vertex:DisjointSet(vertex)}) - #v- number of vertices - v=len(V) - #continue till number of components becomes 2 - while(v>2): - edge=randint(0,len(e)-1) - print("Edge picked = ",e[edge]) - #join components - if(DisjointSet.findSet(DSForest[e[edge][0]])!=DisjointSet.findSet(DSForest[e[edge][1]])): - DisjointSet.union(DSForest[e[edge][0]],DSForest[e[edge][1]]) - v-=1 - #remove processed edge - print("[INFO]:removing edge",e[edge]) - del e[edge] - #remove edges edges belonging to same components(self-loop) - for edge in range(len(e)-1,-1,-1): - if DisjointSet.sameComponent(DSForest[e[edge][0]],DSForest[e[edge][1]]): - print("[INFO]In same component : removing edge",e[edge]) - del e[edge] - else: - print("[INFO]In different component : ",e[edge]) - for j in DSForest.keys(): - print("vertex= ",j," Parent : ",DSForest[j].p.v," Rank : ",DSForest[j].rank) - print("Remaining Edges : ",e) - print("Number of cuts required : ",len(e)) - #update min-cut - if len(e) -using namespace std; - - -//the function to generate all primes smaller than or equal to a number -void SieveOfEratosthenes(int n) -{ - - bool prime[n+1]; - memset(prime, true, sizeof(prime)); - - for(int p=2; p*p<=n; p++) - { - if(prime[p]==true) - { - for(int i=p*2;i<=n;i+=p) - prime[i]=false; - } - } - for(int p=2;p<=n;p++) - if(prime[p]) - cout< -int GCD(int,int); -void main() -{ - int p,q,result; - printf("Enter two numbers GCD: \n"); - scanf("%d%d",&p,&q); - result=GCD(p,q); - printf("The GCD %d",result); -} -int GCD(int m,int n) -{ - if(n>m) - GCD(n,m); - else if(n==0) - return m; - else - GCD(n,m%n); -} diff --git a/BT_To_BST/BT_To_BST.c b/BT_To_BST/BT_To_BST.c deleted file mode 100644 index 9c7600b8..00000000 --- a/BT_To_BST/BT_To_BST.c +++ /dev/null @@ -1,164 +0,0 @@ -#include -#include -int current = 0; -struct BSTnode{ - int Data; - struct BSTnode* LeftNode; - struct BSTnode* RightNode; -}; - - struct BSTnode* InsertNode(struct BSTnode* Root,int Data){ - struct BSTnode *temp,*NewNode; - NewNode = (struct BSTnode*)malloc(sizeof(struct BSTnode)); - if(NewNode == NULL){ - printf("Sorry Malloc is fail...!!!\n"); - return Root; - } - - NewNode->Data = Data; - NewNode->LeftNode = NULL; - NewNode->RightNode = NULL; - - - temp = Root; - - /*while (temp != NULL ) { - if(temp->Data > Root->Data){ - temp = Root->RightNode; - }else{ - temp = Root->LeftNode; - } - } - temp = NewNode;*/ - - if(Root == NULL){ - Root = NewNode; - return Root; - }else if(Data > Root->Data){ - Root->RightNode = InsertNode(Root->RightNode,Data); - }else{ - Root->LeftNode = InsertNode(Root->LeftNode,Data); - } - return temp; -} -void INORDER_Traversal(struct BSTnode *Root){ - if(Root == NULL) - return; - INORDER_Traversal(Root->LeftNode); - printf("%d ",Root->Data); - INORDER_Traversal(Root->RightNode); -} - -void PREORDER_Traversal(struct BSTnode *Root){ - if(Root == NULL) - return; - printf("%d ",Root->Data); - PREORDER_Traversal(Root->LeftNode); - PREORDER_Traversal(Root->RightNode); -} - - void POSTORDER_Traversal(struct BSTnode *Root){ - if(Root == NULL) - return; - POSTORDER_Traversal(Root->LeftNode); - POSTORDER_Traversal(Root->RightNode); - printf("%d ",Root->Data); -} -int front=-1,rear=-1; -#define MaxSize 100 -struct BSTnode* Q[MaxSize]; - -void InQueue(struct BSTnode* Data){ - if(rear == MaxSize){ - printf("Sorry Queue is Full\n"); - }else{ - if(front == -1 ) front++; - Q[++rear] = Data; - } -} - -struct BSTnode* Dequeue(){ - //if(front == -1) return - if(front == rear){ - struct BSTnode* r = Q[front]; - front = -1; - rear = -1; - return r; - }else{ - return Q[front++]; - } -} - -void LEVELORDER_Traversal(struct BSTnode *Root){ - InQueue(Root); - struct BSTnode *Temp; - while (front != -1) { - Temp = Dequeue(); - if( Temp != NULL ){ - InQueue(Temp->LeftNode); - InQueue(Temp->RightNode); - printf("%d ",Temp->Data); - } - } - printf("\n"); - } -int main(){ - struct BSTnode*Root = NULL; - int n[100]={0},c,h=0; - printf("1) InsertNode In Binary tree in LEVELORDER\n2) Convert Tree in Binary-Search-Tree Display in INORDER_Traversal\n"); - printf("3) Convert Tree in Binary-Search-Tree Display in PREORDER_Traversal\n4) Convert Tree in Binary-Search-Tree Display in POSTORDER_Traversal\n"); - printf("5) Convert Tree in Binary-Search-Tree Display in LEVELORDER_Traversal\n6) Exit\n"); - while(c != 6){ - printf("Enter your choice :"); - scanf("%d",&c); - switch (c) { - case 1: - printf("Enter Node in Binary Value in LEVELORDER: "); - scanf("%d",&n[current++]); - break; - case 2: - h=0; - printf("your Binary-Search-Tree in INORDER_Traversal is..."); - while (n[h] != 0) { - Root= InsertNode(Root,n[h++]); - } - INORDER_Traversal(Root); - Root = NULL; - printf("\n"); - break; - case 3: - h=0; - printf("your Binary-Search-Tree in PREORDER_Traversal is..."); - while (n[h] != 0) { - Root= InsertNode(Root,n[h++]); - } - PREORDER_Traversal(Root); - printf("\n"); - Root = NULL; - break; - case 4: - h=0; - printf("your Binary-Search-Tree in POSTORDER_Traversal is..."); - while (n[h] != 0) { - Root= InsertNode(Root,n[h++]); - } - POSTORDER_Traversal(Root); - Root = NULL; - printf("\n"); - break; - case 5: - h=0; - printf("your Binary-Search-Tree in LEVELORDER_Traversal is..."); - while (n[h] != 0) { - Root= InsertNode(Root,n[h++]); - } - LEVELORDER_Traversal(Root); - Root = NULL; - break; - case 6: - exit(0); - break; - } - } - return 0; -} diff --git a/BT_To_BST/BT_To_BST_OUTPUT.txt b/BT_To_BST/BT_To_BST_OUTPUT.txt deleted file mode 100644 index 0316de9e..00000000 --- a/BT_To_BST/BT_To_BST_OUTPUT.txt +++ /dev/null @@ -1,33 +0,0 @@ -1) InsertNode In Binary tree in LEVELORDER -2) Convert Tree in Binary-Search-Tree Display in INORDER_Traversal -3) Convert Tree in Binary-Search-Tree Display in PREORDER_Traversal -4) Convert Tree in Binary-Search-Tree Display in POSTORDER_Traversal -5) Convert Tree in Binary-Search-Tree Display in LEVELORDER_Traversal -6) Exit -Enter your choice :1 -Enter Node in Binary Value in LEVELORDER: 12 -Enter your choice :1 -Enter Node in Binary Value in LEVELORDER: 14 -Enter your choice :1 -Enter Node in Binary Value in LEVELORDER: 1 -Enter your choice :1 -Enter Node in Binary Value in LEVELORDER: 14 -Enter your choice :1 -Enter Node in Binary Value in LEVELORDER: 87 -Enter your choice :1 -Enter Node in Binary Value in LEVELORDER: 5 -Enter your choice :1 -Enter Node in Binary Value in LEVELORDER: 153 -Enter your choice :1 -Enter Node in Binary Value in LEVELORDER: 6 -Enter your choice :2 -your Binary-Search-Tree in INORDER_Traversal is...1 5 6 12 14 14 87 153 -Enter your choice :3 -your Binary-Search-Tree in PREORDER_Traversal is...12 1 5 6 14 14 87 153 -Enter your choice :4 -your Binary-Search-Tree in POSTORDER_Traversal is...6 5 1 14 153 87 14 12 -Enter your choice :5 -your Binary-Search-Tree in LEVELORDER_Traversal is...12 1 14 5 14 87 6 153 -Enter your choice :6 - -Press any key to continue . . . \ No newline at end of file diff --git a/Backtracking/NQueen.cpp b/Backtracking/NQueen.cpp deleted file mode 100644 index 6042d909..00000000 --- a/Backtracking/NQueen.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include -#define N 5 -using namespace std; - -void printSolution(int board[N][N]) -{ - int i,j; - for(i=0;i=0 && j>=0 ; i-- ,j--) - { - if(board[i][j]) - return false; - } - for(i=row,j = col;i=0 ;i++,j--) - { - if(board[i][j]) - return false; - } - return true; -} - -bool solveNQUtil(int board[N][N], int col) -{ - if (col >= N) - return true; - for (int i = 0; i < N; i++) - { - if ( isSafe(board, i, col) ) - { - board[i][col] = 1; - if ( solveNQUtil(board, col + 1) ) - return true; - board[i][col] = 0; - } - } - return false; -} - -bool solveQueen() -{ - int board[N][N] ,i,j; - for(i=0;i -using namespace std; - -bool reachDestination(int maze[][10], int solution[][10], int i, int j, int n){ - //Base Case - //reached the destination - if(i == n-1 && j == n-1){ - solution[i][j] = 1; - for(int x = 0; x < n; x++){ - for(int y = 0; y < n; y++){ - cout<>n; - int maze[10][10] = {0}; - int solution[10][10] = {0}; - for(int i = 0; i < n; i++){ - for(int j = 0; j < n; j++){ - cin>>maze[i][j]; - } - } - reachDestination(maze, solution, 0, 0, n); - return 0; -} - diff --git a/Betting_Game/Betting_Game.c b/Betting_Game/Betting_Game.c deleted file mode 100644 index 83319797..00000000 --- a/Betting_Game/Betting_Game.c +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -#include -int n,b,r,*a; - -void start(){ - srand(time(0)); - - a = (int*)malloc(3*sizeof(int)); - *a=rand()%3; - *(a+1)= rand()%3; - *(a+2)= rand()%3; - - if(n>0){ - - printf("you have currently $%d \n",n); - printf("enter the bet amount $"); - scanf("%d",&b); - if(b<=n){ - printf("find the place of queen[chose 1,2,3] : "); - scanf("%d",&r); - if(*a==(r-1) ){ - printf("congratulations you win!\n"); - n=n-b+(3*b); - printf("now you have $%d\n",n); - printf("______________________________\n"); - } - else{ - printf("Better luck Next time\n"); - n=n-b; - printf("now you have $%d \n",n); - printf("______________________________\n"); - } - }else{ - printf("you have only $%d and you Bet $%d \n",n,b); - - } - - } - else{ - printf("you are not eligible to play game \n you ave no money\n"); - } - free(a); - -} -int main(){ - - - -printf("______________________________\n"); -printf(" BETTING GAME\n"); -printf("______________________________\n"); -printf(" ***** ***** *****\n"); -printf(" * * * * * *\n"); -printf(" * j * * Q * * K *\n"); -printf(" * * * * * *\n"); -printf(" ***** ***** *****\n"); -printf("______________________________\n"); -printf("rules : \n"); -printf("1) you have to chose between 1 to 3 \n"); -printf("2) you are allow to play till you have $0 \n"); -printf("3) if you will win then you will get 3*(BET AMOUNT)\n\n\n"); -printf("how many amount you have $"); -scanf("%d",&n); -int f; -f=n; - - printf("\n\n*******************************************\n"); - printf(" GAME START \n"); - printf("*******************************************\n\n"); -//printf("you want to play game(y/n):"); -//char m; -//scanf("%c",&m); -while(n!=0){ -start(); -//printf("you want to play game again!(y/n):"); -//scanf("%c",&m); -} -printf("\n\n\n\nyou come with $%d\n",f); -printf("now you have $%d\n\n\n",n); -printf("thank you for play!\n\n\n"); -return 0; - -} - - diff --git a/Betting_Game/Betting_Game_Output.txt b/Betting_Game/Betting_Game_Output.txt deleted file mode 100644 index 52c2b3d2..00000000 --- a/Betting_Game/Betting_Game_Output.txt +++ /dev/null @@ -1,66 +0,0 @@ -______________________________ - BETTING GAME -______________________________ - ***** ***** ***** - * * * * * * - * j * * Q * * K * - * * * * * * - ***** ***** ***** -______________________________ -rules : -1) you have to chose between 1 to 3 -2) you are allow to play till you have $0 -3) if you will win then you will get 3*(BET AMOUNT) - - -how many amount you have $15 - - -******************************************* - GAME START -******************************************* - -you have currently $15 -enter the bet amount $4 -find the place of queen[chose 1,2,3] : 1 -Better luck Next time -now you have $11 -______________________________ -you have currently $11 -enter the bet amount $7 -find the place of queen[chose 1,2,3] : 3 -congratulations you win! -now you have $25 -______________________________ -you have currently $25 -enter the bet amount $20 -find the place of queen[chose 1,2,3] : 2 -Better luck Next time -now you have $5 -______________________________ -you have currently $5 -enter the bet amount $3 -find the place of queen[chose 1,2,3] : 2 -Better luck Next time -now you have $2 -______________________________ -you have currently $2 -enter the bet amount $2 -find the place of queen[chose 1,2,3] : 3 -Better luck Next time -now you have $0 -______________________________ - - - - -you come with $15 -now you have $0 - - -thank you for play! - - - -Process returned 0 (0x0) execution time : 74.211 s -Press any key to continue. diff --git a/Binary Tree in cpp/BinaryTree.cpp b/Binary Tree in cpp/BinaryTree.cpp deleted file mode 100644 index 659653f7..00000000 --- a/Binary Tree in cpp/BinaryTree.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include -using namespace std; -class Node { - public: - char data; - Node* left, * right; -}; -//Creating the tree -Node* binaryCreate(char data) { - Node* node = new Node; - node->data = data; - node->left = node->right = NULL; - return node; -} -//For subsequent elements -Node* find(char data) { - Node* node = new Node; - node->data = data; - node->left = node->right = NULL; - return node; -} -//To insert element into the tree -Node* binaryInsert(char arr[], Node* next, int i, int size) { - if (i < size) { - Node* temp = find(arr[i]); - next = temp; - next->left = binaryInsert(arr, next->left, 2 * i + 1, size); - next->right = binaryInsert(arr, next->right, 2 * i + 2, size); - } - return next; -} - -//To find and print element -void binaryPrint(Node* next, char val) { - char key = val; - if(next != NULL){ - binaryPrint(next->left, key); - if(next->data==val){ - cout << next->data << " "; - } - binaryPrint(next->right, key); - } -} - -void binaryTrav(Node* next) { - if (next != NULL){ - binaryTrav(next->left); - cout << next->data <<" "; - binaryTrav(next->right); - } -} -//Main function -int main() { - char chars[] = {'B','D','R','T','E','M','N','P'}; //input into the tree - int key; - char val; - Node* root = binaryCreate(chars[0]); - int n = sizeof(chars)/sizeof(chars[0]); - root = binaryInsert(chars, root, 0, n); - cout << "Inorder traversal:"; - binaryTrav(root); - cout << "Enter index of element for searching"; - cin >> key; - val = chars[key]; - binaryPrint(root, chars[key]); - -} diff --git a/Binary_Search.c b/Binary_Search.c new file mode 100644 index 00000000..65fb4f02 --- /dev/null +++ b/Binary_Search.c @@ -0,0 +1,90 @@ +#include +#include + +void binarySearch(int searchValue, int a[], int n); + +void selectionSort(int a[], int n) +{ + int i, j, minimunIndex, temp; + + printf("Just started to sort Using the Selection Sort Algorithm\n"); + + for ( i = 0; i < n; i++) + { + minimunIndex = i; + + for ( j = i + 1; j < n; j++) + { + if (a[minimunIndex] > a[j]) + minimunIndex = j; + } + temp = a[i]; + a[i] = a[minimunIndex]; + a[minimunIndex] = temp; + } + + printf("Ended Sorting using Selection Sort\n"); +} + + +void main() +{ + int length, i, searchValue; + printf("Welcome to the Binary Search!!\n"); + printf("You can search the data element from an array, don't worry I will do it for you\n"); + printf("For simplicity here the data elements are considered as integers\n"); + printf("So let me know how many integers you want:\n"); + scanf("%d", &length); + + int *a = (int *) calloc (length, sizeof(int)); + + printf("Please enter the integers now:\n"); + for ( i = 0; i < length; i++) + { + scanf("%d", &a[i]); + } + + printf("Please enter the value you want me to search for:\n"); + scanf("%d", &searchValue); + + // Binary Search needs the sorted array as input without which the search can't occur + // Hence sorting elements before the Search + selectionSort(a, length); + + binarySearch(searchValue, a, length); + + printf("Thanks for Investing time in Me!!"); +} + +void binarySearch(int searchValue, int a[], int n) +{ + int middle, first, last; + first = 0; + last = n-1; + + middle = first + last / 2; + + printf("Starting to search the Data element --> %4d\n", searchValue); + while( first <= last) + { + if(a[middle] < searchValue) + first = middle + 1; + + else if(a[middle] == searchValue) + { + printf("The element %d was found at the location %d starting from 0\n", searchValue, middle); + break; + } + else + last = middle - 1; + + middle = first + last / 2; + } + + if (first > last) + { + printf("Sorry the element you wanted me to Search doesn't exist in the given array\n"); + } + + printf("Just Ended Binary Search\n"); +} \ No newline at end of file diff --git a/Binry_Search_Tree/Binary_Search_Tree.c b/Binry_Search_Tree/Binary_Search_Tree.c deleted file mode 100644 index 7deafd06..00000000 --- a/Binry_Search_Tree/Binary_Search_Tree.c +++ /dev/null @@ -1,276 +0,0 @@ -#include -#include - -struct BSTnode{ - int Data; - struct BSTnode* LeftNode; - struct BSTnode* RightNode; -}; - - struct BSTnode* InsertNode(struct BSTnode* Root,int Data){ - struct BSTnode *temp,*NewNode; - NewNode = (struct BSTnode*)malloc(sizeof(struct BSTnode)); - if(NewNode == NULL){ - printf("Sorry Malloc is fail...!!!\n"); - return Root; - } - - NewNode->Data = Data; - NewNode->LeftNode = NULL; - NewNode->RightNode = NULL; - - - temp = Root; - - /*while (temp != NULL ) { - if(temp->Data > Root->Data){ - temp = Root->RightNode; - }else{ - temp = Root->LeftNode; - } - } - temp = NewNode;*/ - - if(Root == NULL){ - Root = NewNode; - return Root; - }else if(Data > Root->Data){ - Root->RightNode = InsertNode(Root->RightNode,Data); - }else{ - Root->LeftNode = InsertNode(Root->LeftNode,Data); - } - return temp; -} - -int Search(struct BSTnode* Root,int Data){ - if(Root == NULL) return 0; - if(Root->Data == Data) return 1; - else if(Root->Data < Data) return Search(Root->RightNode,Data); - else return Search(Root->LeftNode,Data); - -/* while(Data != Root->Data && Root != NULL){ - - if(Data > Root->Data) - Root = Root->RightNode; - else - Root = Root->LeftNode; - } - if(Root == NULL){ - printf("Node Not Avalable.....\n"); - return 0; - }else{ - return 1; - }*/ - -} - -int FindMin(struct BSTnode *Root){ - if(Root->LeftNode == NULL) - return Root->Data; - else - FindMin(Root->LeftNode); - -/* while(Root != NULL){ - Root = Root->LeftNode; - } - Return Root->Data;*/ - -} - -int FindMax(struct BSTnode *Root){ - if(Root->RightNode == NULL) - return Root->Data; - else - FindMax(Root->RightNode); - -/* while(Root != NULL){ - Root = Root->RightNode; - } - return Root->Data;*/ -} - -int MaxHight(struct BSTnode *Root){ - int L_Hight=0,R_Hight=0; - if(Root == NULL) - return -1; - if(Root->LeftNode == NULL && Root->RightNode == NULL) - return 0; - L_Hight = MaxHight(Root->LeftNode); - R_Hight = MaxHight(Root->RightNode); - if(L_Hight > R_Hight) - return L_Hight+1; - else - return R_Hight+1; -} - - void INORDER_Traversal(struct BSTnode *Root){ - if(Root == NULL) - return; - INORDER_Traversal(Root->LeftNode); - printf("%d ",Root->Data); - INORDER_Traversal(Root->RightNode); - } - - void PREORDER_Traversal(struct BSTnode *Root){ - if(Root == NULL) - return; - printf("%d ",Root->Data); - PREORDER_Traversal(Root->LeftNode); - PREORDER_Traversal(Root->RightNode); - } - - void POSTORDER_Traversal(struct BSTnode *Root){ - if(Root == NULL) - return; - POSTORDER_Traversal(Root->LeftNode); - POSTORDER_Traversal(Root->RightNode); - printf("%d ",Root->Data); -} -int front=-1,rear=-1; -#define MaxSize 100 -struct BSTnode* Q[MaxSize]; - -void InQueue(struct BSTnode* Data){ - if(rear == MaxSize){ - printf("Sorry Queue is Full\n"); - }else{ - if(front == -1 ) front++; - Q[++rear] = Data; - } -} - -struct BSTnode* Dequeue(){ - //if(front == -1) return - if(front == rear){ - struct BSTnode* r = Q[front]; - front = -1; - rear = -1; - return r; - }else{ - return Q[front++]; - } -} - -void LEVELORDER_Traversal(struct BSTnode *Root){ - InQueue(Root); - struct BSTnode *Temp; - while (front != -1) { - Temp = Dequeue(); - if( Temp != NULL ){ - InQueue(Temp->LeftNode); - InQueue(Temp->RightNode); - printf("%d ",Temp->Data); - } - } - printf("\n"); - } - -struct BSTnode * DeleteNode(struct BSTnode* Root,int key){ -struct BSTnode *Temp; - if(Root == NULL ) - return Root; - if(key > Root->Data) - Root->RightNode = DeleteNode(Root->RightNode,key); - else if(key < Root->Data) - Root->LeftNode = DeleteNode(Root->LeftNode,key); - else{ // Root->Data == key; - if(Root->LeftNode == NULL && Root->RightNode == NULL){ - // node with 0 child - free(Root); - return NULL; - } - else if(Root->LeftNode == NULL){ - // node with 1 child - Temp = Root->RightNode; - free(Root); - return Temp; - }else if(Root->RightNode == NULL){ - // node with 1 child - Temp = Root->LeftNode; - free(Root); - return Temp; - }else{ - // node with 2 childe - // find min in right sub-Tree - Temp = Root->RightNode; - while(Temp->LeftNode != NULL){ - Temp = Temp->LeftNode; - } - Root->Data = Temp->Data; - Root->RightNode = DeleteNode(Root->RightNode,Temp->Data); - } - return Root; - } - -} - -int main(){ - int c,n,i; - struct BSTnode *Root= NULL; - printf("1) InsertNode\n2) Hight of Tree\n3) Find Max Data\n4) Find Min Data\n5) Search Data\n6) INORDER_Traversal\n7) PREORDER_Traversal\n8) POSTORDER_Traversal\n9) LEVELORDER_Traversal\n10) DeleteNode\n11) Exit\n\n"); - while (c != 11) { - printf("Enter Your choice :"); - scanf("%d",&c); - switch (c) { - case 1: - printf("Enter Data :"); - scanf("%d",&n); - Root = InsertNode(Root,n); - break; - case 2: - i = MaxHight(Root); - printf("Hight Of tree is %d\n",i); - break; - case 3: - i = FindMax(Root); - printf("Max Data of Tree is %d\n",i); - break; - case 4: - i = FindMin(Root); - printf("Min Data of tree is %d\n",i); - break; - case 5: - printf("Find Data in Tree :"); - scanf("%d",&n); - i= Search(Root,n); - if(i == 1){ - printf("Data found\n"); - }else{ - printf("Data Not found\n"); - } - break; - case 6: - printf("INORDER_Traversal of Tree is...."); - INORDER_Traversal(Root); - printf("\n"); - break; - case 7: - printf("PREORDER_Traversal of Tree is...."); - PREORDER_Traversal(Root); - printf("\n"); - break; - case 8: - printf("POSTORDER_Traversal of Tree is...."); - POSTORDER_Traversal(Root); - printf("\n"); - break; - case 9: - printf("LEVELORDER_Traversal of Tree is...."); - LEVELORDER_Traversal(Root); - break; - case 10: - printf("which Node you want to delete :"); - scanf("%d",&n); - DeleteNode(Root,n); - break; - case 11: - exit(0); - break; - default: - printf("Enter valid choice between 1-10\n"); - break; - } - - } - return 0; -} diff --git a/Binry_Search_Tree/Binary_Search_Tree.js b/Binry_Search_Tree/Binary_Search_Tree.js deleted file mode 100644 index 34f46b8e..00000000 --- a/Binry_Search_Tree/Binary_Search_Tree.js +++ /dev/null @@ -1,103 +0,0 @@ -class BinarySearchTree { - constructor(value, leftSubTree = null, rightSubTree = null) { - if (!value || typeof value !== 'number') { - throw new Error('Type of data not supported'); - } - - /** - * @type {number} - */ - this.value = value; - - /** - * @type {BinarySearchTree} - */ - this.leftSubTree = leftSubTree; - - /** - * @type {BinarySearchTree} - */ - this.rightSubTree = rightSubTree; - } - - insertNode(newValue) { - if (typeof newValue === 'number') { - if (newValue < this.value) { - if (this.leftSubTree == null) { - const newLeftSubTree = new BinarySearchTree(newValue); - this.leftSubTree = newLeftSubTree; - } else { - this.leftSubTree.insertNode(newValue); - } - } else { - if (this.rightSubTree == null) { - const newRightSubTree = new BinarySearchTree(newValue); - this.rightSubTree = newRightSubTree; - } else { - this.rightSubTree.insertNode(newValue); - } - } - } - } - - search(value) { - let searched = false; - if (value == this.value) { - searched = true; - } else { - if (this.leftSubTree != null) { - searched = this.leftSubTree.search(value); - } else if (this.rightSubTree != null) { - searched = this.rightSubTree.search(value); - } - } - return searched; - } - - findMin() { - let minValue = this.value; - if (this.leftSubTree != null) { - minValue = this.leftSubTree.findMin(); - } - - return minValue; - } - - findMax() { - let maxValue = this.value; - if (this.rightSubTree != null) { - maxValue = this.rightSubTree.findMax(); - } - return maxValue; - } - - preorder() { - console.log(this.value); - if (this.leftSubTree != null) { - this.leftSubTree.preorder(); - } - if (this.rightSubTree != null) { - this.rightSubTree.preorder(); - } - } - - inorder() { - if (this.leftSubTree != null) { - this.leftSubTree.inorder(); - } - console.log(this.value); - if (this.rightSubTree != null) { - this.rightSubTree.inorder(); - } - } - - postorder() { - if (this.leftSubTree != null) { - this.leftSubTree.postorder(); - } - if (this.rightSubTree != null) { - this.rightSubTree.postorder(); - } - console.log(this.value); - } -} diff --git a/Binry_Search_Tree/Binary_Search_Tree.py b/Binry_Search_Tree/Binary_Search_Tree.py deleted file mode 100644 index 40511fb9..00000000 --- a/Binry_Search_Tree/Binary_Search_Tree.py +++ /dev/null @@ -1,37 +0,0 @@ -#Defining node as a class and initializing -class Node: - def __init__(self, key): - self.left = None - self.right = None - self.val = key - -#Inserting a node in the BST -def insert_node(root, node): - if root is None: - root = node - else: - if root.val > node.val: - if root.left is None: - root.left = node - else: - insert_node(root.left, node) - else: - if root.right is None: - root.right = node - else: - insert_node(root.right, node) - -#Inorder Traversal of BST -def inorder(root): - if root: - inorder(root.left) - print(root.val, end=" ") - inorder(root.right) - -#User input for root and subsequent nodes -r = Node(int(input("Enter root node: "))) -temp = input("Enter subsequent nodes: ").split() -for i in temp: - insert_node(r, Node(int(i))) -print("Inorder", end=" ") -inorder(r) diff --git a/Binry_Search_Tree/Binary_Search_tree_Output.txt b/Binry_Search_Tree/Binary_Search_tree_Output.txt deleted file mode 100644 index 7be40af1..00000000 --- a/Binry_Search_Tree/Binary_Search_tree_Output.txt +++ /dev/null @@ -1,71 +0,0 @@ -1) InsertNode -2) Hight of Tree -3) Find Max Data -4) Find Min Data -5) Search Data -6) INORDER_Traversal -7) PREORDER_Traversal -8) POSTORDER_Traversal -9) LEVELORDER_Traversal -10) DeleteNode -11) Exit - -Enter Your choice :1 -Enter Data :50 -Enter Your choice :1 -Enter Data :60 -Enter Your choice :1 -Enter Data :40 -Enter Your choice :1 -Enter Data :55 -Enter Your choice :1 -Enter Data :45 -Enter Your choice :1 -Enter Data :35 -Enter Your choice :1 -Enter Data :47 -Enter Your choice :1 -Enter Data :70 -Enter Your choice :1 -Enter Data :67 -Enter Your choice :1 -Enter Data :52 -Enter Your choice :1 -Enter Data :57 -Enter Your choice :2 -Hight Of tree is 3 -Enter Your choice :3 -Max Data of Tree is 70 -Enter Your choice :4 -Min Data of tree is 35 -Enter Your choice :5 -Find Data in Tree :45 -Data found -Enter Your choice :5 -Find Data in Tree :43 -Data Not found -Enter Your choice :6 -INORDER_Traversal of Tree is....35 40 45 47 50 52 55 57 60 67 70 -Enter Your choice :7 -PREORDER_Traversal of Tree is....50 40 35 45 47 60 55 52 57 70 67 -Enter Your choice :8 -POSTORDER_Traversal of Tree is....35 47 45 40 52 57 55 67 70 60 50 -Enter Your choice :9 -LEVELORDER_Traversal of Tree is....50 40 60 35 45 55 70 47 52 57 67 -Enter Your choice :10 -which Node you want to delete :50 -Enter Your choice :6 -INORDER_Traversal of Tree is....35 40 45 47 52 55 57 60 67 70 -Enter Your choice :10 -which Node you want to delete :60 -Enter Your choice :6 -INORDER_Traversal of Tree is....35 40 45 47 52 55 57 67 70 -Enter Your choice :9 -LEVELORDER_Traversal of Tree is....52 40 67 35 45 55 70 47 57 -Enter Your choice :10 -which Node you want to delete :52 -Enter Your choice :9 -LEVELORDER_Traversal of Tree is....55 40 67 35 45 57 70 47 -Enter Your choice :11 - -Press any key to continue . . . \ No newline at end of file diff --git a/Binry_Search_Tree/CheckBST.java b/Binry_Search_Tree/CheckBST.java deleted file mode 100644 index f57c4fe8..00000000 --- a/Binry_Search_Tree/CheckBST.java +++ /dev/null @@ -1,34 +0,0 @@ -package bst_operations; - -public class CheckBST { - public static void main(String[] args) { - new CheckBST().demo(); - } - - private void demo() { - Node n = new Node(4); - // left - n.left = new Node(2); - n.left.left = new Node(1); - n.left.right = new Node(3); - - //right - n.right = new Node(5); - System.out.println(isBST(n, Integer.MIN_VALUE, Integer.MAX_VALUE)); - } - - private boolean isBST(Node n, int min, int max) { - if (n == null) { - return true; - } - if (n.data < min || n.data > max) { - return false; - } - if(n.data == max || n.data == min) { - System.out.println("duplication of data"); - return false; - } - return isBST(n.left, min, n.data) && isBST(n.right, n.data, max); - } -} - diff --git a/Binry_Search_Tree/CorrectBST.java b/Binry_Search_Tree/CorrectBST.java deleted file mode 100644 index 9b54836c..00000000 --- a/Binry_Search_Tree/CorrectBST.java +++ /dev/null @@ -1,56 +0,0 @@ -package bst_operations; - -import java.util.ArrayList; - -/** - * Two nodes of a BST are swapped, correct the BST - */ -public class CorrectBST { - public static void main(String[] args) { - new CorrectBST().demo(); - } - - private void demo() { - Node n = new Node(10); - n.left = new Node(5); - n.left.left = new Node(2); - n.left.right = new Node(20); - n.right = new Node(8); - /* - 10 - / \ - 5 8 - / \ - 2 20 -*/ - ArrayList list = new ArrayList(); - display(n); - balanceBST(n, list, Integer.MIN_VALUE, Integer.MAX_VALUE); - System.out.println(); - list.get(0).data = list.get(0).data + list.get(1).data; - list.get(1).data = list.get(0).data - list.get(1).data; - list.get(0).data = list.get(0).data - list.get(1).data; - System.out.println("After"); - display(n); - } - - private void display(Node n) { - if (n != null) { - display(n.left); - System.out.print(n.data + "\t"); - display(n.right); - } - } - - private void balanceBST(Node n, ArrayList list, int min, int max) { - if (n == null) { - return; - } - if (n.data > max || n.data < min) { - list.add(n); - } - balanceBST(n.left, list, min, n.data); - balanceBST(n.right, list, n.data, max); - - } -} diff --git a/Binry_Search_Tree/FindAncestor.java b/Binry_Search_Tree/FindAncestor.java deleted file mode 100644 index df2bd185..00000000 --- a/Binry_Search_Tree/FindAncestor.java +++ /dev/null @@ -1,74 +0,0 @@ -package bst_operations; - -/** - * Created by kalsi on 30/08/17. - * find lowest ancestor of two items - * I assumed both the elements are present in the tree - */ -public class FindAncestor { - public static void main(String[] args) { - new FindAncestor().demo(); - } - - private void demo() { - - Node n = new Node(8); - // left - n.left = new Node(3); - n.left.left = new Node(1); - n.left.right = new Node(6); - n.left.right.left = new Node(4); - n.left.right.right = new Node(7); - - //right - n.right = new Node(10); - n.right.right = new Node(14); - n.right.right.left = new Node(13); - - /* - 8 - 3 10 - 1 6 14 - 4 7 13 - * - * */ - int m1 = 4; - int m2 = 7; - Node ancestor = lowestAncestor(n, m1, m2); - System.out.println("Lowest ancestor is : " + ancestor.data); - System.out.println("Distance from root to ancestor is: " + findDistance(n, ancestor)); - } - - private int findDistance(Node n, Node ancestor) { - return findDistanceUtil(n,ancestor, 0); - } - - private int findDistanceUtil(Node n, Node ancestor, int level) { - if(n == null || ancestor == null) { - return 0; - } - if(n.data == ancestor.data) { - return level; - } - level++; - return findDistanceUtil(n.left, ancestor, level) + findDistanceUtil(n.right, ancestor, level); - - } - - private Node lowestAncestor(Node n, int m1, int m2) { - if (n == null) { - return null; - } - if (n.data == m2 || n.data == m1) { - return n; - } - Node left = lowestAncestor(n.left, m1, m2); - Node right = lowestAncestor(n.right, m1, m2); - if (left != null && right != null) { - return n; - } - return left == null ? right : left; - } - - -} diff --git a/Binry_Search_Tree/InorderSuccessor.java b/Binry_Search_Tree/InorderSuccessor.java deleted file mode 100644 index 12fddfca..00000000 --- a/Binry_Search_Tree/InorderSuccessor.java +++ /dev/null @@ -1,52 +0,0 @@ -package bst_operations; - -public class InorderSuccessor { - public static void main(String[] args) { - new InorderSuccessor().demo(); - } - - private void demo() { - Node n = new Node(8); - // left - n.left = new Node(3); - n.left.left = new Node(1); - n.left.right = new Node(6); - n.left.right.left = new Node(4); - n.left.right.right = new Node(7); - - //right - n.right = new Node(10); - n.right.right = new Node(14); - n.right.right.left = new Node(13); - - /* - 8 - 3 10 - 1 6 14 - 4 7 13 - * - * */ - System.out.println(getNextSuccessor(n, new Node(8))); - } - - private Integer getNextSuccessor(Node root, Node target) { - if (root == null || target == null) { - return null; - } - Boolean found = false; - Node candidate = null; - Node current = root; - while (current != null) { - if (current.data == target.data) { - found = true; - current = current.right; - } else if (current.data > target.data) { - candidate = current; - current = current.left; - } else { - current = current.right; - } - } - return found && candidate != null ? candidate.data : null; - } -} diff --git a/Binry_Search_Tree/LargestBST.java b/Binry_Search_Tree/LargestBST.java deleted file mode 100644 index bb7000bf..00000000 --- a/Binry_Search_Tree/LargestBST.java +++ /dev/null @@ -1,89 +0,0 @@ -package bst_operations; - - -/** - * Created by kalsi on 06/09/17. - */ -public class LargestBST { - class BSTCount { - Boolean isBST; - int count; - } - - class BST { - int max; - Node n; - - public BST() { - max = -1; - n = null; - } - } - - public static void main(String[] args) { - new LargestBST().demo(); - } - - private void demo() { - Node n = new Node(50); - // left - n.left = new Node(30); - n.left.left = new Node(5); - n.left.right = new Node(20); - - //right - n.right = new Node(60); - n.right.left = new Node(45); - n.right.right = new Node(70); - n.right.right.left = new Node(65); - n.right.right.right = new Node(80); - /* 50 - / \ - 30 60 - / \ / \ - 5 20 45 70 - / \ - 65 80 - */ - largestBST(n); - } - - private void largestBST(Node n) { - BST bst = new BST(); - BSTCount bstCount = new BSTCount(); - largestBSTUtil(n, bst, bstCount); - System.out.println("Largest BST: " + bst.n.data); - System.out.println("Count: " + bst.max); - } - - private void largestBSTUtil(Node n, BST bst, BSTCount bstCount) { - if (n == null) { - return; - } - isBST(n, bstCount); - if (bstCount.isBST && bstCount.count > bst.max) { - bst.max = bstCount.count; - bst.n = n; - } - largestBSTUtil(n.left, bst, bstCount); - largestBSTUtil(n.right, bst, bstCount); - } - - private void isBST(Node n, BSTCount bstCount) { - bstCount.count = 0; - bstCount.isBST = false; - bstCount.isBST = isBSTUtil(n, Integer.MIN_VALUE, Integer.MAX_VALUE, bstCount); - } - - private Boolean isBSTUtil(Node n, int minValue, int maxValue, BSTCount c) { - if (n == null) { - return true; - } - if (minValue < n.data && n.data < maxValue) { - c.count++; - return isBSTUtil(n.left, minValue, n.data, c) && isBSTUtil(n.right, n.data, maxValue, c); - } - - return false; - } -} \ No newline at end of file diff --git a/Binry_Search_Tree/LevelOrder.java b/Binry_Search_Tree/LevelOrder.java deleted file mode 100644 index 4529f020..00000000 --- a/Binry_Search_Tree/LevelOrder.java +++ /dev/null @@ -1,76 +0,0 @@ -package bst_operations; - - -import java.util.*; - -/** - * Created by kalsi on 01/09/17. - */ -public class LevelOrder { - public static void main(String[] args) { - new LevelOrder().demo(); - } - - private void demo() { - - Node n = new Node(8); - // left - n.left = new Node(3); - n.left.left = new Node(1); - n.left.right = new Node(6); - n.left.right.left = new Node(4); - n.left.right.right = new Node(7); - - //right - n.right = new Node(10); - n.right.right = new Node(14); - n.right.right.left = new Node(13); - - /* - 8 - 3 10 - 1 6 14 - 4 7 13 - * - * */ - System.out.println("Without zigzag"); - displayLevelOrder(n, false); //without zigzag - System.out.println("With zigzag"); - displayLevelOrder(n, true); //with zigzag - } - - private void displayLevelOrder(Node n, Boolean zigzag) { - if (n == null) { - return; - } - Boolean flag = true; - Deque parents = new LinkedList(); - parents.add(n); - while (!parents.isEmpty()) { - print(parents, flag, zigzag); - flag = !flag; - int count = parents.size(); - while (count-- > 0) { - Node removedParent = parents.removeFirst(); - if (removedParent.left != null) { - parents.addLast(removedParent.left); - } - if (removedParent.right != null) { - parents.addLast(removedParent.right); - } - } - System.out.println(); - } - } - - private void print(Deque parents, Boolean flag, Boolean zigzag) { - Iterator iterator = parents.descendingIterator(); - if (!zigzag || flag) { - iterator = parents.iterator(); - } - while (iterator.hasNext()) { - System.out.print(iterator.next().data + "\t"); - } - - } -} diff --git a/Binry_Search_Tree/MorrisTraversal.java b/Binry_Search_Tree/MorrisTraversal.java deleted file mode 100644 index f1f55eaa..00000000 --- a/Binry_Search_Tree/MorrisTraversal.java +++ /dev/null @@ -1,64 +0,0 @@ -package bst_operations; - - -/** - * Created by kalsi on 30/08/17. - * Inorder traversal without recursion or stacks - */ -public class MorrisTraversal { - public static void main(String[] args) { - new MorrisTraversal().demo(); - } - - private void demo() { - Node n = new Node(8); - // left - n.left = new Node(3); - n.left.left = new Node(1); - n.left.right = new Node(6); - n.left.right.left = new Node(4); - n.left.right.right = new Node(7); - - //right - n.right = new Node(10); - n.right.right = new Node(14); - n.right.right.left = new Node(13); - - /* - 8 - 3 10 - 1 6 14 - 4 7 13 - * - * */ - inorderIterative(n); - } - - - private void inorderIterative(Node n) { - while (n != null) { - if (n.left == null) { - System.out.print(n.data + "\t"); - n = n.right; - } - Node predecessor = getPredecessor(n); - if (predecessor.right == null) { - predecessor.right = n; - n = n.left; - } else { - predecessor.right = null; - System.out.print(n.data + "\t"); - n = n.right; - } - - } - } - - private Node getPredecessor(Node n) { - Node temp = n.left; - while( temp.right != null && temp.right != n) { - temp = temp.right; - } - return temp; - } -} diff --git a/Binry_Search_Tree/Node.java b/Binry_Search_Tree/Node.java deleted file mode 100644 index fb1343a6..00000000 --- a/Binry_Search_Tree/Node.java +++ /dev/null @@ -1,12 +0,0 @@ -package bst_operations; - -public class Node { - int data; - Node left; - Node right; - - public Node(int d) { - data = d; - } -} - diff --git a/Binry_Search_Tree/PathFromRootToNode.java b/Binry_Search_Tree/PathFromRootToNode.java deleted file mode 100644 index 3eeb94a2..00000000 --- a/Binry_Search_Tree/PathFromRootToNode.java +++ /dev/null @@ -1,63 +0,0 @@ -package bst_operations; - -import java.util.ArrayList; - -/** - * Created by kalsi on 30/08/17. - * Path from root to node - */ -public class PathFromRootToNode { - Boolean found = false; - public static void main(String[] args) { - new PathFromRootToNode().demo(); - } - - private void demo() { - Node n = new Node(8); - // left - n.left = new Node(3); - n.left.left = new Node(1); - n.left.right = new Node(6); - n.left.right.left = new Node(4); - n.left.right.right = new Node(7); - - //right - n.right = new Node(10); - n.right.right = new Node(14); - n.right.right.left = new Node(13); - - /* - 8 - 3 10 - 1 6 14 - 4 7 13 - * - * */ - ArrayList al = new ArrayList(); - findPath(n, 13, al); - display(al); - - } - - private void display(ArrayList al) { - System.out.println("--"); - for (Node n : al) { - System.out.print(n.data + " --> "); - } - System.out.println("NULL"); - } - - private void findPath(Node n, int data, ArrayList al) { - if (n != null && !found) { - al.add(n); - findPath(n.left, data, al); - if (n.data == data) { - found = true; - } - findPath(n.right, data, al); - if (!found) { - al.remove(al.size() - 1); - } - } - } -} diff --git a/C/Searching/BinarySearch.c b/C/Searching/BinarySearch.c deleted file mode 100644 index 2a58ca2a..00000000 --- a/C/Searching/BinarySearch.c +++ /dev/null @@ -1,42 +0,0 @@ -// C program to implement recursive Binary Search -#include - -// A recursive binary search function. It returns -// location of x in given array arr[l..r] is present, -// otherwise -1 -int binarySearch(int arr[], int l, int r, int x) -{ - if (r >= l) { - int mid = l + (r - l) / 2; - - // If the element is present at the middle - // itself - if (arr[mid] == x) - return mid; - - // If element is smaller than mid, then - // it can only be present in left subarray - if (arr[mid] > x) - return binarySearch(arr, l, mid - 1, x); - - // Else the element can only be present - // in right subarray - return binarySearch(arr, mid + 1, r, x); - } - - // We reach here when element is not - // present in array - return -1; -} - -int main(void) -{ - int arr[] = { 2, 3, 4, 10, 40 }; - int n = sizeof(arr) / sizeof(arr[0]); - int x = 10; - int result = binarySearch(arr, 0, n - 1, x); - (result == -1) ? printf("Element is not present in array") - : printf("Element is present at index %d", - result); - return 0; -} diff --git a/C/Searching/InterpolationSearch.c b/C/Searching/InterpolationSearch.c deleted file mode 100644 index 82db90b3..00000000 --- a/C/Searching/InterpolationSearch.c +++ /dev/null @@ -1,57 +0,0 @@ -// C program to implement interpolation search -#include - -// If x is present in arr[0..n-1], then returns -// index of it, else returns -1. -int interpolationSearch(int arr[], int n, int x) -{ - // Find indexes of two corners - int lo = 0, hi = (n - 1); - - // Since array is sorted, an element present - // in array must be in range defined by corner - while (lo <= hi && x >= arr[lo] && x <= arr[hi]) - { - if (lo == hi){ - if (arr[lo] == x) return lo; - return -1; - } - // Probing the position with keeping - // uniform distribution in mind. - int pos = lo + (((double)(hi-lo) / - (arr[hi]-arr[lo]))*(x - arr[lo])); - - // Condition of target found - if (arr[pos] == x) - return pos; - - // If x is larger, x is in upper part - if (arr[pos] < x) - lo = pos + 1; - - // If x is smaller, x is in the lower part - else - hi = pos - 1; - } - return -1; -} - -// Driver Code -int main() -{ - // Array of items on which search will - // be conducted. - int arr[] = {10, 12, 13, 16, 18, 19, 20, 21, 22, 23, - 24, 33, 35, 42, 47}; - int n = sizeof(arr)/sizeof(arr[0]); - - int x = 18; // Element to be searched - int index = interpolationSearch(arr, n, x); - - // If element was found - if (index != -1) - printf("Element found at index %d", index); - else - printf("Element not found."); - return 0; -} diff --git a/C/Searching/LinearSearch.c b/C/Searching/LinearSearch.c deleted file mode 100644 index 9c86389c..00000000 --- a/C/Searching/LinearSearch.c +++ /dev/null @@ -1,26 +0,0 @@ -// C code to linearly search x in arr[]. If x -// is present then return its location, otherwise -// return -1 - -#include - -int search(int arr[], int n, int x) -{ - int i; - for (i = 0; i < n; i++) - if (arr[i] == x) - return i; - return -1; -} - -int main(void) -{ - int arr[] = { 2, 3, 4, 10, 40 }; - int x = 10; - int n = sizeof(arr) / sizeof(arr[0]); - int result = search(arr, n, x); - (result == -1) ? printf("Element is not present in array") - : printf("Element is present at index %d", - result); - return 0; -} diff --git a/C/Searching/TernarySearch.C b/C/Searching/TernarySearch.C deleted file mode 100644 index 652ea1bd..00000000 --- a/C/Searching/TernarySearch.C +++ /dev/null @@ -1,52 +0,0 @@ - -#include -#include -#include - -int TernS(long int arr[], long int l, long int r, long int x) -{ - if (r >= l) - { - int mid1 = l + (r - l) / 3; // We calculate 2 mid points to divide the array into 3 parts - int mid2 = mid1 + (r - l) / 3; - if (arr[mid1] == x) - return mid1; - if (arr[mid2] == x) - return mid2; - if (arr[mid1] > x) - return TernS(arr, l, mid1 - 1, x); // We recursively search the 3 parts - if (arr[mid2] < x) - return TernS(arr, mid2 + 1, r, x); - return TernS(arr, mid1 + 1, mid2 - 1, x); - } - return -1; -} - -void main() -{ - srand(time(NULL)); - long int i, n, l = 0, r, te, bi; - clock_t s, e; - double tertime, bintime; - printf("Enter the number of elements in the array "); - scanf("%ld", &n); // The input is used to randomly generate inputs and decide the number of elements in the array - long int arr[n], x; - arr[0] = rand() % 6; - for (int i = 1; i < n; i++) - { - arr[i] = arr[i - 1] + rand() % 6 + 1; - } - x = arr[rand() % n]; - printf("Element chosen to be searched: %ld ", x); - r = n; - s = clock(); - te = TernS(arr, l, r, x); - e = clock(); - tertime = ((double)(e - s)) / (double)(CLOCKS_PER_SEC); // This calculates the time taken to do the ternary search - s = clock(); - if (te == -1) - printf("\nTernary Search: The element(%ld) is not in the array. Time taken to perform the function is %0.10e.", x, tertime); - else - printf("\nTernary Search: The position of %ld is %ld. Time taken to perform the function is %0.10e.", x, te + 1, tertime); - getch(); -} diff --git a/C/Sorting/Count Sort.c b/C/Sorting/Count Sort.c deleted file mode 100644 index c2047d72..00000000 --- a/C/Sorting/Count Sort.c +++ /dev/null @@ -1,49 +0,0 @@ -#include - -int get_max(int arr[],int n) -{ - int max=arr[0]; - for(int i=1;i=0;i--) - { - s_arr[temp[arr[i]] - 1]=arr[i]; - temp[arr[i]]--; - } -} - -int main() -{ - int n,arr[100],s_arr[100],m; - - printf("Enter size: "); - scanf("%d",&n); - printf("Enter elements: "); - for(int i=0;i -#include -#include -#define MAXS 20 - - -int front = 0,rear= -1,top=-1; -struct Node{ - char car[20]; - struct Node *ptr; - int n; -}; - -struct stack1{ - char soldCar[20]; -}; - -struct stack1 sold[20]; - -struct Node *insCar(struct Node *head){ - struct Node *NewNode; - struct Node *temp; - temp = head; - - NewNode = (struct Node*)malloc(sizeof(struct Node)); - if(NewNode == NULL){ - printf("SORRY MALLOC FAILD\n"); - } - - printf("enter the car brand name :"); - scanf("%s",(NewNode->car)); - - printf("How many %s car u have :",NewNode->car); - scanf("%d",&(NewNode->n)); - printf("\n"); - - NewNode->ptr = NULL; - if(temp == NULL){ - head = NewNode; - }else{ - while(temp->ptr != NULL) - temp = temp->ptr; - temp->ptr = NewNode; - } - - return head; - -} -void push(char c[]){ - strcpy(sold[++top].soldCar,c); -} - -void Buy(struct Node *head){ - printf("\n"); - printf("costumer %d :\n",top+2); - printf("which car u Want to Buy :"); - char costumerCar[20]; - scanf("%s",costumerCar); - struct Node *temp; - temp = head; - while(strcmp(temp->car,costumerCar)){ - temp = temp->ptr; - if(temp == NULL){ - break; - } - } - if(temp == NULL){ - printf("that car Brand is not available\n"); - Buy(head); - - }else{ - if(temp->n == 0){ - printf("Sorry Out of stock\n"); - Buy(head); - } - else{ - (temp->n)--; - push(costumerCar); - - } - } - -} - - -void display(struct Node *head){ - struct Node *temp; - temp = head; - printf("\n\n"); - - while(temp != NULL){ - printf("car : %s | amount : %d\n", temp->car,temp->n); - temp = temp->ptr; - } - } -int main(){ - struct Node *Head; - Head = NULL; - printf("**********************************\n\tWelcome to car shop\n**********************************"); - int b,i; - printf("\nHow many car Brand u Want to add :"); - scanf("%d",&b); - printf("\n"); - for(i=0;i -#include -#include -using namespace std; - -//Declaring Node -struct node -{ - int data; - struct node *next; - struct node *prev; -}*begin, *end; -int index = 0; - -//Declaring a Circular Doubly Linked List Class -class circular_dlist -{ - public: - node *create_node(int); - void insert_begin(); - void insert_end(); - void insert_position(); - void insert_before(); - void insert_after(); - void delete_begin(); - void delete_end(); - void delete_position(); - void show_list(); - - circular_dlist() - { - begin = NULL; - end = NULL; - } -}; - -//Menu driven main function -int main() -{ - int choice; - circular_dlist clist; - while (1) - { - cout<<"\n-----------------------"<>choice; - switch(choice) - { - case 1: - clist.insert_begin(); - break; - case 2: - clist.insert_end(); - break; - case 3: - clist.insert_position(); - break; - case 4: - clist.insert_before(); - break; - case 5: - clist.insert_after(); - break; - case 6: - clist.delete_begin(); - break; - case 7: - clist.delete_end(); - break; - case 8: - clist.delete_position(); - break; - case 9: - clist.show_list(); - break; - case 10: - exit(1); - default: - cout<<"Not a valid choice"<data = info; - tmp->next = NULL; - tmp->prev = NULL; - return tmp; -} - -//Function to insert a node at the beginning -void circular_dlist::insert_begin() -{ - int info; - cout<>info; - struct node *tmp; - tmp = create_node(info); - if (begin == end && begin == NULL) - { - cout<<"List created and first element inserted!"<next = end->next = NULL; - begin->prev = end->prev = NULL; - } - else - { - tmp->next = begin; - begin->prev = tmp; - begin = tmp; - begin->prev = end; - end->next = begin; - cout<<"Node inserted"<>info; - struct node *tmp; - tmp = create_node(info); - if (begin == end && begin == NULL) - { - cout<<"List created and first element inserted!"<next = end->next = NULL; - begin->prev = end->prev = NULL; - } - else - { - end->next = tmp; - tmp->prev = end; - end = tmp; - begin->prev = end; - end->next = begin; - } -} - -//Function to insert a node at a specific position by index -void circular_dlist::insert_position() -{ - int info, pst, i; - cout<>info; - cout<>pst; - struct node *tmp, *s, *p; - tmp = create_node(info); - if (begin == end && begin == NULL) - { - if (pst == 1) - { - begin = end = tmp; - begin->next = end->next = NULL; - begin->prev = end->prev = NULL; - } - else - { - cout<<"Index inserted is out of range"<next; - if (i == pst - 1) - { - p->next = tmp; - tmp->prev = p; - tmp->next = s; - s->prev = tmp; - cout<<"Node inserted"<>info; - s = begin; - for (i = 0;i < index;i++) - { - pst++; - if (s->data == info) - { - cond = true; - break; - } - s = s->next; - } - if (!cond) - { - cout<<"No match found in the list"<>info1; - struct node *tmp, *p; - tmp = create_node(info1); - if (begin == end && begin == NULL) - { - if (pst == 1) - { - begin = end = tmp; - begin->next = end->next = NULL; - begin->prev = end->prev = NULL; - } - } - else - { - s = begin; - for (i = 1;i <= index;i++) - { - p = s; - s = s->next; - if (i == pst - 1) - { - p->next = tmp; - tmp->prev = p; - tmp->next = s; - s->prev = tmp; - cout<<"Node inserted"<>info; - s = begin; - for (i = 0;i < index;i++) - { - pst++; - if (s->data == info) - { - cond = true; - break; - } - s = s->next; - } - if (!cond) - { - cout<<"No match found in the list"<>info1; - struct node *tmp, *p; - tmp = create_node(info1); - if (begin == end && begin == NULL) - { - if (pst == 1) - { - begin = end = tmp; - begin->next = end->next = NULL; - begin->prev = end->prev = NULL; - } - } - else - { - s = begin; - for (i = 1;i <= index;i++) - { - p = s; - s = s->next; - if (i == pst - 1) - { - p->next = tmp; - tmp->prev = p; - tmp->next = s; - s->prev = tmp; - cout<<"Node inserted"<next = s->next; - s->next->prev = end; - begin = s->next; - free(s); - cout<<"First Node Deleted"<next; - p = s->prev; - } - p->next = s->next; - s->next->prev = p; - end = p; - index--; - free(s); - cout<<"Node Deleted"<>pst; - if (index < pst) - { - cout<<"Index inserted is out of range"<next = s->next; - s->next->prev = end; - begin = s->next; - free(s); - cout<<"Node Deleted"<next; - p = s->prev; - } - p->next = s->next; - s->next->prev = p; - if (pst == index) - { - end = p; - } - index--; - free(s); - cout<<"Node Deleted"<data<<"<->"; - s = s->next; - } - cout<data<=0){ - queue_arr[length--]=-1729; //this number cannot be the value of any index. Make it settable. - } - start=-1; - end=-1; - } - - - //overloaded constructor that takes the prohibited value of the queue - public CircularQueue(int length,int c){ - queue_arr=new int[length--]; - this.c=c; - size=length; - while(length>=0){ - queue_arr[length]=c; //this number cannot be the value of any index. Make it settable. - } - start=-1; - end=-1; - } - - //enter an element in the end of queue - public void pushEnd(int value){ - try{ - if( end==start && end !=-1 && queue_arr[end]!=c ) - { - System.out.println("Out of Bounds"); - } - else if(end==start && start==-1){ - end=start=0; - queue_arr[end]=value; - end++; - } - else - { - if(end==size){ - end=0; - } - - queue_arr[end]=value; - end++; - if(end==size){ - end =0;//when elements have not been popped out from queue - } - } - } - - catch(Exception e){ - System.err.println(e); - } - } - - - - //pull out an element from the queue - public int popFront(){ - if(start==size){ - start=0; - } - if(start==-1 || (start==end && queue_arr[start]==c)){ - System.err.println("IndexOutOfBounds"); - - // throw new emptyStackException; - return -1; //Some developer desired constant (not necessary) - } - int ret= queue_arr[start]; - queue_arr[start++]=c; - return ret; - - } - - //checks if the stack is empty - public boolean isEmpty(){ - return start==end && (start==-1 || queue_arr[start]==c); - } - - //checks if stack is full - public boolean isFull(){ - return start==end && queue_arr[end]==c; - } - - //main method to test the code - public static void main(String args[]){ - Scanner sc=new Scanner(System.in); - - System.out.println("Enter the size of Queue"); - int length=sc.nextInt(); - CircularQueue ob=new CircularQueue(length); - - char ch='y';//default set to yes, can be changed to No, doesnot matter - do{ - System.out.println("1-Add an element to the Queue at the end\n2-Check wether the Queue is empty\n3-Return/Delete an Element from the Front\n4-Check if Queue is Full"); - System.out.println("Enter the option"); - int opt=sc.nextInt();//option choosen by the user - switch(opt){ - case 1: - System.out.println("Enter the element to be inserted to Queue"); - int elem=sc.nextInt(); - ob.pushEnd(elem); - break; - case 2: - if(ob.isEmpty()){ - System.out.println("Queue is Empty"); - } - else{ - System.out.println("Queue is Not Empty"); - } - break; - case 3: - System.out.println("Enter the deleted/returned is :"+ob.popFront()); - break; - case 4: - if(ob.isFull()){ - System.out.println("Queue is Full"); - } - else{ - System.out.println("Queue is not Full"); - } - default: - System.out.println("Wrong input"); - } - System.out.println("Do you want to continue for more inputs?(y/n)");//Any random character will be considered as No - ch=sc.next().charAt(0); - ch=Character.toLowerCase(ch); - - }while(ch=='y'); - - /*created Date Monday October 7 2019 1800 hrs - last modified Saturday October 12 2019 1913 hrs - - Further Edits:Change options according to the methods - Check -1 or 0 cases of queue - Run and debug the codes - Add better comments - -*/ - - - } - -} diff --git a/Circular_Dobely_Link_List/Circular_Dobely_Link_List_Output.txt b/Circular_Dobely_Link_List/Circular_Dobely_Link_List_Output.txt deleted file mode 100644 index 4bdca486..00000000 --- a/Circular_Dobely_Link_List/Circular_Dobely_Link_List_Output.txt +++ /dev/null @@ -1,86 +0,0 @@ -1 for insert value at front: -2 for insert value at rear: -3 for insert value according to order: -4 for insert value after specific value: -5 for delete from front -6 for delete from end -7 for delete specific value -8 for display -9 for Exit -1 -Enter the value you want to insert: -12 -Enter which type of the operation you want to apply: -1 -Enter the value you want to insert: -14 -Enter which type of the operation you want to apply: -2 -Enter the value you want to insert: -54 -Enter which type of the operation you want to apply: -2 -Enter the value you want to insert: -32 -Enter which type of the operation you want to apply: -3 -Enter the value you want to insert: -5 -Enter which type of the operation you want to apply: -4 -Enter the value you want to insert: -54 -Enter after which value you want to insert: -32 -Enter which type of the operation you want to apply: -8 -The Numbers in list is..5 14 12 54 32 54 -Enter which type of the operation you want to apply: -5 -5 is removed -Enter which type of the operation you want to apply: -8 -The Numbers in list is..14 12 54 32 54 -Enter which type of the operation you want to apply: -6 -54 is removed -Enter which type of the operation you want to apply: -8 -The Numbers in list is..14 12 54 32 -Enter which type of the operation you want to apply: -7 -Enter value you want to be deleted -12 -12 is removed -Enter which type of the operation you want to apply: -8 -The Numbers in list is..14 54 32 -Enter which type of the operation you want to apply: -6 -32 is removed -Enter which type of the operation you want to apply: -7 -Enter value you want to be deleted -14 -14 is removed -Enter which type of the operation you want to apply: -8 -The Numbers in list is..54 -Enter which type of the operation you want to apply: -2 -Enter the value you want to insert: -10 -Enter which type of the operation you want to apply: -6 -10 is removed -Enter which type of the operation you want to apply: -6 -54 is removed -Enter which type of the operation you want to apply: -6 -The list is Empty -Enter which type of the operation you want to apply: -9 - -Process returned 0 (0x0) execution time : 172.692 s -Press any key to continue. diff --git a/Circular_Dobely_Link_List/Circulr_Dobely_Link_List.c b/Circular_Dobely_Link_List/Circulr_Dobely_Link_List.c deleted file mode 100644 index 718f06d5..00000000 --- a/Circular_Dobely_Link_List/Circulr_Dobely_Link_List.c +++ /dev/null @@ -1,299 +0,0 @@ -#include -#include - -struct Node{ - int value; - struct Node* lptr; - struct Node* rptr; -}; - -void insert_front(struct Node**L,struct Node**R,int val){ - struct Node* newNode; - newNode = (struct Node*)malloc(sizeof(struct Node)); - - newNode->value = val; - - if(*L==NULL){ - newNode->lptr= newNode; - newNode->rptr= newNode; - *L=*R=newNode; - return; - } - newNode->rptr = *L; - newNode->lptr = *R; - (*L)->lptr = newNode; - *L = newNode; - (*R)->rptr = *L; // make circular List -} - -void insert_rear(struct Node**L,struct Node**R,int val){ - struct Node* newNode; - newNode = (struct Node*)malloc(sizeof(struct Node)); - - newNode->value = val; - - if(*L==NULL){ - newNode->lptr= newNode; - newNode->rptr= newNode; - *L=*R=newNode; - return; - } - newNode->lptr = *R; - newNode->rptr = *L; - (*R)->rptr = newNode; - *R = newNode; - (*L)->lptr = *R; //make Circular List -} - -void insert_order(struct Node**L,struct Node**R,int val){ - struct Node* newNode; - struct Node* temp = *L; - newNode = (struct Node*)malloc(sizeof(struct Node)); - - newNode->value = val; - - if(*L==NULL){ - newNode->lptr= newNode; - newNode->rptr= newNode; - *L=*R=newNode; - return; - } - - if(val <= (*L)->value){ - newNode->rptr = *L; - newNode->lptr = *R; - (*L)->lptr = newNode; - *L = newNode; - (*R)->rptr = *L; - } - else if(val >= (*R)->value){ - newNode->lptr = *R; - newNode->rptr = *L; - (*R)->rptr = newNode; - *R = newNode; - (*L)->lptr = *R; - } - else{ - while(temp->value <= val) - temp = temp->rptr; - - newNode->lptr = temp->lptr; - newNode->rptr = temp; - temp->lptr->rptr = newNode; - temp->lptr = newNode; - } -} - -void insert_specs(struct Node**L,struct Node**R,int val){ - int sval,n; - struct Node* newNode; - struct Node* temp = *L; - newNode = (struct Node*)malloc(sizeof(struct Node)); - - newNode->value = val; - //here our list is not empty because user give us value which we have to put after some value, - //so the list is not empty and we dont have to write condition for null list - - do{ - printf("Enter after which value you want to insert:\n"); - scanf("%d",&sval); - n=1; //initialize n here. - temp = *L; //initialize temp also because if one time value not found then temp comes to head part again for new loop - while(temp->value != sval){ - temp = temp->rptr; - - if(temp== NULL){ //this condition is special because written in while loop - printf("your given value is not found\n"); - n=0; - break; - } - } - }while(n==0); - - newNode->lptr = temp; - newNode->rptr = temp->rptr; - if(temp != *R) temp->rptr->lptr = newNode; // write this always above the below sentences. - temp->rptr = newNode; - - if(temp == *R){ //upgradation of rear and front value - *R = newNode; - (*L)->lptr = *R; - } -} - - -void del_front(struct Node** Head,struct Node ** Rear){ - struct Node * delete; - - if(*Head == NULL){ - printf("The list is Empty\n"); - return; - } - if((*Head)->rptr == NULL && (*Head)->lptr == NULL){ - printf("%d is removed\n",(*Head)->value); - free(*Head); - *Head = *Rear = NULL; - return; - } - delete = *Head; - *Head = (*Head)->rptr; - (*Head)->lptr = *Rear; - (*Rear)->rptr = *Head; - printf("%d is removed\n",delete->value); - free(delete); -} - -void del_end(struct Node** Head,struct Node** Rear){ - struct Node * delete; - - if(*Head == NULL){ - printf("The list is Empty\n"); - return; - } - if(*Head == *Rear){ - printf("%d is removed\n",(*Head)->value); - free(*Head); - *Head = *Rear = NULL; - return; - } - - delete = *Rear; - (*Rear) = (*Rear)->lptr; - (*Rear)->rptr = *Head; - (*Head)->lptr = *Rear; - printf("%d is removed\n",delete->value); - free(delete); -} - -void del_specific(struct Node** Head,struct Node** Rear,int val){ - struct Node * temp; - temp = *Head; - - if(*Head == NULL){ - printf("The list is Empty\n"); - return; - } - - if( val == (*Head)->value){ - if(*Head == *Rear){ //condition for only Node - printf("%d is removed\n",(*Head)->value); - free(*Head); - *Head = *Rear = NULL; - } - else{ - temp = *Head; - *Head = (*Head)->rptr; - (*Head)->lptr = *Rear; - (*Rear)->rptr = *Head; - printf("%d is removed\n",temp->value); - free(temp); - } - } - else if(val == (*Rear)->value){ - - temp = *Rear; - (*Rear) = (*Rear)->lptr; - (*Rear)->rptr = *Head; - (*Head)->lptr = *Rear; - printf("%d is removed\n",temp->value); - free(temp); - } - else{ - while (temp != NULL && temp->value != val) - temp = temp->rptr; - - if(temp == NULL){ - printf("Value is not found\n"); - return; - } - //temp points to the node to be deleted - - temp->lptr->rptr = temp->rptr; - temp->rptr->lptr = temp->lptr; - printf("%d is removed\n",temp->value); - free(temp); - } -} - -void display(struct Node * H){ - struct Node *temp; - temp = H; - - if(temp == NULL){ - printf("List is empty\n"); - return; - } - - printf("The Numbers in list is.."); - do{ - printf("%d ",temp->value); - temp = temp->rptr; - }while(temp!= H); - printf("\n"); - -} - - -int main(){ - struct Node* Head; - struct Node* Rear; - Head = Rear = NULL; - int val,n,sval; - - printf("1 for insert value at front:\n"); - printf("2 for insert value at rear:\n"); - printf("3 for insert value according to order:\n"); - printf("4 for insert value after specific value:\n"); - printf("5 for delete from front\n"); - printf("6 for delete from end\n"); - printf("7 for delete specific value\n"); - printf("8 for display\n"); - printf("9 for Exit\n"); - scanf("%d",&n); - - while(n !=9){ - - switch(n){ - case 1: - printf("Enter the value you want to insert:\n"); - scanf("%d",&val); - insert_front(&Head,&Rear,val); - break; - case 2: - printf("Enter the value you want to insert:\n"); - scanf("%d",&val); - insert_rear(&Head,&Rear,val); - break; - case 3: - printf("Enter the value you want to insert:\n"); - scanf("%d",&val); - insert_order(&Head,&Rear,val); - break; - case 4: - printf("Enter the value you want to insert:\n"); - scanf("%d",&val); - insert_specs(&Head,&Rear,val); - break; - case 5: - del_front(&Head,&Rear); - break; - case 6: - del_end(&Head,&Rear); - break; - case 7: - printf("Enter value you want to be deleted\n"); - scanf("%d",&val); - del_specific(&Head,&Rear,val); - break; - case 8: - display(Head); - break; - default: - printf("Enter specific value:\n"); - } - - printf("Enter which type of the operation you want to apply:\n"); - scanf("%d",&n); - } - return 0; -} diff --git a/Circular_Link_List/Circular_Link_List.c b/Circular_Link_List/Circular_Link_List.c deleted file mode 100644 index b32a930f..00000000 --- a/Circular_Link_List/Circular_Link_List.c +++ /dev/null @@ -1,117 +0,0 @@ -#include -#include - -struct node{ - int value; - struct node *ptr; -}; - -struct node* insertOrder(struct node *head,int val){ - struct node* newNode,*temp; - temp = head; - newNode = (struct node*)malloc(sizeof(struct node)); - - newNode->value = val; - - if(head == NULL){ - newNode->ptr = newNode; - head = newNode; - } - else if(val <= head->value){ - newNode->ptr = head; - - while(temp->ptr != head) - temp = temp->ptr; - - head = newNode; // initialize head part here always - temp->ptr = head; - } - else{ - while(temp->ptr != head && val > temp->ptr->value) - temp = temp->ptr; - - newNode->ptr = temp->ptr; - temp->ptr = newNode; - } - return head; -} - - -struct node* deleteVal(struct node* head,int val){ - struct node* temp,*nodeDeleted; - temp = head; - if(head == NULL){ - printf("List is empty\n"); - } - else if(head->value == val){ - while(temp->ptr != head) { - temp = temp->ptr; - } - temp->ptr = head->ptr; - free(head); - head = temp->ptr; - } - else{ - temp = head; - while (temp->ptr != head && val != temp->ptr->value) { - temp = temp->ptr; - } - - if(temp->ptr == head) - printf("Given value is not Found\n"); - else{ - nodeDeleted = temp->ptr; - temp->ptr = temp->ptr->ptr; - free(nodeDeleted); - } - } - return head; -} - -void display(struct node* head){ - struct node*temp = head; - if(head == NULL) - printf("The list is Empty\n"); - else{ - do{ - printf("%d ",temp->value); - temp = temp->ptr; - } while(temp != head); - printf("\n"); - } -} - -void main(){ - struct node * head; - head= NULL; - int n,val; - - printf("Enter 1 for insert in order\n"); - printf("Enter 2 for delete value\n"); - printf("Enter 3 for display\n"); - printf("Enter 4 for EXIT\n"); - scanf("%d",&n); - - while(n!= 4){ - switch(n){ - case 1: - printf("Enter value you want to inserted\n"); - scanf("%d",&val); - head = insertOrder(head,val); - break; - case 2: - printf("Enter value you want to delete\n"); - scanf("%d",&val); - head = deleteVal(head,val); - break; - case 3: - display(head); - break; - default: - printf("Enter proper value\n"); - } - printf("Enter 1/2/3/4\n"); - scanf("%d",&n); - } - -} diff --git a/Circular_Link_List/Circular_Link_List_Output.txt b/Circular_Link_List/Circular_Link_List_Output.txt deleted file mode 100644 index 403c6844..00000000 --- a/Circular_Link_List/Circular_Link_List_Output.txt +++ /dev/null @@ -1,46 +0,0 @@ -Enter 1 for insert in order -Enter 2 for delete value -Enter 3 for display -Enter 4 for EXIT -1 -Enter value you want to inserted -12 -Enter 1/2/3/4 -1 -Enter value you want to inserted -54 -Enter 1/2/3/4 -1 -Enter value you want to inserted -87 -Enter 1/2/3/4 -3 -12 54 87 -Enter 1/2/3/4 -2 -Enter value you want to delete -54 -Enter 1/2/3/4 -3 -12 87 -Enter 1/2/3/4 -2 -Enter value you want to delete -12 -Enter 1/2/3/4 -3 -87 -Enter 1/2/3/4 -2 -Enter value you want to delete -87 -Enter 1/2/3/4 -2 -Enter value you want to delete -32 -Given value is not Found -Enter 1/2/3/4 -4 - -Process returned 4 (0x4) execution time : 80.470 s -Press any key to continue. \ No newline at end of file diff --git a/Circular_Link_List/josephus_problem.c b/Circular_Link_List/josephus_problem.c deleted file mode 100644 index 3188e4a3..00000000 --- a/Circular_Link_List/josephus_problem.c +++ /dev/null @@ -1,243 +0,0 @@ -#include -#include - -// Node of the circular linked list. -typedef struct Node -{ - int data; - struct Node *next; -} Node; - -// Handle of the list. -// Head points to the first node in the list. -// Tail points to the last node in the list. -typedef struct List -{ - Node *head; - Node *tail; - int length; -} List; - -// Initializes a cirucular linked list. -List* initialize_list(); - -// Creates a node and stores the data in it. -Node* create_node(int data); - -// Inserts data at the head of the list. -void insert_head(List* cll, int data); - -// Deletes the node at the head position. No operation if list is empty. -void delete_head(List* cll); - -// Swaps the first(Head) and last(Tail) element. -void swap_first_and_last(List* cll); - -// Prints the data present in the safe node according to the josephus problem. -int josephus(List* cll, int k); - -// Prints the entire list. Prints "EMPTY" if the list is empty. -void display(List* cll); - -// Deallocates resources held by the list. -void destroy_list(List* cll); - - -int main() -{ - List* cll = initialize_list(); - int ele, choice, pos, k; - do - { - scanf("%d",&choice); - switch(choice) - { - // Insert at Head. - case 1: - scanf("%d",&ele); - insert_head(cll,ele); - break; - - // Delete at Head. - case 2: - delete_head(cll); - break; - - // Josephus problem. - case 3: - scanf("%d",&k); - ele = josephus(cll,k); - printf("%d\n",ele); - break; - - // Swap first and last element. - case 4: - swap_first_and_last(cll); - break; - - // Print entire list. - case 5: - display(cll); - break; - } - } - while (choice != 0); - - destroy_list(cll); - return 0; -} - -List* initialize_list() -{ - List *list =(List*)malloc(sizeof(List)); - list->head=NULL; - list->tail=NULL; - list->length=0; - return list; -} - -Node* create_node(int data) -{ - Node *temp=(Node*)malloc(sizeof(Node)); - temp->data=data; - temp->next=NULL; - return temp; -} - -void insert_head(List* cll, int data) -{ - - Node *temp=create_node(data); - if(cll->head==NULL) // if(length==0) - { - temp->next=temp; - cll->head=temp; - cll->tail=temp; - } - else - { - temp->next=cll->head; - cll->tail->next=temp; - cll->head=temp; - }// TODO - cll->length+=1; - return; -} - -void delete_head(List* cll) -{ - if(cll->head==NULL) - { - return; - } - else if(cll->head->next==cll->head) - { - free(cll->head); - cll->head=NULL; - cll->tail=NULL; - cll->length-=1; - return; - } - else - { - Node *temp=cll->head; - cll->head=temp->next; - free(temp); - cll->tail->next=cll->head; - cll->length-=1; - return; - } -} - -void swap_first_and_last(List *cll) -{ - if(cll->length==0) - { - return; - } - else if(cll->length==1) - { - return; - } - else - { - int temp1; - temp1=cll->head->data; - cll->head->data=cll->tail->data; - cll->tail->data=temp1; - } -} - -int josephus(List *cll, int k) -{ - if(cll->length==0 || cll->length==1) - { - return; - } - else if(cll->length==2) - { - if(k==0) - { - free(cll->tail); - cll->tail=cll->head; - - } - else - { - free(cll->head); - cll->head=cll->tail; - } - cll->length-=1; - return cll->head->data; - } - else - { - Node *temp=cll->head; - for(int i=0;inext; - } - Node *kill; - while(cll->length>1) - { - kill=temp->next; - temp->next=kill->next; - temp=kill->next; - free(kill); - cll->length-=1; - } - cll->head=temp; - cll->tail=temp; - return temp->data; - } -} - -void display(List* cll) -{ - Node *temp=cll->head; - if(cll->head==NULL) - { - printf("EMPTY"); - } - else - { - while(temp->next!=cll->head) - { - printf("%d ",temp->data); - temp=temp->next; - } - printf("%d ",temp->data); - }printf("\n"); -} - -void destroy_list(List* cll) -{ - Node *temp; - while(cll->length!=0) - { - temp=cll->head; - cll->head=temp->next; - free(temp); - cll->length-=1; - } -} diff --git a/Circular_Queue/Circular Queue b/Circular_Queue/Circular Queue deleted file mode 100644 index 7d3b6772..00000000 --- a/Circular_Queue/Circular Queue +++ /dev/null @@ -1,98 +0,0 @@ -Implementation using C++ programming - -#include -#define SIZE 5 /* Size of Circular Queue */ - -using namespace std; - -class Queue { -private: - int items[SIZE], front, rear; - -public: - Queue(){ - front = -1; - rear = -1; - } - - bool isFull(){ - if(front == 0 && rear == SIZE - 1){ - return true; - } - if(front == rear + 1) { - return true; - } - return false; - } - - bool isEmpty(){ - if(front == -1) return true; - else return false; - } - - void enQueue(int element){ - if(isFull()){ - cout << "Queue is full"; - } else { - if(front == -1) front = 0; - rear = (rear + 1) % SIZE; - items[rear] = element; - cout << endl << "Inserted " << element << endl; - } - } - - int deQueue(){ - int element; - if(isEmpty()){ - cout << "Queue is empty" << endl; - return(-1); - } else { - element = items[front]; - if(front == rear){ - front = -1; - rear = -1; - } /* Q has only one element, so we reset the queue after deleting it. */ - else { - front=(front+1) % SIZE; - } - return(element); - } - } - - void display() - { - /* Function to display status of Circular Queue */ - int i; - if(isEmpty()) { - cout << endl << "Empty Queue" << endl; - } - else - { - cout << "Front -> " << front; - cout << endl << "Items -> "; - for(i=front; i!=rear;i=(i+1)%SIZE) - cout << items[i]; - cout << items[i]; - cout << endl << "Rear -> " << rear; - } - } - -}; - - -int main() -{ - Queue q; - - - - - q.enQueue(1); - q.enQueue(2); - q.enQueue(3); - q.enQueue(4); - q.enQueue(5); - q.enQueue(6); - q.deQueue(); - q.deQueue(); -} diff --git a/Circular_Queue/CircularQueue.py b/Circular_Queue/CircularQueue.py deleted file mode 100644 index a55bc0c4..00000000 --- a/Circular_Queue/CircularQueue.py +++ /dev/null @@ -1,62 +0,0 @@ -class CircularQueue(object): - def __init__(self, size): - """ - Initializing CircularQueue - """ - self.size = size - self._front = -1 - self._rear = -1 - self.queue = [None] * size - - - def front(self): - """ - Return the value at the front of the queue - """ - return self.queue[self._front] - - - def rear(self): - """ - Return the value at the rear of the queue - """ - return self.queue[self._rear] - - - def enqueue(self, value): - """ - Insert value into the rear of the circular queue - """ - if (self._rear == self.size - 1 and self._front == 0) or (self._rear == self._front-1): - # Queue is full - raise OverflowError("CircularQueue is full") - elif (self._rear == -1): - # Setting up for first item - self._rear = 0 - self._front = 0 - else: - # Calculating rear index after insert - self._rear = (self._rear + 1) % self.size - - self.queue[self._rear] = value - - - def dequeue(self): - """ - Remove and return the value at the front of the circular queue - """ - if(self._front==-1): - # Queue is empty - raise IndexError("CircularQueue is empty") - elif(self._front == self._rear): - # Remove the last remaning value and indicate the queue empty - value = self.queue[self._front] - self._front = -1 - self._rear = -1 - return value - else: - # Return the value at the front of the queue - value = self.queue[self._front] - self.queue[self._front] = None - self._front = (self._front + 1 ) % self.size - return value \ No newline at end of file diff --git a/Circular_Queue/Circular_Queue.c b/Circular_Queue/Circular_Queue.c deleted file mode 100644 index bfe0c923..00000000 --- a/Circular_Queue/Circular_Queue.c +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include -#define MAXSIZE 5 -int front=-1,rear=-1,q[MAXSIZE]; -void insert(int a){ - if(front==(rear+1)%MAXSIZE) printf("Error!!\nQueue is full!!!\n"); - else{ - if(front==-1) front=0; - rear =(rear+1)%MAXSIZE; - q[rear]=a; - } -} - -void delete(){ - if(front==rear){ - printf("Error!! \n Underflow condition\n Queue is empty\n"); - } - else{ - q[front]=0; - front=(front+1)%MAXSIZE; - printf("element successfully delete\n"); - } -} - -void display(){ - int i=0; - for(i=0;i const data = [5,6,1,4,5,7,8,2,4,6,3,2,6,6,3,5,5,2,10]; -> counter(data, 5); -Output: -<- 4 -*/ \ No newline at end of file diff --git a/Counting_Sort_C/countingSort.c b/Counting_Sort_C/countingSort.c deleted file mode 100644 index 72009223..00000000 --- a/Counting_Sort_C/countingSort.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - input: a unsorted array - output: a sorted array using Counting Sort algorithm. -*/ - -#include -#include - -#define MAX 100 - -void countingSort(int *vet,int *org, int tam){ - int i, j, k; - int baldes[MAX]; - - //Cria um vetor zerado com todos os indices de dados possíveis - for(i = 0 ; i < MAX ; i++) - baldes[i] = 0; - - //Marca as ocorrência dos dados a serem ordenados no vetor dos indices - for(i = 0 ; i< tam ; i++) - baldes[vet[i]]++; - - //Faz a distribuição de quantidade ao longo de todo o vetor. - for(i = 1 ; i < MAX ; i++){ - baldes[i] = baldes[i] + baldes[i-1]; - } - - //Preenche o vetor de saída com as ocorrência presentes no vetor de indice. - for(i = (tam-1) ; i >=0 ; i--){ - org[baldes[vet[i]]] = vet[i]; - baldes[vet[i]]--; - } -} - -int main(){ - int *vet, *org; - int i, tam; - - printf("Entre com o tamanho do vetor: "); - scanf("%d", &tam); - - - vet = (int*)malloc(tam*sizeof(int)); - org = (int*)malloc(tam*sizeof(int)); - - printf("Entre com os valores dos elementos\n"); - for(i = 0 ; i < tam ; i++){ - printf("Elemento %d: ",i+1); - scanf("%d", &vet[i]); - } - - printf("Seu vetor desordenado\n"); - for(i = 0 ; i < tam ; i++){ - printf("%d\t", vet[i]); - } - - countingSort(vet, org,tam); - - printf("\nSeu vetor Ordenado\n"); - for(i = 1 ; i < tam+1 ; i++){ - printf("%d\t", org[i]); - } - return 0; -} \ No newline at end of file diff --git a/DFS_FILE.pdf b/DFS_FILE.pdf deleted file mode 100644 index 6dba4338..00000000 Binary files a/DFS_FILE.pdf and /dev/null differ diff --git a/Dequeue/deque.cpp b/Dequeue/deque.cpp deleted file mode 100644 index d4b4466f..00000000 --- a/Dequeue/deque.cpp +++ /dev/null @@ -1,191 +0,0 @@ -# include -# define MAX 5 -int deque_arr[10]; -int left = -1; -int right = -1; - -/*Begin of insert_right*/ -void insert_right() -{ - int added_item; - if((left == 0 && right == MAX-1) || (left == right+1)) - { cout<<"Queue Overflow\n"; - return;} - if (left == -1) /* if queue is initially empty */ - { left = 0; - right = 0;} - else - if(right == MAX-1) /*right is at last position of queue */ - right = 0; - else - right = right+1; - cout<<"Input the element for adding in queue : "; - cin>>added_item; - deque_arr[right] = added_item ; -} -/*End of insert_right*/ - -/*Begin of insert_left*/ -void insert_left() -{ int added_item; - if((left == 0 && right == MAX-1) || (left == right+1)) - { cout<<"Queue Overflow\n"; - return; } - if (left == -1)/*If queue is initially empty*/ - { left = 0; - right = 0; } - else - if(left== 0) - left=MAX-1; - else - left=left-1; - cout<<"Input the element for adding in queue : "; - cin>>added_item; - deque_arr[left] = added_item ; - } -/*End of insert_left*/ - -/*Begin of delete_left*/ -void delete_left() -{ if (left == -1) - { cout<<"Queue Underflow\n"; - return ; } - cout<<"Element deleted from queue is : "<>choice; - - switch(choice) - { case 1: - insert_right(); - break; - case 2: - delete_left(); - break; - case 3: - delete_right(); - break; - case 4: - display_queue(); - break; - case 5: - break; - default: - cout<<"Wrong choice\n"; - } - }while(choice!=5); -} -/*End of input_que*/ - -/*Begin of output_que*/ -void output_que() -{ int choice; - do - { cout<<"1.Insert at right\n"; - cout<<"2.Insert at left\n"; - cout<<"3.Delete from left\n"; - cout<<"4.Display\n"; - cout<<"5.Quit\n"; - cout<<"Enter your choice : "; - cin>>choice; - switch(choice) - { - case 1: - insert_right(); - break; - case 2: - insert_left(); - break; - case 3: - delete_left(); - break; - case 4: - display_queue(); - break; - case 5: - break; - default: - cout<<"Wrong choice\n"; - } - }while(choice!=5); -} -/*End of output_que*/ - -/*Begin of main*/ -main() -{ int choice; - cout<<"1.Input restricted dequeue\n"; - cout<<"2.Output restricted dequeue\n"; - cout<<"Enter your choice : "; - cin>>&choice); - switch(choice) - { - case 1 : - input_que(); - break; - case 2: - output_que(); - break; - default: - cout<<"Wrong choice\n"; - } -} -/*End of main*/ diff --git a/Doble_Link_List/Doble_Link_List.c b/Doble_Link_List/Doble_Link_List.c deleted file mode 100644 index 508825eb..00000000 --- a/Doble_Link_List/Doble_Link_List.c +++ /dev/null @@ -1,176 +0,0 @@ -#include -#include - -struct node{ - int value; - struct node *lptr,*rptr; -}; - -void insert(struct node **LH,struct node **RH,int val){ - struct node *NewNode,*temp; - temp = *LH; - NewNode = (struct node*)malloc(sizeof(struct node)); - NewNode->value = val; - if(*LH == NULL && *RH == NULL){ - (*LH) = (*RH) = NewNode; - NewNode->lptr = NewNode->rptr =NULL; - }else{ - printf("1) insert at first\n2) insert at last\n3) insert at order\nWhere u want to insert :"); - int n; - scanf("%d",&n); - if(n==1){ - // insert at front - NewNode->rptr = *LH; - NewNode->lptr = NULL; - (*LH)->lptr = NewNode; - *LH = NewNode; - }else if(n==2){ - NewNode->rptr = NULL; - NewNode->lptr = *RH; - (*RH)->rptr = NewNode; - *RH = NewNode; - }else if(n==3){ - if(temp->lptr == NULL){ - if(temp->value >= val){ - NewNode->rptr = *LH; - NewNode->lptr = NULL; - (*LH)->lptr = NewNode; - *LH = NewNode; - } - else{ - NewNode->rptr = NULL; - NewNode->lptr = *RH; - (*RH)->rptr = NewNode; - *RH = NewNode; - } - return; - } - if(temp->value >= val){ - NewNode->rptr = *LH; - NewNode->lptr = NULL; - (*LH)->lptr = NewNode; - *LH = NewNode; - return; - } - - while(temp->rptr->value <= val) - temp = temp->rptr; - if(temp->rptr == *RH){ - NewNode->rptr = NULL; - NewNode->lptr = *RH; - (*RH)->rptr = NewNode; - *RH = NewNode; - }else{ - NewNode->lptr = temp; - NewNode->rptr = temp->rptr; - temp->rptr->lptr = NewNode; - temp->rptr = NewNode; - } - } - else printf("\nsomething wrong"); - } -} -void Display(struct node **LH,struct node ** RH){ - struct node *temp; - temp = *LH; - while(temp != NULL){ - printf(" %d ",temp->value); - temp = temp->rptr; - } - printf("\n"); -} - -void delete_Link(struct node **LH,struct node **RH){ - struct node *NodetoBeDeleted,*temp; - int n, val; - temp = *LH; - if(*LH == NULL ){ - printf("\nsorry Link List is Empty\n"); - return; - } - if(temp->lptr == NULL && temp->rptr == NULL){ - NodetoBeDeleted = temp; - free(NodetoBeDeleted); - *LH= *RH= NULL; - }else{ - printf("1) Delete first Link\n2) Delete Last link\n3) Delete specific value \nwhich value u want to Delete"); - scanf("%d",&n); - if(n==3){ - printf("Enter value :"); - scanf("%d",&val); - } - if(n==1){ - NodetoBeDeleted = *LH; - (*LH) = (*LH)->rptr; - free(NodetoBeDeleted); - return; - }else if(n==2){ - NodetoBeDeleted = *RH; - (*RH)->lptr->rptr = NULL; - *RH = (*RH)->lptr; - free(NodetoBeDeleted); - return; - - }else if(n==3){ - if((*RH)->value == val){ - NodetoBeDeleted = *RH; - - *RH = (*RH)->lptr; - free(NodetoBeDeleted); - return; - } - if((*LH)->value == val){ - NodetoBeDeleted = *LH; - *LH = (*LH)->rptr; - free(NodetoBeDeleted); - return; - }else{ - - - while(temp->rptr->value != val) - temp = temp->rptr; - - NodetoBeDeleted = temp->rptr; - temp->rptr = temp->rptr->rptr; - temp->rptr->lptr = temp; - free(NodetoBeDeleted); - } - }else{ - printf("sorry something wrong\n"); - } - - } -} - -int main(){ - struct node *LH,*RH; - LH = RH = NULL; - int c,n; - printf("1) Insert\n2) Delete\n3) Display\n4) Exit"); - while(c!=4){ - printf("\nEnter your choice :"); - scanf("%d",&c); - switch(c){ - case 1: - printf("Enter value :"); - scanf("%d",&n); - insert(&LH,&RH,n); - break; - case 2: - delete_Link(&LH,&RH); - break; - case 3: - Display(&LH,&RH); - break; - case 4: - exit(0); - break; - default: - printf("Please Enter Between 1 to 4"); - break; - - } - } - - return 0; -} diff --git a/Doble_Link_List/Doble_Link_List_Output.txt b/Doble_Link_List/Doble_Link_List_Output.txt deleted file mode 100644 index 661295a8..00000000 --- a/Doble_Link_List/Doble_Link_List_Output.txt +++ /dev/null @@ -1,89 +0,0 @@ -1) Insert -2) Delete -3) Display -4) Exit -Enter your choice :1 -Enter value :12 - -Enter your choice :1 -Enter value :37 -1) insert at first -2) insert at last -3) insert at order -Where u want to insert :1 - -Enter your choice :1 -Enter value :19 -1) insert at first -2) insert at last -3) insert at order -Where u want to insert :3 - -Enter your choice :3 - 19 37 12 - -Enter your choice :1 -Enter value :57 -1) insert at first -2) insert at last -3) insert at order -Where u want to insert :2 - -Enter your choice :1 -Enter value :47 -1) insert at first -2) insert at last -3) insert at order -Where u want to insert :1 - -Enter your choice :3 - 47 19 37 12 57 - -Enter your choice :2 -1) Delete first Link -2) Delete Last link -3) Delete specific value -which value u want to Delete1 - -Enter your choice :3 - 19 37 12 57 - -Enter your choice :2 -1) Delete first Link -2) Delete Last link -3) Delete specific value -which value u want to Delete2 - -Enter your choice :2 -1) Delete first Link -2) Delete Last link -3) Delete specific value -which value u want to Delete3 -Enter value :37 - -Enter your choice :3 - 19 12 - -Enter your choice :2 -1) Delete first Link -2) Delete Last link -3) Delete specific value -which value u want to Delete2 - -Enter your choice :3 - 19 - -Enter your choice :2 -1) Delete first Link -2) Delete Last link -3) Delete specific value -which value u want to Delete1 - -Enter your choice :2 - -sorry Link List is Empty - -Enter your choice :4 - -Process returned 0 (0x0) execution time : 302.976 s -Press any key to continue. \ No newline at end of file diff --git a/FizzBuzz_Javascript/fizzbuzz.js b/FizzBuzz_Javascript/fizzbuzz.js deleted file mode 100644 index 9f5c9629..00000000 --- a/FizzBuzz_Javascript/fizzbuzz.js +++ /dev/null @@ -1,13 +0,0 @@ -function fizzBuzz(){ - for(var i=1;i<=100;i++){ - if(i%5 === 0 && i%3 === 0){ - print('FizzBuzz'); - } else if(i%3 === 0){ - print('Fizz'); - } else if(i%5 === 0){ - print('Buzz'); - } else { - print(i); - } - } -} diff --git a/FizzBuzz_Javascript/readme.MD b/FizzBuzz_Javascript/readme.MD deleted file mode 100644 index 788b05b5..00000000 --- a/FizzBuzz_Javascript/readme.MD +++ /dev/null @@ -1,3 +0,0 @@ -## Fizz Buzz in Javascript - -Basic implemtation of Fizz Buzz with Javascript using if statements \ No newline at end of file diff --git a/FizzBuzz_Python/fizzbuzz.python b/FizzBuzz_Python/fizzbuzz.python deleted file mode 100644 index 2919ba15..00000000 --- a/FizzBuzz_Python/fizzbuzz.python +++ /dev/null @@ -1,9 +0,0 @@ -for i in range(1, 101): - line = '' - if i % 3 == 0: - line += "Fizz" - if i % 5 == 0: - line += "Buzz" - if not line: - line += str(i) - print(line) \ No newline at end of file diff --git a/FizzBuzz_Python/readme.MD b/FizzBuzz_Python/readme.MD deleted file mode 100644 index 03368885..00000000 --- a/FizzBuzz_Python/readme.MD +++ /dev/null @@ -1,3 +0,0 @@ -## Fizz Buzz in Python - -Basic implemtation of Fizz Buzz with Python using if statements \ No newline at end of file diff --git a/FizzBuzz_Swift/FizzBuzz.swift b/FizzBuzz_Swift/FizzBuzz.swift deleted file mode 100644 index 9a9667ff..00000000 --- a/FizzBuzz_Swift/FizzBuzz.swift +++ /dev/null @@ -1,19 +0,0 @@ -func fizzBuzz(_ numberOfTurns: Int) { - for i in 1...numberOfTurns { - var result = "" - - if i % 3 == 0 { - result += "Fizz" - } - - if i % 5 == 0 { - result += (result.isEmpty ? "" : " ") + "Buzz" - } - - if result.isEmpty { - result += "\(i)" - } - - print(result) - } -} \ No newline at end of file diff --git a/FizzBuzz_Swift/readme.MD b/FizzBuzz_Swift/readme.MD deleted file mode 100644 index 4fd297e8..00000000 --- a/FizzBuzz_Swift/readme.MD +++ /dev/null @@ -1,3 +0,0 @@ -## Fizz Buzz in Swift - -Basic implemtation of Fizz Buzz with Swift using if statements \ No newline at end of file diff --git a/Graph_in_c/BFS.c b/Graph_in_c/BFS.c deleted file mode 100644 index 3bba7a4b..00000000 --- a/Graph_in_c/BFS.c +++ /dev/null @@ -1,103 +0,0 @@ -#ifndef Queue_h -#define Queue_h -#include -struct Node -{ -int data; -struct Node *next; -}*front=NULL,*rear=NULL; -void enqueue(int x) -{ -struct Node *t; -t=(struct Node*)malloc(sizeof(struct Node)); -if(t==NULL) -printf("Queue is FUll\n"); -else -{ -t->data=x; -t->next=NULL; -if(front==NULL) -front=rear=t; -else -{ -rear->next=t; -rear=t; -} -} -} -int dequeue() -{ -int x=-1; -struct Node* t; -if(front==NULL) -printf("Queue is Empty\n"); -else -{ -x=front->data; -t=front; - -front=front->next; -free(t); -} -return x; -} -int isEmpty() -{ -return front==NULL; -} -#endif /* Queue_h */ - -#include -#include "Queue.h" -void BFS(int G[][7],int start,int n) -{ -int i=start,j; -int visited[7]={0}; -printf("%d ",i); -visited[i]=1; -enqueue(i); -while(!isEmpty()) -{ -i=dequeue(); -for(j=1;j { - - private var buckets: [HashNode?] - private var capacity: Int - private(set) var collisions = 0 - private(set) var count = 0 - public var isEmpty: Bool { return count == 0 } - - init(capacity: Int) { - buckets = [HashNode?](repeating: nil, count: capacity) - self.capacity = capacity - } - - mutating func addUpdate(key: Key, value: Value) { - let hashIndex = index(forKey: key) - let destNode = buckets[hashIndex] - - if destNode == nil { - count += 1 - buckets[hashIndex] = HashNode(key: key, value: value) - } else { - var curNode = destNode - while curNode != nil { - if curNode!.key == key { - collisions += 1 - curNode!.value = value - return - } - curNode = curNode!.nextNode - } - - count += 1 - buckets[hashIndex] = HashNode(key: key, value: value, nextNode: destNode) - } - } - - func value(forKey key: Key) -> Value? { - let hashIndex = index(forKey: key) - var node = buckets[hashIndex] - - if node == nil { - return nil - } - - while node != nil { - if node!.key == key { - return node!.value - } - node = node!.nextNode - } - - return nil - } - - mutating func remove(atKey key: Key) -> Value? { - let hashIndex = index(forKey: key) - var node = buckets[hashIndex] - - if node == nil { - return nil - } - - var prevNode: HashNode? - while node != nil { - if node!.key == key { - if prevNode == nil { - if node!.nextNode == nil { - buckets[hashIndex] = nil - } else { - buckets[hashIndex] = node!.nextNode - } - } else if node!.nextNode == nil { - prevNode!.nextNode = nil - } else { - prevNode!.nextNode = node!.nextNode - } - count -= 1 - return node!.value - } - prevNode = node - node = node!.nextNode - } - - return nil - } - - private func index(forKey key: Key) -> Int { - return abs(key.hashValue) % buckets.count - } - -} - - -class HashNode { - - var key: Key - var value: Value - var nextNode: HashNode? - - init(key: Key, value: Value, nextNode: HashNode? = nil) { - self.key = key - self.value = value - self.nextNode = nextNode - } - -} - diff --git a/HashTable/MyHashMap.py b/HashTable/MyHashMap.py deleted file mode 100644 index d79f862c..00000000 --- a/HashTable/MyHashMap.py +++ /dev/null @@ -1,51 +0,0 @@ -class MyHashMap(object): - - def __init__(self): - """ - Initialize your data structure here. - """ - self.hash_map = [None] * 1000 - - - def put(self, key, value): - """ - value will always be non-negative. - :type key: int - :type value: int - :rtype: None - """ - the_hash = hash(key) % 1000 - if not self.hash_map[the_hash]: - self.hash_map[the_hash] = [] - self.remove(key) - self.hash_map[the_hash].append((key, value)) - - - def get(self, key): - """ - Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key - :type key: int - :rtype: int - """ - the_hash = hash(key) % 1000 - - if self.hash_map[the_hash]: - for t in self.hash_map[the_hash]: - if t[0] == key: - return t[1] - return -1 - - def remove(self, key): - """ - Removes the mapping of the specified value key if this map contains a mapping for the key - :type key: int - :rtype: None - """ - the_hash = hash(key) % 1000 - to_delete = None - if self.hash_map[the_hash]: - new_cell = [] - for i in range(len(self.hash_map[the_hash])): - if not self.hash_map[the_hash][i][0] == key: - new_cell.append(self.hash_map[the_hash][i]) - self.hash_map[the_hash] = new_cell diff --git a/Infix to expression tree/infix_to_exp.cpp b/Infix to expression tree/infix_to_exp.cpp deleted file mode 100644 index 80e78dbe..00000000 --- a/Infix to expression tree/infix_to_exp.cpp +++ /dev/null @@ -1,149 +0,0 @@ -#include -#include -using namespace std; -struct node{ -char data; -struct node *right,*left; -}; -class infix -{ - string result = ""; - stack s ; - public: string decode(string exp) - { - for(int i=0;i=0)||(s<='Z' && s>='A')||(s<='z' && s>='a')) - return true; - else return false; - - } - bool isoperator(char x) - { - if(x=='+'||x=='-'||x=='/'||x=='*'||x=='^') - return true; - else return false; - } - int getweight(char m) - { - int weight =-1; - switch(m) - { - case '+': - case '-': - weight = 1; - break; - case '*': - case '/': - weight = 2; - case '^': - weight = 3; - } - return weight; - } - bool isrightassociative(char a) - { - if(a=='^')return true; - else return false; - } - bool hashigherprecedence(char a, char b) - { - int weight1 = getweight(a); - int weight2 = getweight(b); - if(weight1==weight2) - { - if(isrightassociative(a)) - return false; - else - return true; - } - return weight1 > weight2 ? true : false; - - } -node* expressiontree(string postfix) - -{ - stack s; -int size = postfix.size(); -for(int i=0;idata= postfix[i]; - temp->left = NULL; - temp->right = NULL; - s.push(temp); - - - } - else - { - node* t2 = s.top(); - s.pop(); - node* t1 = s.top(); - s.pop(); - node* temp = (node*)malloc(sizeof(node)); - if(temp==NULL) - { - cout<<"memory error"<data = postfix[i]; - temp->left = t1; - temp->right = t2; - s.push(temp); - - } - -} -return s.top(); -} -}; -int main() -{ - infix t; - node* x = t.expressiontree(t.decode("(a+n)*(c+d)")); - return 0; -} \ No newline at end of file diff --git a/Infix_To_Postfix/infix2post_2.cpp b/Infix_To_Postfix/infix2post_2.cpp deleted file mode 100644 index df36117d..00000000 --- a/Infix_To_Postfix/infix2post_2.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include "stack.cpp" -using namespace std; - -void convert(string exp){ - stack s; - string out; - - for(int i =0; i>exp; - convert(exp); - return 0; -} diff --git a/Infix_To_Postfix/infix2postfix.py b/Infix_To_Postfix/infix2postfix.py deleted file mode 100644 index 5b383605..00000000 --- a/Infix_To_Postfix/infix2postfix.py +++ /dev/null @@ -1,43 +0,0 @@ -# Python 3 program to convert infix to postfix -# This program uses Stack from pythonds -# A stack can be easily implemented using python list but for maintaining the feel of the algorithm the code here uses Stack() - -# pip install pythonds -from pythonds.basic import Stack - -# Precedence setting for the operands -def precedence(): - prec = {} - prec['*'] = 3 - prec['/'] = 3 - prec['+'] = 2 - prec['-'] = 2 - prec['('] = 1 - return prec - -def infix2postfix(exp): - prec = precedence() - operations = Stack() - postfixList = [] - tokenList = exp.split() - - for token in tokenList: - if token in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' or token in '0123456789': - postfixList.append(token) - elif token == '(': - operations.push(token) - elif token == ')': - top = operations.pop() - while top != '(': - postfixList.append(top) - top = operations.pop() - else: - while not operations.isEmpty() and prec[operations.peek()] >= prec[token]: - postfixList.append(operations.pop()) - operations.push(token) - while not operations.isEmpty(): - postfixList.append(operations.pop()) - return ''.join(postfixList) - -# Uncomment the following line to test -#print(infix2postfix("A * B + C * D")) diff --git a/Infix_To_Postfix/infix_To_Postfix.c b/Infix_To_Postfix/infix_To_Postfix.c deleted file mode 100644 index 7ced6691..00000000 --- a/Infix_To_Postfix/infix_To_Postfix.c +++ /dev/null @@ -1,86 +0,0 @@ -#include -int top=-1,top2=-1; -int MAXSIZE=100; -char s[100]; - -int isOperand(char x){ - if((x>='a'&& x<='z')||(x>='A' && x<='Z')){ - return 1; - } - else - return 0; -} - -void push(char x){ - - s[++top]=x; - -} - -char pop(){ - - return s[top--]; - -} - -int priority(char x){ - if(x=='('){ - return 0; - } - else if(x=='+' || x=='-'){ - return 1; - } - else if(x=='*' || x=='/'){ - return 2; - } -} - -int main(){ - int p=0; - char exp[MAXSIZE],*e,postfix[MAXSIZE]; - printf("Enter your infix Expression : "); - scanf("%s",exp); - e=exp; - printf(" your Postfix Expression is : "); - while(*e!='\0'){ - - if(isOperand(*e)){ - postfix[p]=*e; - printf("%c",postfix[p++]); - - } - - else if(*e=='('){ - push(*e); - } - - else if(*e==')'){ - while(s[top]!='('){ - postfix[p]=pop(); - printf("%c",postfix[p++]); - - - } - top--; - } - - else{ - while(priority(s[top])>=priority(*e)){ - postfix[p]=pop(); - printf("%c",postfix[p++]); - - - } - push(*e); - } - e++; - } - - while(top!=-1){ - postfix[p]=pop(); - printf("%c",postfix[p++]); - } - printf("\n"); - return 0; -} - diff --git a/Infix_To_Postfix/infix_To_Postfix_Output.txt b/Infix_To_Postfix/infix_To_Postfix_Output.txt deleted file mode 100644 index dae74915..00000000 --- a/Infix_To_Postfix/infix_To_Postfix_Output.txt +++ /dev/null @@ -1,18 +0,0 @@ -Enter your infix Expression : a+b*(c/d)%e - your Postfix Expression is : abcd/e%*+ - -Process returned 0 (0x0) execution time : 21.678 s -Press any key to continue. - - - -****************************************************** - - -Enter your infix Expression : (10+20)*(12/3)*(7%2) - your Postfix Expression is : 1020+213/*72%* - -Process returned 0 (0x0) execution time : 53.655 s -Press any key to continue. - - diff --git a/Infix_To_Postfix/stack.cpp b/Infix_To_Postfix/stack.cpp deleted file mode 100644 index c80eef4a..00000000 --- a/Infix_To_Postfix/stack.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include -using namespace std; - -class stack{ - public: - char arr[10]; - int top; - - stack(){ - top = -1; - } - - void push(char x){ - if(top<10){ - top+=1; - arr[top] = x; - } - else{ - cout<<"Stack is full\n"; - } - } - - char pop(){ - if(isEmpty()){ - cout<<"Stack is Empty\n"; - } - char a = arr[top]; - top-=1; - return a; - } - - char topel(){ - return arr[top]; - } - - bool isEmpty(){ - if(top==-1){ - return true; - } - return false; - } - - void print(){ - for(int i=0; i<=top; i++){ - cout< -int top=-1,top2=-1; -int MAXSIZE=100; -char s[100]; -int d[100]; -int isOperand(char x){ - if((x>='a'&& x<='z')||(x>='A' && x<='Z')){ - return 1; - } - else - return 0; -} - -int isnumber(int n){ - - if(n>=0 && n<=9)return 1; - else return 0; -} -void push_n(int x){ - - d[++top2]=x; - -} - -int pop_n(){ - - return d[top2--]; - -} -void push(char x){ - - s[++top]=x; - -} - -char pop(){ - - return s[top--]; - -} - -int priority(char x){ - if(x=='('){ - return 0; - } - else if(x=='+' || x=='-'){ - return 1; - } - else if(x=='*' || x=='/'){ - return 2; - } -} - -int main(){ - int p=0; - char exp[MAXSIZE],*e,postfix[MAXSIZE]; - printf("Enter your infix Expression : "); - scanf("%s",exp); - e=exp; - printf("\npostfix Expression is : "); - while(*e!='\0'){ - - if(isOperand(*e)){ - postfix[p]=*e; - printf("%c",postfix[p++]); - - } - - else if(*e=='('){ - push(*e); - } - - else if(*e==')'){ - while(s[top]!='('){ - postfix[p]=pop(); - printf("%c",postfix[p++]); - - } - top--; - } - - else{ - while(priority(s[top])>=priority(*e)){ - postfix[p]=pop(); - printf("%c",postfix[p++]); - - } - push(*e); - } - e++; - } - -while(top!=-1){ - postfix[p]=pop(); - printf("%c",postfix[p++]); - -} -printf("\n\n"); - - - -int a,b,c; -int j=0; -while(postfix[j]!='\0'){ - - - if(isOperand(postfix[j])){ - printf("Enter the value of %c : ",postfix[j]); - int p; - scanf("%d",&p); - push_n(p); - - - }else if(postfix[j]==' '){} - else{ - a=pop_n(); - b=pop_n(); - - - - if(postfix[j]=='+') c=b+a; - if(postfix[j]=='-') c=b-a; - if(postfix[j]=='/') c=b/a; - if(postfix[j]=='*') c=b*a; - if(postfix[j]=='%') c=b%a; - push_n(c); - } - j=j+1; - -} -printf("\nEvaluation answer is : %d\n\n",pop_n()); -} - diff --git a/Insertion_sort/InsertionSortExample.java b/Insertion_sort/InsertionSortExample.java deleted file mode 100644 index e7c5e2c2..00000000 --- a/Insertion_sort/InsertionSortExample.java +++ /dev/null @@ -1,30 +0,0 @@ -public class InsertionSortExample { - public static void insertionSort(int array[]) { - int n = array.length; - for (int j = 1; j < n; j++) { - int key = array[j]; - int i = j-1; - while ( (i > -1) && ( array [i] > key ) ) { - array [i+1] = array [i]; - i--; - } - array[i+1] = key; - } - } - - public static void main(String a[]){ - int[] arr1 = {9,14,3,2,43,11,58,22}; - System.out.println("Before Insertion Sort"); - for(int i:arr1){ - System.out.print(i+" "); - } - System.out.println(); - - insertionSort(arr1);//sorting array using insertion sort - - System.out.println("After Insertion Sort"); - for(int i:arr1){ - System.out.print(i+" "); - } - } -} diff --git a/Java/Searching/LinearSearch.java b/Java/Searching/LinearSearch.java deleted file mode 100644 index 6c27df9b..00000000 --- a/Java/Searching/LinearSearch.java +++ /dev/null @@ -1,29 +0,0 @@ -// Java code for linearly searching x in arr[]. If x -// is present then return its location, otherwise -// return -1 - -class GFG -{ -public static int search(int arr[], int x) -{ - int n = arr.length; - for(int i = 0; i < n; i++) - { - if(arr[i] == x) - return i; - } - return -1; -} - -public static void main(String args[]) -{ - int arr[] = { 2, 3, 4, 10, 40 }; - int x = 10; - - int result = search(arr, x); - if(result == -1) - System.out.print("Element is not present in array"); - else - System.out.print("Element is present at index " + result); -} -} diff --git a/Kadanes/Kadane.kt b/Kadanes/Kadane.kt deleted file mode 100644 index 7e52f1fe..00000000 --- a/Kadanes/Kadane.kt +++ /dev/null @@ -1,18 +0,0 @@ -object KadanesAlgorithm { - /** - * **Time**: `O(n)` - * - * **Space**: `O(1)` - */ - fun maxSubArray(nums: IntArray): Int { - var maxSoFar = nums[0] - var maxEndingHere = nums[0] - - (1..nums.lastIndex).forEach { i -> - maxEndingHere = max(nums[i], maxEndingHere + nums[i]) - maxSoFar = max(maxSoFar, maxEndingHere) - } - - return maxSoFar - } -} \ No newline at end of file diff --git a/Linear_Search.c b/Linear_Search.c new file mode 100644 index 00000000..6fb6390a --- /dev/null +++ b/Linear_Search.c @@ -0,0 +1,49 @@ +#include +#include +void linearSearch(int searchValue, int a[], int n); + +void main() +{ + int length, j, searchValue; + printf("Welcome to Linear Search!!\n"); + printf("You can search a data element from an array\n"); + printf("For Simplicity lets start with the data elements as integers.\n"); + printf("Please enter the length of the array you want me to create:\n"); + scanf("%d", &length); + + int *a = (int *) calloc (length, sizeof(int)); + + printf("Now you can enter the integers of your choice:\n"); + for(j = 0; j < length; j++) + { + scanf("%d", &a[j]); + } + + printf("Now Please enter the value you want me to search\n"); + scanf("%d", &searchValue); + + linearSearch(searchValue, a, length); + + printf("Thanks For investing time in Me!!"); +} + +void linearSearch(int searchValue, int a[], int n) +{ + int i; + printf("I have just started to search for the Value --> %4d\n", searchValue); + + for ( i = 0; i < n; i++) + { + if (a[i] == searchValue) + { + printf("Here you go the element %d is found at %d location from the starting from 0\n",searchValue, i); + break; + } + } + + if ( i == n) + { + printf("Sorry, the element you wanted me to found doesn't exist in the given array.\n"); + } + printf("The Linear Search has Ended\n"); +} \ No newline at end of file diff --git a/Link_List/.DS_Store b/Link_List/.DS_Store deleted file mode 100644 index 689a9ebf..00000000 Binary files a/Link_List/.DS_Store and /dev/null differ diff --git a/Link_List/Link_List_Output.txt b/Link_List/Link_List_Output.txt deleted file mode 100644 index 28ec2991..00000000 --- a/Link_List/Link_List_Output.txt +++ /dev/null @@ -1,99 +0,0 @@ -1 for insert value at front: -2 for insert value at rear: -3 for insert value according to order: -4 for clear Link list: -5 for reverse Link list -6 for delete Link Node -7 for sorting Link list in ascending order -8 for display the list -9 for Exit -Enter which type of the operation you want to apply: 1 -Enter the value you want to insert:15 - -Enter which type of the operation you want to apply: 1 -Enter the value you want to insert:27 - -Enter which type of the operation you want to apply: 1 -Enter the value you want to insert:10 - -Enter which type of the operation you want to apply: 1 -Enter the value you want to insert:17 - -Enter which type of the operation you want to apply: 1 -Enter the value you want to insert:39 - -Enter which type of the operation you want to apply: 1 -Enter the value you want to insert:45 - -Enter which type of the operation you want to apply: 8 -45 39 17 10 27 15 - -Enter which type of the operation you want to apply: 5 -Link list Reverse Successful - -Enter which type of the operation you want to apply: 8 -15 27 10 17 39 45 - -Enter which type of the operation you want to apply: 7 - -Enter which type of the operation you want to apply: 8 -10 15 17 27 39 45 - -Enter which type of the operation you want to apply: 3 -Enter the value you want to insert:25 - -Enter which type of the operation you want to apply: 8 -10 15 17 25 27 39 45 - -Enter which type of the operation you want to apply: 5 -Link list Reverse Successful - -Enter which type of the operation you want to apply: 8 -45 39 27 25 17 15 10 - -Enter which type of the operation you want to apply: 6 -Enter your choice: -1 for Delete at first -2 for delete at last -3 for delete specific value1 - -Enter which type of the operation you want to apply: 8 -39 27 25 17 15 10 - -Enter which type of the operation you want to apply: 6 -Enter your choice: -1 for Delete at first -2 for delete at last -3 for delete specific value2 - -Enter which type of the operation you want to apply: 8 -39 27 25 17 15 - -Enter which type of the operation you want to apply: 6 -Enter your choice: -1 for Delete at first -2 for delete at last -3 for delete specific value3 - -enter specific value you want to delete:25 - -Enter which type of the operation you want to apply: 8 -39 27 17 15 - -Enter which type of the operation you want to apply: 2 -Enter the value you want to insert:17 - -Enter which type of the operation you want to apply: 8 -39 27 17 15 17 - -Enter which type of the operation you want to apply: 4 - -Enter which type of the operation you want to apply: 8 - - Ops!!! link list is null - - -Enter which type of the operation you want to apply: 9 - -Process returned 0 (0x0) execution time : 236.883 s -Press any key to continue. \ No newline at end of file diff --git a/Link_List/LinkedList.java b/Link_List/LinkedList.java deleted file mode 100755 index ba8c3776..00000000 --- a/Link_List/LinkedList.java +++ /dev/null @@ -1,197 +0,0 @@ - -import java.util.NoSuchElementException; - -// Class to create a Node that contains data and pointer to next Node -class Node{ - public T data; - public Node next; - - // Constructor to create a Node - public Node(T input){ - this.data = input; - this.next = null; - } -} - -// Class containing methods to be performed on Linked List -public class LinkedList { - - private Node head; - private int sizeOfList = 0; - - public LinkedList(){ - head = null; - } - - // Method to add an element to the start of the Linked List - public void addFront(T input){ - Node newNode = new Node(input); - newNode.next = head; - head = newNode; - sizeOfList++; - } - - // Method to add an element to the end of the Linked List - public void addLast(T input){ - if(head==null){ - Node newNode = new Node(input); - head = newNode; - sizeOfList = 1; - } else{ - Node temp = head; - while (temp.next != null){ - temp = temp.next; - } Node newNode = new Node(input); - temp.next = newNode; - sizeOfList++; - } - } - - - // Method to add an element at a given index - public void add(T input, int index){ - if (index<0 || index>sizeOfList){ - throw new IndexOutOfBoundsException("Invalid index."); - } - Node temp = head; - if(index ==0){ - addFront(input); - } else { - for (int i =0; i< index-1; i++){ - temp = temp.next; - } - Node newNode = new Node(input); - newNode.next = temp.next; - temp.next = newNode; - sizeOfList++; - } - } - - public boolean isEmpty() { - return sizeOfList == 0; - } - - // Method to remove the Firt element from the Linked List - public T removeFront(){ - if(isEmpty()){ - throw new NoSuchElementException(); - } - T removedElement = head.data; - head = head.next; - sizeOfList--; - - return removedElement; - } - - // Method to remove the front element from the Linked List - public T removeLast(){ - if(isEmpty()){ - throw new NoSuchElementException(); - } - T removedElement; - if(head.next == null){ - removedElement = head.data; - head = null; - }else{ - Node temp = head; - while ( temp.next.next!= null) - temp = temp.next; - removedElement = temp.next.data; - temp.next=null; - } - - sizeOfList--; - return removedElement; - } - - // Method to remove element at specific index from the Linked List - public T remove(int index){ - if(isEmpty()){ - throw new NoSuchElementException(); - }if(index<0 || index> sizeOfList){ - throw new IndexOutOfBoundsException(); - } - - if(index>0){ - Node temp = head; - for (int i =0; i temp = head; - for (int i = 0; i < sizeOfList; i++) { - if (temp.data == input) - return i; - else - temp = temp.next; - } - return -1; - } - - public void display() { - Node temp = head; - if (temp != null) { - System.out.print("["); - while (temp != null) { - System.out.print(temp.data+", "); - temp = temp.next; - } - System.out.print("\b\b]\n"); - } - } - - public int size() { - return sizeOfList; - } - - - public static void main(String[] args) { - - LinkedList list = new LinkedList(); - - list.addFront(10); - list.addFront(20); - - list.display(); - - list.addFront(30); - list.addFront(40); - - list.display(); - - list.removeFront(); - - list.display(); - - list.removeLast(); - list.display(); - - list.addLast(50); - list.addLast(60); - - list.remove(2); - list.display(); - - int found = list.search(20); - if (found >= 0) - System.out.println("Element found at index: " + found); - else - System.out.println("Element not found"); - - if (list.isEmpty()) - System.out.println("List is empty"); - else - System.out.println("List is not empty, Size of list: " + list.size()); - - } - -} diff --git a/Link_List/java/LinkedList.java b/Link_List/java/LinkedList.java deleted file mode 100644 index 33267774..00000000 --- a/Link_List/java/LinkedList.java +++ /dev/null @@ -1,81 +0,0 @@ - -public class LinkedList { - private Node head; - - public LinkedList() { - head = null; - } - - public void printHead() { - System.out.println(head.getValue()); - } - - //Creates linkedList with head value - public LinkedList(int v) { - head = new Node(v); - } - - // Method to insert a new node - public void addLast(int data) { - // Create a new node with given data - Node newNode = new Node(data); - newNode.nextNode = null; - - // If the Linked List is empty, - // then make the new node as head - if (this.head == null) { - this.head = newNode; - } - else { - // Else traverse till the last node - // and insert the newNode there - Node last = this.head; - while (last.nextNode != null) { - last = last.nextNode; - } - - // Insert the newNode at last node - last.setNextNode(newNode); - } - - } - - //Adds node to front of linked list - //(Essentially replaces the head) - public void addFirst(int data) { - Node newNode = new Node(data); - if(this.head != null) { - newNode.setNextNode(head); - } - - this.head = newNode; - } - - //removes last node from linked list - public void pop() { - - //Gets head node - Node lastNode = this.head; - - //Iterates over linked list - //Once final node is located it, the previous node's next node is set to null - //The loop is then broken - while(lastNode.getNextNode() != null) { - if(lastNode.getNextNode().getNextNode() == null) { - lastNode.setNextNode(null); - break; - } - - lastNode = lastNode.getNextNode(); - } - } - - //Method to display the content of the linked list - public void display() { - Node currentNode = this.head; - while(currentNode != null) { - System.out.println(currentNode.getValue()); - currentNode = currentNode.getNextNode(); - } - } -} \ No newline at end of file diff --git a/Link_List/java/Node.java b/Link_List/java/Node.java deleted file mode 100644 index dac1fe86..00000000 --- a/Link_List/java/Node.java +++ /dev/null @@ -1,21 +0,0 @@ -public class Node { - int value; - Node nextNode; - - public Node(int v) { - value = v; - nextNode = null; - } - - public int getValue() { - return this.value; - } - - public Node getNextNode() { - return this.nextNode; - } - - public void setNextNode(Node n) { - this.nextNode = n; - } -} \ No newline at end of file diff --git a/Link_List/javascript/linkedlist.js b/Link_List/javascript/linkedlist.js deleted file mode 100644 index 52519a4d..00000000 --- a/Link_List/javascript/linkedlist.js +++ /dev/null @@ -1,113 +0,0 @@ -// This is the javascript program for a linked list. It does basic addition, deletion of the -// elements in the list. - -class linkedlist{ - constructor() - { - this.size = 0; - this.head = null; - -//Node object declared as a class member - - this.elem = - class element{ - constructor(d) - { - this.data = d; - this.next = null; - } - } -} - -//Insert an element at the last of the list - -insert(data) - { - this.size++; - let new_node = new this.elem(data); - - if(this.head === null) - { - this.head = new_node; - - } - else{ - let last_node = this.head; - - while(last_node.next !== null) - last_node = last_node.next; - last_node.next = new_node; - } - } - -//insert an element at the specified index - -insertAt(data,index) - { - this.size++; - let curr_node = this.head, prev_node = curr_node, i = index; - let new_node = new this.elem(data); - if(i===0) - { - new_node.next = this.head; - this.head = new_node; - } - else - { - while(i--!==0) - { - prev_node = curr_node; - curr_node = curr_node.next; - } - prev_node.next = new_node; - new_node.next = curr_node; - -} - } - -//delete an element at the specified index - -deleteAt(index) - { - this.size--; - let curr_node = this.head,prev_node = curr_node, i = index; - if(i===0) - { - this.head = curr_node.next; - curr_node.next = null; - } - else - { - while(i--!==0) - { - prev_node = curr_node; - curr_node = curr_node.next; - } - prev_node.next = curr_node.next; - curr_node.next = null; - } - } - -//print the elemets of the list - -print() - { - let node = this.head; - while(node!==null) - { - console.log(node.data); - node = node.next; - } - console.log(`size: ${this.size}`); - } - -} - -//Demo list - -let my_list = new linkedlist(); -my_list.insert(4); -my_list.insert(6); -my_list.insert(8); -my_list.insert(10); -my_list.print(); \ No newline at end of file diff --git a/Link_List/link_list.c b/Link_List/link_list.c deleted file mode 100644 index 71e95ac8..00000000 --- a/Link_List/link_list.c +++ /dev/null @@ -1,235 +0,0 @@ -#include -#include - -struct Node{ - int value; - struct Node *ptr; -}; - -void InsertAtFront(struct Node **h,int val){ - struct Node* NewNode; - NewNode = (struct Node*)malloc(sizeof(struct Node)); - NewNode->value=val; - if(*h == NULL){ - NewNode->ptr = *h; - *h = NewNode; - }else{ - NewNode->ptr = *h ; - *h = NewNode; - } -} - -void InsertAtEnd(struct Node **h,int val){ - struct Node *NewNode,*temp; - NewNode = (struct Node*)malloc(sizeof(struct Node)); - NewNode->value = val; - temp = *h; - if(*h == NULL){ - *h = NewNode; - NewNode->ptr = NULL; - }else{ - while(temp->ptr != NULL) - temp = temp->ptr; - - temp->ptr = NewNode; - NewNode->ptr = NULL; - } -} - -void InsertAtOrder(struct Node **h,int val){ - struct Node *NewNode,*temp; - NewNode = (struct Node*)malloc(sizeof(struct Node)); - NewNode->value = val; - temp = *h; - if(*h == NULL){ - *h = NewNode; - NewNode->ptr = NULL; - }else if(temp->value >= val ){ - InsertAtFront(h,val); - } - else{ - while(temp->ptr->value <= val){ - temp = temp->ptr; - if(temp->ptr == NULL) - break; - } - if(temp->ptr == NULL){ - InsertAtEnd(h,val); - }else{ - NewNode->ptr = temp->ptr; - temp->ptr = NewNode; - } - } - -} - -void ReverseLinklist(struct Node **h){ - struct Node *temp_current, *NEXT, *PREVOUS; - temp_current = *h; - PREVOUS = NULL; - while(temp_current != NULL){ - NEXT = temp_current->ptr; - temp_current->ptr = PREVOUS; - PREVOUS = temp_current; - temp_current = NEXT; - } - *h = PREVOUS; -} - -void DELETE(struct Node **h){ - struct Node *temp,*NodetoBeDeleted; - temp = *h; - if(temp == NULL)printf("\nsorry, Link list is empty.\n"); - else if(temp->ptr == NULL){ - NodetoBeDeleted = temp; - free(NodetoBeDeleted); - }else{ - printf("Enter your choice:\n1 for Delete at first\n2 for delete at last\n3 for delete specific value"); - int n; - scanf("%d",&n); - if(n == 1){ - NodetoBeDeleted = temp; - temp = temp->ptr; - (*h) = temp; - free(NodetoBeDeleted); - }else if(n == 2){ - while(temp->ptr->ptr != NULL) temp = temp->ptr; - free(temp->ptr); - temp->ptr = NULL; - }else if(n == 3){ - printf("\nenter specific value you want to delete:"); - int val; - scanf("%d",&val); - while(temp->ptr->value != val) - temp = temp->ptr; - NodetoBeDeleted = temp->ptr; - temp->ptr = temp->ptr->ptr; - free(NodetoBeDeleted); - } - } -} - -void a_shorting(struct Node **h){ - struct Node *temp,*c,*i,*j; - int temp_value; - /* int n=0,p,q; - temp = *h; - c = *h; - while(c != NULL){ - c = c->ptr; - n=n+1; - } - printf("%d",n); - for(p=0;pvalue > temp->ptr->value && temp != NULL){ - temp_value = temp->value; - temp->value = temp->ptr->value; - temp->ptr->value = temp_value; - } - temp = temp->ptr; - } - temp=*h; - }*/ - - i=j=temp=*h; - while(i != NULL){ - while(j->ptr != NULL){ - if(j->value > j->ptr->value ){ - temp_value = j->value; - j->value = j->ptr->value; - j->ptr->value = temp_value; - } - j = j->ptr; - } - i = i->ptr; - j= *h; - } - -} -void clear_L(struct Node **h){ - struct Node *temp,*NodetobeDelete; - temp = *h; - while(temp != NULL){ - NodetobeDelete = temp; - temp = temp->ptr; - free(NodetobeDelete); - - } - (*h) = temp; -} - - -void Display(struct Node**h){ - struct Node *temp; - temp = *h; - if(temp == NULL) printf("\n Ops!!! link list is null \n"); - else {while(temp != NULL){ - printf("%d ",temp->value); - temp = temp->ptr; - } - } -} - - -int main(){ - struct Node *HEAD; - int n,val; - HEAD = NULL; - printf("1 for insert value at front:\n"); - printf("2 for insert value at rear:\n"); - printf("3 for insert value according to order:\n"); - printf("4 for clear Link list:\n"); - printf("5 for reverse Link list\n"); - printf("6 for delete Link Node\n"); - printf("7 for sorting Link list in ascending order \n"); - printf("8 for display the list\n"); - printf("9 for Exit\n"); - printf("Enter which type of the operation you want to apply: "); - scanf("%d",&n); - - while(n !=9){ - - switch(n){ - case 1: - printf("Enter the value you want to insert:"); - scanf("%d",&val); - InsertAtFront(&HEAD,val); - break; - case 2: - printf("Enter the value you want to insert:"); - scanf("%d",&val); - InsertAtEnd(&HEAD,val); - break; - case 3: - printf("Enter the value you want to insert:"); - scanf("%d",&val); - InsertAtOrder(&HEAD,val); - break; - case 4: - clear_L(&HEAD); - break; - case 5: - printf("Link list Reverse Successful\n"); - ReverseLinklist(&HEAD); - break; - case 6: - DELETE(&HEAD); - break; - case 7: - a_shorting(&HEAD); - break; - case 8: - Display(&HEAD); - printf("\n"); - break; - default: - printf("Enter specific value:\n"); - } - - printf("\nEnter which type of the operation you want to apply: "); - scanf("%d",&n); - } - return 0; - -} diff --git a/Link_List/linked-list.py b/Link_List/linked-list.py deleted file mode 100644 index cecc7a16..00000000 --- a/Link_List/linked-list.py +++ /dev/null @@ -1,41 +0,0 @@ -# A simple Python program for traversal of a linked list - -# Node class -class Node: - - # Function to initialise the node object - def __init__(self, data): - self.data = data # Assign data - self.next = None # Initialize next as null - - -# Linked List class contains a Node object -class LinkedList: - - # Function to initialize head - def __init__(self): - self.head = None - - # This function prints contents of linked list - # starting from head - def printList(self): - temp = self.head - while (temp): - print temp.data, - temp = temp.next - - -# Code execution starts here -if __name__=='__main__': - - # Start with the empty list - llist = LinkedList() - - llist.head = Node(1) - second = Node(2) - third = Node(3) - - llist.head.next = second; # Link first node with second - second.next = third; # Link second node with the third node - - llist.printList() diff --git a/Link_List/swift/.DS_Store b/Link_List/swift/.DS_Store deleted file mode 100644 index d7200f9b..00000000 Binary files a/Link_List/swift/.DS_Store and /dev/null differ diff --git a/Link_List_Python/Reverse_Linked_list.py b/Link_List_Python/Reverse_Linked_list.py deleted file mode 100644 index d5a07dea..00000000 --- a/Link_List_Python/Reverse_Linked_list.py +++ /dev/null @@ -1,53 +0,0 @@ -# Define class Node -class Node: - def __init__(self, data): - self.data = data - self.next = None - - -# Define class Linked List -class LinkedList: - def __init__(self): - self.head = None - self.last_node = None - - def append(self, data): - if self.last_node is None: - self.head = Node(data) - self.last_node = self.head - else: - self.last_node.next = Node(data) - self.last_node = self.last_node.next - - def display(self): - current = self.head - while current is not None: - print(current.data, end=' ') - current = current.next - - -# Reverse a Linked Lists -def reverse_llist(llist): - before = None - current = llist.head - if current is None: - return - after = current.next - while after: - current.next = before - before = current - current = after - after = after.next - current.next = before - llist.head = current - - -a_list = LinkedList() -n = int(input('Enter how many elements')) -for i in range(n): - data = int(input()) - a_list.append(data) - -reverse_llist(a_list) -# Print the reversed Linked List -print(a_list.display()) diff --git a/Link_List_Python/link_list.py b/Link_List_Python/link_list.py deleted file mode 100644 index 1617c167..00000000 --- a/Link_List_Python/link_list.py +++ /dev/null @@ -1,70 +0,0 @@ -# A simple Python program to introduce a linked list - -# Node class -class Node: - - # Function to initialise the node object - def __init__(self, data): - self.data = data # Assign data - self.next = None # Initialize next as null - - -# Linked List class contains a Node object -class LinkedList: - - # Function to initialize head - def __init__(self): - self.head = None - - -# Code execution starts here -if __name__=='__main__': - - # Start with the empty list - llist = LinkedList() - - llist.head = Node(1) - second = Node(2) - third = Node(3) - - ''' - Three nodes have been created. - We have references to these three blocks as head, - second and third - - llist.head second third - | | | - | | | - +----+------+ +----+------+ +----+------+ - | 1 | None | | 2 | None | | 3 | None | - +----+------+ +----+------+ +----+------+ - ''' - - llist.head.next = second; # Link first node with second - - ''' - Now next of first Node refers to second. So they - both are linked. - - llist.head second third - | | | - | | | - +----+------+ +----+------+ +----+------+ - | 1 | o-------->| 2 | null | | 3 | null | - +----+------+ +----+------+ +----+------+ - ''' - - second.next = third; # Link second node with the third node - - ''' - Now next of second Node refers to third. So all three - nodes are linked. - - llist.head second third - | | | - | | | - +----+------+ +----+------+ +----+------+ - | 1 | o-------->| 2 | o-------->| 3 | null | - +----+------+ +----+------+ +----+------+ - ''' - diff --git a/Map/Maps.go b/Map/Maps.go deleted file mode 100644 index 3eac938b..00000000 --- a/Map/Maps.go +++ /dev/null @@ -1,47 +0,0 @@ -// Map is used for key value association and it is built on top of hashtable. -// Map is unordered group of elements. -//Below is the basic operations of Map in Go programming language. - -package main - -import "fmt" - -func main() { - //1. creating a map having key of string type and value is of integer type. - m := make(map[string]int) - - //2. Add elements in map - m["key1"] = 10 - m["key2"] = 20 - m["key3"] = 30 - fmt.Println("Map : ", m) - - //3. Length of map - fmt.Println("Length of map : ", len(m)) - - //4. Delete from Map - delete(m, "key2") - fmt.Println("Map after deletion : ", m) - - //5. Check wether the key present in map or not - val, status := m["key2"] - fmt.Println("Key having value", val, "is present in map : ", status) - - val1, status1 := m["key3"] - fmt.Println("Key having value", val1, "is present in map : ", status1) - - //6. Update the value - m["key1"] = 50 - fmt.Println("Updated Map : ", m) -} - -// To run the program, use following command -// go run Maps.go - -// Output is as follows: -// Map : map[key1:10 key2:20 key3:30] -// Length of map : 3 -// Map after deletion : map[key1:10 key3:30] -// Key having value 0 is present in map : false -// Key having value 30 is present in map : true -// Updated Map : map[key1:50 key3:30] \ No newline at end of file diff --git a/Mathematical_Algos/Prime_Number_Test b/Mathematical_Algos/Prime_Number_Test deleted file mode 100644 index 8f7f596f..00000000 --- a/Mathematical_Algos/Prime_Number_Test +++ /dev/null @@ -1,50 +0,0 @@ -/To Check a number is Prime or not. -int isprime(long int n) -{ - - //if n is 2 or 3 than it is a prime and the function will return 1. - if(n == 2) - return 1; - if(n == 3) - return 1; - - //if n has 2 or 3 as it's factor than it is a not prime and the function will return 0. - if(n % 2 == 0) - return 0; - if(n % 3 == 0) - return 0; - -//All even factors are eliminated because we have checked for n%2. -//now we have to check for odd numbers ,which are not a multiple of 3. -//Because we have already checked for n%3 ,so the number will not have a factor which is multiple of 3. - - long int i = 5; - long int w = 2; - -//we are running a while loop till square root of n - while(i * i <= n) - { - //To check if i divides n or not. - if(n % i == 0) - return 0; - ` //to skip the even factors we are adding a even number to i which is odd. - //odd + even results odd - i += w; - - //each time we are changing value of w to skip multiples of 3 - //initially w is 2 and i is 5 - // After first iteration i becomes 5+2 = 7 - //if we do not change the value of w and add 2 to 7, - //it will result to 9 which is a factor of 3, that is what we want to avoid. - //So we are toggling the value of w between 2,4 - //7+4 = 11 - //then again w becomes 2 - //i=11+2=13 and so on. - - - w = 6 - w; - } - - - return 1; -} diff --git a/Mathematical_Algos/SieveOfEratosthenes.cpp b/Mathematical_Algos/SieveOfEratosthenes.cpp deleted file mode 100644 index 04db8bbc..00000000 --- a/Mathematical_Algos/SieveOfEratosthenes.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include - -using namespace std; - -///time complexity : O(sqrt(n)log(log(n))) - -///Create a list of consecutive integers from 2 to n: (2, 3, 4, , n). -///Initially, let p equal 2, the first prime number. -///Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked. -///Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3. - -int main() -{ - - int n; - cout<<"Enter n: "; - cin>>n; - - bool* arr = new bool[n+1]; - - for(int i=1;i<=n;i++){ //creating a list of the first n integers - arr[i] = true; - } - ///or use memset(arr,true,sizeof(arr)) in cstring library to initialize all elements to true - for(int i=2;i<=sqrt(n);i++){ - - if(arr[i]){ //element is prime - for(int j=2*i;j<=n;j+=i){ - arr[j] = false; - } - } - } - - for(int i=2;i<=n;i++){ - if(arr[i]){ - cout< -using namespace std; -#define ll long long - -#define M 1000000007 -// The exp function calucates (a^b)%M value for larger inputs of a,b and M should be a larhe prime number -ll exp(ll x,ll n,ll m) -{ - if(n==0) - return 1; - else if(n%2 == 0) //n is even - return exp((x*x)%m,n/2,m); - else //n is odd - return (x*exp((x*x)%m,(n-1)/2,m))%m; - -} -int main() { - - ll a=167; - ll b=89; - - ll ans=exp(a,b,M); - cout< -#define SIZE 3 - -int main() -{ - int A[SIZE][SIZE]; - int row, col, total=0; - - printf("Enter elements in matrix of size 3x3: \n"); - for(row=0; row= (row * col)/2) - { - printf("\nThe given matrix is a Sparse matrix."); - } - else - { - printf("\nThe given matrix is not Sparse matrix."); - } - - return 0; -} diff --git a/Matrix/MatrixTranspose.c b/Matrix/MatrixTranspose.c deleted file mode 100644 index 4ff20336..00000000 --- a/Matrix/MatrixTranspose.c +++ /dev/null @@ -1,135 +0,0 @@ -#include -#include -#include -#include - -#define SIZE 20 - -struct DataItem { - int data; - int key; -}; - -struct DataItem* hashArray[SIZE]; -struct DataItem* dummyItem; -struct DataItem* item; - -int hashCode(int key) { - return key % SIZE; -} - -struct DataItem *search(int key) { - //get the hash - int hashIndex = hashCode(key); - - //move in array until an empty - while(hashArray[hashIndex] != NULL) { - - if(hashArray[hashIndex]->key == key) - return hashArray[hashIndex]; - - //go to next cell - ++hashIndex; - - //wrap around the table - hashIndex %= SIZE; - } - - return NULL; -} - -void insert(int key,int data) { - - struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem)); - item->data = data; - item->key = key; - - //get the hash - int hashIndex = hashCode(key); - - //move in array until an empty or deleted cell - while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) { - //go to next cell - ++hashIndex; - - //wrap around the table - hashIndex %= SIZE; - } - - hashArray[hashIndex] = item; -} - -struct DataItem* delete(struct DataItem* item) { - int key = item->key; - - //get the hash - int hashIndex = hashCode(key); - - //move in array until an empty - while(hashArray[hashIndex] != NULL) { - - if(hashArray[hashIndex]->key == key) { - struct DataItem* temp = hashArray[hashIndex]; - - //assign a dummy item at deleted position - hashArray[hashIndex] = dummyItem; - return temp; - } - - //go to next cell - ++hashIndex; - - //wrap around the table - hashIndex %= SIZE; - } - - return NULL; -} - -void display() { - int i = 0; - - for(i = 0; ikey,hashArray[i]->data); - else - printf(" ~~ "); - } - - printf("\n"); -} - -int main() { - dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem)); - dummyItem->data = -1; - dummyItem->key = -1; - - insert(1, 20); - insert(2, 70); - insert(42, 80); - insert(4, 25); - insert(12, 44); - insert(14, 32); - insert(17, 11); - insert(13, 78); - insert(37, 97); - - display(); - item = search(37); - - if(item != NULL) { - printf("Element found: %d\n", item->data); - } else { - printf("Element not found\n"); - } - - delete(item); - item = search(37); - - if(item != NULL) { - printf("Element found: %d\n", item->data); - } else { - printf("Element not found\n"); - } -} diff --git a/Matrix/Matrix_transpose.c b/Matrix/Matrix_transpose.c deleted file mode 100644 index 4ff20336..00000000 --- a/Matrix/Matrix_transpose.c +++ /dev/null @@ -1,135 +0,0 @@ -#include -#include -#include -#include - -#define SIZE 20 - -struct DataItem { - int data; - int key; -}; - -struct DataItem* hashArray[SIZE]; -struct DataItem* dummyItem; -struct DataItem* item; - -int hashCode(int key) { - return key % SIZE; -} - -struct DataItem *search(int key) { - //get the hash - int hashIndex = hashCode(key); - - //move in array until an empty - while(hashArray[hashIndex] != NULL) { - - if(hashArray[hashIndex]->key == key) - return hashArray[hashIndex]; - - //go to next cell - ++hashIndex; - - //wrap around the table - hashIndex %= SIZE; - } - - return NULL; -} - -void insert(int key,int data) { - - struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem)); - item->data = data; - item->key = key; - - //get the hash - int hashIndex = hashCode(key); - - //move in array until an empty or deleted cell - while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) { - //go to next cell - ++hashIndex; - - //wrap around the table - hashIndex %= SIZE; - } - - hashArray[hashIndex] = item; -} - -struct DataItem* delete(struct DataItem* item) { - int key = item->key; - - //get the hash - int hashIndex = hashCode(key); - - //move in array until an empty - while(hashArray[hashIndex] != NULL) { - - if(hashArray[hashIndex]->key == key) { - struct DataItem* temp = hashArray[hashIndex]; - - //assign a dummy item at deleted position - hashArray[hashIndex] = dummyItem; - return temp; - } - - //go to next cell - ++hashIndex; - - //wrap around the table - hashIndex %= SIZE; - } - - return NULL; -} - -void display() { - int i = 0; - - for(i = 0; ikey,hashArray[i]->data); - else - printf(" ~~ "); - } - - printf("\n"); -} - -int main() { - dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem)); - dummyItem->data = -1; - dummyItem->key = -1; - - insert(1, 20); - insert(2, 70); - insert(42, 80); - insert(4, 25); - insert(12, 44); - insert(14, 32); - insert(17, 11); - insert(13, 78); - insert(37, 97); - - display(); - item = search(37); - - if(item != NULL) { - printf("Element found: %d\n", item->data); - } else { - printf("Element not found\n"); - } - - delete(item); - item = search(37); - - if(item != NULL) { - printf("Element found: %d\n", item->data); - } else { - printf("Element not found\n"); - } -} diff --git a/Matrix/TransposeSparse.c b/Matrix/TransposeSparse.c deleted file mode 100644 index 17e0652f..00000000 --- a/Matrix/TransposeSparse.c +++ /dev/null @@ -1,69 +0,0 @@ -#include - -#define MAX 20 - -void printsparse(int[][3]); -void readsparse(int[][3]); -void transpose(int[][3],int[][3]); - -int main() -{ - int b1[MAX][3],b2[MAX][3],m,n; - printf("Enter the size of matrix (rows,columns):"); - - scanf("%d%d",&m,&n); - b1[0][0]=m; - b1[0][1]=n; - - readsparse(b1); - transpose(b1,b2); - printsparse(b2); -} - -void readsparse(int b[MAX][3]) -{ - int i,t; - printf("\nEnter no. of non-zero elements:"); - scanf("%d",&t); - b[0][2]=t; - - for(i=1;i<=t;i++) - { - printf("\nEnter the next triple(row,column,value):"); - scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]); - } -} - -void printsparse(int b[MAX][3]) -{ - int i,n; - n=b[0][2]; //no of 3-triples - - printf("\nAfter Transpose:\n"); - - printf("\nrow\t\tcolumn\t\tvalue\n"); - for(i=0;i<=n;i++) - printf("%d\t\t%d\t\t%d\n",b[i][0],b[i][1],b[i][2]); -} - -void transpose(int b1[][3],int b2[][3]) -{ - int i,j,k,n; - b2[0][0]=b1[0][1]; - b2[0][1]=b1[0][0]; - b2[0][2]=b1[0][2]; - - k=1; - n=b1[0][2]; - - for(i=0;i -#include -#define mod 1000000007 -typedef long long int lli; -lli fib(lli n,lli memo[]) -{ - lli res; - if(memo[n]!=0) - return memo[n]; - if(n==1 || n==2) - res = 1; - else - res = fib(n-1,memo) + fib(n-2,memo); - memo[n]=res; - return res; -} -using namespace std; -int main() -{ - lli memo[100000]={0},n,p; - cout<<"Enter the value of n :\n"; - cin>>n; - p = fib(n,memo); - cout< transactions = new ArrayList<>(); - transactions.add("aa"); - transactions.add("bb"); - transactions.add("dd"); - transactions.add("ee"); - - transactions.add("22"); - transactions.add("11"); - transactions.add("44"); - transactions.add("33"); - transactions.add("55"); - - MerkleTree merkleTree = new MerkleTree(transactions); - System.out.println(merkleTree.getMerkleRoot().get(0)); - } - - - -} diff --git a/Merkle Tree/MerkleTree.java b/Merkle Tree/MerkleTree.java deleted file mode 100644 index bb49a9ea..00000000 --- a/Merkle Tree/MerkleTree.java +++ /dev/null @@ -1,36 +0,0 @@ -package blockchain; -import java.util.*; - - -public class MerkleTree { - private List transactions; - public MerkleTree(List transactions) { - this.transactions=transactions; - } - - public List getMerkleRoot(){ - return construct(this.transactions); - } - - private List construct (List transactions){ - - if(transactions.size() ==1) return transactions; - - List updatedList = new ArrayList<>(); - - for(int i=0; i = N: - return True - - # Consider this column and try placing - # this queen in all rows one by one - for i in range(N): - - if isSafe(board, i, col): - # Place this queen in board[i][col] - board[i][col] = 1 - - # recur to place rest of the queens - if solveNQUtil(board, col + 1) == True: - return True - - # If placing queen in board[i][col - # doesn't lead to a solution, then - # queen from board[i][col] - board[i][col] = 0 - - # if the queen can not be placed in any row in - # this colum col then return false - return False - -# This function solves the N Queen problem using -# Backtracking. It mainly uses solveNQUtil() to -# solve the problem. It returns false if queens -# cannot be placed, otherwise return true and -# placement of queens in the form of 1s. -# note that there may be more than one -# solutions, this function prints one of the -# feasible solutions. -def solveNQ(): - board = [ [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0] - ] - - if solveNQUtil(board, 0) == False: - print "Solution does not exist" - return False - - printSolution(board) - return True - -# driver program to test above function -solveNQ() diff --git a/Python/Searching/CocktailSort.py b/Python/Searching/CocktailSort.py deleted file mode 100644 index 290f64bb..00000000 --- a/Python/Searching/CocktailSort.py +++ /dev/null @@ -1,24 +0,0 @@ -def cocktail(a): - for i in range(len(a)//2): - swap = False - for j in range(1+i, len(a)-i): - # test whether the two elements are in the wrong order - if a[j] < a[j-1]: - # let the two elements change places - a[j], a[j-1] = a[j-1], a[j] - swap = True - # we can exit the outer loop here if no swaps occurred. - if not swap: - break - swap = False - for j in range(len(a)-i-1, i, -1): - if a[j] < a[j-1]: - a[j], a[j-1] = a[j-1], a[j] - swap = True - if not swap: - break - -num_list = [75, 16, 55, 19, 48, 14, 2, 61, 22, 100] -print("Before: ", num_list) -cocktail(num_list) -print("After: ", num_list) diff --git a/Python/Searching/LinearSearch.py b/Python/Searching/LinearSearch.py deleted file mode 100644 index be8ac371..00000000 --- a/Python/Searching/LinearSearch.py +++ /dev/null @@ -1,20 +0,0 @@ -# Python3 code to linearly search x in arr[]. -# If x is present then return its location, -# otherwise return -1 - -def search(arr, n, x): - - for i in range (0, n): - if (arr[i] == x): - return i; - return -1; - -# Driver Code -arr = [ 2, 3, 4, 10, 40 ]; -x = 10; -n = len(arr); -result = search(arr, n, x) -if(result == -1): - print("Element is not present in array") -else: - print("Element is present at index", result); diff --git a/Queue_By_Stack/Simple_Queue_By_Stack.c b/Queue_By_Stack/Simple_Queue_By_Stack.c deleted file mode 100644 index 39b4ee96..00000000 --- a/Queue_By_Stack/Simple_Queue_By_Stack.c +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include -#define max 10 -int top1=-1,top2=-1,s1[max],s2[max]; -void insert(int p){ - if(top1==max) - printf("queue is full"); - else{ - top1++; - s1[top1] = p; - } -} -void push2(int p){ -if(top2==max) - printf("queue is full"); - else{ - top2++; - s2[top2] = p; - } -} -int pop1(){ - int r; - r=s1[top1]; - top1--; - return r; - -} -void delete1(){ - int i; - if(top2==-1 && top1==-1){ - printf("queue is empty"); - } - else if(top2==-1){ - for(i=top1;i>=0;i--){ - push2(pop1()); - } - - printf("Deleted iteam is %d\n",s2[top2--]); - } - else{ - printf("Deleted iteam is %d\n",s2[top2--]); - } -} -void display(){ - int i; - if(top1==-1 && top2==-1) printf("queue is empty\n"); - for(i=top2;i>=0;i--){ - printf("%d | ",s2[i]); - } - for(i=0;i<=top1;i++){ - printf("%d | ",s1[i]); - } - printf("\n"); - -} - - -int main() -{ - printf(" 1-insert \n 2-delete \n 3-display\n 4-exit\n"); - int ins,c=0; - while(c!=4){ - printf("enter your choise : "); - scanf("%d",&c); - - switch(c){ - case 1: - printf("enter element: "); - scanf("%d",&ins); - insert(ins); - break; - case 2: delete1(); - break; - case 3: display(); - - break; - default: exit(0); - break; - } - } - return 0; -} - - diff --git a/Queue_By_Stack/Simple_Queue_By_Stack_Output.txt b/Queue_By_Stack/Simple_Queue_By_Stack_Output.txt deleted file mode 100644 index 11b81a79..00000000 --- a/Queue_By_Stack/Simple_Queue_By_Stack_Output.txt +++ /dev/null @@ -1,30 +0,0 @@ - 1-insert - 2-delete - 3-display - 4-exit -enter your choise : 1 -enter element: 10 -enter your choise : 1 -enter element: 20 -enter your choise : 1 -enter element: 30 -enter your choise : 3 -10 | 20 | 30 | -enter your choise : 2 -Deleted iteam is 10 -enter your choise : 3 -20 | 30 | -enter your choise : 2 -Deleted iteam is 20 -enter your choise : 3 -30 | -enter your choise : 2 -Deleted iteam is 30 -enter your choise : 3 -queue is empty - -enter your choise : 4 - -Process returned 0 (0x0) execution time : 57.880 s -Press any key to continue. - diff --git a/README.md b/README.md deleted file mode 100644 index 4fcccee4..00000000 --- a/README.md +++ /dev/null @@ -1,52 +0,0 @@ -# 🎃 Data-Structure Project 🎃 - - -Use this project to make your first contribution to an open source project on GitHub. Practice making your first pull request to a public repository before doing the real thing! - -Celebrate [Hacktoberfest](https://hacktoberfest.digitalocean.com/) by getting involved in the open source community by completing some simple tasks in this project. - -This repository is open to all members of the GitHub community. Any member may contribute to this project without being a collaborator. - -https://github.com/Vatsalparsaniya/Data-Structure - - -## What is Hacktoberfest? - -A month-long celebration from October 1st - 31st sponsored by Digital Ocean and GitHub to get people involved in Open Source. Create your very first pull request to any public repository on GitHub and contribute to the open source developer community. - -https://hacktoberfest.digitalocean.com/ - -## How to contribute to this project - -Here are quick and painless ways to contribute to this project: - -- Add your name to the CONTRIBUTORS.md file -- Create Data-structure Program in a language of your choice - -Choose one or all , make a pull request for your work and wait for it to be merged! - -### check [CONTRIBUTING.md](/CONTRIBUTING.md) - -## Getting started - -1) Fork this repository (Click the Fork button in the top right of this page, click your Profile Image) -2) Clone your fork down to your local machine - -`git clone https://github.com/your-username/Data-Structure.git` - -3) Create a branch - -`git checkout -b branch-name` - -4) Make your changes (choose from any task listed above) -5) Commit and push - -```bash -git add . -git commit -m 'Commit message' -git push origin branch-name -``` - -6) Create a new pull request from your forked repository (Click the New Pull Request button located at the top of your repo) -7) Wait for your PR review and merge approval! -8) Star this repository if you had fun! diff --git a/Red_black_tree/red_black.cpp b/Red_black_tree/red_black.cpp deleted file mode 100644 index 81844c5a..00000000 --- a/Red_black_tree/red_black.cpp +++ /dev/null @@ -1,360 +0,0 @@ -#include - using namespace std; - -struct node { - int data{}; - node* left = nullptr; - node* right = nullptr; - node* parent = nullptr; - string color; -}; - - class RB_TREE { - - node* root; - - public: - RB_TREE() : root(nullptr) {} - - node* GetRoot(){ return root; } - - void InsertNode(int stuff) { - if(root == nullptr){ - root = new node(); - root->data = stuff; - root->parent = nullptr; - root->color = "BLACK"; - cout << "Element inserted.\n"; - } - else { - auto linker = GetRoot(); - node* newnode = new node(); - newnode->data = stuff; - - while(linker != nullptr){ - if(linker->data > stuff){ - if(linker->left == nullptr){ - linker->left = newnode; - newnode->color = "RED"; - newnode->parent = linker; - cout << "Element inserted.\n"; break; } - else { linker = linker->left; } - } else { - if(linker->right == nullptr){ - linker->right = newnode; - newnode->color = "RED"; - newnode->parent = linker; - cout << "Element inserted.\n"; break; } - else { linker = linker->right; } - } - } - RB_Insert_Fixup(newnode); - } - } - - void RB_Insert_Fixup(node* z) { - while(z->parent->color == "RED") { - auto grandparent = z->parent->parent; - auto uncle = GetRoot(); - if(z->parent == grandparent->left) { - if(grandparent->right) { uncle = grandparent->right; } - if(uncle->color == "RED"){ - z->parent->color = "BLACK"; - uncle->color = "BLACK"; - grandparent->color = "RED"; - if(grandparent->data != root->data){ z = grandparent; } - else { break; } - } - else if(z == grandparent->left->right) { - LeftRotate(z->parent); - } - else { - z->parent->color = "BLACK"; - grandparent->color = "RED"; - RightRotate(grandparent); - if(grandparent->data != root->data){ z = grandparent; } - else { break; } - } - } - else { - if(grandparent->left) { uncle = grandparent->left; } - if(uncle->color == "RED"){ - z->parent->color = "BLACK"; - uncle->color = "BLACK"; - grandparent->color = "RED"; - if(grandparent->data != root->data){ z = grandparent; } - else { break; } - } - else if(z == grandparent->right->left){ - RightRotate(z->parent); - } - else { - z->parent->color = "BLACK"; - grandparent->color = "RED"; - LeftRotate(grandparent); - if(grandparent->data != root->data){ z = grandparent; } - else { break; } - } - } - } - root->color = "BLACK"; - } - - - void RemoveNode(node* parent, node* curr, int stuff) { - if(curr == nullptr) { return; } - if(curr->data == stuff) { - //CASE -- 1 - if(curr->left == nullptr && curr->right == nullptr) { - if(parent->data == curr->data){ root = nullptr; } - else if(parent->right == curr) { - RB_Delete_Fixup(curr); - parent->right = nullptr; - } - else { - RB_Delete_Fixup(curr); - parent->left = nullptr; - } - } - //CASE -- 2 - else if(curr->left != nullptr && curr->right == nullptr) { - int swap = curr->data; - curr->data = curr->left->data; - curr->left->data = swap; - RemoveNode(curr, curr->right, stuff); - } - else if(curr->left == nullptr && curr->right != nullptr) { - int swap = curr->data; - curr->data = curr->right->data; - curr->right->data = swap; - RemoveNode(curr, curr->right, stuff); - } - //CASE -- 3 - else { - bool flag = false; - node* temp = curr->right; - while(temp->left) { flag = true; parent = temp; temp = temp->left; } - if(!flag) { parent = curr; } - int swap = curr->data; - curr->data = temp->data; - temp->data = swap; - RemoveNode(parent, temp, swap); - } - } - } - - void Remove(int stuff) { - auto temp = root; - auto parent = temp; - bool flag = false; - if(!temp) { RemoveNode(nullptr, nullptr, stuff); } - - while(temp) { - if(stuff == temp->data) { flag = true; RemoveNode(parent, temp, stuff); break; } - else if(stuff < temp->data) { parent = temp ; temp = temp->left; } - else { parent = temp ; temp = temp->right; } - } - - if(!flag) { cout << "\nElement doesn't exist in the table"; } - } - - void RB_Delete_Fixup(node* z) { - while(z->data != root->data && z->color == "BLACK") { - auto sibling = GetRoot(); - if(z->parent->left == z) { - if(z->parent->right){ sibling = z->parent->right; } - if(sibling) { - //CASE -- 1 - if(sibling->color == "RED") { - sibling->color = "BLACK"; - z->parent->color = "RED"; - LeftRotate(z->parent); - sibling = z->parent->right; - } - //CASE -- 2 - if(sibling->left == nullptr && sibling->right == nullptr) { - sibling->color = "RED"; - z = z->parent; - } - else if(sibling->left->color == "BLACK" && sibling->right->color == "BLACK") { - sibling->color = "RED"; - z = z->parent; - } - //CASE -- 3 - else if(sibling->right->color == "BLACK") { - sibling->left->color = "BLACK"; - sibling->color = "RED"; - RightRotate(sibling); - sibling = z->parent->right; - } else { - sibling->color = z->parent->color; - z->parent->color = "BLACK"; - if(sibling->right){ sibling->right->color = "BLACK"; } - LeftRotate(z->parent); - z = root; - } - } - } else { - if(z->parent->right == z){ - if(z->parent->left){ sibling = z->parent->left; } - if(sibling) { - //CASE -- 1 - if(sibling->color == "RED"){ - sibling->color = "BLACK"; - z->parent->color = "RED"; - RightRotate(z->parent); - sibling = z->parent->left; - } - //CASE -- 2 - if(sibling->left == nullptr && sibling->right == nullptr) { - sibling->color = "RED"; - z = z->parent; - } - else if(sibling->left->color == "BLACK" && sibling->right->color == "BLACK") { - sibling->color = "RED"; - z = z->parent; - } - //CASE -- 3 - else if(sibling->left->color == "BLACK") { - sibling->right->color = "BLACK"; - sibling->color = "RED"; - RightRotate(sibling); - sibling = z->parent->left; - } else { - sibling->color = z->parent->color; - z->parent->color = "BLACK"; - if(sibling->left){ sibling->left->color = "BLACK"; } - LeftRotate(z->parent); - z = root; - } - } - } - - } - } - z->color = "BLACK"; - } - - node* TreeSearch(int stuff) { - auto temp = GetRoot(); - if(temp == nullptr) { return nullptr; } - - while(temp) { - if(stuff == temp->data){ return temp; } - else if(stuff < temp->data){ temp = temp->left; } - else { temp = temp->right; } - } - return nullptr; - } - - void LeftRotate(node* x) { - node* nw_node = new node(); - if(x->right->left) { nw_node->right = x->right->left; } - nw_node->left = x->left; - nw_node->data = x->data; - nw_node->color = x->color; - x->data = x->right->data; - - x->left = nw_node; - if(nw_node->left){ nw_node->left->parent = nw_node; } - if(nw_node->right){ nw_node->right->parent = nw_node; } - nw_node->parent = x; - - if(x->right->right){ x->right = x->right->right; } - else { x->right = nullptr; } - - if(x->right){ x->right->parent = x; } - } - - void RightRotate(node* x) { - node* nw_node = new node(); - if(x->left->right){ nw_node->left = x->left->right; } - nw_node->right = x->right; - nw_node->data = x->data; - nw_node->color = x->color; - - x->data = x->left->data; - x->color = x->left->color; - - x->right = nw_node; - if(nw_node->left){ nw_node->left->parent = nw_node; } - if(nw_node->right){ nw_node->right->parent = nw_node; } - nw_node->parent = x; - - if(x->left->left){ x->left = x->left->left; } - else { x->left = nullptr; } - - if(x->left){ x->left->parent = x; } - } - - void PreorderTraversal(node* temp) { - if(!temp){ return; } - cout << "--> " << temp->data << "<" << temp->color << ">"; - PreorderTraversal(temp->left); - PreorderTraversal(temp->right); - } - - void PostorderTraversal(node *temp) { - if(!temp){ return; } - PostorderTraversal(temp->left); - PostorderTraversal(temp->right); - cout << "--> " << temp->data << "<" << temp->color << ">"; - } - }; - - void menu(){ - cout << "\n__________________________________________"; - cout << "\n\n --HEIGHT BALANCED BINARY SEARCH TREE--"; - cout << "\n --(Red-Black-Tree)--"; - cout << "\n__________________________________________"; - cout << "\n\n1. Insert elements into the tree."; - cout << "\n2. Search for an element."; - cout << "\n3. PRE-ORDER Tree-Walk."; - cout << "\n4. POST-ORDER Tree-Walk."; - cout << "\n5. Remove an element from the tree."; - cout << "\n6. Exit."; - cout << "\n__________________________________________"; - cout << "\nYour Choice -- "; - } - - - int main(){ - RB_TREE demo; - int info, input; - menu(); - cin >> info; - while(info != 6){ - switch (info){ - case 1: cout << "\nElement to be inserted -- "; - cin >> input; demo.InsertNode(input); - break; - - case 2: cout << "\nElement to be searched -- "; - cin >> input; - if(demo.TreeSearch(input)) { cout << "Element found.\n"; } - else { cout << "Element not found.\n"; } - break; - - case 3: cout << "Pre-Order Tree Walk "; - demo.PreorderTraversal(demo.GetRoot()); - cout << endl; - break; - - case 4: cout << "Post-Order Tree Walk "; - demo.PostorderTraversal(demo.GetRoot()); - cout << endl; - break; - - case 5: cout << "\nElement to be deleted? -- "; - cin >> input; - demo.Remove(input); - break; - - default: cout << "Wrong Choice.\n"; - } - cout << "\nAnything Else?"; - cin >> info; - } - cout << "\nTerminating.... "; - return 0; - } \ No newline at end of file diff --git a/Search In Linked List/Search_linked_list.c b/Search In Linked List/Search_linked_list.c deleted file mode 100644 index c53381fc..00000000 --- a/Search In Linked List/Search_linked_list.c +++ /dev/null @@ -1,102 +0,0 @@ -#include -#include -void create(); -void display(); -void search(); -struct Node{ - int data; - struct Node *next; -} -*Start =NULL; -int main() -{ - printf("This is my first link list program\n"); - while(1) - { - int choice; - printf("\nEnter your choice\n1.For creating\n2.For Displaying\n3.Searching element in liste\n4.For exit\n"); - scanf("%d",&choice); - switch(choice) - { - case 1:create(); - break; - case 2:display(); - break; - case 3:search(); - break; - case 4:exit(0); - } - } - - - return 0; -} - -void create() -{ - - int a; - do{ - - struct Node *current,*newnode; - newnode=(struct Node*)malloc(sizeof(struct Node)); - printf("\nEnter the data to be entered in the node: "); - scanf("%d",&newnode->data); - newnode->next=NULL; - if(Start==NULL) - { - current=newnode; - Start=newnode; - } - else{ - current->next=newnode; - current=newnode; - } - printf("Do you want to continue creating nodes\n1. to continue\n0. to stop: "); - scanf("%d",&a); - } - while(a!=0); -} -void display() -{ - struct Node *temp; - temp=Start; - printf("The linked list is: "); - if(Start==NULL) - { - printf("the list is empty\n"); - } - else{ - while(temp!=NULL) - { - printf("%d ",temp->data); - temp=temp->next; - } - printf("\n"); - } -} -void search() -{ - struct Node *temp; - temp=Start; - int item,ans=0; - printf("\nEnter the element to Search in list: "); - scanf("%d",&item); - if(Start==NULL) - printf("The list is empty "); - else{ - while(temp!=NULL) - { - if(temp->data==item) - { - ans=1; - break; - } - temp=temp->next; - } - if(ans==1) - printf("\nItem found\n"); - else - printf("\nItem not in list\n"); - } -} diff --git a/Search_Binary/SearchBinary.py b/Search_Binary/SearchBinary.py deleted file mode 100644 index 97bc51b3..00000000 --- a/Search_Binary/SearchBinary.py +++ /dev/null @@ -1,40 +0,0 @@ -class SearchBinary(object): - - def __init__(self, array): - """ - Initialize SearchBinary with a sorted array to be searched - :type l: list - """ - self.list = [] - self.replace_list(array) - - def replace_list(self, array): - if isinstance(array, list): - for i in array: - if not (isinstance(i,int) or isinstance(i, float)): - raise ValueError("all elements must be type number") - else: - raise AttributeError("search array must be list type") - - self.list = array - - - def find(self, key): - """ - Find the index of the key in the list given to SearchBinary - """ - left_boundry = 0 - right_boundry = len(self.list) - 1 - - while left_boundry <= right_boundry: - middle = (left_boundry + right_boundry) // 2 - if self.list[middle] < key: - left_boundry = middle + 1 - - elif self.list[middle] > key: - right_boundry = middle - 1 - - else: - return middle - # Returning -1 (invalid index) indicates not found - return -1 \ No newline at end of file diff --git a/Search_Binary/binarySearch.kt b/Search_Binary/binarySearch.kt deleted file mode 100644 index 87cd8408..00000000 --- a/Search_Binary/binarySearch.kt +++ /dev/null @@ -1,30 +0,0 @@ -class BinarySearch { - fun doBinarySearch () { - println("Please, enter some integer numbers using space bar") - val inputList = readLine()!!.split(' ').map(String::toInt) - val listSorted = inputList.sorted() - println("Please, enter the number you want to find") - val item = readLine()!!.toInt() - var low = 0 - var high = listSorted.size - 1 - var stepCount = 0 - var isItemFound = false - while (low <= high) { - val mid = (low + high) / 2 - val guess = listSorted[mid] - stepCount++ - when { - guess == item -> { - println("Your number $item was found in $stepCount steps") - isItemFound = true - } - guess > item -> high = mid -1 - else -> low = mid + 1 - } - if (isItemFound) break //"break" is not allowed in "when" statement - } - if (!isItemFound) { - println("Your number wasn't found") - } - } -} diff --git a/Shortest_Path_Algos/floyd-warshall.cpp b/Shortest_Path_Algos/floyd-warshall.cpp deleted file mode 100644 index e059ab50..00000000 --- a/Shortest_Path_Algos/floyd-warshall.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include -#define INFINITY 100000 -using namespace std; -int main(void) -{ - int num_vertices,num_edges,i,j,k,a,b,weight; - cout<<"Enter number of vertex: "<>num_vertices; - cout<<"Enter number of edges: "<>num_edges; - long cost[num_vertices][num_vertices] ; - for(i=0;i>a>>b>>weight; - cost[a-1][b-1] = weight; - } - int edges = num_edges; - for(i=0;i"< -#include -#define INFINITY 100000 -using namespace std; -void ShortestPathDistance(long *cost,int num_vertices,bool * visited) -{ - long dist[num_vertices] = {100000}; - vector distances; - long i,cnt = 2,j,u; - for(i=1;i>num_vertices; - if(num_vertices<=1) - return 0; - cout<<"Enter number of edges: "<>num_edges; - - long cost[num_vertices][num_vertices] ; - bool visited[num_vertices] = {false}; - - for(i=0;i>a>>b>>weight; - cost[a-1][b-1] = weight; - } - - cout< - -int n; - -void bubble_sort(int v[]){ - int i,k,aux = 0; - for(i = 0; i < n; i++){ - for(k = 0; k < n-1; k++){ - if(v[k] > v[k+1]){ - aux = v[k+1]; - v[k+1] = v[k]; - v[k] = aux; - } - } - } - -} - - -int main(){ - int j; - scanf("%d",&n); - int v[n]; - - for(j = 0; j < n; j++){ - scanf("%d",&v[j]); - } - bubble_sort(v); - for(j = 0; j < n; j++){ - if(j != n-1){ - printf("%d ",v[j]); - } - - else{ - printf("%d\n",v[j]); - } - } - - return 0; -} diff --git a/Shorting/Merge_sort.c b/Shorting/Merge_sort.c deleted file mode 100644 index cc33dfd7..00000000 --- a/Shorting/Merge_sort.c +++ /dev/null @@ -1,96 +0,0 @@ -#include -#include - -#define SIZE 8 - -void merge(int a[], int tmp[], int left, int mid, int right); -void msort(int a[], int tmp[], int left, int right); -void merge_sort(int a[], int tmp[], const int size); -void display(int a[],const int size); - -int main() -{ - int a[SIZE] = {5,6,3,1,7,8,2,4}; - int tmp[SIZE]; - - printf("--- C Merge Sort Demonstration --- \n"); - - printf("Array before sorting:\n"); - display(a,SIZE); - - merge_sort(a, tmp, SIZE); - - printf("Array after sorting:\n"); - display(a,SIZE); - - return 0; -} - -void merge_sort(int a[], int tmp[], const int size) -{ - msort(a, tmp, 0, size - 1); -} - -void msort(int a[], int tmp[], int left, int right) -{ - int mid; - if (right > left) - { - mid = (right + left) / 2; - msort(a, tmp, left, mid); - msort(a, tmp, mid + 1, right); - merge(a, tmp, left, mid + 1, right); - } -} - -void merge(int a[], int tmp[], int left, int mid, int right) -{ - int i, left_end, count, tmp_pos; - left_end = mid - 1; - tmp_pos = left; - count = right - left + 1; - - while ((left <= left_end) && (mid <= right)) - { - if (a[left] <= a[mid]) - { - tmp[tmp_pos] = a[left]; - tmp_pos = tmp_pos + 1; - left = left +1; - } - else - { - tmp[tmp_pos] = a[mid]; - tmp_pos = tmp_pos + 1; - mid = mid + 1; - } - } - - while (left <= left_end) - { - tmp[tmp_pos] = a[left]; - left = left + 1; - tmp_pos = tmp_pos + 1; - } - while (mid <= right) - { - tmp[tmp_pos] = a[mid]; - mid = mid + 1; - tmp_pos = tmp_pos + 1; - } - - for (i = 0; i <= count; i++) - { - a[right] = tmp[right]; - right = right - 1; - } -} - -void display(int a[],const int size) -{ - int i; - for(i = 0; i < size; i++) - printf("%d ",a[i]); - - printf("\n"); -} diff --git a/Shorting/insertionSort.java b/Shorting/insertionSort.java deleted file mode 100644 index cc9c27d7..00000000 --- a/Shorting/insertionSort.java +++ /dev/null @@ -1,36 +0,0 @@ -class InsertionSort -{ - void insertionSort(int arr[]) - { - for (int i = 1; i < arr.length; ++i) - { - int key = arr[i]; - int j = i - 1; - while (j >= 0 && arr[j] > key) - { - arr[j + 1] = arr[j]; - j = j - 1; - } - arr[j + 1] = key; - } - } - - static void display(int arr[]) - { - for (int i = 0; i < arr.length; ++i) - System.out.print(arr[i] + " "); - - System.out.println(); - } - - public static void main(String args[]) - { - int arr[] = { 22, 21, 23, 15, 18, 5, 8 }; - System.out.println("Array before sorting: "); - display(arr); - InsertionSort obj = new InsertionSort(); - obj.insertionSort(arr); - System.out.println("\nArray after sorting: "); - display(arr); - } -} \ No newline at end of file diff --git a/Shorting/quick_sort.py b/Shorting/quick_sort.py deleted file mode 100644 index ce8d9ca8..00000000 --- a/Shorting/quick_sort.py +++ /dev/null @@ -1,23 +0,0 @@ -def partition(arr, first, last): - pivot = arr[last] - i = first - 1 - for j in range(first, last): - if (arr[j] <= pivot): - i += 1 - arr[i], arr[j] = arr[j], arr[i] - arr[i + 1], arr[last] = arr[last], arr[i + 1] - return (i + 1) - -#function to perform quicksort in array -def quickSort(arr, first, last): - if first < last: - #pivot is correct position of arr[last] - pivot = partition(arr, first, last) - quickSort(arr, first, pivot - 1) - quickSort(arr, pivot + 1, last) - -if __name__=="__main__": - arr= list(map(int, input().split())) - n=len(arr) - quickSort(arr, 0, n-1) - print (arr) \ No newline at end of file diff --git a/Shorting/selection_sort.py b/Shorting/selection_sort.py deleted file mode 100644 index 203acca6..00000000 --- a/Shorting/selection_sort.py +++ /dev/null @@ -1,26 +0,0 @@ -#Selection sort in Python - -a = [] -size = int(input("Enter the size of list\n")) -for i in range(size): - a.append(int(input())) - -for i in range(len(a)): - min = a[i] - index = i - - - for j in range(i+1,len(a)): - if min>a[j]: - min = a[j] - index = j - - - - - temp = a[i] - a[i] = min - a[index] = temp - -print("The sorted list is :") -print(a) diff --git a/Simple_Queue/Simple_Queue.c b/Simple_Queue/Simple_Queue.c deleted file mode 100644 index 339e83b7..00000000 --- a/Simple_Queue/Simple_Queue.c +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include -#define MAXSIZE 5 -int front=-1,rear=-1,q[MAXSIZE]; -void insert(int a){ - if(rear>=MAXSIZE){ - printf("sorry!! \n queue is full \n"); - } - else{ - if(front==-1) front=0; - rear++; - q[rear]=a; - printf("element %d is successfully added\n",a); - } -} - -void delete(){ - if(front<0){ - printf("Error!! \n Underflow condition\n"); - } - else if(front==rear){ - q[front]=0; - front=-1; - rear=-1; - - printf("element successfully delete\n"); - } - else{ - q[front]=0; - front++; - printf("element successfully delete\n"); - } - - -} - -void display(){ - int i=0; - for(i=0;i<=MAXSIZE;i++){ - printf(" %d |",q[i]); - } - printf("\n"); -} - -int main(){ - int choise=0,inse; - printf(" 1) insert element \n 2) delete element \n 3) display queue \n 4) exit\n "); - while(choise != 4){ - printf("enter your choice : "); - scanf("%d",&choise); - switch(choise) - { - case 1: - printf("enter a number which you want to add in queue:"); - scanf("%d",&inse); - insert(inse); - break; - case 2: - printf("you chose delete option\n"); - delete(); - break; - case 3: - printf("you chose display option\n"); - display(); - break; - case 4: - printf("\nI hope u enjoy this program \n"); - break; - } -} -return 0; - -} - - diff --git a/Simple_Queue/Simple_Queue.js b/Simple_Queue/Simple_Queue.js deleted file mode 100644 index 9c7ba0ca..00000000 --- a/Simple_Queue/Simple_Queue.js +++ /dev/null @@ -1,29 +0,0 @@ -class Queue { - constructor() { - this.queue = []; - } - - add(element) { - this.queue.push(element); - } - - remove() { - if (this.isEmpty()) { - return 'Empty Queue'; - } - return this.queue.shift(); - } - - peek() { - if (this.isEmpty()) { - return 'Empty Queue'; - } - - return this.queue[0]; - } - - isEmpty() { - return this.queue.length === 0; - } - } - \ No newline at end of file diff --git a/Simple_Queue/Simple_Queue_Output.txt b/Simple_Queue/Simple_Queue_Output.txt deleted file mode 100644 index 8780f37d..00000000 --- a/Simple_Queue/Simple_Queue_Output.txt +++ /dev/null @@ -1,82 +0,0 @@ - 1) insert element - 2) delete element - 3) display queue - 4) exit - enter your choice : 1 -enter a number which you want to add in queue:155 -element 155 is successfully added -enter your choice : 1 -enter a number which you want to add in queue:34 -element 34 is successfully added -enter your choice : 3 -you chose display option - 155 | 34 | 0 | 0 | 0 | 0 | -enter your choice : 2 -you chose delete option -element successfully delete -enter your choice : 3 -you chose display option - 0 | 34 | 0 | 0 | 0 | 0 | -enter your choice : 2 -you chose delete option -element successfully delete -enter your choice : 3 -you chose display option - 0 | 0 | 0 | 0 | 0 | 0 | -enter your choice : 1 -enter a number which you want to add in queue:27 -element 27 is successfully added -enter your choice : 1 -enter a number which you want to add in queue:65 -element 65 is successfully added -enter your choice : 1 -enter a number which you want to add in queue:87 -element 87 is successfully added -enter your choice : 1 -enter a number which you want to add in queue:32 -element 32 is successfully added -enter your choice : 3 -you chose display option - 27 | 65 | 87 | 32 | 0 | 0 | -enter your choice : 1 -enter a number which you want to add in queue:98 -element 98 is successfully added -enter your choice : 1 -enter a number which you want to add in queue:185 -element 185 is successfully added -enter your choice : 3 -you chose display option - 27 | 65 | 87 | 32 | 98 | 185 | -enter your choice : 1 -enter a number which you want to add in queue:188 -sorry!! - queue is full -enter your choice : 2 -you chose delete option -element successfully delete -enter your choice : 2 -you chose delete option -element successfully delete -enter your choice : 2 -you chose delete option -element successfully delete -enter your choice : 2 -you chose delete option -element successfully delete -enter your choice : 2 -you chose delete option -element successfully delete -enter your choice : 2 -you chose delete option -element successfully delete -enter your choice : 2 -you chose delete option -Error!! - Underflow condition -enter your choice : 4 - -I hope u enjoy this program - -Process returned 0 (0x0) execution time : 106.070 s -Press any key to continue. - diff --git a/Simple_Queue/queue.java b/Simple_Queue/queue.java deleted file mode 100644 index afff1876..00000000 --- a/Simple_Queue/queue.java +++ /dev/null @@ -1,109 +0,0 @@ -import java.util.*; - -// Class for queue -class Queue -{ - private int arr[]; // array to store queue elements - private int front; // front points to front element in the queue - private int rear; // rear points to last element in the queue - private int capacity; // maximum capacity of the queue - private int count; // current size of the queue - - // Constructor to initialize queue - Queue(int size) - { - arr = new int[size]; - capacity = size; - front = 0; - rear = -1; - count = 0; - } - - // Utility function to remove front element from the queue - public void dequeue() - { - // check for queue underflow - if (isEmpty()) - { - System.out.println("UnderFlow\nProgram Terminated"); - System.exit(1); - } - - System.out.println("Removing " + arr[front]); - - front = (front + 1) % capacity; - count--; - } - - // Utility function to add an item to the queue - public void enqueue(int item) - { - // check for queue overflow - if (isFull()) - { - System.out.println("OverFlow\nProgram Terminated"); - System.exit(1); - } - - System.out.println("Inserting " + item); - - rear = (rear + 1) % capacity; - arr[rear] = item; - count++; - } - - // Utility function to return front element in the queue - public int peek() - { - if (isEmpty()) - { - System.out.println("UnderFlow\nProgram Terminated"); - System.exit(1); - } - return arr[front]; - } - - // Utility function to return the size of the queue - public int size() - { - return count; - } - - // Utility function to check if the queue is empty or not - public Boolean isEmpty() - { - return (size() == 0); - } - - // Utility function to check if the queue is empty or not - public Boolean isFull() - { - return (size() == capacity); - } - - // Queue implementation in java - public static void main (String[] args) - { - // create a queue of capacity 5 - Queue q = new Queue(5); - - q.enqueue(1); - q.enqueue(2); - q.enqueue(3); - - System.out.println("Front element is: " + q.peek()); - q.dequeue(); - System.out.println("Front element is: " + q.peek()); - - System.out.println("Queue size is " + q.size()); - - q.dequeue(); - q.dequeue(); - - if (q.isEmpty()) - System.out.println("Queue Is Empty"); - else - System.out.println("Queue Is Not Empty"); - } -} -//SOURCE: https://www.techiedelight.com/queue-implementation-in-java/ diff --git a/Simple_Queue/queue.swift b/Simple_Queue/queue.swift deleted file mode 100644 index d0c98f41..00000000 --- a/Simple_Queue/queue.swift +++ /dev/null @@ -1,271 +0,0 @@ -// -// queue.swift -// Queue -// -// Created by Steven Sim on 8/10/19. -// Copyright © 2019 Steven Sim. All rights reserved. -// - -import Foundation - -//To create Node property -public class Node { - var value: T - - init(value: T){ - self.value = value - } - - var next: Node? - weak var prev: Node? -} - -//Data Structure logics -public class DoubleLinkedList { - fileprivate var head: Node? - private var tail: Node? - - public var isEmpty: Bool { - return head == nil - } - - public var first: Node? { - return head; - } - - public var last: Node? { - return tail - } - - public func push(value: T) { - - let curr = Node(value: value) - - if let tailNode = tail { - curr.prev = tailNode - tailNode.next = curr - } else { - head = curr - } - - tail = curr - } - - public func getNode(index: Int) -> Node? { - - if index >= 0 { - var curr = head - var i = index - - while curr != nil { - if i == 0 { return curr } - i -= 1 - curr = curr!.next - } - } - - return nil - } - - public func getSize() -> Int{ - var curr = head - var i: Int = 0 - - while curr != nil { - i += 1 - curr = curr!.next - } - - return i - } - - public func pop(curr: Node) -> T { - let prev = curr.prev - let next = curr.next - - if let prev = prev { - prev.next = next - } else { - head = next - } - - next?.prev = prev - - if next == nil { - tail = prev - } - - curr.prev = nil - curr.next = nil - - return curr.value - } - - public func popAll() { - head = nil - tail = nil - } -} - - -public struct Queue { - - fileprivate var list = DoubleLinkedList() - - public var isEmpty: Bool { - return list.isEmpty - } - - public mutating func enqueue(_ element: T){ - list.push(value: element) - } - - public mutating func dequeue() -> T? { - guard !list.isEmpty, let element = list.first else { return nil } - - let removedElement = list.pop(curr: element) - - return removedElement - } - - public func peek() -> T? { - return list.first?.value - } - - public func getSize() -> Int { - return list.getSize() - } - - public func peekAt(index: Int) -> Node? { - return list.getNode(index: index) - } - - public func dequeueAll(){ - list.popAll() - } - -} - -public class BookStoreCustomer { - var queueId: String - var bookQty: Int - - init(queueId: String, bookQty: Int) { - self.queueId = queueId - self.bookQty = bookQty - } -} - -//Main Program -public func enqueueBuyer(){ - print("Enter queue id: ", terminator: "") - let queueId: String? = readLine() - print("Enter book quantity to buy: ", terminator: "") - let bookQty: Int? = Int(readLine()!) - - queue.enqueue(BookStoreCustomer(queueId: queueId!, bookQty: bookQty!)) -} - -public func clrScr(){ - for _ in 1...25 { - print("") - } -} - -public func separatorBar(length : Int){ - for _ in 1...length { - print("=", terminator: "") - } - print("") -} - -public func showQueue(){ - - if queue.isEmpty { - print("No data yet") - } - else{ - let header: String = String(format: "| %-4s | %-12s | %-10s |", ("No" as NSString).utf8String!,("ID" as NSString).utf8String!, ("Quantity" as NSString).utf8String!) - - separatorBar(length: header.count) - print(header) - separatorBar(length: header.count) - for i in 0 ..< queue.getSize() { - let curr = queue.peekAt(index: i) - print(String(format: "| %-4d | %-12s | %-10d |", i+1, (curr!.value.queueId as NSString).utf8String!, curr!.value.bookQty)) - } - separatorBar(length: header.count) - } - -} - -public func dequeueBuyer(){ - if queue.isEmpty { - print("No data yet") - } - else{ - if let buyer = queue.dequeue() { - print("Buyer with ID: \(buyer.queueId) served") - } - } -} - -public func dequeueAllBuyer(){ - queue.dequeueAll() - - print("All Buyer served") -} - -var queue = Queue() - -//Dummy data -queue.enqueue(BookStoreCustomer(queueId: "Q12", bookQty: 2)) -queue.enqueue(BookStoreCustomer(queueId: "Q14", bookQty: 3)) -queue.enqueue(BookStoreCustomer(queueId: "Q16", bookQty: 4)) - -while true { - - clrScr() - var choose: Int! = 0; - - print("Buying Book Queue Program") - print("=====================") - if let nextCustomer = queue.peek() { - print("Next Queue ID to Serve: \(nextCustomer.queueId)") - print("=====================") - } - print("Menu") - print("1. Queue Buyer") - print("2. Show") - print("3. Serve Buyer") - print("4. Serve All Buyer") - print("5. Exit") - - repeat { - print("Input: ", terminator: "") - if let str = readLine(){ - choose = Int(str) - } - } while choose < 1 || choose > 5; - - switch choose { - case 1: - enqueueBuyer() - case 2: - showQueue() - case 3: - dequeueBuyer() - case 4: - dequeueAllBuyer() - case 5: - print("Thank you for using this program :)") - default: - print("") - } - - _ = readLine() - - if choose == 5 { - break - } -} diff --git a/Simple_Queue/queue_linkedlist.c b/Simple_Queue/queue_linkedlist.c deleted file mode 100644 index e08b8a3a..00000000 --- a/Simple_Queue/queue_linkedlist.c +++ /dev/null @@ -1,103 +0,0 @@ -#include -#include -struct node -{ - int data; - struct node *next; - }; -struct node *front; -struct node *rear; -void insert(); -void delete(); -void display(); -void main () - { - int choice; - while(choice != 4) - { - printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n"); - printf("\nEnter your choice ?"); - scanf("%d",& choice); - switch(choice) - { - case 1: - insert(); - break; - case 2: - delete(); - break; - case 3: - display(); - break; - case 4: - exit(0); - break; - default: - printf("\nEnter valid choice??\n"); - } - } - } - -void insert() -{ -struct node *ptr; -int item; - -ptr = (struct node *) malloc (sizeof(struct node)); - if(ptr == NULL) - { - printf("\nOVERFLOW\n"); - return; - } - else - { - printf("\nEnter value?\n"); - scanf("%d",&item); - ptr -> data = item; - if(front == NULL) - { - front = ptr; - rear = ptr; - front -> next = NULL; - rear -> next = NULL; - } - else - { - rear -> next = ptr; - rear = ptr; - rear->next = NULL; - } - } -} -void delete () -{ - struct node *ptr; - if(front == NULL) - { - printf("\nUNDERFLOW\n"); - return; - } - else - { - ptr = front; - front = front -> next; - free(ptr); - } -} -void display() -{ - struct node *ptr; - ptr = front; - if(front == NULL) - { - printf("\nEmpty queue\n"); - } - else - { printf("\nprinting values \n"); - while(ptr != NULL) - { - printf("\n%d\n",ptr -> data); - ptr = ptr -> next; - } - } - } diff --git a/Sorting/Bitonicsort.cpp b/Sorting/Bitonicsort.cpp deleted file mode 100644 index 1764443f..00000000 --- a/Sorting/Bitonicsort.cpp +++ /dev/null @@ -1,77 +0,0 @@ - -// Source : https://www.geeksforgeeks.org/bitonic-sort/ - -/* C++ Program for Bitonic Sort. Note that this program - works only when size of input is a power of 2. */ - -#include -#include -using namespace std; - -/*The parameter dir indicates the sorting direction, ASCENDING - or DESCENDING; if (a[i] > a[j]) agrees with the direction, - then a[i] and a[j] are interchanged.*/ -void compAndSwap(int a[], int i, int j, int dir) -{ - if (dir == (a[i] > a[j])) - swap(a[i], a[j]); -} - -/*It recursively sorts a bitonic sequence in ascending order, - if dir = 1, and in descending order otherwise (means dir=0). - The sequence to be sorted starts at index position low, - the parameter cnt is the number of elements to be sorted.*/ -void bitonicMerge(int a[], int low, int cnt, int dir) -{ - if (cnt > 1) - { - int k = cnt / 2; - for (int i = low; i < low + k; i++) - compAndSwap(a, i, i + k, dir); - bitonicMerge(a, low, k, dir); - bitonicMerge(a, low + k, k, dir); - } -} - -/* This function first produces a bitonic sequence by recursively - sorting its two halves in opposite sorting orders, and then - calls bitonicMerge to make them in the same order */ -void bitonicSort(int a[], int low, int cnt, int dir) -{ - if (cnt > 1) - { - int k = cnt / 2; - - // sort in ascending order since dir here is 1 - bitonicSort(a, low, k, 1); - - // sort in descending order since dir here is 0 - bitonicSort(a, low + k, k, 0); - - // Will merge wole sequence in ascending order - // since dir=1. - bitonicMerge(a, low, cnt, dir); - } -} - -/* Caller of bitonicSort for sorting the entire array of - length N in ASCENDING order */ -void sort(int a[], int N, int up) -{ - bitonicSort(a, 0, N, up); -} - -// Driver code -int main() -{ - int a[] = {3, 7, 4, 8, 6, 2, 1, 5}; - int N = sizeof(a) / sizeof(a[0]); - - int up = 1; // means sort in ascending order - sort(a, N, up); - - printf("Sorted array: \n"); - for (int i = 0; i < N; i++) - printf("%d ", a[i]); - return 0; -} diff --git a/Sorting/Bubble_Sort.c b/Sorting/Bubble_Sort.c deleted file mode 100644 index 27db70b6..00000000 --- a/Sorting/Bubble_Sort.c +++ /dev/null @@ -1,40 +0,0 @@ -#include - -int n; - -void bubble_sort(int v[]){ - int i,k,aux = 0; - for(i = 0; i < n; i++){ - for(k = 0; k < n-1; k++){ - if(v[k] > v[k+1]){ - aux = v[k+1]; - v[k+1] = v[k]; - v[k] = aux; - } - } - } - -} - - -int main(){ - int j; - scanf("%d",&n); - int v[n]; - - for(j = 0; j < n; j++){ - scanf("%d",&v[j]); - } - bubble_sort(v); - for(j = 0; j < n; j++){ - if(j != n-1){ - printf("%d ",v[j]); - } - - else{ - printf("%d\n",v[j]); - } - } - - return 0; -} diff --git a/Sorting/Counting_sort.c b/Sorting/Counting_sort.c deleted file mode 100644 index dc79973f..00000000 --- a/Sorting/Counting_sort.c +++ /dev/null @@ -1,33 +0,0 @@ -#include - -void counting_sort(int a[],int n,int max) -{ - int count[50]={0},i,j; - - for(i=0;imax) - max=a[i]; - } - - counting_sort(a,n,max); - return 0; -} \ No newline at end of file diff --git a/Sorting/Cpp/Sorting/Bubble_sort.cpp b/Sorting/Cpp/Sorting/Bubble_sort.cpp deleted file mode 100644 index b584cba1..00000000 --- a/Sorting/Cpp/Sorting/Bubble_sort.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -using namespace std; - -template -void bubble_sort(vector & vec) -{ - for(size_t i = 0; i < vec.size() - 1; i++) - { - for(size_t j = i + 1; j < vec.size(); j++) - { - if(vec[j - 1] > vec[j]) - swap(vec[j - 1], vec[j]); - } - } -} - -int main() -{ - size_t n; - cin>>n; - - vector arr(n); - - for(size_t i = 0; i < n; i++) - cin>>arr[i]; - - bubble_sort(arr); - - for(size_t i = 0; i < n; i++) - cout< -#include -using namespace std; - -void insertion_sort(vector & vec) -{ - const double EPS = 1e-9; - - for(size_t i = 0; i < vec.size(); i++) - { - for(size_t j = i + 1; j < vec.size(); j++) - { - if(vec[i] - vec[j] > EPS) - swap(vec[i], vec[j]); - } - } -} - -void bucket_sort(vector & vec) -{ - size_t n = vec.size(); - - vector bucket[n]; - - for(size_t i = 0; i < n; i++) - { - bucket[((size_t) vec[i] * n)].push_back(vec[i]); - } - - for(size_t i = 0; i < n; i++) - { - insertion_sort(bucket[i]); - } - - size_t next = 0; - - for(size_t i = 0; i < n; i++) - { - for(size_t j = 0; j < bucket[i].size(); j++) - { - vec[next++] = bucket[i][j]; - } - } -} - -int main(int argc, char ** argv) -{ - bool print_input = true; - - if(argc > 1 and argv[1][1] == 'v') - print_input = false; - - size_t n; - - if(print_input) - cout<<"Array size: "; - - cin>>n; - - vector arr(n); - - if(print_input) - cout<<"Array elements: "; - - for(size_t i = 0; i < n; i++) - cin>>arr[i]; - - bucket_sort(arr); - - if(print_input) - cout<<"Sorted array: "; - - for(size_t i = 0; i < n; i++) - cout< -#include -using namespace std; - -void insertion_sort(vector & vec) -{ - const double EPS = 1e-9; - - for(size_t i = 0; i < vec.size(); i++) - { - for(size_t j = i + 1; j < vec.size(); j++) - { - if(vec[i] - vec[j] > EPS) - swap(vec[i], vec[j]); - } - } -} - -void bucket_sort(vector & vec) -{ - size_t n = vec.size(); - - vector bucket[n]; - - for(size_t i = 0; i < n; i++) - { - bucket[((size_t) vec[i] * n)].push_back(vec[i]); - } - - for(size_t i = 0; i < n; i++) - { - insertion_sort(bucket[i]); - } - - size_t next = 0; - - for(size_t i = 0; i < n; i++) - { - for(size_t j = 0; j < bucket[i].size(); j++) - { - vec[next++] = bucket[i][j]; - } - } -} - -int main(int argc, char ** argv) -{ - bool print_input = true; - - if(argc > 1 and argv[1][1] == 'v') - print_input = false; - - size_t n; - - if(print_input) - cout<<"Array size: "; - - cin>>n; - - vector arr(n); - - if(print_input) - cout<<"Array elements: "; - - for(size_t i = 0; i < n; i++) - cin>>arr[i]; - - bucket_sort(arr); - - if(print_input) - cout<<"Sorted array: "; - - for(size_t i = 0; i < n; i++) - cout< -#include -using namespace std; - -template -bool greater_c(const T a,const T b) -{ - return a > b; -} - -template -bool lower_c(const T a, const T b) -{ - return a < b; -} - -size_t left(const size_t node) -{ - return node << 1; -} - -size_t right(const size_t node) -{ - return 1 + (node << 1); -} - -size_t parent(const size_t node) -{ - return node >> 1; -} - -template -void max_heapify(vector & heap, size_t position, size_t heap_size, bool comparator(T, T)) -{ - size_t l = left(position); - size_t r = right(position); - size_t high_priority; - - if(l < heap_size and comparator(heap[position], heap[l])) - { - high_priority = l; - } - else - { - high_priority = position; - } - - if(r < heap_size and comparator(heap[high_priority], heap[r])) - { - high_priority = r; - } - - if(high_priority != position) - { - swap(heap[position], heap[high_priority]); - max_heapify(heap, high_priority, heap_size, comparator); - } -} - -template -void build_heap(vector & heap, bool comparator(T, T)) -{ - - for(size_t i = heap.size() - 1; ; i--) - { - - max_heapify(heap, i, heap.size(), comparator); - if(i == 0) - break; - - } - -} - -template -void heap_sort(vector & heap, bool comparator(T, T) = lower_c) -{ - build_heap(heap, comparator); - - size_t heap_size = heap.size(); - - for(size_t i = heap.size() - 1; i >= 1; i--) - { - - swap(heap[0], heap[i]); - heap_size--; - max_heapify(heap, 0, heap_size, comparator); - - } - -} - -int main(int argc, char ** argv){ - - bool print_input = true; - - if(argc > 1 and argv[1][1] == 'v') - print_input = false; - - size_t n; - - if(print_input) - cout<<"Array size: "; - - cin>>n; - - vector arr(n); - - if(print_input) - cout<<"Array elements: "; - - for(size_t i = 0; i < n; i++) - cin>>arr[i]; - - heap_sort(arr); - - if(print_input) - cout<<"Sorted array: "; - - for(size_t i = 0; i < n; i++) - cout< -#include -using namespace std; - -template -void insertion_sort(vector & vec) -{ - for(size_t i = 0; i < vec.size() - 1; i++) - { - for(size_t j = i + 1; j < vec.size(); j++) - { - if(vec[i] > vec[j]) - swap(vec[i], vec[j]); - } - } -} - -int main() -{ - size_t n; - cin>>n; - - vector arr(n); - - for(size_t i = 0; i < n; i++) - cin>>arr[i]; - - insertion_sort(arr); - - for(size_t i = 0; i < n; i++) - cout< // input and output -#include // vector -using namespace std; - -template -void merge_sort(vector & vec, size_t l = 0, size_t r = 0) -{ - - static bool started = false; - - if(!started) - { - l = 0; - r = vec.size() - 1; - started = true; - } - - if(l == r) - return; - - size_t mid = (l + r) / 2; - - merge_sort(vec, l, mid); - merge_sort(vec, mid + 1, r); - - size_t pl = l; - size_t pr = mid + 1; - size_t next = 0; - - vector sorted(r - l + 1); - - while(pl <= mid or pr <= r) - { - - if(pl <= mid and pr <= r) - { - - if(vec[pr] < vec[pl]) - sorted[next] = vec[pr++]; - else - sorted[next] = vec[pl++]; - - } - else if(pl <= mid) - { - sorted[next] = vec[pl++]; - } - else - { - sorted[next] = vec[pr++]; - } - - next++; - - } - - for(size_t i = l; i <= r; i++) - vec[i] = sorted[i - l]; - - if(l == 0 and r == vec.size() - 1) - started = false; - -} - -int main(int argc, char ** argv) -{ - - bool print_input = true; - - if(argc > 1 and argv[1][1] == 'v') - print_input = false; - - size_t n; - if(print_input) - cout<<"Array size: "; - - cin>>n; - - vector vec(n); - - if(print_input) - cout<<"Array elements: "; - - for(size_t i = 0; i < n; i++) - cin>>vec[i]; - - merge_sort(vec); - - if(print_input) - cout<<"Sorted array: "; - - for(size_t i = 0; i < n; i++) - - cout< -#include -using namespace std; - -template -size_t partition(vector & vec, size_t p, size_t r) -{ - T x = vec[r]; - size_t i = p - 1; - for(size_t j = p; j < r; j++) - { - if(vec[j] <= x) - { - i++; - swap(vec[i], vec[j]); - } - } - swap(vec[i + 1], vec[r]); - return i + 1; -} - -template -void quick_sort(vector & vec, size_t p = 0, size_t r = 0) -{ - static bool started = false; - - if(!started) - { - started = true; - p = 1; - r = vec.size() - 1; - } - - if(p < r) - { - size_t q = partition(vec, p, r); - quick_sort(vec, p, q - 1); - quick_sort(vec, q + 1, r); - } - - if(p == 0 and r == vec.size() - 1) - started = false; -} - -int main(int argc, char ** argv) -{ - bool print_input = true; - - if(argc > 1 and argv[1][1] == 'v') - print_input = false; - - if(print_input) - cout<<"Array size: "; - - size_t n; - cin>>n; - - vector arr(n + 1); - arr[0] = 0; - - if(print_input) - cout<<"Array elements: "; - - for(size_t i = 1; i <= n; i++) - cin>>arr[i]; - - quick_sort(arr); - - if(print_input) - cout<<"Sorted array: "; - - for(size_t i = 1; i <= n; i++) - cout< -#include -using namespace std; - -void counting_sort(vector & origin, size_t digit, size_t exponent) -{ - vector temp(exponent + 1); - vector target(origin.size()); - - for(size_t i = 0; i < exponent; i++) - temp[i] = 0; - - for(size_t j = 0; j < origin.size(); j++) - { - temp[(origin[j]/digit) % exponent]++; - } - cout< & vec, size_t max_input, const int exponent = 10) -{ - size_t highest_base = 0; - while(max_input > 0) - { - highest_base++; - max_input /= exponent; - } - - size_t divisor = 1; - - for(size_t i = 0; i < highest_base; i++) - { - counting_sort(vec, divisor, exponent); - divisor *= exponent; - } -} - -int main(int argc, char ** argv) -{ - bool print_input = true; - - if(argc > 1 and argv[1][1] == 'v') - print_input = false; - - size_t n; - - if(print_input) - cout<<"Array size: "; - - cin>>n; - - vector arr(n); - - if(print_input) - cout<<"Array elements: "; - - size_t max_input = 0; - - for(size_t i = 0; i < n; i++) - { - cin>>arr[i]; - max_input = max(arr[i], max_input); - } - - radix_sort(arr, max_input); - - if(print_input) - cout<<"Sorted array: "; - - for(size_t i = 0; i < n; i++) - cout< -#include -#include -#include -using namespace std; - -size_t random_number(const size_t l, const size_t r){ - - static queue nums; - - if(nums.empty()) - { - random_device rd; - mt19937 mt(rd()); - uniform_int_distribution random_index((int)0, (int)1000000); - for(size_t i = 0; i < 10000; i++) - nums.push(random_index(mt)); - } - - size_t value = nums.front(); - nums.pop(); - return value % (r - l + 1) + l; -} - -template -size_t partition(vector & vec, size_t p, size_t r) -{ - size_t exc = random_number(p, r); - swap(vec[exc], vec[r]); - T x = vec[r]; - size_t i = p - 1; - for(size_t j = p; j < r; j++) - { - if(vec[j] <= x) - { - i++; - swap(vec[i], vec[j]); - } - } - swap(vec[i + 1], vec[r]); - return i + 1; -} - -template -void quick_sort(vector & vec, size_t p = 0, size_t r = 0) -{ - static bool started = false; - - if(!started) - { - started = true; - p = 1; - r = vec.size() - 1; - } - - if(p < r) - { - size_t q = partition(vec, p, r); - quick_sort(vec, p, q - 1); - quick_sort(vec, q + 1, r); - } - - if(p == 0 and r == vec.size() - 1) - started = false; -} - -int main(int argc, char ** argv) -{ - bool print_input = true; - - if(argc > 1 and argv[1][1] == 'v') - print_input = false; - - if(print_input) - cout<<"Array size: "; - - size_t n; - cin>>n; - - vector arr(n + 1); - arr[0] = 0; - - if(print_input) - cout<<"Array elements: "; - - for(size_t i = 1; i <= n; i++) - cin>>arr[i]; - - quick_sort(arr); - - if(print_input) - cout<<"Sorted array: "; - - for(size_t i = 1; i <= n; i++) - cout< -#include -using namespace std; - -template -void Shellsort(vector & vec) -{ - size_t j; - T temp; - - for(size_t aux = vec.size() / 2; aux > 0; aux /= 2) - { - for(size_t i = aux; i < vec.size(); ++i) - { - temp = vec[i]; - - for(j = i; j >= aux and vec[j - aux] > temp; j -= aux) - vec[j] = vec[j - aux]; - - vec[j]=temp; - } - } -} - -int main() -{ - int array_size; - - cout<<"Array size: "; - cin>>array_size; - - vector vec(array_size); - - cout<<"Array elements: "; - - for(int i = 0; i < array_size; i++) - cin>>vec[i]; - - Shellsort(vec); - - cout<<"Sorted array: "; - for(int i = 0; i < array_size; i++) - cout< -using namespace std; -const int RUN = 32; -void insertionSort(int arr[], int left, int right) -{ - for (int i = left + 1; i <= right; i++) - { - int temp = arr[i]; - int j = i - 1; - while (arr[j] > temp && j >= left) - { - arr[j+1] = arr[j]; - j--; - } - arr[j+1] = temp; - } -} -void merge(int arr[], int l, int m, int r) -{ - int len1 = m - l + 1, len2 = r - m; - int left[len1], right[len2]; - for (int i = 0; i < len1; i++) - left[i] = arr[l + i]; - for (int i = 0; i < len2; i++) - right[i] = arr[m + 1 + i]; - - int i = 0; - int j = 0; - while (i < len1 && j < len2) - { - if (left[i] <= right[j]) - { - arr[k] = left[i]; - i++; - } - else - { - arr[k] = right[j]; - j++; - } - k++; - } - - // copy remaining elements of left, if any - while (i < len1) - { - arr[k] = left[i]; - k++; - i++; - } - - // copy remaining element of right, if any - while (j < len2) - { - arr[k] = right[j]; - k++; - j++; - } -} - -// iterative Timsort function to sort the -// array[0...n-1] (similar to merge sort) -void timSort(int arr[], int n) -{ - // Sort individual subarrays of size RUN - for (int i = 0; i < n; i+=RUN) - insertionSort(arr, i, min((i+31), (n-1))); - - // start merging from size RUN (or 32). It will merge - // to form size 64, then 128, 256 and so on .... - for (int size = RUN; size < n; size = 2*size) - { - // pick starting point of left sub array. We - // are going to merge arr[left..left+size-1] - // and arr[left+size, left+2*size-1] - // After every merge, we increase left by 2*size - for (int left = 0; left < n; left += 2*size) - { - // find ending point of left sub array - // mid+1 is starting point of right sub array - int mid = left + size - 1; - int right = min((left + 2*size - 1), (n-1)); - - // merge sub array arr[left.....mid] & - // arr[mid+1....right] - merge(arr, left, mid, right); - } - } -} - -// utility function to print the Array -void printArray(int arr[], int n) -{ - for (int i = 0; i < n; i++) - printf("%d ", arr[i]); - printf("\n"); -} - -// Driver program to test above function -int main() -{ - int arr[] = {5, 21, 7, 23, 19}; - int n = sizeof(arr)/sizeof(arr[0]); - printf("Given Array is\n"); - printArray(arr, n); - - timSort(arr, n); - - printf("After Sorting Array is\n"); - printArray(arr, n); - return 0; -} diff --git a/Sorting/Insertion_Sort.c b/Sorting/Insertion_Sort.c deleted file mode 100644 index 43f5e1b3..00000000 --- a/Sorting/Insertion_Sort.c +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include - -void insertionSort(int arr[], int n) -{ - int i, key, j; - for (i = 1; i < n; i++) { - key = arr[i]; - j = i - 1; - while (j >= 0 && arr[j] > key) { - arr[j + 1] = arr[j]; - j = j - 1; - } - arr[j + 1] = key; - } -} - -void printArray(int arr[], int n) -{ - int i; - for (i = 0; i < n; i++) - printf("%d ", arr[i]); - printf("\n"); -} - -int main() -{ - int arr[] = { 12, 11, 13, 5, 6 }; - int n = sizeof(arr) / sizeof(arr[0]); - - insertionSort(arr, n); - printArray(arr, n); - - return 0; -} \ No newline at end of file diff --git a/Sorting/Kotlin/KotlinBubleSort b/Sorting/Kotlin/KotlinBubleSort deleted file mode 100644 index ecf22c17..00000000 --- a/Sorting/Kotlin/KotlinBubleSort +++ /dev/null @@ -1,20 +0,0 @@ -fun sort(arr: Array): Array { - for (current in 0 until arr.size) { - for (next in (current + 1) until arr.size) { - if (arr[current] > arr[next]) { - val temp = arr[current] - arr[current] = arr[next] - arr[next] = temp - } - } - } - - return arr -} - -fun main() { - val testCase = arrayOf(5, 3, 4, 2, 1, 10, 9) - sort(testCase).forEach { - print("$it ") - } -} \ No newline at end of file diff --git a/Sorting/Merge_sort.c b/Sorting/Merge_sort.c deleted file mode 100644 index cc33dfd7..00000000 --- a/Sorting/Merge_sort.c +++ /dev/null @@ -1,96 +0,0 @@ -#include -#include - -#define SIZE 8 - -void merge(int a[], int tmp[], int left, int mid, int right); -void msort(int a[], int tmp[], int left, int right); -void merge_sort(int a[], int tmp[], const int size); -void display(int a[],const int size); - -int main() -{ - int a[SIZE] = {5,6,3,1,7,8,2,4}; - int tmp[SIZE]; - - printf("--- C Merge Sort Demonstration --- \n"); - - printf("Array before sorting:\n"); - display(a,SIZE); - - merge_sort(a, tmp, SIZE); - - printf("Array after sorting:\n"); - display(a,SIZE); - - return 0; -} - -void merge_sort(int a[], int tmp[], const int size) -{ - msort(a, tmp, 0, size - 1); -} - -void msort(int a[], int tmp[], int left, int right) -{ - int mid; - if (right > left) - { - mid = (right + left) / 2; - msort(a, tmp, left, mid); - msort(a, tmp, mid + 1, right); - merge(a, tmp, left, mid + 1, right); - } -} - -void merge(int a[], int tmp[], int left, int mid, int right) -{ - int i, left_end, count, tmp_pos; - left_end = mid - 1; - tmp_pos = left; - count = right - left + 1; - - while ((left <= left_end) && (mid <= right)) - { - if (a[left] <= a[mid]) - { - tmp[tmp_pos] = a[left]; - tmp_pos = tmp_pos + 1; - left = left +1; - } - else - { - tmp[tmp_pos] = a[mid]; - tmp_pos = tmp_pos + 1; - mid = mid + 1; - } - } - - while (left <= left_end) - { - tmp[tmp_pos] = a[left]; - left = left + 1; - tmp_pos = tmp_pos + 1; - } - while (mid <= right) - { - tmp[tmp_pos] = a[mid]; - mid = mid + 1; - tmp_pos = tmp_pos + 1; - } - - for (i = 0; i <= count; i++) - { - a[right] = tmp[right]; - right = right - 1; - } -} - -void display(int a[],const int size) -{ - int i; - for(i = 0; i < size; i++) - printf("%d ",a[i]); - - printf("\n"); -} diff --git a/Sorting/Python/Bubble_Sort.py b/Sorting/Python/Bubble_Sort.py deleted file mode 100644 index 5ac00832..00000000 --- a/Sorting/Python/Bubble_Sort.py +++ /dev/null @@ -1,31 +0,0 @@ -# Bubble Sort -import random - - -def populate(arr, items): - for i in range(items): - arr.append(random.randint(1,items*100)) - return arr - - -def bubbleSort(arr): - for i in range(len(arr)): - swapped = False - for j in range(len(arr) - 1): - if (arr[j] > arr[j + 1]): - temp = arr[j] - arr[j] = arr[j + 1] - arr[j + 1] = temp - swapped = True - if swapped == False: - return arr - - -if __name__ == '__main__': - arr = [] - items = 10 - populate(arr, items) - print('Unsorted: ', arr) - bubbleSort(arr) - print('Sorted: ', arr) - diff --git a/Sorting/Python/Selection_sort.py b/Sorting/Python/Selection_sort.py deleted file mode 100644 index b7c6caf3..00000000 --- a/Sorting/Python/Selection_sort.py +++ /dev/null @@ -1,18 +0,0 @@ -#coding: utf-8; - -def selection_sort(array, indice): - if len(array) - 1 <= 0: - return -1; - - minIndice = indice; - - for i in xrange(indice + 1 , len(array)): - if array[i] < array[minIndice]: - minIndice = i; - - aux = array[indice]; - array[indice] = array[minIndice]; - array[minIndice] = aux; - - selection_sort(array, indice + 1); - return array; diff --git a/Sorting/Python/insertion_sort.py b/Sorting/Python/insertion_sort.py deleted file mode 100644 index 4eb13af8..00000000 --- a/Sorting/Python/insertion_sort.py +++ /dev/null @@ -1,12 +0,0 @@ -#coding: utf-8; - -def insertion_sort(array): - for i in range(1, len(array)): - key = array[i]; - aux = i - 1; - while aux >= 0 and array[aux] > key: - array[aux + 1] = array[aux]; - aux -= 1; - array[aux + 1] = key; - - return array; diff --git a/Sorting/Python/merge_sort.py b/Sorting/Python/merge_sort.py deleted file mode 100644 index be5e5a76..00000000 --- a/Sorting/Python/merge_sort.py +++ /dev/null @@ -1,36 +0,0 @@ -#coding: utf-8 - -def merge_sort(array): - - if len(array) < 2: - return; - - mid = len(array) / 2; - - left = array[:mid]; - right = array[mid:]; - - leftI = 0; - rightI = 0; - arrayI = 0; - - while leftI < len(left) and rightI < len(right): - if left[leftI] < right[rightI]: - array[arrayI] = left[leftI]; - leftI += 1; - else: - array[arrayI] = right[rightI]; - rightI += 1; - - arrayI += 1; - - while leftI < len(left): - array[arrayI] = left[leftI]; - leftI += 1; - arrayI += 1; - - while rightI < len(right): - array[arrayI] = right[rightI]; - rightI += 1; - arrayI += 1; - diff --git a/Sorting/Python/quick_sort.py b/Sorting/Python/quick_sort.py deleted file mode 100644 index d9e65d38..00000000 --- a/Sorting/Python/quick_sort.py +++ /dev/null @@ -1,30 +0,0 @@ -#coding: utf-8; - -def quick(array, init, end): - pivot = array[init]; - - while True: - while array[init] < pivot: - init += 1; - - while array[end] > pivot: - end -= 1; - - if init >= end: - return end; - - swap(array, init, end); - - init += 1; - end -= 1; - -def swap(array, i, j): - array[i], array[j] = array[j], array[i]; - - -def quick_sort(array, init, end): - if init < end: - partition = quick(array, init, end); - quick_sort(array, init, partition); - quick_sort(array, partition + 1, end); - diff --git a/Sorting/Python/selection_sort.py b/Sorting/Python/selection_sort.py deleted file mode 100644 index b7c6caf3..00000000 --- a/Sorting/Python/selection_sort.py +++ /dev/null @@ -1,18 +0,0 @@ -#coding: utf-8; - -def selection_sort(array, indice): - if len(array) - 1 <= 0: - return -1; - - minIndice = indice; - - for i in xrange(indice + 1 , len(array)): - if array[i] < array[minIndice]: - minIndice = i; - - aux = array[indice]; - array[indice] = array[minIndice]; - array[minIndice] = aux; - - selection_sort(array, indice + 1); - return array; diff --git a/Sorting/Quick_sort.cpp b/Sorting/Quick_sort.cpp deleted file mode 100644 index 4b7b3da3..00000000 --- a/Sorting/Quick_sort.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -using namespace std; - -int partition(int arr[],int l,int h) -{ - int pivot=arr[h]; - int i=l-1; - for(int j=l;j>n; - int arr[n+1]; - cout<<"Enter the elements of array\n"; - for(int i=0;i>arr[i]; - } - quick_sort(arr,0,n); - display(arr,n); - return 0; -} diff --git a/Sorting/Radix/radix.c b/Sorting/Radix/radix.c deleted file mode 100644 index d3ccdde0..00000000 --- a/Sorting/Radix/radix.c +++ /dev/null @@ -1,43 +0,0 @@ -#include "radix.h" -#include - -int getMax(int *array, int n){ - int max = array[0]; - for(int i = 1; i < n; i++) - if(array[i] > max) - max = array[i]; - return max -} - -void countSort (int *array, int n, int exp){ - int output[n]; - int i; - int count[10] = {0}; - - // Store count of occurrences in count[] - for (i = 0; i < n; i++) - count[(arr[i] / exp) % 10]++; - - // Change count[i] so that count[i] now contains actual - // position of this digit in output[] - for (i = 1; i < 10; i++) - count[i] += count[i - 1]; - - // Build the output array - for (i = n - 1; i >= 0; i--){ - output[count[(arr[i] / exp) % 10] - 1] = arr[i]; - count[(arr[i] / exp) % 10] --; - } - - // Copy the output array to arr[], so that arr[] now - // contains sorted numbers according to current digit - for (i = 0; i < n; i++) - arr[i] = output[i]; -} - -void radixSort (int *array, int n){ - int max = getMax(array, n); - - for (int exp = 1; m/exp > 0; exp *= 10) - countSort(arr, n, exp); -} diff --git a/Sorting/Radix/radix.h b/Sorting/Radix/radix.h deleted file mode 100644 index 519f3c58..00000000 --- a/Sorting/Radix/radix.h +++ /dev/null @@ -1,8 +0,0 @@ -/* This is a library module supporting Radix Sort for standard ints */ - -/* - *array - pointer to array - n - size of array *array -*/ -void radixSort(int *array, int n); - diff --git a/Sorting/Radix_Sort.cpp b/Sorting/Radix_Sort.cpp deleted file mode 100644 index 3a446729..00000000 --- a/Sorting/Radix_Sort.cpp +++ /dev/null @@ -1,86 +0,0 @@ -//Radix Sort Program in C++ -//Time complexity is O(N*k) where N = num_elements and k = number of digits in maximum number present in input. -//Space complexity if O(N) -/*-------------Radix Sort-------------*/ - -#include -using namespace std; - -int getMaxNum(vector &num_arr) -{ - int max=num_arr[0]; - int size = num_arr.size(); - for(int i=1;imax) - max = num_arr[i]; - } - return max; -} - -void countSort(vector &num_arr, int exp) // Function to sort numbers according to the digit exp -{ - vector result(num_arr.size()); - vector freq(10,0); //Array to count frequency of digits. //Initialised from 0. - - int size = num_arr.size(); //Size of input array. - - for(int i=0;i=0;i--) //Generating the output result array according to the current digit considered. - { - result[freq[ (num_arr[i]/exp)%10 ] - 1] = num_arr[i]; - freq[ (num_arr[i]/exp)%10 ]--; //Decreasing the frequency of the digit once used. - } - - for (int i = 0; i < size; i++) - num_arr[i] = result[i]; - - return; -} - -void radixSort(vector &num_arr) -{ - int max_num; - max_num = getMaxNum(num_arr); //Getting the maximum number out of vector. - - int exp; - for(exp=1;max_num/exp>0;exp*=10) - { - countSort(num_arr,exp); - } - -} - -int main(void) -{ - int num_elements; - cout<<"Enter number of elements: "; - cin>>num_elements; - vector num_arr(num_elements); //Declaring vector of elements. - - for(int i=0;i> num_arr[i]; //Getting the input numbers. - } - - radixSort(num_arr); - - cout<<"The sorted array is: "; - - for(int i=0;i array[i+1] - array[i], array[i+1] = array[i+1], array[i] - swapped = true - end - end - - break if not swapped - end - - array -end - -arr = Array.new - -(0..5).each do |i| - arr.push(rand 100) -end - -p bubble_sort(arr) \ No newline at end of file diff --git a/Sorting/Selection_sort.c b/Sorting/Selection_sort.c deleted file mode 100644 index 89852a5f..00000000 --- a/Sorting/Selection_sort.c +++ /dev/null @@ -1,47 +0,0 @@ -#include - -void swap(int *xp, int *yp) -{ - int temp = *xp; - *xp = *yp; - *yp = temp; -} - -void selectionSort(int arr[], int n) -{ - int i, j, min_idx; - - // One by one move boundary of unsorted subarray - for (i = 0; i < n-1; i++) - { - // Find the minimum element in unsorted array - min_idx = i; - for (j = i+1; j < n; j++) - if (arr[j] < arr[min_idx]) - min_idx = j; - - // Swap the found minimum element with the first element - swap(&arr[min_idx], &arr[i]); - } -} - -/* Function to print an array */ -void printArray(int arr[], int size) -{ - int i; - for (i=0; i < size; i++) - printf("%d ", arr[i]); - printf("\n"); -} - -// Driver program to test above functions -int main() -{ - int arr[] = {64, 25, 12, 22, 11}; - int n = sizeof(arr)/sizeof(arr[0]); - selectionSort(arr, n); - printf("Sorted array: \n"); - printArray(arr, n); - return 0; -} - diff --git a/Sorting/heap-sort.c b/Sorting/heap-sort.c deleted file mode 100644 index e8b22573..00000000 --- a/Sorting/heap-sort.c +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include -int arr[100]; -void heapify(int a) -{ - int b=log(a)/log(2); - printf("a is %d\n",a); - printf("b is %d\n",b); - int i,par,ch,temp,ch1,ch2,mxc; - for(i=pow(2,b)-2;i>-1;i--) - { - par=i; - printf("parent is %d\n",i); - while(par<=a-1) - { - ch1=2*par+1; - ch2=2*par+2; - mxc=ch1; - if(ch1>=a) - arr[ch1]=-999; - if(ch2>=a) - arr[ch2]=-999; - - if(arr[ch2]>arr[mxc]) - mxc=ch2; - - if(arr[mxc] - -void print_array(int array[],int left,int right) -{ - int i; - for (i=left; i <= right; i++) - printf("%d\t", array[i]); - printf("\n"); -} - -void merge(int array[], int left, int mid, int right) -{ - int i, j, k; - int n1 = mid - left + 1; - int n2 = right - mid; - - int Left[n1], Right[n2]; - - /* Copy data to temp arrays L[] and R[] */ - for (i = 0; i < n1; i++) - Left[i] = array[left + i]; - for (j = 0; j < n2; j++) - Right[j] = array[mid + 1+ j]; - - i = 0; // Initial index of first subarray - j = 0; // Initial index of second subarray - k = left; // Initial index of merged subarray - while (i < n1 && j < n2) - { - if (Left[i] <= Right[j]) - array[k++] = Left[i++]; - else - array[k++] = Right[j++]; - } - - /* Copy the remaining elements of L[], if there are any */ - while (i < n1) - array[k++] = Left[i++]; - - /* Copy the remaining elements of R[], if there are any */ - while (j < n2) - array[k++] = Right[j++]; -} - -void mergeSort(int array[], int left, int right) -{ - int i; - if (left < right) - { - int mid = left + (right - left) / 2; - printf("middle element is %d at index %d: \n",array[mid],mid); - // Sort left and right halves - printf("Left half : \n"); - print_array(array,left,mid); - mergeSort(array, left, mid); - printf("Right half : \n"); - print_array(array,mid+1,right); - mergeSort(array, mid+1, right); - - merge(array, left, mid, right); - printf("After merging sorted sub-array : \n"); - print_array(array,left,right); - } -} - - -int main() -{ - int array[] = {24, 17, 13, 22, 19, 21, 16, 12}; - int array_size = sizeof(array)/sizeof(array[0]); - int i; - - printf("Given array is :\n"); - print_array(array,0,array_size-1); - mergeSort(array, 0, array_size - 1); - - printf("\nSorted array is :\n"); - print_array(array,0,array_size-1); - return 0; -} diff --git a/Sorting/pancakesort.py b/Sorting/pancakesort.py deleted file mode 100644 index d2da23d9..00000000 --- a/Sorting/pancakesort.py +++ /dev/null @@ -1,68 +0,0 @@ -# Python3 program to -# sort array using -# pancake sort - -# Reverses arr[0..i] */ -def flip(arr, i): - start = 0 - while start < i: - temp = arr[start] - arr[start] = arr[i] - arr[i] = temp - start += 1 - i -= 1 - -# Returns index of the maximum -# element in arr[0..n-1] */ -def findMax(arr, n): - mi = 0 - for i in range(0,n): - if arr[i] > arr[mi]: - mi = i - return mi - -# The main function that -# sorts given array -# using flip operations -def pancakeSort(arr, n): - - # Start from the complete - # array and one by one - # reduce current size - # by one - curr_size = n - while curr_size > 1: - # Find index of the maximum - # element in - # arr[0..curr_size-1] - mi = findMax(arr, curr_size) - - # Move the maximum element - # to end of current array - # if it's not already at - # the end - if mi != curr_size-1: - # To move at the end, - # first move maximum - # number to beginning - flip(arr, mi) - - # Now move the maximum - # number to end by - # reversing current array - flip(arr, curr_size-1) - curr_size -= 1 - -# A utility function to -# print an array of size n -def printArray(arr, n): - for i in range(0,n): - print ("%d"%( arr[i]),end=" ") - -# Driver program -arr = [23, 10, 20, 11, 12, 6, 7] -n = len(arr) -pancakeSort(arr, n); -print ("Sorted Array ") -printArray(arr,n) - diff --git a/Sorting/quick_sort.c b/Sorting/quick_sort.c deleted file mode 100644 index 6d8a2920..00000000 --- a/Sorting/quick_sort.c +++ /dev/null @@ -1,63 +0,0 @@ -#include - -void printArray(int array[], int size) -{ - int i; - for (i=0; i < size; i++) - printf("%d\t", array[i]); - printf("\n"); -} - -void swap(int* a, int* b) -{ - int t = *a; - *a = *b; - *b = t; -} - -int partition (int array[], int low, int high) -{ - int pivot = array[high]; - int i = (low - 1); - int j; - - for (j = low; j <= high- 1; j++) - { - if (array[j] <= pivot) - { - i++; - swap(&array[i], &array[j]); - } - } - swap(&array[i + 1], &array[high]); - return (i + 1); -} - - -void quickSort(int array[], int low, int high) -{ - if (low < high) - { - int pivot_index; - pivot_index = partition(array, low, high); - printf("Pivot element is %d \n",array[pivot_index]); - printf("Array after pivot partitioning : \n"); - printArray(array,8); - - - quickSort(array, low, pivot_index - 1); - quickSort(array, pivot_index + 1, high); - } -} -int main() -{ - int array[] = {17, 13, 20, 16, 19, 24, 22, 21}; - int n = sizeof(array)/sizeof(array[0]); - printf("Given array: \n"); - printArray(array, n); - quickSort(array, 0, n-1); - printf("Sorted array: \n"); - printArray(array, n); - return 0; -} - diff --git a/Sorting/quick_sort.py b/Sorting/quick_sort.py deleted file mode 100644 index 44a163bb..00000000 --- a/Sorting/quick_sort.py +++ /dev/null @@ -1,83 +0,0 @@ -class QuickSorter(object): - - def __init__(self,array): - """ - Initialize QuickSorter with an array of numbers that should be sorted - """ - self.list = [] - self.replace_list(array) - - def replace_list(self, array): - if isinstance(array, list): - for i in array: - if not (isinstance(i,int) or isinstance(i, float)): - raise ValueError("all elements must be type number") - else: - raise AttributeError("search array must be list type") - - self.sorted = False - self.list = array - - - def __quicksort(self, low, high): - size = high - low + 1 - stack = [0] * (size) - - top = -1 - - top = top + 1 - stack[top] = low - top = top + 1 - stack[top] = high - - while top >= 0: - - high = stack[top] - top = top - 1 - low = stack[top] - top = top - 1 - - partition = self.__partition( low, high ) - - if partition-1 > low: - top = top + 1 - stack[top] = low - top = top + 1 - stack[top] = partition - 1 - - if partition+1 < high: - top = top + 1 - stack[top] = partition + 1 - top = top + 1 - stack[top] = high - - def __partition(self,low,high): - i = ( low - 1 ) - x = self.list[high] - - for j in range(low , high): - if self.list[j] <= x: - i = i+1 - self.list[i],self.list[j] = self.list[j],self.list[i] - - self.list[i+1],self.list[high] = self.list[high],self.list[i+1] - return (i+1) - - - def sort(self): - """ - Executing Hoare partition scheme quick sort algorithm, iterativly - """ - self.__quicksort( 0, len(self.list) - 1 ) - self.sorted = True - - - - def result(self): - """ - Return the sorted list, and sort before if not already sorted - """ - if not self.sorted: - self.sort() - - return self.list diff --git a/Sorting/quicksort.hs.txt b/Sorting/quicksort.hs.txt deleted file mode 100644 index c4e4abbe..00000000 --- a/Sorting/quicksort.hs.txt +++ /dev/null @@ -1,18 +0,0 @@ -import Data.List --- simple code of quicksort in haskell --- its easy - - -quicksort :: [Int] -> (Int -> Int -> Bool) -> [Int] -quicksort [] _ = [] -quicksort (x:xs) comparator = (quicksort lessthenhead (comparator)) ++ [x] ++ (quicksort biggerthenhead (comparator) ) - where - lessthenhead = filter (comparator x) xs - biggerthenhead = (xs \\ lessthenhead) - -main = do - unsortedList <- readLn :: IO [Int] - let ascending = quicksort unsortedList (<) - let descending = quicksort unsortedList (>) - print ascending - print descending \ No newline at end of file diff --git a/Sorting/zombiesort.c b/Sorting/zombiesort.c deleted file mode 100644 index 8d861ff0..00000000 --- a/Sorting/zombiesort.c +++ /dev/null @@ -1,61 +0,0 @@ -#include -int main() -{ - int size; - printf("Enter the number of creature :"); - scanf("%d",&size); - int arr[size],even[size],evenlist=0,evensum=0,odd[size],oddlist=0,oddsum=0,key,j,smallest; - printf("Enter the power and list:"); - for(int m=0;m=0 && odd[j]>key) - { - odd[j+1]=odd[j]; - j--; - odd[j+1]=key; - } - } - for(int i=1;i<=sevenlist;i++) - { - key=even[i]; - j=i-1; - while(j>=0 && even[j]>key) - { - even[j+1]=even[j]; - j--; - even[j+1]=key; - } - } - for(int k=0;k>(list:MutableList) { - - var items: MutableList = list - - - fun isEmpty():Boolean = this.items.isEmpty() - - fun count():Int = this.items.count() - - fun push(element:T) { - val position = this.count() - this.items.add(position, element) - } - - override fun toString() = this.items.toString() - - fun pop():T? { - if (this.isEmpty()) { - return null - } else { - val item = this.items.count() - 1 - return this.items.removeAt(item) - } - } - - fun peek():T? { - if (isEmpty()) { - return null - } else { - return this.items[this.items.count() - 1] - } - } - -} \ No newline at end of file diff --git a/Stack/Stack_using_arrays.cpp b/Stack/Stack_using_arrays.cpp deleted file mode 100644 index a6837cd0..00000000 --- a/Stack/Stack_using_arrays.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include - -void push(); -void pop(); -void dis(); - -int top=-1,ar[10],n=10; - -void main() -{ - int ch; - - do{ - cout<<"SELECT AN OPTION : \n\n"; - cout<<"1. ENTER AN ELEMENT\n"; - cout<<"2. DELETE AN ELEMENT\n"; - cout<<"3. DISPLAY THE ELEMENTS\n"; - cout<<"4. EXIT\n"; - cin>>ch; - - switch(ch) - { - case 1 : {push(); - break;} - case 2 : {pop(); - break;} - case 3 : {dis(); - break;} - case 4 : break; - default : {cout<<"WRONG OPTION!!\n"; - break;} - } - }while(ch!=4); -} - -void push() -{ - if(top >= n-1) - { - cout<<"Stack Overflow.\n"; - } - else - { - top++; - cout<<"Enter the element : "; - cin>>ar[top]; - } -} - -void pop() -{ - if(top <= -1) - { - cout<<"Stack Underflow.\n"; - } - else - { - cout<<"Element deleted: "<=0) - { - cout<<"Elements are :\n"; - for(int i=top;i>=0;i--) - { - cout<postfix and infix->prefix conversion).c b/Stack/stack application(infix->postfix and infix->prefix conversion).c deleted file mode 100644 index d9523299..00000000 --- a/Stack/stack application(infix->postfix and infix->prefix conversion).c +++ /dev/null @@ -1,277 +0,0 @@ -#include -#include -#include -#include -#define MAX 40 -typedef struct{ - char ele[MAX]; - int top; -}stack; -void reverse(char ex[]); -void infixtopost(char exp[],stack s); -int check(char exp[],stack s); -void infixtoprefix(char exp[],stack s); - -char pop(stack *s); -void push(stack *s,char item); -char peek(stack s); -int isEmpty(stack *s); -int isFull(stack *s); -void deletion(stack s); -void display(stack s); -int prec2(char ch); -int prec1(char ch); -void reverse(char exp[],stack s); -char post[MAX]; -int main(){ - stack s; - s.top=-1; - do{ - int ch; - char exp[MAX]; - - printf("\n\nMenue\n\n\nEnter 1 for infix to postfix\n\n\nEnter 2 for infix to prefix\n"); - printf("\n\nEnter your choice\n\n"); - scanf("%d",&ch); - switch(ch){ - case 1: - printf("\nEnter the expression to be evaluated\n"); - scanf("%s",exp); - if(check(exp,s)) - - infixtopost(exp,s); - else{ - printf("\n\nThe entered expression is invalid enter some valid expression\n\n"); - } - - break; - - case 2: - printf("\nEnter the expression to be evaluated\n"); - scanf("\n%s",exp); - if(check(exp,s)){ - - infixtoprefix(exp,s); - - } - else{ - printf("\n\nThe entered expression is invalid enter some valid expression\n\n"); - } - break; - case 3: - printf("\n\nBye!Bye you exit\n\n"); - exit(0); - - default: - printf("\n\nWrong Choice\n\n"); - - } - }while(1); -} -int check(char exp[],stack s){ - - int i,k; - k=strlen(exp); - for(i=0;exp[i]!='\0';i++) - { - - if(exp[i]=='(') - push(&s,exp[i]); - else if(exp[i]==')') - { - if(s.top==-1) - break; - else - pop(&s); - } - - } - if(s.top==-1&& i==k) - return 1; - else - return 0; - - - } - -void reverse(char ex[]){ - int i,n,end; - char temp; - n=strlen(ex); - end=n-1; - for(i=0;itop++; - s->ele[s->top]=item; - - } -} -char pop(stack *s){ - char item; - if(isEmpty(s)){ - printf("\nStack Underflow......\n"); - return -1; - - } - else{ - item=s->ele[s->top]; - s->top--; - return item; - } -} -char peek(stack s){ - char item; - if(isEmpty(&s)){ - printf("\nStack nderflow\n"); - return -1; - } - else - { - item=s.ele[s.top]; - return item; - } - } - -int isEmpty(stack *s) -{ - if(s->top==-1) - { - return 1; - } - else{ - return 0; - } -} -int isFull(stack *s) -{ - if(s->top==MAX) - { - return 1; - } - else{ - return 0; - } -} - - - - - diff --git a/Stack/stack.c b/Stack/stack.c deleted file mode 100644 index 268242a6..00000000 --- a/Stack/stack.c +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include -int top=-1; - -void push(int s[],int n,int x){ - if(top=0;i--){ -printf("\n| %4d |",s[i]); - } - printf("\n--------------\n"); -} - - -int main(){ -int n,x; -printf("enter size of stack : "); -scanf("%d",&n); - -int s[n]; -int choise; -printf("\n\n ------------------------------------------------------\n"); -printf("choise what do u whant\n"); -printf("1. push \n2. pop \n3. disply \n4. exit\n"); -printf("------------------------------------------------------\n"); - -pick : - -printf("enter your choise : "); -scanf("%d",&choise); - -switch(choise){ -case 1: - printf("enter number you want to add in stack :"); - scanf("%d",&x); - push(s,n,x); - -break; - -case 2: - printf("you poped : %d\n", pop(s,n)); -break; - -case 3: - display(s); -break; - -case 4: - exit(0); -} - -goto pick; -return 0; -} diff --git a/Stack/stack.cpp b/Stack/stack.cpp deleted file mode 100644 index e1ca9a94..00000000 --- a/Stack/stack.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include - -using namespace std; - -template -class stack { -private: - vector items; - int size; - -public: - void push(T); - T pop(); - int getSize(); - T top(); -}; - -template -void stack::push(T item) { - items.push_back(item); - size++; -} - -template -T stack::pop() { - if (size == 0) { - throw "No items to pop"; - } - else { - T temp = items[size - 1]; - items.pop_back(); - size--; - return temp; - } -} - -template -int stack::getSize() { - return size; -} - -template -T stack::top() { - return items[size - 1]; -} \ No newline at end of file diff --git a/Stack/stack.java b/Stack/stack.java deleted file mode 100644 index d3936000..00000000 --- a/Stack/stack.java +++ /dev/null @@ -1,52 +0,0 @@ -import java.util.EmptyStackException; -import java.util.Iterator; -import java.util.LinkedList; - -public class Stack implements Iterable { - - private LinkedList list = new java.util.LinkedList (); - - // Create an empty stack - public Stack() { } - - // Create a Stack with an initial element - public Stack(T firstElem) { - push(firstElem); - } - - // Return the number of elements in the stack - public int size() { - return list.size(); - } - - // Check if the stack is empty - public boolean isEmpty() { - return size() == 0; - } - - // Push an element on the stack - public void push(T elem) { - list.addLast(elem); - } - - // Pop an element off the stack - // Throws an error is the stack empty - public T pop() { - if (isEmpty()) - throw new EmptyStackException(); - return list.removeLast(); - } - - // Peek the top of the stack without removing an element - // Throws an exception if the stack is empty - public T peek() { - if (isEmpty()) - throw new EmptyStackException(); - return list.peekLast(); - } - - // Allow users to iterate through the stack using an iterator - @Override public Iterator iterator () { - return list.iterator(); - } -} diff --git a/Stack/stack.js b/Stack/stack.js deleted file mode 100644 index ae5d25d9..00000000 --- a/Stack/stack.js +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Implementation of stack with an array - */ - -class Stack { - constructor() { - this.stack = []; - } - - // Insert an element on top of the stack - push(element) { - this.stack.push(element); - } - - //Removes the element at the top of the stack and returns that same element - pop() { - if (this.isEmpty()) - return 'Stack is empty!'; - return this.stack.pop(); - } - - // Returns the element that is on top of the stack - peek() { - if (this.isEmpty()) - return 'Stack is empty'; - return this.stack[this.stack.length - 1]; - } - - // helper method - isEmpty() { - return !this.stack.length; - } -} \ No newline at end of file diff --git a/Stack/stack.py b/Stack/stack.py deleted file mode 100644 index 43c84361..00000000 --- a/Stack/stack.py +++ /dev/null @@ -1,24 +0,0 @@ -class Stack(object): - - def __init__(self,size): - self.index = -1 - self.stack = [None] * size - - def push(self, value): - if self.index != len(self.stack) -1: - self.index = self.index + 1 - self.stack[self.index] = value - - def pop(self): - if not self.empty(): - value = self.stack[self.index] - self.stack[self.index] = None - self.index = self.index - 1 - return value - - def peek(self): - if not self.empty(): - return self.stack[self.index] - - def empty(self): - return self.index == -1 \ No newline at end of file diff --git a/Stack/stack_generic.cpp b/Stack/stack_generic.cpp deleted file mode 100644 index 7e1ac7f6..00000000 --- a/Stack/stack_generic.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include -using namespace std; - -//Node class used as building block for Stack Class/Object -template -class Node{ - T data; - Node* next; - public: - Node(T num){ - data = num; - next = NULL; - } - T getData(){ - return data; - } - Node* getNext(){ - return next; - } - void setData(T num){ - data = num; - } - void setNext(Node* temp){ - next = temp; - } -}; -template -class Stack{ - Node* head; - public: - Stack(){ - head = NULL; - } - void push(T data){ - if(head == NULL){ - head = new Node(data); - return; - } - Node* temp = new Node(data); - temp->setNext(head); - head = temp; - } - void pop(){ - if(isEmpty()) - return; - Node* temp = head; - head = head->getNext(); - delete temp; - } - T top(){ - return head->getData(); - } - bool isEmpty(){ - return (head == NULL)?true:false; - } - void print(){ - Node* temp = head; - while(temp != NULL){ - cout<getData()<<" "; - temp = temp->getNext(); - } - } -}; -bool balanced(string str){ - Stack s; - for(int i=0; i>str; - (balanced(str))?cout<<"Balanced":cout<<"Not Balanced"; - cout<<"\nDo you want to try again?(y/n) "; - cin>>choice; - }while(choice != 'n'); - return 0; -} diff --git a/Tower_Of_Hanoi/Tower_Of_Hanoi.c b/Tower_Of_Hanoi/Tower_Of_Hanoi.c deleted file mode 100644 index 8b35f877..00000000 --- a/Tower_Of_Hanoi/Tower_Of_Hanoi.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -void towers(int, char, char, char); -int main() - -{ - int num; - printf("Enter the number of disks : "); - scanf("%d", &num); - towers(num, 'A', 'C', 'B'); - return 0; -} - -void towers(int num, char from, char to, char aux) - -{ if (num == 1) - { - printf("\n Move disk 1 from %c to %c", from, to); - return; - } - towers(num - 1, from, aux, to); - printf("\n Move disk %d from %c to %c", num, from, to); - towers(num - 1, aux, to, from); -} diff --git a/Tower_Of_Hanoi/Tower_Of_Hanoi_Output.txt b/Tower_Of_Hanoi/Tower_Of_Hanoi_Output.txt deleted file mode 100644 index 5f263374..00000000 --- a/Tower_Of_Hanoi/Tower_Of_Hanoi_Output.txt +++ /dev/null @@ -1,20 +0,0 @@ -Enter the number of disks : 4 - - Move disk 1 from A to B - Move disk 2 from A to C - Move disk 1 from B to C - Move disk 3 from A to B - Move disk 1 from C to A - Move disk 2 from C to B - Move disk 1 from A to B - Move disk 4 from A to C - Move disk 1 from B to C - Move disk 2 from B to A - Move disk 1 from C to A - Move disk 3 from B to C - Move disk 1 from A to B - Move disk 2 from A to C - Move disk 1 from B to C -Process returned 0 (0x0) execution time : 1.964 s -Press any key to continue. - diff --git a/Tower_Of_Hanoi/Tower_of_Hanoi.js b/Tower_Of_Hanoi/Tower_of_Hanoi.js deleted file mode 100644 index c626f1b2..00000000 --- a/Tower_Of_Hanoi/Tower_of_Hanoi.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict'; - -function stepsToSolveHanoiT(height, srcP, desP, bufferP) { - if (height >= 1) { - - // Move a tower of height-1 to the buffer peg, using the destination peg. - stepsToSolveHanoiT(height - 1, srcP, bufferP, desP); - - // Move the remaining disk to the destination peg. - console.log('Move disk from Tower ', srcP, ' to Tower ', desP); - - // Move the tower of `height-1` from the `buffer peg` to the `destination peg` using the `source peg`. - stepsToSolveHanoiT(height - 1, bufferP, desP, srcP); - } - - return; -} - -stepsToSolveHanoiT(3, "A", "C", "B"); \ No newline at end of file diff --git a/Tower_Of_Hanoi/Towers of Hanoi.cpp b/Tower_Of_Hanoi/Towers of Hanoi.cpp deleted file mode 100644 index 98c4c1b1..00000000 --- a/Tower_Of_Hanoi/Towers of Hanoi.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -using namespace std; -void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) -{ - if (n == 1) - { - cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod<>n; - towerOfHanoi(n, 'A', 'C', 'B'); - return 0; -} - - diff --git a/Tower_Of_Hanoi/towerofhanoi.py b/Tower_Of_Hanoi/towerofhanoi.py deleted file mode 100644 index 2227249e..00000000 --- a/Tower_Of_Hanoi/towerofhanoi.py +++ /dev/null @@ -1,9 +0,0 @@ -def TowerOfHanoi(n , from_rod, to_rod, aux_rod): - if n == 1: - print "Move disk 1 from rod",from_rod,"to rod",to_rod - return - TowerOfHanoi(n-1, from_rod, aux_rod, to_rod) - print "Move disk",n,"from rod",from_rod,"to rod",to_rod - TowerOfHanoi(n-1, aux_rod, to_rod, from_rod) - - diff --git a/b-tree/b-tree.py b/b-tree/b-tree.py deleted file mode 100644 index 7d739fa4..00000000 --- a/b-tree/b-tree.py +++ /dev/null @@ -1,101 +0,0 @@ -class BTreeNode(object): - """A B-Tree Node. - - attributes - ===================== - leaf : boolean, determines whether this node is a leaf. - keys : list, a list of keys internal to this node - c : list, a list of children of this node - """ - def __init__(self, leaf=False): - self.leaf = leaf - self.keys = [] - self.c = [] - - def __str__(self): - if self.leaf: - return "Leaf BTreeNode with " + len(self.keys) + "keys:\n\t"+ self.keys +"\n\tChildren:" + self.c + "\n" - else: - return "Internal BTreeNode with " + len(self.keys) + "keys:\n\t"+ self.keys +"\n\tChildren:" + self.c + "\n" - - -class BTree(object): - def __init__(self, t): - self.root = BTreeNode(leaf=True) - self.t = t - - def search(self, k, x=None): - """Search the B-Tree for the key k. - - args - ===================== - k : Key to search for - x : (optional) Node at which to begin search. Can be None, in which case the entire tree is searched. - - """ - if isinstance(x, BTreeNode): - i = 0 - while i < len(x.keys) and k > x.keys[i]: # look for index of k - i += 1 - if i < len(x.keys) and k == x.keys[i]: # found exact match - return (x, i) - elif x.leaf: # no match in keys, and is leaf ==> no match exists - return None - else: # search children - return self.search(k, x.c[i]) - else: # no node provided, search root of tree - return self.search(k, self.root) - - def insert(self, k): - r = self.root - if len(r.keys) == (2*self.t) - 1: # keys are full, so we must split - s = BTreeNode() - self.root = s - s.c.insert(0, r) # former root is now 0th child of new root s - self._split_child(s, 0) - self._insert_nonfull(s, k) - else: - self._insert_nonfull(r, k) - - def _insert_nonfull(self, x, k): - i = len(x.keys) - 1 - if x.leaf: - # insert a key - x.keys.append(0) - while i >= 0 and k < x.keys[i]: - x.keys[i+1] = x.keys[i] - i -= 1 - x.keys[i+1] = k - else: - # insert a child - while i >= 0 and k < x.keys[i]: - i -= 1 - i += 1 - if len(x.c[i].keys) == (2*self.t) - 1: - self._split_child(x, i) - if k > x.keys[i]: - i += 1 - self._insert_nonfull(x.c[i], k) - - def _split_child(self, x, i): - t = self.t - y = x.c[i] - z = BTreeNode(leaf=y.leaf) - - # slide all children of x to the right and insert z at i+1. - x.c.insert(i+1, z) - x.keys.insert(i, y.keys[t-1]) - - # keys of z are t to 2t - 1, - # y is then 0 to t-2 - z.keys = y.keys[t:(2*t - 1)] - y.keys = y.keys[0:(t-1)] - - # children of z are t to 2t els of y.c - if not y.leaf: - z.c = y.c[t:(2*t)] - y.c = y.c[0:(t-1)] - - def __str__(self): - r = self.root - return r.__str__() + '\n'.join([child.__str__() for child in r.c]) \ No newline at end of file diff --git a/bubble-sort/bubble.c b/bubble-sort/bubble.c deleted file mode 100644 index 8de68738..00000000 --- a/bubble-sort/bubble.c +++ /dev/null @@ -1,58 +0,0 @@ -#include - -long array[100005]; - -void bubbleSort(long n) // Defining 'bubble sort' function. -{ - long i, j, t; - - for (i = 0 ; i< n - 1; i++) - { - for (j = 0 ; j< n - i - 1; j++) - { - if (array[j] > array[j+1]) { - - t=array[j]; // Swapping index j and j+1 - array[j]=array[j+1]; - array[j+1]=t; - } - } - } - - return; -} - -int main() -{ - long n, i; - - printf(" *\n"); - printf(" ***\n"); - printf(" ******\n"); - printf(" **********\n"); - printf(" ***************\n"); - printf(" SORTING ALGORITHM!!\n\n"); - - printf("Enter number of elements you want to sort:\n"); - scanf("%ld", &n); printf("\n"); - - if (n>100000) { - printf("Please enter number less than 10⁵ and start again.\n"); - return 0; - } - - printf("Now please input %ld integers:\n", n); - - for (i= 0; i< n; i++) - scanf("%ld", &array[i]); - - bubbleSort(n); - - printf("\nHere is your sorted list in ascending order:\n"); - - for (i=0; i< n; i++) - printf("%ld ", array[i]); - printf("\n\n This list was sorted using Bubble Sort technique.\n\n"); - - return 0; -} \ No newline at end of file diff --git a/bubble-sort/sample1.txt b/bubble-sort/sample1.txt deleted file mode 100644 index 737f011f..00000000 --- a/bubble-sort/sample1.txt +++ /dev/null @@ -1,11 +0,0 @@ - * - *** - ****** - ********** - *************** - SORTING ALGORITHM!! - -Enter number of elements you want to sort: -1000000 - -Please enter number less than 10⁵. diff --git a/bubble-sort/sample2.txt b/bubble-sort/sample2.txt deleted file mode 100644 index 13fb1bde..00000000 --- a/bubble-sort/sample2.txt +++ /dev/null @@ -1,17 +0,0 @@ - * - *** - ****** - ********** - *************** - SORTING ALGORITHM!! - -Enter number of elements you want to sort: - -Now please input 7 integers: -823 31 -1023 7 -523 39148 231 - -Here is your sorted list in ascending order: --1023 -523 7 31 231 823 39148 - - This list was sorted using Bubble Sort technique. - diff --git a/c++/Searching/BinarySearch.cpp b/c++/Searching/BinarySearch.cpp deleted file mode 100644 index 15806c50..00000000 --- a/c++/Searching/BinarySearch.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include -typedef long long int ll; -using namespace std; - -int bin_search(int l,int h,int key,vector a,bool searchIndex){ - int mid,result = -1; - while(l<=h){ - mid = ceil((l+h)/2); - if(key==a[mid]){ - result = mid; - if(searchIndex) - h = mid-1; - else - l = mid+1; - } - else if(keya[mid]){ - l=mid+1; - } - } - return result; -} - - - -int main() -{ - vector a; - int ele,l,n,h,key,res; - cin>>n; - cin>>key; - - for (int i=0;i>ele; - a.push_back(ele); - } - l = a[0]; - h = a[n-1]; - - int firstIndex = bin_search(l,h,key,a,true); - if(firstIndex==-1){ - cout<<0< -using namespace std; - -int search(int arr[], int n, int x) -{ - int i; - for (i = 0; i < n; i++) - if (arr[i] == x) - return i; - return -1; -} - -int main(void) -{ - int arr[] = { 2, 3, 4, 10, 40 }; - int x = 10; - int n = sizeof(arr) / sizeof(arr[0]); - int result = search(arr, n, x); -(result == -1)? cout<<"Element is not present in array" - : cout<<"Element is present at index " <a) - a=-1;//not possible - else - a=getsum(b,ans,a); - System.out.print(a); - - } - public static int getsum(int b,int ans[],int a) - { - int sol=0; - while(b!=0) - { - System.out.println("we added "+ ans[b-1]); - sol+=ans[b-1]; - b=b-(b&(-b)); - }return sol; - } -} diff --git a/libqueue_in_c/Makefile b/libqueue_in_c/Makefile deleted file mode 100644 index 06f9e75f..00000000 --- a/libqueue_in_c/Makefile +++ /dev/null @@ -1,26 +0,0 @@ -# -# @file: Makefile -# - -SRC_FILE = queue.c -SRC_TEST_FILE = queue_test.c - -OBJ_FILE = queue.o -OBJ_TEST_FILE = queue_test.o -TEST_EXEC = qtest - -CC = gcc -RM = rm -rf - -CFLAGS = -c -Wall - -all: clean lib - -lib: - $(CC) $(CFLAGS) -o $(OBJ_FILE) $(SRC_FILE) -test: - $(CC) $(CFLAGS) -o $(OBJ_TEST_FILE) $(SRC_TEST_FILE) - $(CC) -o $(TEST_EXEC) $(OBJ_FILE) $(OBJ_TEST_FILE) -clean: - $(RM) $(OBJ_FILE) $(OBJ_TEST_FILE) $(TEST_EXEC) - diff --git a/libqueue_in_c/queue.c b/libqueue_in_c/queue.c deleted file mode 100644 index 3cc02ef2..00000000 --- a/libqueue_in_c/queue.c +++ /dev/null @@ -1,134 +0,0 @@ -/* - * @file: queue.c - */ - -#include -#include -#include -#include - -#include "queue.h" - -struct node { - void *data; - struct node *next; -}; - -struct Queue { - int length; - struct node *head; - struct node *tail; -}; - -/* allocates memory for node and initialize it */ -static struct node *createnode(void *data) -{ - struct node *entry; - - entry = (struct node *)malloc(sizeof(struct node)); - if(entry == NULL) { - fprintf(stderr, "failed to allocate memory\n"); - return NULL; - } - - entry->data = data; - entry->next = NULL; - - return entry; -} - -/* push data to queue tail */ -int queue_push_tail(struct Queue *queue, void *data) -{ - struct node *entry; - - if(queue == NULL) - return -1; - - entry = createnode(data); - if(entry == NULL) { - return -ENOMEM; - } - - /* For empty queue */ - if(queue->head == NULL) - queue->head = entry; - else - queue->tail->next = entry; - queue->tail = entry; - - queue->length++; - return 0; -} - -/* removes head and return its data */ -void *queue_pop_head(struct Queue *queue) -{ - void *data; - struct node *entry; - - if(queue == NULL || queue->length == 0) - return NULL; - - if(queue->head == NULL) { - return NULL; - } - - entry = queue->head; - queue->head = queue->head->next; - data = entry->data; - queue->length--; - free(entry); - - return data; -} - -int queue_get_length(struct Queue *queue) -{ - if (queue == NULL) - return 0; - - return queue->length; -} - -struct Queue *queue_new() -{ - struct Queue *queue; - - queue = (struct Queue *)malloc(sizeof(struct Queue)); - if(queue == NULL) { - fprintf(stderr, "failed to allocate memory\n"); - return NULL; - } - - queue->length = 0; - queue->head = NULL; - queue->tail = NULL; - - return queue; -} - -void queue_destroy(struct Queue *queue, QueueDataFreeFunc function) -{ - struct node *tmp; - - if(queue == NULL || queue->length == 0) - goto cleanup; - - if(queue->head == NULL) - free(queue); - else { - tmp = queue->head; - while(queue->head != NULL) { - tmp = queue->head->next; - if (function != NULL) - function(queue->head->data); - free(queue->head); - queue->head=tmp; - } - } - -cleanup: - free(queue); -} - diff --git a/libqueue_in_c/queue.h b/libqueue_in_c/queue.h deleted file mode 100644 index 67882896..00000000 --- a/libqueue_in_c/queue.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * @file: queue.h - */ - -#ifndef __QUEUE_H -#define __QUEUE_H - -struct Queue; - -typedef void (* QueueDataFreeFunc)(void *data); - -extern struct Queue *queue_new(); -extern void queue_destroy(struct Queue *queue, QueueDataFreeFunc function); - -extern int queue_push_tail(struct Queue *queue, void *data); -extern void *queue_pop_head(struct Queue *queue); -extern int queue_get_length(struct Queue *queue); - -#endif diff --git a/libqueue_in_c/queue_test.c b/libqueue_in_c/queue_test.c deleted file mode 100644 index adacf108..00000000 --- a/libqueue_in_c/queue_test.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * @file: queue_test.c - */ - -#include -#include -#include - -#include "queue.h" - -void print_len(struct Queue *q) -{ - printf("%d\n", queue_get_length(q)); -} - -int main(int argc, char **argv) -{ - struct Queue *strings = queue_new(); - - queue_push_tail(strings, strdup("hello")); - print_len(strings); - queue_push_tail(strings, strdup("world")); - print_len(strings); - queue_push_tail(strings, strdup("in")); - print_len(strings); - queue_push_tail(strings, strdup("C")); - print_len(strings); - - char *str = queue_pop_head(strings); - printf("%s\n", str); - free(str); - - print_len(strings); - str = queue_pop_head(strings); - printf("%s\n", str); - free(str); - - queue_destroy(strings, free); - -// print_len(strings); -// str = queue_pop_head(strings); -// printf("%s\n", str); -// free(str); -// -// print_len(strings); -// str = queue_pop_head(strings); -// printf("%s\n", str); -// free(str); -// -// print_len(strings); -// str = queue_pop_head(strings); -// if (str) { -// printf("%s\n", str); -// free(str); -// } -// print_len(strings); - - return 0; -} diff --git a/libqueue_in_c/readme b/libqueue_in_c/readme deleted file mode 100644 index 6dae742a..00000000 --- a/libqueue_in_c/readme +++ /dev/null @@ -1,3 +0,0 @@ -# Queue implementation in C -* Build the library using `make` command. -* To test library use `make test` and run `qtest` binary diff --git a/matrix-multiplication/matrixmult.c b/matrix-multiplication/matrixmult.c deleted file mode 100644 index ac49d73f..00000000 --- a/matrix-multiplication/matrixmult.c +++ /dev/null @@ -1,56 +0,0 @@ -#include - -int main() -{ - int m, n, p, q, c, d, k, sum = 0; - int first[10][10], second[10][10], multiply[10][10]; - - printf("\nEnter number of rows and columns of first matrix:\n"); - scanf("%d%d", &m, &n); - - printf("Enter number of rows and columns of second matrix:\n"); - scanf("%d%d", &p, &q); - - if (n != p) { - printf("The matrices can't be multiplied with each other.\n"); - return 0; - } - - printf("\n"); - - printf("Enter elements of first matrix:\n"); - - for (c = 0; c < m; c++) - for (d = 0; d < n; d++) - scanf("%d", &first[c][d]); - - printf("\n"); - - printf("Enter elements of second matrix:\n"); - - for (c = 0; c < p; c++) - for (d = 0; d < q; d++) - scanf("%d", &second[c][d]); - - for (c = 0; c < m; c++) { - for (d = 0; d < q; d++) { - for (k = 0; k < p; k++) { - sum = sum + first[c][k]*second[k][d]; - } - - multiply[c][d] = sum; - sum = 0; - } - } - - printf("\nProduct of the matrices:\n"); - - for (c = 0; c < m; c++) { - for (d = 0; d < q; d++) - printf("%d\t", multiply[c][d]); - - printf("\n"); - } - - return 0; -} \ No newline at end of file diff --git a/matrix-multiplication/sample1.txt b/matrix-multiplication/sample1.txt deleted file mode 100644 index 0efb5c35..00000000 --- a/matrix-multiplication/sample1.txt +++ /dev/null @@ -1,5 +0,0 @@ -Enter number of rows and columns of first matrix: -3 5 -Enter number of rows and columns of second matrix: -4 6 -The matrices can't be multiplied with each other. diff --git a/matrix-multiplication/sample2.txt b/matrix-multiplication/sample2.txt deleted file mode 100644 index 3b717c04..00000000 --- a/matrix-multiplication/sample2.txt +++ /dev/null @@ -1,20 +0,0 @@ -Enter number of rows and columns of first matrix: -3 4 -Enter number of rows and columns of second matrix: -4 4 - -Enter elements of first matrix: -1 7 2 2 -6 3 0 8 -9 5 4 7 - -Enter elements of second matrix: -1 0 0 0 -0 1 0 0 -0 0 1 0 -0 0 0 1 - -Product of the matrices: -1 7 2 2 -6 3 0 8 -9 5 4 7 diff --git a/power/power.cpp b/power/power.cpp deleted file mode 100644 index ce6ba10e..00000000 --- a/power/power.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include -using namespace std; -long long power(long long a,int n){ - if(n==0) - return 1; - if((n%2)==0) - return power(a*a,n/2); - else - return a*power(a*a,n/2); -} -int main(){ - long long a; - int n; - cout<<"Enter the value of a"<>a; - cout<<"Enter the value of the power"<<"\n"; - cin>>n; - cout<<"The value of "<>>>>>> -REVERSE -<<<<<<< - -Enter the sentence to be reversed : -This is a sample input string - -string input sample a is This - - End of Program :) - diff --git a/reverse-sentences/reverse.c b/reverse-sentences/reverse.c deleted file mode 100644 index d3765f71..00000000 --- a/reverse-sentences/reverse.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include - -int main() { - - printf("\n"); - printf(">>>>>>>\n"); - printf("REVERSE\n"); // opening - printf("<<<<<<<\n\n"); - printf("Enter the sentence to be reversed :\n"); - - char str[100005]; - scanf("%[^\n]s",str); - // To scan the whole sentence - - int i, l = strlen(str); - printf("\n"); - - for (i=l-1;i>=0;i--) { - - if (str[i]==' ') { - printf("%s ",&str[i+1]); // Print the last word before the next NULL - str[i] = '\0'; // Settint the last space to a NULL - } - - } - - printf("%s\n",str); // Printing the first word - printf("\n End of Program :)\n\n"); // Closing - - return 0; -} \ No newline at end of file diff --git a/reverse-sentences/reverse.py b/reverse-sentences/reverse.py deleted file mode 100644 index 1970549f..00000000 --- a/reverse-sentences/reverse.py +++ /dev/null @@ -1,3 +0,0 @@ -input_str = input('Enter input string: ') -input_str = ' '.join(input_str.split(' ')[::-1]) -print ('Reverse of input string: ', input_str) \ No newline at end of file diff --git a/searching/Go/BinarySearch.go b/searching/Go/BinarySearch.go deleted file mode 100644 index af1e4e86..00000000 --- a/searching/Go/BinarySearch.go +++ /dev/null @@ -1,36 +0,0 @@ - -package main -import "fmt" - -func binarySearch(below int, up []int) bool { - - low := 0 - high := len(up) - 1 - - for low <= high{ - median := (low + high) / 2 - - if up[median] < below { - low = median + 1 - }else{ - high = median - 1 - } - } - - if low == len(up) || up[low] != below { - return false - } - - return true -} - -func main(){ - items := []int{3, 15, 21, 36, 46, 67, 93, 110} - var searchKey = 21 - if (binarySearch(searchKey, items) == true){ - fmt.Println(searchKey,"is available in the array") - }else{ - fmt.Println(searchKey,"isn't available in the array") - } - -} \ No newline at end of file diff --git a/searching/Sublist Search.cpp b/searching/Sublist Search.cpp deleted file mode 100644 index 7ea8235b..00000000 --- a/searching/Sublist Search.cpp +++ /dev/null @@ -1,105 +0,0 @@ -// C++ program to find if a list is present in another list -#include -using namespace std; - -// A Linked List node -struct Node -{ - int data; // To store Data - Node* next; -}; - -// Returns true if first list is present in second -// list -bool findList(Node* first, Node* second) -{ - Node* ptr1 = first, *ptr2 = second; - - // To return true if both linked lists are empty - if (first == NULL && second == NULL) - return true; - - // To return false if one list is empty and other list is not empty - if ( first == NULL || - (first != NULL && second == NULL)) - return false; - - // To traverse the second list one by one - while (second != NULL) - { - // Initialize ptr2 with value of current node of second - ptr2 = second; - - // Matching first list with second list - while (ptr1 != NULL) - { - // If second list becomes empty and first not then return false - if (ptr2 == NULL) - return false; - - // If data part is same, go to next of both lists - else if (ptr1->data == ptr2->data) - { - ptr1 = ptr1->next; - ptr2 = ptr2->next; - } - // If not equal then break the loop - else break; - } - - // Return true if first list gets traversed completely that means it is matched. - if (ptr1 == NULL) - return true; - - // Initialize ptr1 with first again - ptr1 = first; - - // And go to next node of second list - second = second->next; - } - - return false; -} - -// function to print all nodes of the list -void printList(Node* node) -{ - while (node != NULL) - { - printf("%d ", node->data); - node = node->next; - } -} - -// Function to add new node to linked lists -Node *newNode(int key) -{ - Node *temp = new Node; - temp-> data= key; - temp->next = NULL; - return temp; -} - -/* Driver program to test above functions*/ -int main() -{ - /* Let us create two linked lists to test - the above functions. Created lists shall be - a: 1->2->3->4 - b: 1->2->1->2->3->4*/ - Node *a = newNode(1); - a->next = newNode(2); - a->next->next = newNode(3); - a->next->next->next = newNode(4); - - Node *b = newNode(1); - b->next = newNode(2); - b->next->next = newNode(1); - b->next->next->next = newNode(2); - b->next->next->next->next = newNode(3); - b->next->next->next->next->next = newNode(4); - - findList(a,b) ? cout << "LIST FOUND" : cout << "LIST NOT FOUND"; - - return 0; -} \ No newline at end of file diff --git a/searching/a.out b/searching/a.out deleted file mode 100755 index 3128aecd..00000000 Binary files a/searching/a.out and /dev/null differ diff --git a/searching/binary_search.cpp b/searching/binary_search.cpp deleted file mode 100644 index 94146813..00000000 --- a/searching/binary_search.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include -using namespace std; -int binarySearch(int arr[],int low,int high,int elem) -{ - int mid; - if(low<=high) - { - mid=low+(high-low)/2; -// cout<elem) - return binarySearch(arr,low,mid-1,elem); - else - return binarySearch(arr,mid+1,high,elem); - } - return -1; -} -int main() -{ - //Provided array is sorted else we will have to use sort function. - int n,elem; - cout<<"Enter the size of the array:\n"; - cin>>n; - int arr[n+1]; - cout<<"Enter the elements of array:\n"; - for(int i=0;i>arr[i]; - - cout<<"Enter the elements to be searched:\n"; - cin>>elem; -// sort(arr,arr+n); - int index=binarySearch(arr,0,n-1,elem); - - if(index == -1) - { - cout<<"Element is not present in the given array!\n"; - } - else - { - cout<<"Element is present at index "< -using namespace std; - -int interpolationSearch(int arr[], int n, int x) -{ - - int lo = 0, hi = (n - 1); - - - while (lo <= hi && x >= arr[lo] && x <= arr[hi]) - { - if (lo == hi) - { - if (arr[lo] == x) return lo; - return -1; - } - - int pos = lo + (((double)(hi - lo) / - (arr[hi] - arr[lo])) * (x - arr[lo])); - - - if (arr[pos] == x) - return pos; - - if (arr[pos] < x) - lo = pos + 1; - - - else - hi = pos - 1; - } - return -1; -} - - -int main() -{ - int n,elem; - cout<<"Enter the size of the array:\n"; - cin>>n; - int * arr = new int[n+1]; - cout<<"Enter the elements of array:\n"; - for(int i=0;i>arr[i]; - - cout<<"Enter the elements to be searched:\n"; - cin>>elem; - - int index=interpolation_search(arr,n,elem); - - if(index == -1) - { - cout<<"Element is not present in the given array!\n"; - } - else - { - cout<<"Element is present at index "< -using namespace std; -int linear_search(int arr[],int n,int elem) -{ - for(int i=0;i>n; - int arr[n+1]; - cout<<"Enter the elements of array:\n"; - for(int i=0;i>arr[i]; - - cout<<"Enter the elements to be searched:\n"; - cin>>elem; - - int index=linear_search(arr,n,elem); - - if(index == -1) - { - cout<<"Element is not present in the given array!\n"; - } - else - { - cout<<"Element is present at index "<R or rR or l>r: - return None - if L>=l and R<=r: - return self.segment[i] - val1 = self.__query(2*i+1,L,int((L+R)/2),l,r) - val2 = self.__query(2*i+2,int((L+R+2)/2),R,l,r) - print(L,R," returned ",val1,val2) - if val1 != None: - if val2 != None: - return self.fn(val1,val2) - return val1 - return val2 - - - def query(self,L,R): - return self.__query(0,0,len(self.arr)-1,L,R) - -''' -Example - -mytree = SegmentTree([2,4,5,3,4],max) -mytree.query(2,4) -mytree.query(0,3) ... -mytree = SegmentTree([4,5,2,3,4,43,3],sum) -mytree.query(1,8) -... -''' diff --git a/singly linked in C/Singly_linked.c b/singly linked in C/Singly_linked.c deleted file mode 100644 index fb612a60..00000000 --- a/singly linked in C/Singly_linked.c +++ /dev/null @@ -1,77 +0,0 @@ -#include -#include -void randominsert(int); -void create(int); -struct node -{ - int data; - struct node *next; -}; -struct node *head; -void main () -{ - int choice,item,loc; - do - { - printf("nEnter the item which you want to insert?n"); - scanf("%d",&item); - if(head == NULL) - { - create(item); - } - else - { - randominsert(item); - } - printf("nPress 0 to insert more ?n"); - scanf("%d",&choice); - }while(choice == 0); -} -void create(int item) -{ - - struct node *ptr = (struct node *)malloc(sizeof(struct node *)); - if(ptr == NULL) - { - printf("nOVERFLOWn"); - } - else - { - ptr->data = item; - ptr->next = head; - head = ptr; - printf("nNode insertedn"); - } -} -void randominsert(int item) - { - struct node *ptr = (struct node *) malloc (sizeof(struct node)); - struct node *temp; - int i,loc; - if(ptr == NULL) - { - printf("nOVERFLOW"); - } - else - { - - printf("Enter the location"); - scanf("%d",&loc); - ptr->data = item; - temp=head; - for(i=0;inext; - if(temp == NULL) - { - printf("ncan't insertn"); - return; - } - - } - ptr ->next = temp ->next; - temp ->next = ptr; - printf("nNode inserted"); - } - - } diff --git a/trie/trie.dart b/trie/trie.dart deleted file mode 100644 index 73fa5ce4..00000000 --- a/trie/trie.dart +++ /dev/null @@ -1,123 +0,0 @@ -final int asize=26; - -trieNode root=trieNode(); - -void main(){ - insert('abcdefg'); - insert('eminem'); - insert('a'); - insert('samyak'); - insert('example'); - insert('exam'); - insert('am'); - insert('vora'); - insert('axe'); -// print(search('am')); - List letters=['a','e','x','m','p','l']; -// getPossbleWordsFromCharacters(letters); - autoSuggestion('a'); -} - - class trieNode{ - bool end=false; - List children=new List(asize); -} - - -void insert(String name){ - int length=name.length; - trieNode crawl=root;//initially start to crawl from the root - - for(int height=0;height letters){ - //prepare a list to check availability of characters - List hash=List(asize);//true or null - - for(int i=0;i hash,String name){ -// Case if the word is a subset of another word - if(spy.end) - print(name); - -// else traverse every node and check if it forms a word ahead - for(int index=0;index