# C program to find the largest element in the singly linked list

Here, we are going to learn how to find the largest element in the singly linked list using C program?
Submitted by Nidhi, on August 23, 2021

Problem Solution:

Given a singly linked list, we have to find the largest element.

Program:

The source code to find the largest element in a singly linked list is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

```// C program to find the largest element

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

//Self-referential structure to create the node.
typedef struct tmp {
int item;
struct tmp* next;
} Node;

//structure for creating the linked list.
typedef struct
{
Node* tail;

} List;

//Initialize List
void initList(List* lp)
{
lp->tail = NULL;
}

//Create node and return the reference of it.
Node* createNode(int item)
{
Node* nNode;

nNode = (Node*)malloc(sizeof(Node));

nNode->item = item;
nNode->next = NULL;

return nNode;
}

//Add a new item at the end of the list.
{
Node* node;
node = createNode(item);

//if list is empty.
lp->tail = node;
}
else {
lp->tail->next = node;
lp->tail = lp->tail->next;
}
}

//Add a new item at the beginning of the list.
{
Node* node;
node = createNode(item);

//if list is empty.
lp->tail = node;
}
else {
}
}

//To print the list from start to end of the list.
void printList(List* lp)
{
Node* node;

printf("\nEmpty List");
return;
}

while (node != NULL) {
printf("| %05d |", node->item);
node = node->next;

if (node != NULL)
printf("--->");
}
printf("\n\n");
}

int findLarge(List* lp)
{
Node* temp;
int large = 0;

while (temp != NULL) {
if (large < temp->item)
large = temp->item;

temp = temp->next;
}

return large;
}

//Main function to execute program.
int main()
{
List* lp;
int large = 0;

lp = (List*)malloc(sizeof(List));

initList(lp);

printf("List:\n");
printList(lp);

large = findLarge(lp);

printf("Largest element in Linked list is: %d\n", large);

return 0;
}
```

Output:

```List:
| 00100 |--->| 00200 |--->| 00300 |--->| 00200 |--->| 00100 |

Largest element in Linked list is: 300
```

Explanation:

Here, we created a self-referential structure to implement a linked list, a function to add a node at the start and end of the list, a function isPalindrome() to check linked list is palindrome or not. The isPalindrome() function returns 1 if the list is palindrome otherwise it will return 0.

In the main() function, we created a linked list. Then we checked the given list is palindrome or not using the isPalindrome() function and printed the appropriate message.

Preparation

What's New

Top Interview Coding Problems/Challenges!