Skip to content

Mybranch #268

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

Closed
wants to merge 6 commits into from
Closed
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
162 changes: 162 additions & 0 deletions heap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
int firstfill(int *arr)
{
int ele;
printf("Enter:");
scanf("%d",&ele);
int x=0;
while(ele!=-1)
{
arr[x]=ele;
x++;
printf("Enter:");
scanf("%d",&ele);//keep scanning elements and place them in the array, stop when -1 is given
}
return x;//returns the number of elements
}
void swap(int * arr,int p,int c)//swaps the parent node and child node
{
int temp=arr[p];
arr[p]=arr[c];
arr[c]=temp;
}
void construction(int *arr,int x)
{
int p=x/2-1;
while(1)//x/2-1 to 0
{
int temp=p;
while(1)//each parent swap should check if it caused any invalid further occurances till last child
{
int lc=2*p+1;//formula of left child
int rc;
int max1;
int flag;
if(2*p+2<=x-1)//check if right child exists(if it is a parent node then left definitely exists)
{
rc=2*p+2;
max1=(arr[lc]<arr[rc])?rc:lc;//choose max child
if(max1==rc)
flag=2;//1 means left is max 2 means right
else
flag=1;
}
else//right child doesn't exist
{
rc=-1;
max1=lc;
flag=1;
}
if(arr[p]>=arr[max1])//check if parent greater than max child
flag=0;
else//if not then swap
swap(arr,p,max1);
if(flag==0)//if no swap then no changes so break
break;
else if(flag==1)//check till last child, if left was swapped pass through left subtree
{
if(2*lc+1<=x-1)//check if left child exists
{
p=lc;//if it does then do the same steps again with the left child as the parent
continue;
}
else
break;
}
else
{
if(2*rc+1<=x-1)//check if right child exists
{
p=rc;//if it does then make it the parent and repeat the process
continue;
}
else
break;
}
}
p=temp-1;//when inner loop ends the last parent node and the subtrees are fixed, now move to the previous parent
if(p==-1)
break;//when root reached stop the process
}
}
int delete(int *arr,int *len)
{
int res=arr[0];
arr[0]=arr[(*len)-1];//replace head(root) with last element
int p=0;
(*len)--;//means the last element is now not accessible using array
while(1)//same process as inner loop in insert(done to fix the heaps property when one node is deleted)
{
int lc=2*p+1;
int rc;
int max1;
int flag;
if(2*p+2<=(*len)-1)
{
rc=2*p+2;
max1=(arr[lc]<arr[rc])?rc:lc;
if(max1==rc)
flag=2;
else
flag=1;
}
else
{
rc=-1;
max1=lc;
flag=1;
}
if(arr[p]>=arr[max1])
flag=0;
else
swap(arr,p,max1);
if(flag==0)
break;
else if(flag==1)
{
if(2*lc+1<=(*len)-1)
{
p=lc;
continue;
}
else
break;
}
else
{
if(2*rc+1<=(*len)-1)
{
p=rc;
continue;
}
else
break;
}
}
return res;//return the deleted element
}

int main()
{
int arr[MAX];
int len=firstfill(arr);//initially all elements are directly placed into an array
printf("Number of elements:%d\n",len);
printf("Before swaps:\n");
for(int i=0;i<len;i++)
printf("%d\n",arr[i]);//prints raw structure before heapify
construction(arr,len);//constructs heap
printf("After construction\n");
for(int i=0;i<len;i++)//level order traversal
printf("%d\n",arr[i]);//prints heap(after bottom down method is executed)
while(len!=0)//gives descending order(as this is max heap)
{
int del=delete(arr,&len);
printf("Deleted element:%d\n",del);
printf("Heap after deletion\n");
for(int i=0;i<len;i++)
printf("%d\n",arr[i]);
}
return 0;
}
66 changes: 66 additions & 0 deletions matrixmultiplication.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//DMA:using double pointer
#include<stdio.h>
#include<stdlib.h>
void mul(int **a,int **b,int **c,int r1,int c1,int c2)//same as using sma but the notations differ
{
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
c[i][j]=0;
for(int k=0;k<c1;k++)
{
*(*(c+i)+j)+=*(*(a+i)+k)*(*(*(b+k)+j));
}
}
}
}
int** allocat(int r,int c)
{
int **ptr;
ptr=malloc(r*sizeof(int*));//dynamically allocate memory for pointer of pointer
for(int i=0;i<c;i++)
{
ptr[i]=malloc(c*sizeof(int));//dynamically allocate memory for each pointer
}
return ptr;
}
void inp(int **ptr,int r,int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
scanf("%d",(*(ptr+i)+j));//take input elements
}
}
}
void out(int **ptr,int r,int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
printf("%d ",*(*(ptr+i)+j));//output the multiplied matrix
}
printf("\n");
}
}
int main()
{
int r1,c1,r2,c2;
int **ma,**mb,**mc;//double pointers used
printf("Enter the row1 col1 ro2 col2\n");
scanf("%d %d %d %d",&r1,&c1,&r2,&c2);
ma=allocat(r1,c1);//first memory allocated for each
mb=allocat(r2,c2);
mc=allocat(r1,c2);
printf("Enter elements of first\n");
inp(ma,r1,c1);
printf("Enter elements of second\n");
inp(mb,r2,c2);
mul(ma,mb,mc,r1,c1,c2);//multiplied
printf("Multiplied matrix:\n");
out(mc,r1,c2);
return 0;
}
109 changes: 109 additions & 0 deletions parenthesis_balancing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*left bracket is missing ({})} stack got empty expression not exhausted
right bracket is missing({} stack not empty expression exhausted
mismatch({)}incoming and bracket at top is not matching
*/
#include<stdio.h>
#include<stdlib.h>
struct stack
{
char *ptr_s;
int size;
int top;
};
typedef struct stack stack;
void createstack(stack* ptr)
{
ptr->ptr_s=malloc(sizeof(char)*ptr->size);//allocates memory for stack array
ptr->top=-1;//sets top to -1
}
void push(stack* ptr,char ele)
{
if(ptr->top==(ptr->size)-1)//when mac size reached
printf("Stack is full(overflow)");
else
{
ptr->ptr_s[++(ptr->top)]=ele;//push ele into stack
}
}
char pop(stack* ptr)
{
char ele;
if(ptr->top==-1)
{
return -1;//empty stack
}
else
{
ele=ptr->ptr_s[ptr->top--];//pop element and store in ele
return ele;
}
}
void display(stack st)
{
printf("Stack display:\n");
if(st.top==-1)
printf("Empty stack\n");
else
{
while(st.top!=-1)
{
printf("%c\n",st.ptr_s[st.top--]);//show contents of stack(LIFO order)
}
}
}
void traverse(char inp[100],stack* st)
{
char token=inp[0];
int count=0;
int k=1;
while(token!='\0')
{
if(token=='(' || token=='[' ||token=='{')//if opening brackets encountered push them into stack
push(st,token);
else if(token==')' || token=='}' || token==']')
{

if(st->top==-1)//if closing bracket exists and stack empty it means there are extra closing brackets
{
printf("Extra closing brackets\n");
count=1;
break;
}
char ch=st->ptr_s[st->top];
if((ch=='(' && token==')') ||(ch=='[' && token==']')||(ch=='{' && token=='}'))//when the opening and closing are of same type pop out the opening bracket from stack
{
int x=pop(st);
}
else//if they don't match then the brackets have a mismatch
{
printf("Mismatch\n");
count=1;
break;
}
}
token=inp[k];//go to next character in input
k++;
}
if(st->top!=-1 && count==0)//when input is exhausted but stack not empty it means extra opening brackets
printf("Extra opening brackets\n");
else//if it did not break from any condition
{
if(count==0)
printf("Balanced parenthesis\n");//then it is balanced
}

}
int main()
{
stack* ptr_st;
ptr_st=malloc(sizeof(stack));//allocate memory for a stack pointer
printf("Enter the size of the stack");
scanf("%d",&ptr_st->size);//input size
char str[ptr_st->size];
createstack(ptr_st);//initialize stack structure
printf("enter the string");
scanf("%s",str);
traverse(str,ptr_st);
return 0;
}