Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstddef>
- #include <cstdio>
- #include <cstdlib>
- struct List {
- int value;
- List *next;
- List (int val = 0, List *p = NULL) {
- value = val;
- next = p;
- }
- };
- List *Ins_first(int n, List *head) {
- List *q = new List(n, head);
- return q;
- }
- List *Create_list() {
- const int N = 2;
- char str[N];
- List *head = NULL;
- puts("Create list. Enter numbers:");
- for (;;) {
- gets(str);
- if (!str[0]) break;
- head = Ins_first(atoi(str), head);
- }
- return head;
- }
- int get_first_value(List *head) {
- if (head != NULL) return head->value;
- }
- int get_last_value(List *head) {
- List *p = head;
- if (p != NULL)
- while (p->next != NULL)
- p = p->next;
- return p->value;
- }
- struct Matches {
- int first_value_count = 0;
- int last_value_count = 0;
- };
- Matches count_first_last_value_matches(List *head) {
- if (head != NULL) {
- int first_value = get_first_value(head);
- int last_value = get_last_value(head);
- List *p = head->next;
- Matches matches;
- while (p->next != NULL) {
- if (p->value == first_value) matches.first_value_count++;
- else if (p->value == last_value) matches.last_value_count++;
- p = p->next;
- }
- return matches;
- }
- }
- int main() {
- List *list = Create_list();
- Matches matches = count_first_last_value_matches(list);
- printf("\n%d\n%d\n", matches.first_value_count, matches.last_value_count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement