//这个接上一章的数据结构,只是修改部分函数
Item STmove(Key aKey)
{
Item tempItem = 0;
int i = 0;
int n = STcount();
int tempIndex = 0;
for (int j = 0;j < n;++j)
{
tempIndex = g_cache[0];
for (i = 0;i < n-1;++i)
{
g_cache[i] = g_cache[i+1];
}
g_cache[i] = tempIndex;
if (g_Item[g_cache[i]] == aKey)
{
tempItem = 1;
break;
}
}
if (!tempItem)
{
printf("search result failed!!");
}
return tempItem;
}
//过滤重复数据
void STfilterData()
{
int *tempCache = malloc(sizeof(&tempCache)*M);
if (!tempCache)
{
printf("filter is created failed");
}
memset(tempCache,0, sizeof(&tempCache)*M);
for (int i = 0; i < STcount(); ++i)
{
tempCache[g_cache[i]] = g_cache[i];
}
int j = 0;
memset(g_cache,0, sizeof(int)*sizeof(g_cache)/sizeof(g_cache[0]));
for (int i = 0; i < M; ++i)
{
if (tempCache[i])
{
g_cache[j++] = tempCache[i];
}
}
indexX = j;
free(tempCache);
tempCache = NULL;
}
int STdeleteRepeat(Item aItem)
{
STfilterData();
int ret = 0;
int i = 0;
int n = STcount();
int tempIndex = 0;
for (int j = 0;j < n;++j)
{
tempIndex = g_cache[0];
for (i = 0;i < n-1;++i)
{
g_cache[i] = g_cache[i+1];
}
if (tempIndex == aItem)
{
ret += 1;
g_cache[i] = 0;
break;
}
g_cache[i] = tempIndex;
}
if (!ret)
{
printf("delete failed");
}
indexX -=ret;
return ret;
}
Item STselect(int aSelect)
{
if (aSelect < 0||aSelect >= M)
{
return 0;
}
//冒泡排序,因为总数<30
for (int i = 0;i < STcount();++i)
{
for (int j = 0;j < STcount()-i-1;++j)
{
if (g_cache[j] > g_cache[j+1])
{
Item temp = g_cache[j];
g_cache[j] = g_cache[j+1];
g_cache[j+1] = temp;
}
}
}
if (g_Item[g_cache[aSelect]])
{
printf("select successfull");
}
else
{
printf("select failed");
}
return g_cache[aSelect];
}