请教:关于单链表和赫夫曼树算法的2道题

125857fht 2008-12-09 04:38:58
1、2个带头结点的单链表求交集的算法。

2、求赫夫曼树的WPL算法。
(都是用c语言写,并说明思路加注释)
...全文
154 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
UncleQiong 2008-12-14
  • 打赏
  • 举报
回复
我用C语言写的。LZ参考下

// 赫夫曼编码.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "malloc.h"
#include "string.h"
typedef struct
{
unsigned int weight;
unsigned int parent,lchild,rchild;
}HTNode,*HuffmanTree;

typedef char **HuffmanCode;

void Select(HuffmanTree HT,unsigned int n,unsigned int &s1,unsigned int &s2)
{
HuffmanTree p;
p=HT+1;
unsigned int i;
unsigned int min,max;

for(i=1,p=HT+1,max=p->weight;i<=n;i++,p++)
{
if(p->weight>max)
max=p->weight;
}


for(i=1,min=max+1,p=HT+1;i<=n;p++,i++)
{
if(p->parent==0)
{
if(p->weight<min)
{
min=p->weight;s1=i;
}
}
}
for(i=1,min=max+1,p=HT+1;i<=n;p++,i++)
{
if(p->parent==0&&i!=s1)
{
if(p->weight<min)
{
min=p->weight;
s2=i;
}
}
}
}

void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC, int *w, unsigned int n)
{
HuffmanTree p;
int c,f;
if(n<=1)
return;
int m=2*n-1;//m表示有m个节点
int i;
unsigned int s1,s2;//最小的两个
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));//忽略0号,所以设置m+1个空间
for(p=HT+1,i=1;i<=n;p++,i++,w++)
{
p->weight=*w;
p->lchild=0;
p->rchild=0;
p->parent=0;
}
for(i=n+1;i<=m;i++,p++)
{
p->weight=0;
p->lchild=0;
p->rchild=0;
p->parent=0;
}
for(i=n+1;i<=m;i++)
{
Select(HT,i-1,s1,s2);
HT[s1].parent=i;
HT[s2].parent=i;
HT[i].lchild=s1;
HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
HC=(HuffmanCode)malloc((n+1)*sizeof(char *));
char *cd=(char*)malloc(n*sizeof(char));
cd[n-1]='\0';
for(i=1;i<=n;i++)
{
int start=n-1;
for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
if(HT[f].lchild==c)
cd[--start]='0';
else
cd[--start]='1';
HC[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
free(cd);
}




int main(int argc, char* argv[])
{
HuffmanTree HT;
HuffmanCode HC;
int *w;
w=(int *)malloc(8*sizeof(int));
w[0]=7;
w[1]=19;
w[2]=2;
w[3]=6;
w[4]=32;
w[5]=3;
w[6]=21;
w[7]=10;
HuffmanCoding(HT,HC,w,8);
for(int i=1;i<=8;i++)
{
printf("%2d->",w[i-1]);
printf("%s\n",HC[i]);
}
return 0;
}
  • 打赏
  • 举报
回复
mark
candyice 2008-12-10
  • 打赏
  • 举报
回复
那不就是很简单的嘛,一会儿写代码
125857fht 2008-12-10
  • 打赏
  • 举报
回复
回复:candyice

是的,谢谢关注。

补充:第一题:求出2个单链表的交集放入第3个单链表中。
superzbo 2008-12-10
  • 打赏
  • 举报
回复
不会写C代码。。。。。。LZ收下给点分吧。。。。。
superzbo 2008-12-10
  • 打赏
  • 举报
回复

class HaffManTree
{
//2、求赫夫曼树的WPL算法
private int[] Weight ={ 5 ,29 ,7 ,8 ,14 ,23 ,3 ,11 };
private List<TreeNode> Forest;//所有的haffman树上的有值的节点
private List<TreeNode> FinishedBuildNodes; //所有已经建立好关系的节点
public void InitialAll()
{
this.Forest = new List<TreeNode>();
this.FinishedBuildNodes = new List<TreeNode>();
for(int i=0;i<this.Weight.Length;i++)
{
TreeNode node=new TreeNode();
node.Value=Weight[i];
this.Forest.Add(node);
}
}

public void BuildTree()
{
while (this.Forest.Count > 1)
{
BuildSmallestTwoNodes();
}
FinishedBuildNodes.Add(Forest[0]);
}

private void BuildSmallestTwoNodes()
{
for(int i=1;i<Forest.Count;i++)
{
if (Forest[0].Value>Forest[i].Value)
{
swap(0 ,i);
}
}
for (int i = 2 ; i < Forest.Count ; i++)
{
if (Forest[1].Value > Forest[i].Value)
{
swap(1 ,i);
}
}

TreeNode node = new TreeNode();
node.Value = Forest[0].Value + Forest[1].Value;
node.IsRelationNode = true;

//最小两个移除
node.LeftChild = Forest[0];
FinishedBuildNodes.Add(Forest[0]);
Forest.RemoveAt(0);
node.RightChild = Forest[0];
FinishedBuildNodes.Add(Forest[0]);
Forest.RemoveAt(0);
//加入新的node
Forest.Add(node);
}

private void swap(int p ,int i)
{
TreeNode node = Forest[p];
Forest[p] = Forest[i];
Forest[i] = node;
}
}
class TreeNode
{
public bool IsRelationNode=false;
public Int32 Value;
public TreeNode LeftChild;
public TreeNode RightChild;
}
125857fht 2008-12-10
  • 打赏
  • 举报
回复
好的,谢谢candyice

另道题呢,请哪位大侠解答
candyice 2008-12-10
  • 打赏
  • 举报
回复
/*------------------------------------------
求链表A和链表B的交集C,单链表结构
===========================================*/

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

//定义节点结构
typedef struct linknode
{
int data;
struct linknode *next;
}node;

//创建带头节点的链表,数据由键盘输入
void creatlist(node *head)
{
node *r,*s;
int x;
r=head;
printf("input some member of the aggregate:\n");
scanf("%d",&x);
while(x!=0)
{
s=(node*)malloc(sizeof(node));
s->data=x;
r->next=s;
s->next=NULL;
r=s;
scanf("%d",&x);
}
}
//删除链表A中重复的节点
void DelNode(node *head)
{
node *r,*s,*temp,*pre;
s = (node *)malloc(sizeof(node));
pre = (node *)malloc(sizeof(node));
r = head->next;
while(r)
{

s = r;
while(s->next != NULL)
{
pre = s;
s = s->next;
if(r->data == s->data)
{
temp = s;
pre->next = s->next;
s = pre;
delete(temp);
temp = NULL;
}

}
r = r->next;
}
}
//求两个链表A B的交集,存放于了链表C中,并打印出来
void Merge()
{
node *pa,*pb,*pc,*heada,*headb,*headc,*s;
heada=(node*)malloc(sizeof(node));
headb=(node*)malloc(sizeof(node));
creatlist(heada);
creatlist(headb);
DelNode(heada);
pa=heada->next;
headc=(node*)malloc(sizeof(node));
pc=headc;
//依次检查表A中的各个元素
while(pa != NULL)
{
pb=headb->next;
//依次扫描B表中的各个元素,从第一个元素开始
while(pb != NULL)
{
if(pb->data == pa->data)
{
s=(node*)malloc(sizeof(node));//创建新的节点
s->data=pa->data;//节点数据为当前A表中所指向的元素,也就是存在于A表,而不存在于B表中的元素
pc->next=s;//pc指针指向的节点的next指针指向后继节点s
s->next=NULL;//置该链表尾指针为NULL
pc=s;//指针指向节点s
}
pb=pb->next;
}
pa=pa->next;//指针指向A表中的下一个元素
}
if(pc==headc)
{
pc=NULL;
}
else
{
pc->next=NULL;//置链表C的尾指针为NULL
pc = headc->next;
}

if(pc==NULL)
printf("the result is NULL\n");
if(pc!=NULL)
printf("the result is:\n");
while(pc)
{
printf("%d\t",pc->data);
pc=pc->next;
}

}

//主程序
void main()
{
Merge();
}
candyice 2008-12-09
  • 打赏
  • 举报
回复
单链表求交集是不是求出两个链表中相同的节点,只要是相同的,不管在逻辑上是否相连的,是这个意思吗?

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