S
S
sddvxd2018-12-22 00:53:55
C++ / C#
sddvxd, 2018-12-22 00:53:55

What do the parts of this code mean?

Hello!
I apologize that without the code tag (I write the code on a virtual machine)
The function in the picture looks for a name in an array of pointers to a structure of type name* table[TBLSZ].
If a name is found, it returns this double value. Something like variables, for a calculator
The code is an example and its author is not me, I did not understand a few things in it:

  • Why take the remainder of the division from the hash
  • How does this relationship work in the form of a list of variables
  • Why is nn->next assigned to table[ii]

In general, I did not understand the principle of linking this list. Please explain why this is done. Lines marked with breakpoint
breakpoints
5c1d60f09d03a491674048.png
struct name {
  char* string;
  name* next;
  double value;
};

const TBLSZ = 23;
name* table[TBLSZ];

name* look(const char* p, int ins =0)
{
  int ii = 0;        // хэш-код
  const char* pp = p;
  while (*pp) ii = ii<<1 ^ *pp++;
  if (ii < 0) ii = -ii;
  ii %= TBLSZ;

  for (name* n=table[ii]; n; n=n->next)    // поиск
      if (strcmp(p,n->string) == 0) return n;

  if (ins == 0) error("имя не найдено");

  name* nn = new name;                     // занесение
  nn->string = new char[strlen(p)+1];
  strcpy(nn->string,p);
  nn->value = 1;
  nn->next = table[ii];
  table[ii] = nn;
  return nn;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2018-12-22
@sddvxd

Stroustrup has everything written
www.tdoc.ru/c/programming/cpp/cpp-language-straust...
List www.intuit.ru/studies/courses/648/504/lecture/11456

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question