Hash, link method to resolve conflicts

Source: Internet
Author: User

#include <iostream>
using namespace Std;

struct ListNode;
typedef struct LISTNODE *position;
struct hash_table;
typedef hash_table *HASHTAB;
typedef Position List;

#define MIN_TABLE_SIZE 10

struct ListNode
{
int emelent;
Position Next;
};

struct hash_table
{
int table_size;
List *thelist;//linked list to linked list
};

Correlation function Declaration//////////////////////
Hashtab inittable (int table_size); Initialize a hash
Position Find (int x, Hashtab H); Finds element x, returns the corresponding position
int Hash (int x); hash function
void Insert (int Key, Hashtab H); Inserting an element into a hash key
void Delete (int Key, Hashtab H); Delete an element in a hash key

Correlation function Definition////////////////////
Hashtab inittable (int table_size)
{
Hashtab H;
if (Table_size < min_table_size)
{
cout << "Table size is too small" << Endl;
return NULL;
}

H = (hashtab) malloc (sizeof (hash_table));
if (H = = NULL) cout << "Out of Space" << Endl;
H->table_size = table_size;
H->thelist = (list*) malloc (sizeof (Position) * h->table_size);//Do not know how many elements in Position can allocate memory?
if (h->thelist = = NULL) cout << "Out of Space" << Endl;
for (int i = 0; I! = h->table_size; ++i)
{
H->thelist[i] = (Position) malloc (sizeof (ListNode));
if (h->thelist[i] = = NULL) cout << "Out of Space" << Endl;
Else
{
H->thelist[i]->emelent = i;
H->thelist[i]->next = NULL;
}
}
return H;
}

int Hash (int x)//to 10 take remainder
{
return x% 10;
}

Position Find (int x, Hashtab H)
{
Position P;
List L;

L = H->thelist[hash (x)]; Point to the header that contains that element
P = l->next;
while (P! = NULL && p->emelent! = x)
P = p->next;
return P;
}

void Insert (int Key, Hashtab H)
{
Position Pos, Newcell;
List L;
Pos = Find (Key, H); Find out first, there's no key, it's okay
if (Pos = = NULL)
{
Newcell = (Position) malloc (sizeof (ListNode));
if (Newcell = = NULL) cout << "Out of Space" << Endl;
else//Insert the first position behind the slot
{
L = H->thelist[hash (Key)];
Newcell->next = l->next;
Newcell->emelent = Key;
L->next = Newcell;
}
}
}

void Delete (int Key, Hashtab H)
{
 position p, Tmpcell;
 list L;
 p = Find (Key, H);
 if (p = = NULL)
  cout << "not find the" << Key << Endl;
 else
 {
  l = H->thelist[hash (Key)];
  p = L;
  while (P->next! = NULL && p->next->emelent! = key)   //Find a precursor node for Key
   {
   p = p->next;
  }
  //
  tmpcell = p->next;
  p->next = tmpcell->next;
  free (Tmpcell);
 }

}
int main ()
{
Hashtab H = inittable (11);
cout << h->thelist[9]->emelent << Endl;
Insert (1, H);
Insert (4, H);
Insert (9, H);
Insert (+, H);
Insert (+, H);
Insert (H);
Insert (H);
Delete (+, H);

cout << h->thelist[9]->next->next->emelent << Endl;
return 0;
}

Hash, link method to resolve conflicts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.