溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

【list】 可用于哈夫曼樹(shù)的一種建樹(shù) 選數(shù) 的鏈表方法 替代堆

發(fā)布時(shí)間:2020-07-25 05:56:03 來(lái)源:網(wǎng)絡(luò) 閱讀:329 作者:wzdouban 欄目:編程語(yǔ)言
#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                                       //鏈表的準(zhǔn)備
3                                                         //新添加的結(jié)點(diǎn)
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]$


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI