Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | None | 0 0
  1. #include <cstddef>
  2. #include <cstdio>
  3. #include <cstdlib>
  4.  
  5. struct List {
  6.     int value;
  7.     List *next;
  8.     List (int val = 0, List *p = NULL) {
  9.         value = val;
  10.         next = p;
  11.     }
  12. };
  13.  
  14. List *Ins_first(int n, List *head) {
  15.     List *q = new List(n, head);
  16.     return q;
  17. }
  18.  
  19. List *Create_list() {
  20.     const int N = 2;
  21.     char str[N];
  22.     List *head = NULL;
  23.     puts("Create list. Enter numbers:");
  24.     for (;;) {
  25.         gets(str);
  26.         if (!str[0]) break;
  27.         head = Ins_first(atoi(str), head);
  28.     }
  29.     return head;
  30. }
  31.  
  32. int get_first_value(List *head) {
  33.     if (head != NULL) return head->value;
  34. }
  35.  
  36. int get_last_value(List *head) {
  37.     List *p = head;
  38.     if (p != NULL)
  39.         while (p->next != NULL)
  40.             p = p->next;
  41.     return p->value;
  42. }
  43.  
  44. struct Matches {
  45.     int first_value_count = 0;
  46.     int last_value_count = 0;
  47. };
  48.  
  49. Matches count_first_last_value_matches(List *head) {
  50.     if (head != NULL) {
  51.         int first_value = get_first_value(head);
  52.         int last_value = get_last_value(head);
  53.         List *p = head->next;
  54.         Matches matches;
  55.         while (p->next != NULL) {
  56.             if (p->value == first_value) matches.first_value_count++;
  57.             else if (p->value == last_value) matches.last_value_count++;
  58.             p = p->next;
  59.         }
  60.         return matches;
  61.     }
  62. }
  63.  
  64. int main() {
  65.     List *list = Create_list();
  66.     Matches matches = count_first_last_value_matches(list);
  67.     printf("\n%d\n%d\n", matches.first_value_count, matches.last_value_count);
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement