C Program to find largest element in linked list
Simple C program to find largest element in linked list.
#include<stdio.h> #include<stdlib.h> #include<limits.h> struct Node{ int data; struct Node *next; }; struct Node *head=NULL; //global head node to use anywhere void pushAtEnd(int n) { struct Node *newNode=(struct Node*)malloc(sizeof(struct Node)); struct Node *temp=head; newNode->data=n; newNode->next=NULL; if(head==NULL) { head=newNode; } else { while(temp->next!=NULL) temp=temp->next; temp->next=newNode; } } void printlist() { struct Node *current=head; while(current!=NULL) { printf("%d ",current->data); current=current->next; } printf("\n"); } int Maximum(struct Node *head) { int max=INT_MIN; struct Node *temp=head; while(temp!=NULL) { if(max<temp->data) max=temp->data; temp=temp->next; } return max; } int main() { pushAtEnd(2); pushAtEnd(3); pushAtEnd(4); pushAtEnd(5); pushAtEnd(7); printlist(); printf("Max in linked list = %d",Maximum(head)); printf("\n"); pushAtEnd(24); pushAtEnd(32); pushAtEnd(4); pushAtEnd(50); pushAtEnd(77); printlist(); printf("Max in linked list = %d",Maximum(head)); return 0; }