當前位置:首頁 » 編程語言 » c語言後序

c語言後序

發布時間: 2025-09-04 16:28:26

『壹』 用c語言定義二叉樹的二叉鏈表存儲結構,完成二叉樹的建立,先序中序後序遍歷的操作,求所有葉子結點總數

#include<stdio.h>

#include<malloc.h>


typedef int ElemType;


typedef struct LNode{

ElemType data;

struct LNode *lchild,*rchild;

}LNode,*TLNode;


void create(TLNode * Tree){ //創建

ElemType e;

scanf("%d",&e);

if(e==0)

*Tree=NULL;

else{

(*Tree)=(TLNode)malloc(sizeof(LNode));

(*Tree)->data=e;

printf("input %d lchild: ",e);

create(&(*Tree)->lchild);

printf("input %d rchild: ",e);

create(&(*Tree)->rchild);

}

}


void print1(TLNode Tree){ //先序遍歷

if(Tree!=NULL){

printf("%d-",Tree->data);

print1(Tree->lchild);

print1(Tree->rchild);

}

}


void print2(TLNode Tree){ //中序遍歷

if(Tree!=NULL){

print2(Tree->lchild);

printf("%d-",Tree->data);

print2(Tree->rchild);

}

}

void print3(TLNode Tree){ //後序遍歷

if(Tree!=NULL){

print3(Tree->lchild);

print3(Tree->rchild);

printf("%d-",Tree->data);

}

}


int leaf=0; //求葉子節點數

int depth(TLNode Tree){ //深度

int s1,s2;

if(Tree==NULL)

return 0;

else{

s1=depth(Tree->lchild);

s2=depth(Tree->rchild);

if(s1==0 && s2==0) leaf++;

return (s1>s2?s1:s2)+1;

}

}


int Cnode(TLNode Tree){ //總結點

int s1,s2;

if(Tree==NULL)

return 0;

else{

s1=Cnode(Tree->lchild);

s2=Cnode(Tree->rchild);

return s1+s2+1;

}

}


void main(){

TLNode Tree;

printf("input 根節點: ");

create(&Tree);

printf("先序遍歷:");

print1(Tree);

printf("中序遍歷");

print2(Tree);

printf("後序遍歷");

print3(Tree);

printf(" 深 度:%d ",depth(Tree));

printf("總結點數:%d ",Cnode(Tree));

printf("葉子結點數:%d ",leaf);

}

『貳』 c++中後綴名.c 與.cpp的區別

.c是c語言的源程序格祥毀式,.cpp是c++語言的源程序格式。這是兩種不同的語言。

代碼的問題原因:

編譯器問題——C89和C99

C89規定,在任何執隱察行語句之前,在塊的開頭聲明所有局部變數。在C99以及C++中則沒有這個限制,即在首次使用之前,可在塊的任何位置都可以聲明變數。

因此,變數聲明放在前面就不報錯了。

(2)c語言後序擴展閱讀:

C語言是一門通用計算機編程語言,廣泛應用於底層開發。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。

C語言缺點:

1.C語言的缺點主要表現在數據的封裝性上,這一點使得C在數據的安全性上有很大缺陷,這也是C和C++的一大區別。

2.C語言的語法限制不太嚴格,對變數的類型約束不嚴格,影響程序的安全性,對數組下標越界不作檢查等。從應用的角度,灶宴茄C語言比其他高級語言較難掌握。也就是說,對用C語言的人,要求對程序設計更熟練一些。

C++是C語言的繼承,它既可以進行C語言的過程化程序設計,又可以進行以抽象數據類型為特點的基於對象的程序設計,還可以進行以繼承和多態為特點的面向對象的程序設計。

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:585
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:881
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:574
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:761
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:676
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1005
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:249
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:108
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:798
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:705