Skip to content
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
11 changes: 11 additions & 0 deletions Arrays/1D Array/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>

int main(){
int num,arr[100],i;
scanf("%d",&num);
for(i=0;i<num;i++)
scanf("%d",&arr[i]);// Reading input from STDIN
for(i=num-1;i>=0;i--)
printf("%d\n",arr[i]);// Writing output to STDOUT
}

34 changes: 34 additions & 0 deletions Arrays/Multi-D Array/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*#include<stdio.h>
void main()
{
int i,r,c,j,arr[10][10];
scanf("%d %d ",&r,&c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&arr[i][j]);
for(i=0;i<c;i++)
{ for(j=0;j<r;j++)
{printf("%d ",arr[j][i]);
}
printf("\n");
}

}
*/
#include<stdio.h>
int main()
{
int arr[100][100],m,n,i,j;
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&arr[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",arr[j][i]);
printf("\n");
}
}
23 changes: 23 additions & 0 deletions Hash Tables/Hash Tables/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<stdio.h>
main()
{
int roll,n,i=0,ans[100],j;
char name[100],hash[100][100];
scanf("%d",&n);
while(n>i)
{
scanf("%d %s%*c",&roll,name);
for(j=0;name[j]!='\0';j++)
hash[roll][j]=name[j];
i++;
}
scanf("%d",&n);
i=0;
while(n>i)
{
scanf("%d",&ans[i]);
i++;
}
for(i=0;i<n;i++)
printf("%s\n",hash[ans[i]]);
}
34 changes: 34 additions & 0 deletions Queues/Queues/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
int main()
{
int Q[100],front,rear,x,size;
int N;
char T;
front=rear=0;
scanf("%d",&N);
while(N--)
{
scanf(" %c",&T);
if(T=='E')
{
scanf("%d",&x);
Q[rear]=x;
rear++;
size=rear-front;
printf("%d\n",size);
}
else if(T=='D')
{
if(front==rear)
printf("1 ");
else
{
printf("%d ",Q[front]);
Q[front]=0;
front++;}
size=rear-front;
printf("%d\n",size);

}
}
}
85 changes: 85 additions & 0 deletions Stacks/Stacks/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*lude<stdio.h>
int top=-1,val,stack[100];
int ch,n,i;
void pop()
{
top=top-1;
}
void push(int stack[],int val)
{
top=top+1;
stack[top]=val;
}
void main()
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&ch);
if(ch==1)
{
if(top==0)
printf("\nNo food");
else
{
printf("%d\n",stack[top]);
pop();
}
}
if(ch==2)
{
if(top!=99)
{
scanf("%d",&val);
push(stack,val);
}
}

}
}
*/
#include <stdio.h>
#include <math.h>
int top=-1;
void pop()
{
top=top-1;
}
void push(int stack[],int val)
{
top=top+1;
stack[top]=val;
}
int main()
{
int n,a,b,val,max;
scanf("%d",&n);
max=n;
int stack[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a);
if(a==1)
{
if(top==-1)
{
printf("No food\n");
}
else
{
printf("%d\n",stack[top]);
pop();
}
}
else
{
if(top!=max-1)
{
scanf("%d",&val);
push(stack,val);
}
}

}
return 0;
}
67 changes: 67 additions & 0 deletions Trees/Binary Search Tree/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node* lchild;
struct node* rchild;
};
struct node* root;
struct node* insert(struct node* root,int it)
{
if(root==NULL)
{
root=(struct node *)malloc(sizeof(struct node));
root->lchild=NULL;
root->rchild= NULL;
root->data = it;
return root;
}
else
{
if(it< root->data )
root->lchild=insert(root->lchild,it);
else if(it>root->data)
root->rchild=insert(root->rchild,it);
else
printf(" Duplicate Element !! Not Allowed !!!");
return(root);
}
}
void create(int n)
{int i,it;
for(i=0;i<n;i++)
{
scanf("%d",&it);
root=insert(root,it);
}
}
void preorder(struct node *root)
{
if(root!=NULL)
{
printf("%d\n",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
void find(int m,struct node *root)
{ if(root->data==m)
preorder(root);
else if(root->data<m)
find(m,root->rchild);
else
find(m,root->lchild);
}


main()
{ int m,n;
scanf("%d",&n);
create(n);
scanf("%d",&m);
find(m,root);
}



77 changes: 77 additions & 0 deletions Trees/Binary Tree/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node* lchild;
struct node* rchild;
}*ptr;
struct node* root;
struct node* getnode(int val){
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=val;
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
}
int getMax(int a, int b){
if(a >= b)
return a;
else
return b;
}

int getHeight(struct node *root){
int leftHeight, rightHeight;
if(root == NULL)
return 0;
leftHeight = getHeight(root->lchild);
rightHeight = getHeight(root->rchild);

return getMax(leftHeight, rightHeight) + 1;
}


int getDiameter(struct node *nodePtr) {
/* Empty Tree */
if (nodePtr == NULL)
return 0;


int leftHeight = getHeight(nodePtr->lchild);
int rightHeight = getHeight(nodePtr->rchild);

int leftDiameter = getDiameter(nodePtr->lchild);
int rightDiameter = getDiameter(nodePtr->rchild);
return getMax(leftHeight + rightHeight + 1,
getMax(leftDiameter, rightDiameter));
}

main()
{int i,n,m;
char s[20];
scanf("%d %d",&n,&m);
root=getnode(m);
for(i=1;i<n;i++)
{
scanf("%s %d",&s,&m);
int j=0;
ptr=root;
while(s[j]!='\0')
{
if(s[j]=='L'){
if(ptr->lchild==NULL)
ptr->lchild=getnode(m);
ptr=ptr->lchild;}
else
{
if(ptr->rchild==NULL)
ptr->rchild=getnode(m);
ptr=ptr->rchild;
}
j++;
}

}
printf("%d\n",getDiameter(root));
}
29 changes: 29 additions & 0 deletions Trees/Heap/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<stdio.h>
main()
{
int N,A[100],i,type,q,a,b,j;
scanf("%d",&N);
for(i=0;i<N;i++)
scanf("%d",&A[i]);
scanf("%d",&q);
while(q>0)
{
scanf("%d",&a);
if(a==1)
{ scanf("%d",&b);
for(j=0;j<N;j++);
A[j]=b;
N++;
}
if(a==2)
{ int large=A[0];
for(j=1;j<N;j++)
{
if(A[j]>large)
large=A[j];
}
printf("%d\n",large);
}
q--;
}
}
Loading