lua Table的插入简单实现

</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的插入操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值