二叉树打印叶子节点,非递归_使用递归打印链接列表中的备用节点
二叉樹打印葉子節點,非遞歸
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):BeginIF(temp!=NULL){Print "temp->data"If(temp->next==NULL)temp=NULL;Elsealternate_display(temp->next->next);}Elsereturn control to the main functionEnd IF-ELSE End FUNCTIONC 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 inputdo{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 beginingprintf("\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;elsealternatedisp(temp->next->next);}elsereturn; }Output
輸出量
Enter the element in the list : 7If you wish to add m ore data on the list enter 1 : 1Enter the element in the list : 6If you wish to add m ore data on the list enter 1 : 1Enter the element in the list : 5If you wish to add m ore data on the list enter 1 : 1Enter the element in the list : 4 If you wish to add m ore data on the list enter 1 : 1Enter the element in the list : 3If you wish to add m ore data on the list enter 1 : 0Displaying 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
二叉樹打印葉子節點,非遞歸
總結
以上是生活随笔為你收集整理的二叉树打印叶子节点,非递归_使用递归打印链接列表中的备用节点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dfs文件服务器访问权限,fastDFS
- 下一篇: python凯撒密码实现_密码:凯撒密码