开散列表

 

#include "stdafx.h"
//#include "test.h"
#include<iostream>
//#include<stdlib.h>
using namespace std;
//void main()
//{
// Time A;
// A.settime(23,23,23);
// A.showtime();
// 
// 
// 
// system("pause");
//
//}
#include<stdio.h>
#include<time.h>
#include<stdlib.h>

 int const INDEXBOX=7;
 int const MAXNUM=13;
 void creat_table(int);
 void print_data(int);

 struct node
 {
    int value;
    struct node *next;
 };

 typedef struct node node;
 typedef node *link;

  node index_table[INDEXBOX];

  void main()
  { int i;
     int data[MAXNUM];
     srand(time(NULL));
     for (int i=0; i<INDEXBOX; i++)
     {
 index_table[i].value=-1;
 index_table[i].next=NULL;
     }
     printf("Former Data:\n");
     for ( i=0; i<MAXNUM; i++)
     {
 data[i]=rand()%30+1;
 printf("[%2d] ",data[i]);
 if ((i+1)%8==0) putchar('\n');
     }
     putchar('\n');
     printf("Hash List:\n");
     for ( i=0; i<MAXNUM; i++)
       creat_table(data[i]);
     for (i=0; i<INDEXBOX; i++)
       print_data(i);
     putchar('\n');
  system("pause");
  }

  void creat_table(int value)
  {
     link new_node;
     link current_node;
     int hash;
     hash=value%INDEXBOX;
     new_node=(link)malloc(sizeof(node));
     current_node=(link)malloc(sizeof(node));
     new_node->value=value;
     new_node->next=NULL;
     *current_node=index_table[hash];
     if (current_node->next==NULL)
 index_table[hash].next=new_node;
     else
       while (current_node->next!=NULL)
   current_node=current_node->next;
       current_node->next=new_node;
  }

  void print_data(int value)
  {
     link head;
     int i=0;
     head=index_table[value].next;
     printf("%2d\t",value);
     while (head!=NULL)
     {
 printf("[%2d]",head->value);
 printf("-");
 i++;
 if (i%8==7)
   putchar('\n');
 head=head->next;
     }
     printf("\b\n");
 
  }

 

你可能感兴趣的:(struct,list,table,null,System,include)