Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 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. void display()
  33. {
  34. struct list* current;
  35. if (isEmpty(head))
  36. {
  37. printf("List is empty");
  38. }
  39. else {
  40. current = head;
  41. do {
  42. printf("%d", current->data);
  43. current = current->next;
  44. } while (current != head);
  45. printf("\n");
  46. }
  47. }
  48. struct list* bigger(list* head)
  49. {
  50. struct list *tmp = head;
  51. int max = head->data;
  52. int beforeMax = head->data;
  53. while (tmp != NULL)
  54. {
  55. if (tmp->data > max)
  56. {
  57. max = tmp->data;
  58. }
  59.  
  60.  
  61. else if (head->data < max && head->data > beforeMax)
  62. {
  63. beforeMax = head->data;
  64. }
  65.  
  66. head = head->next;
  67.  
  68. }
  69. return beforeMax;
  70. }
  71.  
  72.  
  73. void main()
  74. {
  75. list* node = head;
  76. int num, len, i;
  77. do {
  78. printf("\nHow many numbers you want in the list ? ");
  79. scanf_s("%d", &len);
  80. if (len <= 0)printf("List must have more numbers than 0");
  81. } while (len <= 0);
  82. printf("\nPlease enter %d numbers", len);
  83. for (i = 0; i < len; i++)
  84. {
  85. scanf_s("%d", &num);
  86. addNode(num);
  87. }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement