二叉樹打印葉子節點,非遞歸_使用遞歸打印鏈接列表中的備用節點

二叉樹打印葉子節點,非遞歸

Solution:

解:

Input: A singly linked list whose address of the first node is stored in a pointer, say head

輸入:一個單鏈表 ,其第一個節點的地址存儲在指針中,例如head

Output: The alternative nodes in the list

輸出:列表中的備用節點

Data structure used: Singly linked list where each node contains a data element say data, and the address of the immediate next node say next, with Head holding the address of the first node.

所使用的數據結構:單鏈列表,其中每個節點包含一個數據元素,例如data ,直接下一個節點的地址說next ,其中Head保留第一個節點的地址。

Recursive function to print alternative nodes (Pseudocode):

遞歸函數以打印備用節點(偽代碼):

Function alternate_display(temp):
Begin
IF(temp!=NULL){
Print "temp->data"
If(temp->next==NULL)
temp=NULL;
Else
alternate_display(temp->next->next);
}
Else
return control to the main function
End IF-ELSE
End FUNCTION

C Implementation:

C實現:

#include <stdio.h>
#include <stdlib.h>
typedef struct list //node structure
{
int data;
struct list *next;
}node;
//recursive function
void alternatedisp(node *temp); 
int main(){
node *head=NULL,*temp,*temp1;
int choice,count=0,key;
//Taking the linked list as input
do
{
temp=(node *)malloc(sizeof(node));
if(temp!=NULL)
{
printf("\nEnter the element in the list : ");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL)
{	
head=temp;
}
else
{
temp1=head;
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
}
}
else
{
printf("\nMemory not avilable...node allocation is not possible");
}
printf("\nIf you wish to add m ore data on the list enter 1 : ");
scanf("%d",&choice);
}while(choice==1);
//Now displaying the alternative nodes starting from the begining
printf("\nDisplaying the alternative items of the list starting from the begining : \n");
alternatedisp(head);
printf("NULL\n");
return 0;
}
//recursive function to display alternative nodes
void alternatedisp(node *temp) 
{
if(temp!=NULL)
{
printf("%d->",temp->data);
if(temp->next==NULL)	
temp=NULL;
else
alternatedisp(temp->next->next);
}
else
return;
}

Output

輸出量

Enter the element in the list : 7
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 6
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 5
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 4 
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 3
If you wish to add m ore data on the list enter 1 : 0
Displaying the alternative items of the list starting from the begining :
7->5->3->NULL

翻譯自: https://www.includehelp.com/c-programs/print-the-alternate-nodes-in-a-linked-list-using-recursion.aspx

二叉樹打印葉子節點,非遞歸

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542696.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542696.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542696.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

TYVJ P1012 火柴棒等式 Label:枚舉

背景 NOIP2008年提高組第二題描述 給你n根火柴棍&#xff0c;你可以拼出多少個形如“ABC”的等式&#xff1f;等式中的A、B、C是用火柴棍拼出的整數&#xff08;若該數非零&#xff0c;則最高位不能是0&#xff09;。用火柴棍拼數字0-9的拼法如圖所示&#xff1a;注意&#xff…

java math max_Java Math類靜態double max(double d1,double d2)示例

