WOW
now i stopped working on that and used cpp instead that code works fine.
but now i was implementing very simple hash table with chaining.
in this code again the program crashed after inserting third element(similar to bst crash problem)
here is the code
Code:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
struct data
{
char str[10];
struct data* link;
};
typedef struct data* node;
struct table
{
struct data* link;
}table[137];
int hash(char *,int);
int chc(void);
void insert(int,char *);
void search(int,char *);
int main()
{
char s[10];
while(1)
{
switch(chc())
{
case 1:
printf("\nEnter any string :");
scanf("%s",s);
insert(hash(s,strlen(s)),s);
break;
case 2:
break;
case 3:
printf("\nEnter any string :");
scanf("%s",s);
search(hash(s,strlen(s)),s);
getch();
break;
case 9:
exit(0);
break;
default:
break;
}
}
}
int chc()
{
int a;
system("cls");
printf("1:Make new entry\n2:Delete any key\n3:Search any key\n9:Exit\nEnter Your Choice: ");
scanf("%d",&a);
return a;
}
int hash(char *hm,int len)
{
int i,has;
for(i=0;i<len;i++)
{
has+=(*hm++)*i;
}
has=has%137;
printf("%d",has);
getch();
return has;
}
void insert(int key,char *str)
{
node scroll,temp;
if(table[key].link)
{
scroll=table[key].link;
while(scroll->link)
{
scroll=scroll->link;
}
temp=malloc(sizeof(node));
scroll->link=temp;
strcpy(temp->str,str);
temp->link=NULL;
}
else
{
temp=malloc(sizeof(node));
table[key].link=temp;
strcpy(temp->str,str);
printf("%s,",temp->str);
temp->link=NULL;
}
}
void search(int key,char *str)
{
node scroll;
if(table[key].link==NULL)
{
printf("No such element");
}
else
{
scroll=table[key].link;
while(scroll!=NULL)
{
if(!strcmp(str,scroll->str))
{
printf("\nwow it works");
break;
}
scroll=scroll->link;
}
}
}
for example i ran it and pressed 1 to enter new element and then repeated the same procedure three times and program crashed.
i ran it on codeblocks and captured following every time error code is same.
and it comes after third entry.
this same prob was there in bst.
PS: i am running on amd can that be a prob ?>?
1:Make new entry
2:Delete any key
3:Search any key
9:Exit
Enter Your Choice: 1
Enter any string :keasd
0keasd,
1:Make new entry
2:Delete any key
3:Search any key
9:Exit
Enter Your Choice: 1
Enter any string :askajdj
44askajdj,
1:Make new entry
2:Delete any key
3:Search any key
9:Exit
Enter Your Choice: 1
Enter any string :adskajsd
1
Process returned -1073741819 (0xC0000005) execution time : 8.406 s
Press any key to continue.