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--;
}
}
}