leftist (左偏堆,自动测试)

windows下运行正确,linux 却出错。

主要是这行代码的问题。

 wrapleftist  *temp = (wrapleftist *)( (int)(node)-sizeof (wrapleftist *) );

windows下写为

 wrapleftist  *temp = (wrapleftist *)( (int)(node)-4);用数字做了假定。

 

#include  <stdio.h>
#include  <stdlib.h>
#include  <time.h>

#define  NIL   99999
#define   RAND   100


typedef  struct   _leftist
{
  int  key;
  int  dis;
  struct _leftist  *left;
  struct  _leftist  *right;
}leftist;

typedef  struct  _wrapleftist
{
  struct  _wrapleftist  *next;
  leftist  real;
}wrapleftist;


wrapleftist  global[RAND];
wrapleftist  *head;
wrapleftist  *tail;

void  leftist_init( )
{
  int  i;
  for(i=0;i<RAND-1;i++)
    {
      global[i].next=&global[i+1];
    }
  head=&global[0];
  tail=&global[RAND-1];
}


leftist  *mycalloc ( )
{
  leftist  *temp=&head->real;
  temp->dis=temp->left=temp->right=NULL;
  head=head->next;
  return  temp;
}

void  myfree (leftist  *node)
{
  wrapleftist  *temp = (wrapleftist *)( (int)node-4);
  temp->next=0;
  node->left=node->right=node->key=node->dis=0;
  tail->next=temp;
  tail=temp;
  return ;
}

leftist  *newconstruct (  int  key)
{
  leftist  *obj=(leftist *)mycalloc ( );
  obj->dis=0;
  obj->key=key;
  return  obj;
}

/*
leftist  *nilstruct ( )
{
  static   leftist  *obj=NULL;
  if  (!obj )
    {
      obj=newconstruct ( NIL);
      obj->key=NIL;
      obj->dis=0;
    }
  else
    {
    }
  return  obj;
}
*/


leftist  *leftist_adjust ( leftist  *left)
{
  leftist  *tmp;
  if(!left->left)
    {
    }
  else
    {
      if(left->left->dis>= left->right->dis)
 {
   left->dis=left->right->dis+1;
   return  left;
 }
    }

  tmp=left->left;
  left->left=left->right;
  left->right=tmp;

  if(!left->right)
    {
      left->dis=0;
    }
  else
    {
      left->dis=left->right->dis+1;
    }
  return  left;
}

leftist * leftist_merge ( leftist *left ,leftist  *right)
{
  if(!left)
    {
      return  right;
    }
  if (!right)
    {
      return  left;
    }
  if (left->key < right->key)
    {
      left->right=leftist_merge ( left->right ,right );
      return   leftist_adjust (left);

    }
  else
    {
      right->right=leftist_merge  ( left ,right->right);
      return  leftist_adjust ( right );
    }
}

leftist  *leftist_pop ( leftist  **head)
{
  leftist  *result=*head;
  *head=leftist_merge  (  result->left ,result->right );
  return  result;

int main()
{
  int  i;
  int  leftist_count=0;
  leftist  *head=NULL;
  leftist  *newobj=NULL;

  leftist_init( );
  while(1)
    {

      printf("\n\ninto  insert  mode\n\n");
      while(leftist_count <  RAND *3/4)
 {
   i=rand()%RAND;
   newobj=newconstruct ( i );
   head=leftist_merge ( head, newobj );
   leftist_count++;

 }
      while(leftist_count >  RAND *1/3)
 {
   newobj=leftist_pop ( &head);
   printf("%d  ", newobj->key); 
   myfree (newobj);
   leftist_count--;
 }
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值