Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct stack_node
  4.   {
  5.     int data;
  6.     struct stack_node *next;
  7.   };
  8.  
  9. void fill(struct stack_node **n)
  10.   {
  11.     *n = (struct stack_node *)malloc(sizeof(struct stack_node));
  12.     (*n)->next = (struct stack_node *)malloc(sizeof(struct stack_node));
  13.     (*n)->next->next = (struct stack_node *)malloc(sizeof(struct stack_node));
  14.     (*n)->next->next->next = (struct stack_node *)malloc(sizeof(struct stack_node));
  15.     (*n)->next->next->next->next = (struct stack_node *)malloc(sizeof(struct stack_node));
  16.     (*n)->next->next->next->next->next = NULL;
  17.  
  18.     (*n)->data =0;
  19.     (*n)->next->data = 1;
  20.     (*n)->next->next->data = 2;
  21.     (*n)->next->next->next->data = 3;
  22.     (*n)->next->next->next->next->data = 4;
  23.   }
  24.  
  25. struct stack_node *push(struct stack_node *n, int data)
  26.   {
  27.     struct stack_node *new_node = (struct stack_node *)malloc(sizeof(struct stack_node));
  28.     if(new_node)
  29.       {
  30.         new_node->data = data;
  31.         new_node->next = n;
  32.         n = new_node;
  33.       }
  34.     return n;
  35.   }
  36.   void printing(struct stack_node *n)
  37.   {
  38.       while(n)
  39.       {
  40.           printf("%d ", n->data);
  41.           n=n->next;
  42.       }
  43.   }
  44.  
  45.   int min_element(struct stack_node **n)
  46.   {
  47.     int min=(*n)->data;
  48.  
  49.     while(*n)
  50.     {
  51.       if(min>(*n)->data)
  52.         {
  53.           min = (*n)->data;
  54.         }
  55.         (*n) = (*n)->next;
  56.     }
  57.     return min;
  58.   }
  59.  
  60.   int main()
  61.  
  62.   {
  63.       struct stack_node *n = NULL;
  64.       fill(&n);
  65.       printing(n);
  66.       printf("\nMIN: %d",  min_element(&n));
  67.       return 0;
  68.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement