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

added parenthesis_matching.c in c folder #60

Open
wants to merge 2 commits into
base: main
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
62 changes: 62 additions & 0 deletions C/finding_duplicates.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>

void finding_and_counting_duplicates_in_sorted_array(int A[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
if(A[i]==A[i+1])
{
j=i+1;
while(A[j]==A[i]) j++;
printf("%d is appearing %d times \n", A[i],j-i);
i=j-1;
}
}
}

void finding_and_counting_duplicates_in_sortedorunsorted_array_using_hashing(int A[],int H[],int n,int h)
{
int i;
for(i=0;i<n;i++) //for storing count in hashtable
H[A[i]]++;

for(i=0;i<=h;i++)
{
if(H[i]>1)
printf("%d is repeated %d times \n",i,H[i]);
}
}

void finding_and_counting_duplicates_in_unsorted_array(int A[],int n)
{
int count,i,j;
for (i=0;i<n-1;i++)
{
count =1;
if(A[i]!= -1)
{
for(j=i+1;j<n;j++)
{
if(A[i]==A[j])
{
count ++;
A[j]=-1;
}
}
}
if(count >1)
printf("%d is appearing %d times\n",A[i],count);
}
}

int main()
{
int A[10]={3,2,2,7,10,16,12,15,15,20};
int n = 10,h=20;
int H[21]={0};
//finding_and_counting_duplicates_in_sortedorunsorted_array_using_hashing(A,H,n,h);
finding_and_counting_duplicates_in_unsorted_array(A,n);
return 0;
}
79 changes: 79 additions & 0 deletions C/parenthesis_matching.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>

struct Node
{
char data;
struct Node *next;
}*top=NULL;

void push(char x)
{
struct Node *t;
t=(struct Node*)malloc(sizeof(struct Node));
if(t==NULL)
printf("Stack is Full");
else
{
t->data=x;
t->next=top;
top=t;
}
}

char pop()
{
struct Node *t;
char x=-1;

if(top==NULL)
printf("Stack is Empty\n");
else
{
t=top;
top=top->next;
x=t->data;
free(t);
}
return x;
}


void Display()
{
struct Node *p;
p=top;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->data;
}
printf("\n");
}

int isBalanced(char *exp)
{
int i;
for(i=0;exp[i]!='\0';i++)
{
if(exp[i]=='(')
push(exp[i]);
else if(exp[i]==')')
{
if(top==NULL)
return 0;
pop();
}
}
if(top==NULL)
return 1;
else
return 0;
}

int main()
{
char *exp="(((a+b)*(c-d))";
printf("%d ",isBalanced(exp));
return 0;
}