skiplist的简单实现(c语言版)

#include 
#include 
#define LEVEL 4

int level_random()
{
	return rand() % LEVEL;
}

int my_random(int mode)
{
	return rand() % mode;
}

typedef struct _SkipList
{
	int key;
	int value;
	struct _SkipList* next[4]; 	
}* SkipList;

SkipList initHead()
{
	SkipList head = (SkipList)malloc(sizeof(struct _SkipList));
	head->value = 0;
	head->value = 0;
	int i = 0;
	for (i = 0; i < LEVEL; ++i)
	{
		head->next[i] = NULL;	
	}
	return head;
}

SkipList insert(int key_in, int value_in, SkipList head)
{
	int new_level = level_random();
	int i = 0;
	SkipList tmp_node = head;
	SkipList last_node = head;
	SkipList new_node = (SkipList)malloc(sizeof(struct _SkipList));
	new_node->key = key_in;
	new_node->value = value_in;
	for (i = 0; i < LEVEL; ++i)
	{
		new_node->next[i] = NULL;
	}

	for (i = 0; i <= new_level; ++i)
	{
		if (head == NULL)
		{
			head = initHead();
			head->next[i] = new_node;
		}
		else
		{
			tmp_node = head->next[i];
			last_node = head;
			while (tmp_node)
			{	
				if (key_in == tmp_node->key)
				{
					printf("The key have existed!\n");
					return head;
				}
				else if (key_in > tmp_node->key)
				{
					last_node = tmp_node;
					tmp_node = tmp_node->next[i];
				}
				else
				{
					last_node->next[i] = new_node;
					new_node->next[i] = tmp_node;
					break;
				}
			}
			if (last_node)
			{	
				last_node->next[i] = new_node;
			}
			else
			{
				head->next[i] = new_node;
			}
		}
	}
	return head;
}

void print(int level, SkipList head)
{
	if (head == NULL)
	{
		printf("List is empty, please init the head!");
		return;
	}
	SkipList tmp_node = head->next[level];
	printf("Level %d: \n", level);
	while (tmp_node)
	{
		printf("(%d, %d) ", tmp_node->key, tmp_node->value);
		tmp_node = tmp_node->next[level];
	}
	printf("\n");
}

int search(int key, SkipList head)
{
	if (head == NULL)
        {
                printf("List is empty, please init the head!");
                return;
        }

	int level = LEVEL - 1;
	SkipList start = head->next[level];
	SkipList end = NULL;
	SkipList tmp_node = head->next[level];
	SkipList next_start = NULL, next_end = NULL;
	while (level >= 0)
	{
		SkipList tmp_node = start;
		while (tmp_node != end)
		{
			printf("Search current node %d and current level %d\n", tmp_node->key, level);
			if (key == tmp_node->key)
			{
				printf("Key %d, value %d\n", key, tmp_node->value);
				return tmp_node->value;
			}
			else if (key > tmp_node->key)
			{
				next_start = tmp_node;				
			}
			else
			{
				next_end = tmp_node;
				break;
			}
			tmp_node = tmp_node->next[level];
		}
		--level;	
		start = next_start;
		end = next_end;
	}
	printf("Key %d not found!\n", key);
	return -1;
}

int main()
{
	int i = 0;
	int random_key = 0, random_value = 0;
	SkipList head = NULL;
	for (i = 0; i < 20; ++i)
	{
		random_key = my_random(100);
		random_value = my_random(5000);
		head = insert(random_key, random_value, head);
	}

	for (i = 0; i < 4; ++i)
	{
		print(i, head);		
	}

	search(37, head);

	return 0;
}

你可能感兴趣的:(源码剖析,链表,数据结构,c语言,存储)