首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

实现leftisttree的c程序,时间紧迫,明天due

2013-07-09 
求助实现leftisttree的c程序,时间紧迫,明天due#include stdio.h#include math.h#include time.h#inc

求助实现leftisttree的c程序,时间紧迫,明天due
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <malloc.h>
#include <stdlib.h>



struct lnode
{
int s,value;
struct lnode* left;
struct lnode* right;
};

struct qnode
{
  struct lnode* keynode;
  struct qnode* next;
};

struct queue
{
  struct qnode* front;
  struct qnode* end;
  int size;
};

/*File globals*/

File *input;
File *output;

/****************Implement queue in order to output the leftist tree**********************/

/*Create a queue named Q*/
struct queue* Q;





/*Initialize a queue*/
struct queue* InitialQueue()
{
  struct queue* q;
  if (q!=NULL)
  {
    q->front=NULL;
    q->end=NULL;
    q->size=0;
  }
  return q;
}




/*See if a queue is empty*/   
int IsEmpty(struct queue*q)
{
  if(q->front==NULL&&q->end==NULL&&q->size==0)
    return 1;
  else
    return 0;
}
/*Push a new node in the queue*/
void Enqueue(struct lnode* knode)
{
struct qnode* temp=(struct qnode*)malloc(sizeof(struct qnode));
temp->keynode=knode;
temp->next=NULL;

if(IsEmputy(Q))
{
Q->front=temp;
}
else
{
Q->end->next=temp;
}
Q->end=temp;
Q->size=Q->size+1;
}
/*Pop a node from the queue*/
struct lnode* Dequeue()
{
struct lnode* temp;
if (IsEmputy(Q)!=1)
{
temp=Q->front->keynode;
Q->front=Q->front->next;
return temp;
}
  
}
/*************************End of queue**********************************/


/************************Leftist tree***********************************/


struct lnode* lroot;

struct lnode* InitializeRoot()
{
struct lnode* temp;
temp=NULL;
return temp;
}

void insert (int key)


{
struct lnode* temp;
temp=(struct lnode*)malloc(sizeof(struct lnode));
temp->value=key
temp->s=1;
temp->left=NULL;
temp->right=NULL;
lroot=merge(lroot,temp);
}

struct lnode* merge(struct lnode*n1, struct lnode*n2)
{
struct lnode* temp;
if((n1==NULL)&&(n2!=NULL))
return n2;
else if(n2==NULL)
return n1;
else if(n1->value>n2->value)
{
temp=n1;
n1=n2;
n2=temp;
}
n1->right=merge(n1->right,n2);

if((n1->left==NULL)&&(n1->right!=NULL))
{
n1->left=n1->right;
n1->right=NULL;
n1->s=1;
}
else
{
if(n1->left->s<n1->right->s)
{
temp=n1->left;
n1->left=n1->right;
n1->right=temp;
}
n1->s=n1->right->s+1;
}
return n1;
}

void deletemin()
{
if(lroot!=NULL)
{
lroot=merge(lroot->left,lroot->right);
}
}



void random()
{
clock_t start, time;
int a[5000];
int temp,i;
for (i=0;i<5000;i++)
{
a[i]=i;
}

for (i=0;i<5000;i++)
{
int r=rand()%(5000-i)+i;
temp=a[i];
a[i]=a[r];
a[r]=temp;
}

start=clock();

for (i=0;i<5000;i++)
{
temp=a[i];
insert(temp);
}

time=start-clock();
printf("The running time for leftist tree is:%f",time);

}

void main()
{
random();
}


[解决办法]

引用:
c:\ads\123\123\123.c(120) : warning C4013: “merge”未定义;假设外部返回 int


第一个warning才是根源,编译器假设merge返回int,之后lroot=merge(lroot->left,lroot->right);这句才有了问题。所以应该先检查为什么编译器认为没有merge函数。在你贴出来的错误信息之前,还有别的错误吗?

[解决办法]
引用:
而且我输出lroot的value,全是特别大的负数,肯定不对



所以你的重点应该在保证前面函数的正确性上,最后关于输出格式:

1. time=start-clock();

应该反过来,现在这样得到的time为负。

2. clock_t类似与整数,格式化输出应为“%llu”,我用“%f”即使是time不为0时,打印出的结果也为0.

祝好运。

热点排行