Advertisement
mhrabbi

Maximum and Minimum of a Linked List

Oct 15th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node
  5. {
  6.     int data;
  7.     struct node *next;
  8. }*head;
  9.  
  10. int main()
  11. {
  12.     int n,max,min;
  13.  
  14.     printf("Enter Total Number of Node: ");
  15.     scanf("%d",&n);
  16.  
  17.     createList(n);
  18.  
  19.     printf("\nList is\n");
  20.  
  21.     displayList();
  22.  
  23.     max=maximum();
  24.     min=minimum();
  25.  
  26.     printf("\nMaximum number %d and Minimum number is %d ",max,min);
  27.  
  28.     return 0;
  29.  
  30. }
  31. void createList(int n)
  32. {
  33.     struct node *p,*temp;
  34.     int i,data;
  35.     temp=(struct node *)malloc(sizeof(struct node));
  36.     printf("Enter data for node 1: ");
  37.     scanf("%d",&data);
  38.  
  39.     temp->data=data;
  40.     temp->next=NULL;
  41.     p=temp;
  42.     head=temp;
  43.  
  44.     for(i=2; i<=n; i++)
  45.     {
  46.         temp=(struct node *)malloc(sizeof(struct node));
  47.         printf("Enter data for node %d: ",i);
  48.         scanf("%d",&data);
  49.  
  50.         temp->data=data;
  51.         temp->next=NULL;
  52.  
  53.         p->next=temp;
  54.         p=p->next;
  55.     }
  56. }
  57. void displayList()
  58. {
  59.     struct node *temp;
  60.     temp=head;
  61.  
  62.     while(temp != NULL)
  63.     {
  64.         printf("\n%d\n",temp->data);
  65.         temp=temp->next;
  66.     }
  67. }
  68. int maximum()
  69. {
  70.     int maximum=head->data;
  71.     struct node *temp=head;
  72.     while(temp!=NULL)
  73.     {
  74.         if(maximum<temp->data)
  75.         {
  76.             maximum=temp->data;
  77.         }
  78.         temp=temp->next;
  79.     }
  80.     return maximum;
  81. }
  82. int minimum()
  83. {
  84.     int minimum=head->data;
  85.     struct node *temp=head;
  86.     while(temp!=NULL)
  87.     {
  88.         if(minimum>temp->data)
  89.         {
  90.             minimum=temp->data;
  91.         }
  92.         temp=temp->next;
  93.     }
  94.     return minimum;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement