Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1 Added Programs in C, Java, Python #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions C/BST_operations.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
typedef struct node* Node;
Node newNode(int item)
{
Node temp = (Node)malloc(sizeof(struct node));
temp->data=item;
temp->left=temp->right=NULL;
return temp;
}
Node insert(Node node,int key)
{
if(node == NULL)
return newNode(key);
if(key < node->data)
node->left=insert(node->left,key);
if(key > node->data)
node->right = insert(node->right,key);
return node;
}
int search(Node root,int key)
{
if(root==NULL)
return -1;
if(root->data==key)
return 1;
if(root->data<key)
return search(root->right,key);
return search(root->left,key);
}
void inorder(Node root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d\t",root->data);
inorder(root->right);
}
}
void preorder(Node root)
{
if(root!=NULL)
{
printf("%d\t",root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(Node root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t",root->data);
}
}
int main()
{
int n,i,ch,ch1,key,pos;
Node root=NULL;
while(1)
{
printf("\nEnter your choice\n1:Create BST\n2:Traversal\n3:Search for key\n4:Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the no. of nodes in the BST\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter the element to be inserted\n");
scanf("%d",&key);
root=insert(root,key);
}
break;
case 2:printf("\nEnter your choice\n1:Preorder\n2:Inorder\n3:Postorder");
scanf("%d",&ch1);
if(ch1==1)
preorder(root);
else if(ch1==2)
inorder(root);
else
postorder(root);
break;
case 3:printf("\nEnter the key to be searched\n");
scanf("%d",&key);
pos=search(root,key);
if(pos==-1)
printf("\nKey is not found\n");
else
printf("\nKey is found\n");
break;
case 4:exit(0);
}
}
return 0;
}
8 changes: 8 additions & 0 deletions C/Hello-World.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
int main()
{
printf("Hello World!");
printf("\nHi");
return 0;
}

94 changes: 94 additions & 0 deletions C/MergeSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include<stdlib.h>
#include<stdio.h>

void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;


int L[n1], R[n2];


for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];


i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}


while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}


while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}


void mergeSort(int arr[], int l, int r)
{
if (l < r)
{

int m = l+(r-l)/2;


mergeSort(arr, l, m);
mergeSort(arr, m+1, r);

merge(arr, l, m, r);
}
}

int main()
{
int n,i,j;
scanf("%d", &n);

int a[n];

for(i=0;i<n;i++)
{
scanf("%d" ,&a[i]);
}

mergeSort(a, 0, n-1);

for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}

return 0;

}


32 changes: 32 additions & 0 deletions C/anagram.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* The following program is an implementation of the popular Anangram problem wherein if two words are formed off the same set of
characters, they can be called as anagrams. Example of anagrams are 'tab' and 'bat' which are formed off the same set of characters
{a,b,t}. Hence here is an implementation of the Anagrams problem which calculates the sum of value of ASCII characters to check if
two words are Anangram or not. */

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main() {
char word[100], word1[100];
int l1, l2, i, s1 = 0, s2 = 0;
printf("*************Program to Check for Anangrams*************\n");
printf("Enter the first word: \n");
gets(word);
printf("Enter the second word: \n");
gets(word1);
l1 = strlen(word);
l2 = strlen(word1);
for (i = 0; i < l1; i++) {
s1 = s1 + (int) word[i];
}
for (i = 0; i < l2; i++) {
s2 = s2 + (int) word1[i];
}
if (s1 == s2) {
printf("The words are Anagrams! \n");
} else {
printf("The Words are not Anagrams! \n");
}
getch();
}
Loading