</pre><pre name="code" class="cpp">#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define gnext(n) (n)->next
typedef struct Node{
int key;
char value;
struct Node *next;
} Node;
Node *node;
Node * mainposition(Node *key)
{
return &(node[key->key + 3]);
}
Node * gerfreepos()
{
int i;
for (i = 19; i >= 0; i--)
{
if (node[i].key == NULL)
return &node[i];
}
return NULL;
}
void * push(Node *key)
{
Node *mp, *othern, *n;
mp = mainposition(key);
if (mp->key != 0)
{
othern = mainposition(mp);
n = gerfreepos();
if (othern != mp)
{
while (gnext(othern) != mp) othern = gnext(othern);
gnext(othern) = n;
*n = *mp;
gnext(mp) = NULL;
}
else {
gnext(n) = gnext(mp);
gnext(mp) = n;
mp = n;
}
}
mp->value = key->value;
mp->key = key->key;
}
void initNode ()
{
int i;
for (i = 0; i < 20; i++)
{
node[i].key = NULL;
node[i].next = NULL;
}
}
int main()
{
int xx ;
Node *node1 = (Node *) malloc(sizeof(Node));
Node *node1_1 = (Node *) malloc(sizeof(Node));
Node *node16 = (Node *) malloc(sizeof(Node));
// printf("%d",sizeof(Node));
node = (Node *) malloc(sizeof(Node)*20);
initNode();
node1->key = 1;
node1->value = 'a';
node1->next = NULL;
node1_1->key = 1;
node1_1->value = 'b';
node1_1->next = NULL;
node16->key = 16;
node16->value = 'x';
node16->next = NULL;
push(node1);
push(node1_1);
push(node16);
printf("%c",node[4].next->value);
printf("%c\n",node[18].value);
printf("%c\n",node[19].value);
scanf("%d",&xx);
return 0;
}
模拟lua Table的插入操作