跳表

/*
     调表:有序链表
     一个节点有点个指针,然后每个结点除了本来的链式结构,还用链表连接了其他的结构体数据
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct   dataType
{
      int   first;
      char   second[20];
};

struct   Node
{
     struct   dataType   data;
     struct    Node*next;
}
//创造一个新的普通节点
struct   Node*createNode(struct   dataType  data)
{
     struct   Node*newNode = (struct   Node*)malloc(sizeof(struct   Node));
     newNode->data = data;
     newNode->next =  NULL;
	return    newNode;
}

//跳表中特有的结点的样子
struct   skipListNode
{
      struct    dataType   data;  //数据
      struct    Node*firstNode;  //存储哈希冲突的元素
      struct    skipListNode* next; //指向下一个跳表特有的个体的指针
}//跳表的结点
struct   skipListNode*createSkipListNode(struct   dataType   data)
{
       struct     skipListNode*newNode = (struct    skipListNode*)malloc(struct  skipListNode);
       newNode->data = data;
       newNode->next = NULL;
       newNode->firstNode = NULL;
       return  NULL;
}

//跳表结构描述
struct    skipList
{
    struct   skipListNode* headNode;    用一个节点表示整个跳表结构
    int   sizeSkip;
    int  divisor;
};

//该写哈希还是写哈希
struct     skipList* createSkipList(int  divisor)
{
	struct   skipList* list = (struct   skipList*) malloc (sizeof(struct   skipList));
	list->headNode = NULL;
	list->sizeSkip = 0;
	list->divisor = divisor;
	return  list;
}

//插入
void   inserNodeBySort(struct   skipList* list,struct  dataType  data)
{
      //先用取余法求哈希地址
      int   dataHashPos == data.first%list->divisor;
      struct    skipListNode*newSkipNode= createSkipListNode(data);
      /*链表是空的*/
      if(list->headNode == NULL)
      {
	       list->headNode = newSkipNode;//是空的,就创造一个
	       list->sizeSkip++;
	  }
	  else  //有元素的时候,就做一个有序链表
	  {
	        /*链表的指定位置插入*/
	        struct   skipListNode*pMove = list->headNode;
	        struct   skipListNode*pMoveFront = NULL;
	        /*插在头结点,插入元素的哈希地址比头结点小*/
	        if(pMove->data.first%list->divisor>dataHashPos)
	        {
	              newSkipNode->next = list->headNode;
	              list->headNode = newSkipNode;
	              list->sizeSkip++;
	        }
	        /*要找到一个比插入元素的哈希地址大的位置*/
	        else
	        {
	            while(pMove->data.first)%(list->divisor)<(dataHashPos)
	            {
	                 pMoveFront = pMove;
	                 pMove = pMoveFront-next;
	                 //如果没找到
	                 if(pMove==NULL)
	                 break;
	            } 
	            //新节点的插入考虑哈希冲突,以及关键字相同的时候的处理
	            if(pMove != NULL && (pMove->data.first%list->divisor) == dataHashPos)
	            {
	                 //如果存在位置,键相同,采用覆盖的方式 
	                 if(pMove->data.first == data.first)
	                 {
	                      strcpy(pMove->data.second,data.second); 
	                 }
	                 else
	                 {
	                       struct  Node*newNode = createNode(data);
	                       struct   Node*ppMove = pMove->firstNode;
	                       if(ppMove == NULL)
	                       {
	                             //无表头的表头法插入 
	                             newNode->next = pMove->firstNode;
	                             pMove->firstNode = newNode;
	                             list->sizeSkip++;
	                       }
	                       else  //找合适位置插入冲突的元素
	                       {
	                               //pMove是跳表的节点
	                               while(pMove->data.first!=data.first)
	                               {
	                                     ppMove = ppMove->next;
	                                     if(ppMove == NULL)
	                                     {
	                                           break; 
	                                     }
	                               }
	                               if(ppMove == NULL)
	                               {
	                                    newNode->next = pMove->firstNode;
	                                    pMove->firstNode= newNode;
	                                    list->sizeSkip++     
	                               }
	                               else
	                               {
	                                     //存在相同键的冲突元素,采用覆盖
	                                     strcpy(ppMove->data.second,data.second); 
	                               }
	                       }
	                 }
	            }
	            else
	            {
	                 //考虑不冲突的情况
	                 pMoveFront->next = newSkipNode;
	                 newSkipNode->next = pMove;
	                 list->sizeSkip++;
	            }
	        }
	  }
}

void   printList(struct   skipList*list)
//打印跳表结点和普通结点的数据
{
     struct   skipListNode* pMove = list->headNode;
     while(pMove)
     {
           printf("(%d  %s)\t",pMove->data.first,pMove->data.second);
           struct   Node*ppMove = pMove->firstNode;
           while(pMove)
           {
                printf("(%d  %s)\t",ppMove->data.first,ppMove->data.second);
                ppMove = ppMove->next; 
           }
           pMove = pMove->next;
           printf("\n");
     }
}

int  main()
{
     struct  skipList* list = createSkipList(10);//产生十个地址
     struct    dataType   array[7] = 
     for(int  i = 0;i<7;i++)
     {
         insertNodeBySort(list,array[i]);
     }
     printList(list);

    system("pause");
	return   0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 PostgreSQL 数据库中,并没有内置的跳表(Skip List)索引实现。PostgreSQL 提供了多种索引类型,如B树索引、哈希索引、GiST索引和GIN索引等,但没有直接支持跳表的索引类型。 B树索引是 PostgreSQL 中最常用的索引类型之一。它适用于范围查询和等值查询,并且可以保持数据有序性。B树索引在处理数据块的平衡性和查询效率方面具有很好的性能。 除了B树索引之外,PostgreSQL 还提供了其他类型的索引用于特定的场景。例如,哈希索引适用于等值查询,可以提供快速的哈希查找;GiST 索引(通用搜索树)和 GIN 索引(通用倒排索引)适用于全文搜索和复杂的匹配查询。 虽然 PostgreSQL 不提供内置的跳表索引实现,但是你可以使用扩展或自定义索引实现跳表的功能。通过编写自定义插件或使用第三方扩展,你可以在 PostgreSQL 中实现跳表索引。这需要一定的开发工作,并且需要充分测试和评估性能。 需要注意的是,自定义实现的跳表索引可能会受到 PostgreSQL 内核版本更新的影响,并且可能无法享受到 PostgreSQL 内置索引的一些优化和支持。 总之,PostgreSQL 并没有内置的跳表索引实现,但提供了其他类型的索引,如B树索引、哈希索引、GiST索引和GIN索引等,用于满足不同的查询需求。如果需要使用跳表索引,你可以考虑自定义实现或使用第三方扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值