键索数组引符号表

来源:互联网 发布:linux 映射内网端口 编辑:程序博客网 时间:2024/05/21 16:59

键索数组引符号表使用数组模拟符号表

使用固定数组大小,为了便于调试。

代码是参照c算法的书上

typedef int Key;

typedef int Item;

#define ITEMNULL 0

Item st[10];

int M;

#define key(A)  (A)

void STinit(int n)

{

   M = n;

}


int STcount()

{

   int count =0;

   for (int i =0;i <M;++i)

    {

       if (st[i] !=ITEMNULL)

        {

            count +=1;

        }

    }

   return count;

}


void STinsert(Item aItem)

{

   st[key(aItem)] = aItem;

}


Item STsearch(Key aKey)

{

   returnst[aKey];

}


void STdelete(Item aItem)

{

   st[key(aItem)] =ITEMNULL;

}


Item STselect(int k)

{

   int i =0;

   Item tempItem =ITEMNULL;

   for (;i <M; ++i)

    {

       if (st[i] !=ITEMNULL)

        {

           if (k-- ==0)

            {

                tempItem =st[i];

               break;

            }

        }

    }

   return tempItem;

}


void STsort(void (*visit)(Item))

{

   for (int i =0;i <M;++i)

    {

       if (st[i] !=ITEMNULL)

        {

            visit(st[i]);

        }

    }

}



0 0