Advertisement
Osher15151

sdfsdf

Dec 8th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include < stdlib.h>
  3. typedef int list_item;
  4. typedef struct list {
  5.     int data;
  6.     struct list* next;
  7. }list;
  8.  
  9. struct list* head = NULL, *pos;
  10.  
  11. int isEmpty(list* head) {
  12.     return (head == NULL) ? 1 : 0;
  13. }
  14.  
  15. struct list* addNode(int num)
  16. {
  17.     struct list *node = (list*)malloc(sizeof(list));
  18.     node->data = num;
  19.     node->next = NULL;
  20.     if (isEmpty(head))
  21.     {
  22.         head = node;
  23.         head->next = NULL;
  24.         pos = head;
  25.     }
  26.     else {
  27.         pos->next = node;
  28.         pos = pos->next;
  29.         pos->next = head;
  30.     }
  31. }
  32.  
  33. struct list* bigger(list *head)
  34. {
  35.  
  36.     int max = head->data;
  37.     int before = head->data;
  38.  
  39.     while (head != NULL)
  40.     {
  41.         if (head->data > max)
  42.         {
  43.             max = head->data;
  44.         }
  45.         else if (head->data < max && head->data > before)
  46.         {
  47.             before = head->data;
  48.         }
  49.        
  50.         head = head->next;
  51.     }
  52.    
  53.     free(before);
  54.  
  55.  
  56.  
  57. }
  58.  
  59.  
  60. void display()
  61. {
  62.     struct list* current;
  63.     if (isEmpty(head))
  64.     {
  65.         printf("List is empty");
  66.     }
  67.     else {
  68.         current = head;
  69.         do {
  70.             printf("%d ", current->data);
  71.             current = current->next;
  72.         } while (current != head);
  73.         printf("\n");
  74.     }
  75. }
  76.  
  77.  
  78. void main()
  79. {
  80.     list* node = head;
  81.     int num, len, i;
  82.     do {
  83.         printf("\nHow many numbers you want in the list ? ");
  84.         scanf("%d", &len);
  85.         if (len <= 0)printf("List must have more numbers than 0");
  86.     } while (len <= 0);
  87.     printf("\nPlease enter %d numbers", len);
  88.     for (i = 0; i < len; i++)
  89.     {
  90.         scanf("%d", &num);
  91.         addNode(num);
  92.     }
  93.     bigger(head);
  94.     display();
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement