数组重复次数最多的元素递归_在不使用递归的情况下计算链接列表中元素的出现次数...

数组重复次数最多的元素递归

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.

一个单链表 ,其第一个节点的地址存储在一个指针中,例如head和key是我们必须计算其出现次数的数据。

Output:

输出:

The number of times key occurred in the list, (Count)

密钥在列表中出现的次数,( Count )

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保留第一个节点的地址。

Pseudo code:

伪代码:

    Begin
        temp=Head
        Count = 0
        while(temp != NULL)
            begin
                if(temp->data = key)
                    count=count+1
                endif
                temp=temp->link
        End while
    End

C程序,用于计算链表中元素的出现次数,而无需使用递归 (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 list
	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);
	
	//finding occurence of key
	printf("\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 : 1

If you wish to add m ore data on the list enter 1 : 1

Enter the element in the list : 2

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 : 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 : 1

If you wish to add m ore data on the list enter 1 : 0

Enter the data to find it's occurrence : 1

1 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

数组重复次数最多的元素递归

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值