数组重复次数最多的元素递归_在不使用递归的情况下计算链接列表中元素的出现次数...
數(shù)組重復(fù)次數(shù)最多的元素遞歸
Solution:
解:
Input:
輸入:
A singly linked list whose address of the first node is stored in a pointer, say head and key is the data of which we have to count the number of occurrences.
一個單鏈表 ,其第一個節(jié)點的地址存儲在一個指針中,例如head和key是我們必須計算其出現(xiàn)次數(shù)的數(shù)據(jù)。
Output:
輸出:
The number of times key occurred in the list, (Count)
密鑰在列表中出現(xiàn)的次數(shù),( Count )
Data structure used:
使用的數(shù)據(jù)結(jié)構(gòu):
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.
單鏈列表,其中每個節(jié)點包含一個數(shù)據(jù)元素,例如data ,直接下一個節(jié)點的地址說next ,其中Head保留第一個節(jié)點的地址。
Pseudo code:
偽代碼:
Begintemp=HeadCount = 0while(temp != NULL)beginif(temp->data = key)count=count+1endiftemp=temp->linkEnd whileEndC程序,用于計算鏈表中元素的出現(xiàn)次數(shù),而無需使用遞歸 (C program to Count the number of occurrences of an element in a linked list without using recursion)
#include <stdio.h> #include <stdlib.h>typedef struct list //linked list node {int data;struct list *next; }node;int main() {node *head=NULL,*temp,*temp1;int choice,count=0,key;//building the linked listdo{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);//finding occurence of keyprintf("\nEnter the data to find it's occurrence : ");scanf("%d",&key);temp=head;while(temp!=NULL){if(temp->data==key){count=count+1;}temp=temp->next;}printf("\n %d occurred %d times in the list",key,count);return 0; }Output
輸出量
Enter the element in the list : 1If you wish to add m ore data on the list enter 1 : 1Enter the element in the list : 2If 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 : 1Enter the element in the list : 4If you wish to add m ore data on the list enter 1 : 1Enter the element in the list : 1If you wish to add m ore data on the list enter 1 : 0Enter the data to find it's occurrence : 11 occurred 2 times in the list翻譯自: https://www.includehelp.com/c-programs/count-the-number-of-occurrences-of-an-element-in-a-linked-list-without-using-recursion.aspx
數(shù)組重復(fù)次數(shù)最多的元素遞歸
總結(jié)
以上是生活随笔為你收集整理的数组重复次数最多的元素递归_在不使用递归的情况下计算链接列表中元素的出现次数...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kotlin 查找id_Kotlin程序
- 下一篇: dir函数_PHP dir()函数与示例