Advertisement
Mukit1234

Untitled

Oct 21st, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Node
  4. {
  5.     int data;
  6.     struct Node *next;
  7.  
  8. }*head;
  9.  
  10. void createlinklist(int n);
  11. void display();
  12. void maxmin();
  13. int main()
  14. {
  15.     int n;
  16.     printf("Enter the number of nodes: ");
  17.     scanf("%d",&n);
  18.     createlinklist(n);
  19.     printf("\nData entered in the list are:\n");
  20.     display();
  21.     maxmin();
  22.     return 0;
  23. }
  24. void createlinklist(int n)
  25. {
  26.     struct Node *p, *temp;
  27.     int data, i;
  28.     head= (struct Node*)malloc(sizeof(struct Node));
  29.     printf("Input data for node 1: ");
  30.     scanf("%d",&data);
  31.     head->data=data;
  32.     head->next=NULL;
  33.     p=head;
  34.     temp=head;
  35.     for(i=2; i<=n; i++)
  36.     {
  37.         printf("Enter data for node %d: ",i);
  38.         scanf("%d",&data);
  39.         temp=(struct Node*)malloc(sizeof(struct Node));
  40.         temp->data=data;
  41.         temp->next=NULL;
  42.         p->next=temp;
  43.         p=p->next;
  44.  
  45.     }
  46. }
  47. void display()
  48. {
  49.     struct Node *temp;
  50.     temp=head;
  51.     while(temp!=NULL)
  52.     {
  53.         printf("%d\n",temp->data);
  54.         temp=temp->next;
  55.     }
  56. }
  57. void maxmin()
  58. {
  59.     struct Node *temp;
  60.     temp=head;
  61.     int max,min;
  62.     max=temp->data;
  63.     min=temp->data;
  64.  
  65.     while(temp!=NULL)
  66.     {
  67.         if(temp->data>max)
  68.         {
  69.             max=temp->data;
  70.         }
  71.         if(temp->data<min)
  72.         {
  73.             min=temp->data;
  74.         }
  75.  
  76.         temp=temp->next;
  77.     }
  78.     printf("Maximum element is : %d\n",max);
  79.     printf("Minimum element is : %d\n",min);
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement