Write an efficient function that deletes all the characters from string src which are given in string remove. Your function should not change the relative order of characters in src. What are the time and space complexities of your solution?
Code:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define INIT_SIZE 26
#define LF 10
struct ListNode
{
int data;
struct ListNode *next;
};
struct HashTbl
{
int size;
int nelement;
struct ListNode **array;
};
struct HashTbl* createHashTable();
int add(struct HashTbl*,int);
int ListInsert(struct ListNode*,int);
void Rehash(struct HashTbl*);
void insert(struct ListNode*,struct ListNode*);
int find(struct HashTbl*,int);
int ListFind(struct ListNode*,int);
int removelst(struct HashTbl*,int);
int ListDelete(struct ListNode*,int);
struct HashTbl* createHashTable()
{
int i;
struct ListNode *temp;
struct HashTbl *htbl;
htbl=(struct HashTbl*) malloc(sizeof(struct HashTbl));
htbl->nelement=0;
htbl->size=INIT_SIZE;
htbl->array=(struct ListNode**) malloc(INIT_SIZE * sizeof(struct ListNode*));
for(i=0;i<INIT_SIZE;i++)
{
temp=(struct ListNode*) malloc(sizeof(struct ListNode));
temp->next=NULL;
htbl->array[i]=temp;
}
return(htbl);
}
int add(struct HashTbl *htbl,int key)
{
int pos;
if((htbl->nelement)/(htbl->size)>LF)
Rehash(htbl);
pos=hash(key,htbl->size);
if(ListInsert(htbl->array[pos],key))
{
(htbl->nelement)++;
return(1);
}
else
return(0);
}
int hash(int k,int s)
{
return(k%26);
}
int ListInsert(struct ListNode *p,int k)
{
struct ListNode *current,*temp;
current=p;
while(current->next!=NULL)
{
if(current->data==k)
return(0);
current=current->next;
}
if(current->data==k)
return(0);
temp=(struct ListNode*) malloc(sizeof(struct ListNode));
temp->data=k;
temp->next=NULL;
current->next=temp;
return(1);
}
void Rehash(struct HashTbl *htbl)
{
int i,newsize,pos;
struct ListNode *temp,**newarray,*current;
printf("\nrehashing\n");
newsize=htbl->size*2;
newarray=(struct ListNode**) malloc(newsize * sizeof(struct ListNode*));
for(i=0;i<newsize;i++)
{
temp=(struct ListNode*) malloc(sizeof(struct ListNode));
temp->next=NULL;
newarray[i]=temp;
}
for(i=0;i<htbl->size;i++)
{
for(current=htbl->array[i];current->next!=NULL;)
{
temp=current->next;
current->next=temp->next;
pos=hash(temp->data,newsize);
insert(newarray[pos],temp);
}
}
htbl->size=newsize;
free(htbl->array);
htbl->array=newarray;
}
void insert(struct ListNode *a,struct ListNode *t)
{
t->next=a->next;
a->next=t;
}
int find(struct HashTbl *htbl,int key)
{
int pos;
pos=hash(key,htbl->size);
return(ListFind(htbl->array[pos],key));
}
int ListFind(struct ListNode *a,int k)
{
struct ListNode *current=a->next;
while(current!=NULL)
{
if(current->data==k)
return(1);
current=current->next;
}
return(0);
}
int removelst(struct HashTbl *htbl,int key)
{
int pos;
pos=hash(key,htbl->size);
return ListDelete(htbl->array[pos],key);
}
int ListDelete(struct ListNode *a,int k)
{
struct ListNode *current,*prev;
prev=a;
current=a->next;
while(current!=NULL)
{
if(current->data==k)
{
prev->next=current->next;
free(current);
return(1);
}
current=current->next;
prev=prev->next;
}
return(0);
}
void main()
{
int i,l=0;
char a[20],b[5],c[20];
struct HashTbl *head;
head=createHashTable();
printf("\nHash Table created\n");
printf("\nEnter string A:");
scanf("%s",a);
for(i=0;i<strlen(a);i++)
add(head,a[i]);
printf("\nEnter the string B:");
scanf("%s",b);
for(i=0;i<strlen(b);i++)
{
if(find(head,b[i])==1)
removelst(head,b[i]);
}
for(i=0;i<strlen(a);i++)
{
if(find(head,a[i])==1)
{
c[l]=a[i];
l++;
}
}
c[l]='\0';
printf("\nString after modification: %s\n",c);
}
consider function type
void RemoveChar(char [] src, char []remove)
Input: Gopipodila<-src
ila <-remove
Output: Goipodi <-src
Algorithm:
im using hashtable and for storing data from remove string then for every charcter in src string whether charecter is present in hash table or not if it is avilable skip that charcter Code:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define INIT_SIZE 26
#define LF 10
struct ListNode
{
int data;
struct ListNode *next;
};
struct HashTbl
{
int size;
int nelement;
struct ListNode **array;
};
struct HashTbl* createHashTable();
int add(struct HashTbl*,int);
int ListInsert(struct ListNode*,int);
void Rehash(struct HashTbl*);
void insert(struct ListNode*,struct ListNode*);
int find(struct HashTbl*,int);
int ListFind(struct ListNode*,int);
int removelst(struct HashTbl*,int);
int ListDelete(struct ListNode*,int);
struct HashTbl* createHashTable()
{
int i;
struct ListNode *temp;
struct HashTbl *htbl;
htbl=(struct HashTbl*) malloc(sizeof(struct HashTbl));
htbl->nelement=0;
htbl->size=INIT_SIZE;
htbl->array=(struct ListNode**) malloc(INIT_SIZE * sizeof(struct ListNode*));
for(i=0;i<INIT_SIZE;i++)
{
temp=(struct ListNode*) malloc(sizeof(struct ListNode));
temp->next=NULL;
htbl->array[i]=temp;
}
return(htbl);
}
int add(struct HashTbl *htbl,int key)
{
int pos;
if((htbl->nelement)/(htbl->size)>LF)
Rehash(htbl);
pos=hash(key,htbl->size);
if(ListInsert(htbl->array[pos],key))
{
(htbl->nelement)++;
return(1);
}
else
return(0);
}
int hash(int k,int s)
{
return(k%26);
}
int ListInsert(struct ListNode *p,int k)
{
struct ListNode *current,*temp;
current=p;
while(current->next!=NULL)
{
if(current->data==k)
return(0);
current=current->next;
}
if(current->data==k)
return(0);
temp=(struct ListNode*) malloc(sizeof(struct ListNode));
temp->data=k;
temp->next=NULL;
current->next=temp;
return(1);
}
void Rehash(struct HashTbl *htbl)
{
int i,newsize,pos;
struct ListNode *temp,**newarray,*current;
printf("\nrehashing\n");
newsize=htbl->size*2;
newarray=(struct ListNode**) malloc(newsize * sizeof(struct ListNode*));
for(i=0;i<newsize;i++)
{
temp=(struct ListNode*) malloc(sizeof(struct ListNode));
temp->next=NULL;
newarray[i]=temp;
}
for(i=0;i<htbl->size;i++)
{
for(current=htbl->array[i];current->next!=NULL;)
{
temp=current->next;
current->next=temp->next;
pos=hash(temp->data,newsize);
insert(newarray[pos],temp);
}
}
htbl->size=newsize;
free(htbl->array);
htbl->array=newarray;
}
void insert(struct ListNode *a,struct ListNode *t)
{
t->next=a->next;
a->next=t;
}
int find(struct HashTbl *htbl,int key)
{
int pos;
pos=hash(key,htbl->size);
return(ListFind(htbl->array[pos],key));
}
int ListFind(struct ListNode *a,int k)
{
struct ListNode *current=a->next;
while(current!=NULL)
{
if(current->data==k)
return(1);
current=current->next;
}
return(0);
}
int removelst(struct HashTbl *htbl,int key)
{
int pos;
pos=hash(key,htbl->size);
return ListDelete(htbl->array[pos],key);
}
int ListDelete(struct ListNode *a,int k)
{
struct ListNode *current,*prev;
prev=a;
current=a->next;
while(current!=NULL)
{
if(current->data==k)
{
prev->next=current->next;
free(current);
return(1);
}
current=current->next;
prev=prev->next;
}
return(0);
}
void main()
{
int i,l=0;
char a[20],b[5],c[20];
struct HashTbl *head;
head=createHashTable();
printf("\nHash Table created\n");
printf("\nEnter string A:");
scanf("%s",a);
for(i=0;i<strlen(a);i++)
add(head,a[i]);
printf("\nEnter the string B:");
scanf("%s",b);
for(i=0;i<strlen(b);i++)
{
if(find(head,b[i])==1)
removelst(head,b[i]);
}
for(i=0;i<strlen(a);i++)
{
if(find(head,a[i])==1)
{
c[l]=a[i];
l++;
}
}
c[l]='\0';
printf("\nString after modification: %s\n",c);
}
No comments:
Post a Comment