Server for Information Technologies ������ ��������������
������� �������������� ����������
(095) 932-9212, 932-9213, 939-0783
E-mail: [email protected]
������ �������� ����(!) ������������� ���������� CIT Forum CD-ROM

HSEARCH(3C)

��������
hsearch, hcreate, hdestroy - ���������� ���-��������� ������

���������

	#include <search.h>
	
	ENTRY *hsearch (item, action)
	ENTRY item;
	ACTION action;
	
	int hcreate (nel)
	unsigned nel;
	
	void hdestroy ( )

��������
������� hsearch ������������� ��� ���������� ������ � ���-������� � ������������ � ����������, ��������� � ����� �. �����: ��������� ���������������� ��� ���. �. 3. ����������, �����. - �.: ���, 1978. ������ 6.4, �������� D.

������� hsearch ���������� ��������� ������ ������� �� ������� ������. �������� item - ��� ��������� ���� ENTRY (������������ �� ���������� ����� <search.h>), ���������� ��� ���������: item.key ��������� �� ������������ ����, � item.data ��������� �� ����� �������������� ������, ��������������� � ���� ������. ��������� �� ���������� �����, �������� �� �����������, ������� ��������������� � ���� "��������� �� ������". �������� action ����� ��� ACTION � ������ ������ �������� � ������ ���������� ������: �������� ENTER ��������, ��� ������� ������� ������� ��������� � �������; �������� FIND ��������, ��� � ������ ������� ����� ������� ������ ��������� NULL.

������� hcreate �������� ����������� ���������� ������������ ��� ������� � ������ ���������� ����� �������������� ������� hsearch. ��������� ���������� nel �������� ��������� ������������ ���������� ��������� �������. ��� ����� ����� ����� � �������, ����� ��������� ������� ����� ������.

������� hdestroy ����������� ������� ������, �� ������� ���� ������� ����� ��������� ����������� ����� ������� �������� ������� hcreate.

����������
������� hsearch ���������� �������� ��������� � ����������������� ���-��������. �������� ����� ������� ������������� � ������ �����������, ������� ����� �������, ���������� hsearch � ������������ ��� ������������� ��������� ����:
DIV ������ ����������������� ���-������� ������������ ������� �� ������� �� ������ ���-�������.
USCR ��� ������ �������� ������� ���������, ��������������� �������������. ������� ������ ���������� hcompar � ����� ���� ������� ������� strcmp [��. string(3C)].
CHAINED ��� ���������� �������� ������������ ������ ���������. ���� ���������� ��� �����, �� ���������� ���������� ����� ��������� �����:
START �������� ����� �������� � ������ ������ ��������� (�� ��������� - � �����).
SORTUP ������������ ������ ��������� ���������������� �� ����� � ������� �����������.
SORTDOWN ������������ ������ ��������� ���������������� �� ����� � ������� ��������.

����� ����, ���� ����� ������������� ��� ��������� ���������� ������ (-DDEBUG) � ��� ��������� �������� ������� � ���������� ������� (-DDRIVER). ��� ��������� ����� ��������� ���������� ������� ���������� � ��������� ������ �������.

������
��������� ��������� ��������� ������: ������� �������� � ��� �����, � ��������� �� � ���-�������, ���������� ���������. ����� ����������� ������� ��������, ��������� � ��������������� ��������������� �������� ���-�������.

	#include <stdio.h> 
	#include <search.h> 

	struct info {         /* �������������� ����������, ��- 
	                         ������������� � ������ */ 
	  int age, room; 
	}; 

	#define NUM_EMPL 5000 /* ���������� ��������� � ������� 
	                         ������ */ 

	main () 
	{ 
	/* ������ ��� ���������� ������� �������� */ 
	  char string_space [NUM_EMPL*20]; 

	/* ������ ��� ���������� ���������� � �������� */ 
	  struct info info_space [NUM_EMPL]; 

	/* ��������� �� ��������� ����� � ������� ������� */ 
	  char *str_ptr = string_space; 

	/* ��������� �� ��������� ����� � ������� �������� */ 
	  struct info *info_ptr = info_space; 

	  ENTRY item, *found_item, *hsearch (); 

	  char name_to_find [30]; /* ������� ��� */ 
	  int i; 

	/* ������� ������� */ 
	  (void) hcreate (NUM_EMPL); 

	/* ���� ������ �������� ���������� */ 
	  while (scanf ("%s%d%d", str_ptr, &info_ptr->age, 
	          &info_ptr->room) != EOF && i++ < NUM_EMPL) { 

	/* ������������ ������� ������� */ 
	    item.key = str_ptr; 
	    item.data = (char *) info_ptr; 
	    str_ptr += strlen (str_ptr) + 1; 
	    info_ptr++; 

	/* ��������� ������� � ������� */ 
	    (void) hsearch(item, ENTER); 
	  }; 

	/* ������ � ������� */ 
	  item.key = name_to_find; 
	  while (scanf ("%s", item.key) != EOF) { 

	    if ((found_item = hsearch (item, FIND)) != NULL) { 

	    /* ���� ������� ������ � ������� */ 
	      (void) printf ("found %s, age= %d, room= %d\n", 
	         found_item->key, 
	         ((struct info *) found_item->data)->age, 
	         ((struct info *) found_item->data)->room); 
	     
	    } else { 
	    /* ���� ������� �� ������ � ������� */ 
	      (void) printf ("no such employee %s\n", 
	         name_to_find) 
	    } 
	  } 
	} 

��. �����
bsearch(3C), lsearch(3C), malloc(3C), malloc(3X), string(3C), tsearch(3C).

�����������
������� hsearch ���������� ������ ��������� NULL, ���� �������� ���������� action ����� FIND � ������� �� ����� ���� ������ ��� ���� �������� ���������� action ����� ENTER � ������� ���������.

���������������
������� hsearch � hcreate ���������� ������� malloc(3C) ��� ��������� ������.

�����������
� ������ ������ ������� ����� ���� ������� ������ ���� ���-�������.
Comments: [email protected]
Designed by Andrey Novikov
Copyright © CIT