当前位置:编程学习 > C/C++ >>

求数据结构(C语言版)题集实习题答案

是第二章上机实习题答案

清华严蔚敏版

要求详细答案,给全部代码追加100分
答案:哈夫曼编/译码器
#include <stdio.h>
#define MAX 1000
#define MAXSYMBS 30
#define MAXNODE 59

typedef struct
{
int weight;
int flag;
int parent;
int lchild;
}huffnode;

typedef struct
{
int bits[MAXSYMBS];
int start;
}huffcode;


main()
{
huffnode huff_node[MAXNODE];
huffnode huff_code[MAXSYMBS],cd;
int i,j,m1,m2,x1,x2,n,c,p;
char symbs[MAXSYMBS],symb;
clrscr();
printf("please input the leaf num of tree:\
");
scanf("%d",&n);
for (i=0;i<2*n-1;i++)
{

buff_node[i].weight=0;

buff_node[i].parent=0;

buff_node[i].flag=0;

buff_node[i].lchild=-1;

buff_node[i].rchild=-1;

}
printf("please input the weight of every leaf\
");
for (i=0;i<n;i++)
scanf("%d",&huff_node[i].weight);
for (i=0;i<n-1;i++)
{

n1=m2=MAX;

x1=x2=0;

for (j=0;j<n+i;j++)

{


if (huff_node[j].weight<m1&&huff_node[j].flag==0)


{


m2=m1;


x2=x1;


m1=huff_node[j].weight;


x1=j;


}


else if (huff_node[j].weight<m2&&huff_node

[j].flag==0)


m2=huff_node[j].weight;


x2=j;

}
}

huff_node[x1].parent=n+i;

huff_node[x2].parent=n+i;

huff_node[x1].flag=1;

huff_node[x2].flag=1;

buff_node[n+i].weight=buff_node[x1].weight+buff_node

[x2].weight

buff_node[n+i].lchild=x1;

buff_node[n+i].rchild=x2;
}
for (i=0;i<n;i++)
{
cd.start=n;
c=i;
p=huff_node[c].parent;
while(p!=0)
{
if (huff_node[p].lchild==c)

cd.bits[cd.start]=0;

else

cd.bits[cd.start]=1;
cd.start=cd.start-1;
c=p;
p=huff_node[p].parent;
}
cd.start++;
for (j=cd.start;j<=n;j++)
huff_code[i].bits[j]=cd.bits[j];
huff_code[i].start=cd.start;
}
puts("the hafman code are:");
for (i=0;i<n;i++)
{
for (j=huff_code[i].start;j<=n;j++)
printf("%10d,huff_code[i].bits[j]");
printf("\
");
}
puts("press any key to quit……");
getch();
}
约瑟夫环
#include<stdio.h>
typedef struct node
{int num;
struct node *next;
}linklist;
linklist *creat(head,n)
linklist *head;
int n;
{linklist *s,*p;
int i;
s=(linklist *)malloc(sizeof(linklist));
head=s;
s->num=1;
p=s;
for(i=2;i<=n;i++)
{s=(linklist *)malloc(sizeof(linklist));
s->num=i;
p->next=s;
p=s;
}
p->next=head;
return head;
}
linklist *select(head,m)
linklist *head;
int m;
{linklist *p,*q;
int i,t,n;
p=head;
t=1;
q=p;
do
{p=q->next;
t=t+1;
if(t%n==0)
{printf("%4d",p->num);
q->next=p->next;
free(p);
}
else q=p;
}while(q==p);
head=p;
return(head);
}
main()
{int n,m;
linklist *head;
printf("input the total number\
");
scanf("%d",&n);
printf("input the number to call:\
");
scanf("%d",&m);
creat(head,n);
select(head,m);
printf("the last one :is\
",head->num);
}
不知
什么东东?

上一个:求C语言完整代码下载,一定是C语言,不要是C++啊!
下一个:(C语言)ASCII码前32个控制字符的含义?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,