Click here to Skip to main content
15,889,034 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: how to paint a bitmap to the dialog client dc from the memory dc Pin
Niklas L6-Jun-10 11:15
Niklas L6-Jun-10 11:15 
QuestionHow to overlay Direct3d model in live webcam feed Pin
GreenGiant835-Jun-10 8:58
GreenGiant835-Jun-10 8:58 
QuestionHelp with hash tables in C Pin
arnluci5-Jun-10 6:48
arnluci5-Jun-10 6:48 
AnswerRe: Help with hash tables in C Pin
Luc Pattyn5-Jun-10 8:10
sitebuilderLuc Pattyn5-Jun-10 8:10 
GeneralRe: Help with hash tables in C Pin
arnluci5-Jun-10 11:09
arnluci5-Jun-10 11:09 
GeneralRe: Help with hash tables in C Pin
Luc Pattyn5-Jun-10 11:31
sitebuilderLuc Pattyn5-Jun-10 11:31 
GeneralRe: Help with hash tables in C Pin
Saurabh.Garg6-Jun-10 2:16
Saurabh.Garg6-Jun-10 2:16 
GeneralRe: Help with hash tables in C [modified] Pin
arnluci21-Jun-10 10:31
arnluci21-Jun-10 10:31 
I've decided to write the code form scratch(inspiring myself from the internet) and this what i came up with:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>

//We need to define our list structure
typedef struct _List_t{
char *string;
struct _List_t *next;
}List_t;

//We need to define our hash table itself
typedef struct _HashTable_t{
int size;
List_t **table;
}HashTable_t;

HashTable_t *CreateTable(int);
HashTable_t *CreateTable(int size)
{
HashTable_t *NewTable;

//We wil need to alocate memory both for the table structure 
//and the table itself
NewTable=(HashTable_t*)malloc(sizeof(HashTable_t));
NewTable->table=(List_t**)malloc(sizeof(HashTable_t));

//Initially we will need for the table elements to be NULL
for(int i=0;i<size;i++)
 NewTable->table[i]=NULL;
//We also need to set size of the table
NewTable->size=size;
return NewTable;
}
unsigned int hash(HashTable_t *hashtable, char *str)
{
int a,b,p;
unsigned int hashval=0;
for(int i=0;i<<9; i++) 
hashval = str[i] + (hashval << 5) - hashval;  
               
return hashval%hashtable->size;
} 

//We need to define our search function
List_t *search(HashTable_t *hashtable, char *str)
{
List_t *list;
unsigned int hashval=hash(hashtable,str);
for(list=hashtable->table[hashval];list!=NULL;list=list->next)
 if(strcmp(str,list->string)==0)
{
    return list;
}
    return NULL;
}
//We need the insertion function
int insert(HashTable_t *hashtable, char *str)
{
List_t *NewList;
List_t *CurentList;
unsigned int hashval=hash(hashtable,str);

NewList=(List_t*)malloc(sizeof(List_t));

//Is the item already in the list?
CurentList=search(hashtable,str);
//Handling a positive search
if(CurentList !=NULL) return 2;

//Finally the insertion
NewList->string=_strdup(str);
NewList->next=hashtable->table[hashval];
hashtable->table[hashval]=NewList;
return 0;
}

//We'll need to implement 2  clear functions:one for clearing a specific value in the table, and another one to clear up the table itself
void ClearKey(HashTable_t *hashtable,char *str);
void ClearKey(HashTable_t *hashtable,char *str){
	List_t *list,*temp;
	int hashval=hash(hashtable,str);
//We position ourselves on index of the table, because that's where our head of the list is 
	list=hashtable->table[hashval];
	while(list!=NULL){ 
//If we found what we are looking for erase it
	if(list->string=str)
	  temp=list;
	  list=list->next;
	  free(temp);
	                 }
}

void ClearTable(HashTable_t);
void ClearTable(HashTable_t *hashtable)
{
	List_t *list,*temp;
	if(hashtable!=NULL)
	for(int i=0;i<hashtable->size;i++){
		list=hashtable->table[i];
	while(list!=NULL){
		temp=list;
		list=list->next;
		free(temp);   }
	                                  }
	free(hashtable->table);
	free(hashtable);
}

