| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 467 人关注过本帖
标题:求改错,关于哈夫曼树的
只看楼主 加入收藏
a1074886137
Rank: 2
等 级:论坛游民
帖 子:5
专家分:10
注 册:2010-11-6
收藏
 问题点数:0 回复次数:1 
求改错,关于哈夫曼树的
构造哈夫曼树出现了这个情况,求解答啊。。。
main.obj : error LNK2001: 无法解析的外部符号 "int * a" (?a@@3PAHA)
我main函数里没有这样的符号啊,怎么回事啊。。

#include<iostream>
using namespace std;
#define m 100
struct ptree
{
    int w;
    struct ptree *lchild;
    struct ptree *rchild;
};
struct pforest
{
    struct pforest *link;
    struct ptree *root;
};
int wpl=0,count=0;
int a[];
struct ptree *hafm(int n,int *w);
void preorder(struct ptree *head);
struct pforest *inforest(struct pforest *f,struct ptree *t);


void main()
{
    struct ptree *head;
    int n,i,j,w[m];
    cout<<"please input the sum of node"<<endl;//提示输入结点数
    cin>>n;
    cout<<"please input weight of every node"<<endl;//提示输入每个结点的权值
    for(i=0;i<=n-1;i++)
        cin>>w[i];
    head=hafm(n,w);
    preorder(head);
    for(j=1;j<=count-1;j++)
        wpl+=a[count];
    cout<<"the length of the best path is WPL="<<wpl<<endl;
}


void preorder(struct ptree *head)
{
    if(head!=NULL)
    {
        cout<<head->w<<" ";
        a[count]=head->w;
        count++;
        preorder(head->lchild);
        preorder(head->rchild);
    }
}


struct ptree *hafm(int n,int *w)
{
    struct pforest *p1,*p2,*f;
    struct ptree *ti,*t,*t1,*t2;
    int i;
    f=(struct pforest *)malloc(sizeof(struct pforest));
    f->link=NULL;
    for(i=1;i<=n;i++)                          //产生n棵只有根结点的二叉树
    {
        ti=(struct ptree *)malloc(sizeof(struct ptree));//开辟新的结点空间
        ti->w=w[i-1];                          //对结点赋权值      
        ti->lchild=NULL;
        ti->rchild=NULL;
        f=inforest(f,ti);
    }
    while(((f->link)->link)!=NULL)
    {
        p1=f->link;
        p2=p1->link;
        f->link=p2->link;               //取前两棵树
        t1=p1->root;
        t2=p2->root;
        free(p1);                       //释放p1
        free(p2);
        t=(struct ptree *)malloc(sizeof(struct ptree));
        t->w=t1->w+t2->w;               //权相加
        t->lchild=t1;
        t->rchild=t2;
        f=inforest(f,t);
    }
    p1=f->link;
    t=p1->root;
    free(f);
    return t;
}


struct pforest *inforest(struct pforest *f,struct ptree *t)
{
    struct pforest *p,*q,*r;
    struct ptree *ti;
    r=(struct pforest *)malloc(sizeof(struct pforest));
    r->root=t;
    q=f;
    p=f->link;
    while(p!=NULL)
    {
        ti=p->root;
        if(t->w>ti->w)
        {
            q=p;
            p=p->link;
        }
        else
            p=NULL;
    }
    r->link=q->link;
    q->link=r;
    return f;
}
搜索更多相关主题的帖子: 哈夫曼 改错 
2010-12-06 20:28
a1074886137
Rank: 2
等 级:论坛游民
帖 子:5
专家分:10
注 册:2010-11-6
收藏
得分:0 
终于找到错误了。
是定义的时候int a[]应改为int a[m]
2010-12-07 14:25
快速回复:求改错,关于哈夫曼树的
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017088 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved