链接法 散列表 双向链接_在双向链接列表中找到最大的元素| C程序

链接法 散列表 双向链接

Algorithm:

算法:

Function: largest_DLL()

函数:largest_DLL()

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

输入:双向链表,其第一个节点的地址存储在指针“ head”中

Output: The largest no of the linked list say max

输出:链表中最大的数字表示最大

Data structure used: A doubly linked list where each node contains a data part, say data and two link parts say prev (to store the address of immediate previous node) and say next (to store the address of immediate next node).

使用的数据结构:双链表,其中每个节点包含一个数据部分,例如data和两个链接部分说prev (存储紧邻的前一个节点的地址)和next (存储紧邻的下一个节点的地址)。

Pseudo code:

伪代码:

Begin
temp=head
max=temp->data //max to store maximum
while(temp!=NULL)
begin
	if(temp->data>max)
		max=temp->data
		temp=temp->next
	end if
end while
End

C code:

C代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct list
{
	int data; 
	struct list *next;
	struct list *prev;
}node;

void display(node *temp)
{
	printf("The list is as follows :\n");
	while(temp!=NULL)
	{
		printf("%d->",temp->data);
		temp=temp->next;
	}
	printf("NULL");
	return;
}

int main(){
	node *head=NULL,*temp,*temp1;
	int choice,max;
	
	//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)
			{	
				temp->prev=head;
				temp->next=head;
				head=temp;
			}
			else
			{
				temp1=head;
				while(temp1->next!=NULL)
				{
					temp1=temp1->next;
				}
				temp1->next=temp;
				temp->prev=temp1;
			}
		}
		else
		{
			printf("\nMemory not avilable...node allocation is not possible");
		}
		printf("\nIf you wish to add more data on the list enter 1 : ");
		scanf("%d",&choice);
	}while(choice==1);
	
	display(head);
	
	//finding max
	temp=head;
	max=temp->data;
	while(temp!=NULL)
	{
		if(temp->data>max)
			max=temp->data;
		temp=temp->next;
	}
	printf("\nThe largest element in the list is : %d",max);
	
	return 0;
}

Output

输出量

Enter the element in the list : 1

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

Enter the element in the list : 2

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

Enter the element in the list : -3

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

Enter the element in the list : 56

If you wish to add more data on the list enter 1 : 0
The list is as follows :
1->2->-3->56->NULL
The largest element in the list is : 56


翻译自: https://www.includehelp.com/c-programs/find-the-largest-element-in-a-doubly-linked-list.aspx

链接法 散列表 双向链接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值