void main()
{
HashTable_t *MyHashTable;
char *s;
int sel,TableSize=12;
MyHashTable=CreateTable(TableSize);
printf("HashTable created!\n");
for(int i=0;i<MyHashTable->size;i++)
printf("%d",MyHashTable->table[i]);


printf("Select an operation\n");
printf("1. Insert   2. Search   3. Erase   Other=Quit\n");
scanf("%c",&sel);
printf("Your selection is:%c",sel);
printf("\nEnter the key:");
s=(char*)malloc(sizeof(10));
scanf("%s",s);
printf("Key is %s",s);
while(sel<5){
	if(sel=1)
		insert(MyHashTable,s);
	else if(sel=2)
		search(MyHashTable,s);
	     else if(sel=3)
              ClearKey(MyHashTable,s);
		      else
		      printf("Invalid Selection");
	         }
getch();
}

this is what i get when I compile it in Visual Studio
First-chance exception at 0x77052374 in HT.exe: 0xC0000005: Access violation reading location 0x00000004.

First-chance exception at 0x7703317f in HT.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x7707713d in HT.exe: 0xC0000005: Access violation reading location 0x00000014.
First-chance exception at 0x7708ac07 in HT.exe: 0xC0000005: Access violation reading location 0x06b2efe2.
First-chance exception at 0x66a2d914 (msvcr90d.dll) in HT.exe: 0xC0000005: Access violation writing location 0x06b2f010.
The thread 'Win32 Thread' (0x138) has exited with code -1073741510 (0xc000013a).
Unhandled exception at 0x66a2d914 (msvcr90d.dll) in HT.exe: 0xC0000005: Access violation writing location 0x06b2f010.


modified on Monday, June 21, 2010 8:00 PM

Questionwhy does it use so many memory [modified][Solved] Pin
yu-jian5-Jun-10 5:25
yu-jian5-Jun-10 5:25 
AnswerRe: why does it use so many memory Pin
Aescleal5-Jun-10 6:17
Aescleal5-Jun-10 6:17 
GeneralRe: why does it use so many memory Pin
yu-jian6-Jun-10 3:10
yu-jian6-Jun-10 3:10 
GeneralRe: why does it use so many memory Pin
Aescleal6-Jun-10 7:18
Aescleal6-Jun-10 7:18 
AnswerRe: why does it use so many memory Pin
Hristo-Bojilov5-Jun-10 6:24
Hristo-Bojilov5-Jun-10 6:24 
QuestionRe: why does it use so many memory Pin
Randor 5-Jun-10 7:02
professional Randor 5-Jun-10 7:02 
QuestionRe: why does it use so many memory Pin
David Crow7-Jun-10 4:17
David Crow7-Jun-10 4:17 
QuestionHow to Save Image from WebBrowser control Pin
voo doo125-Jun-10 5:23
voo doo125-Jun-10 5:23 
AnswerRe: How to Save Image from WebBrowser control Pin
Hristo-Bojilov5-Jun-10 6:35
Hristo-Bojilov5-Jun-10 6:35 
GeneralRe: How to Save Image from WebBrowser control Pin
voo doo125-Jun-10 7:21
voo doo125-Jun-10 7:21 
GeneralRe: How to Save Image from WebBrowser control Pin
Hristo-Bojilov5-Jun-10 8:14
Hristo-Bojilov5-Jun-10 8:14 
GeneralRe: How to Save Image from WebBrowser control Pin
voo doo125-Jun-10 16:09
voo doo125-Jun-10 16:09 
GeneralRe: How to Save Image from WebBrowser control [modified] Pin
Hristo-Bojilov6-Jun-10 2:57
Hristo-Bojilov6-Jun-10 2:57 
GeneralRe: How to Save Image from WebBrowser control Pin
voo doo126-Jun-10 4:54
voo doo126-Jun-10 4:54 
GeneralRe: How to Save Image from WebBrowser control Pin
Hristo-Bojilov6-Jun-10 5:06
Hristo-Bojilov6-Jun-10 5:06 
Question0xC0000138: Ordinal Not Found Pin
eusto5-Jun-10 1:51
eusto5-Jun-10 1:51 
AnswerRe: 0xC0000138: Ordinal Not Found Pin
Randor 5-Jun-10 6:28
professional Randor 5-Jun-10 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.