【list】可用于哈夫曼树的一种建树选数的链表方法替代堆

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int a[]={1,2,3,4,5,6,7,8,9};
struct node
{
node(int xx):x(xx),next(NULL){}
int x; 
 node *next;
};
void fun()
{
 
}
int main()
{
node *head=new node(0);
node *p=head;
for(int i=0;i<9;i++)
{
node *add=new node(a[i]);
p->next=add;
p=add;
}

 node *q=head;
while(q)
{
cout<<q->x<<" ";
q=q->next;
}
cout<<endl;
  
 p=head->next;
q=p->next;
while(p->next!=NULL)
{

node *add=new node(p->x+q->x);
node  *qq=q; 
 while(add->x > qq->x && qq->next!=NULL)
     {
      qq=qq->next;
     }
  add->next=qq->next;
  qq->next=add;
  cout<<add->x<<endl;
p=q->next;
q=p->next;
}


 q=head;
while(q)
{
cout<<q->x<<" ";
q=q->next;
}
cout<<endl;
  
return 0;
}

[sts@bogon 20160731]$ g++ 0.cc
[sts@bogon 20160731]$ ./a.out
0 1 2 3 4 5 6 7 8 9                                       //链表的准备
3                                                         //新添加的结点
6
9
12
15
18
27
45 
0 1 2 3 3 4 5 6 6 7 8 9 9 12 15 18 27 45                  //最后的情况
[sts@bogon 20160731]$

当前名称:【list】可用于哈夫曼树的一种建树选数的链表方法替代堆
本文链接:https://www.cdcxhl.com/article24/pgghce.html

成都网站建设公司_创新互联,为您提供网站设计公司外贸建站电子商务ChatGPTApp开发网站收录

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设公司