java math max數學類靜態double max(double d1&#xff0c;double d2) (Math Class static double max(double d1,double d2) ) This method is available in java.lang package. 此方法在java.lang包中可用。 This method is used to return the maximum one of both the give…

python怎么開發軟件_怎么使用python進行軟件開發

一、下載pyinstaller 我使用的版本為PyInstaller-2.1&#xff0c;支持python版本2.3-2.7&#xff0c;點擊這里下載。 二、安裝pyinstaller 下載完成后&#xff0c;解壓即可。我的解壓目錄為D:\Python27\PyInstaller-2.1\ 三、使用pyinstaller打包.py成.exe應用程序 1.注意使用前…

28、清華大學腦機接口實驗組SSVEP數據集:通過視覺觸發BCI[飛一般的趕腳!]

前言&#xff1a; 哈嘍&#xff0c;最近對清華大學腦機接口的數據進行了嘗試&#xff0c;輸入到了DL模型中&#xff0c;以下是本人對于清華BCI數據的個人見解。 數據地址&#xff1a; 清華大學腦機接口研究組 (tsinghua.edu.cn) 打開網站可以看到有很多個數據&#xff0c;官…

python Pexpect

http://www.cnblogs.com/dkblog/archive/2013/03/20/2970738.htmlhttp://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/index.htmlhttp://www.cnblogs.com/dkblog/archive/2013/03/20/2970738.htmlpython Pexpect Pexpect 是一個用來啟動子程序并對其進行自動控制的純 P…

python 冪運算 整數_在Python中檢查一個數字是否是另一個數字的冪

python 冪運算 整數To solve this problem simply, we will use the log() function from the math module. The math module provides us various mathematical operations and here we will use the log() function from this module. In Python working of log() function, …

3dmax鏡像后模型線條亂了_3dMax入門教程來啦!小白趕緊收藏!

3D Studio Max&#xff0c;常簡稱為3d Max或3ds MAX&#xff0c;是Discreet公司開發的&#xff08;后被Autodesk公司合并&#xff09;基于PC系統的三維動畫渲染和制作軟件&#xff0c; 3dmax軟件主要功能有建模&#xff0c;動畫&#xff0c;渲染&#xff0c;特效等&#xff0c;…

java中哲學家就餐死鎖_哲學家就餐問題與死鎖總結

死鎖的四個條件&#xff1a;(1) 互斥條件&#xff1a;一個資源每次只能被一個進程使用。(2) 請求與保持條件&#xff1a;一個進程因請求資源而阻塞時&#xff0c;對已獲得的資源保持不放。(3) 不剝奪條件:進程已獲得的資源&#xff0c;在末使用完之前&#xff0c;不能強行剝奪。…

linux掃描工具之nmap

Linux下有很多強大網絡掃描工具&#xff0c;網絡掃描工具可以分為&#xff1a;主機掃描、主機服務掃描、路由掃描等,nmap支持批量主機掃描和主機服務掃描。檢測安裝&#xff1a;[rootbier ~]# rpm -qa nmap nmap-5.51-4.el6.x86_64如果沒有安裝就安裝一下nmap的安裝直接使用&am…

如何將多個一維列表轉化為二維列表_數據分析2_如何處理一維、二維數據

吞一塊大餅&#xff0c;還不如切成小塊吃得香常見的數據集&#xff0c;要么是數列&#xff0c;要么是表格&#xff1b;因此&#xff0c;數據分析最首要的是&#xff0c;處理一維、二維數據。主要知識點可參考如圖。如需要&#xff0c;可點擊以下百度網盤鏈接下載數據分析基礎知…

關于java中鎖的面試題_Java面試題-Java中的鎖

1. 如何實現樂觀鎖(CAS)&#xff1f;如何避免ABA問題&#xff1f;答&#xff1a;1)讀取內存值的方式實現了樂觀鎖(比如&#xff1a;SVN系統)&#xff0c;方法&#xff1a;第一&#xff0c;比較內存值和期望值&#xff1b;第二&#xff0c;替換內存值為要替換值。2)帶參數版本來…

NSUserDefaults

2019獨角獸企業重金招聘Python工程師標準>>> NSUserDefaults 轉載于:https://my.oschina.net/18829297883/blog/737931

什么是算術運算和邏輯運算_8086微處理器的算術和邏輯運算

什么是算術運算和邏輯運算邏輯指令 (Logical Instructions) a) AND: Logical AND a)AND&#xff1a;邏輯AND Atleast one of the operant should be a register or a memory operant both the operant cannot be a memory location or immediate operant. 操作中的至少一個應該…

python文件讀寫用到的庫_Python使用pyshp庫讀取shapefile信息的方法

通過pyshp庫&#xff0c;可以讀寫shapefile文件&#xff0c;查詢相關信息&#xff0c;github地址為 import shapefile # 使用pyshp庫 file shapefile.reader("data\\市界.shp") shapes file.shapes() # print(file.shapetype) # 輸出shp類型null 0 point 1 poly…

h5引入json_Vue中如何使用本地Json文件?

我需要將菜單配置成Json文件&#xff0c;然后再程序中引入{{menu.name}}import menuListConfig from ../../config/menu.jsonexport default {name: "Sider",data(){return {menuList:JSON.parse(JSON.stringify(menuListConfig))}}}需要如何做&#xff0c;才能v-for…

深入學習jQuery選擇器系列第四篇——過濾選擇器之屬性選擇器

前面的話 屬性過濾選擇器的過濾規則是通過元素的屬性來獲取相應的元素&#xff0c;對應于CSS中的屬性選擇器。屬性過濾選擇器可分為簡單屬性選擇器、具體屬性選擇器和條件屬性選擇器三種。本文將詳細該部分內容 簡單屬性選擇器 [attribute] [attribute]選擇器選擇擁有該屬性的元…

c++ scanf讀取_使用scanf()讀取內存地址并在C中打印其值

c scanf讀取Here, we have to input a valid memory address and print the value stored at memory address in C. 在這里&#xff0c;我們必須輸入一個有效的內存地址并在C中打印存儲在內存地址中的值。 To input and print a memory address, we use "%p" format…

python正則匹配_Python正則表達式只匹配一次

我正在嘗試創建一個簡單的降價乳膠轉換器,只是為了學習 python和基本的正則表達式,但我不知道試圖弄清楚為什么下面的代碼不起作用&#xff1a; re.sub (r\[\*\](.*?)\[\*\]: ?(.*?)$, r\\footnote{\2}\1, s, flagsre.MULTILINE|re.DOTALL) 我想轉換像&#xff1a; s "…

Virtual Network (1) - How to use it in a guest

本文將講述一個問題&#xff1a;kvm guest使用libvirt xml定義如何使用virtual network&#xff1f;1&#xff09;nat&#xff0c; route &#xff0c;isolated, open類型在host中定義virtual network會創建一個虛擬的bridge&#xff0c;相當于一個交換機。guest只需要連接到這…

java string做除法_如果用java來實現傳統方式的除法,用String來保存結果,想精確多少位都行,那改怎么做?...

我會加分的&#xff0c;提個思路都行&#xff0c;目前做了個乘法和加法&#xff0c;但是現在對除法沒有什么思路。以下是我編寫的功能&#xff1a;publicclassCalculator{publicstaticStringmulti(Strings1,Strings2){if(s1nu...我會加分的&#xff0c;提個思路都行&#xff0c…