Compare Tens
Difficulty: ⚫◯◯◯
Tags: linked-list, counting, comparison
Hurdle: Passing this question is sufficient to pass the linked lists hurdle.
Description
Write a function compare_tens that is given two arguments, head1 and head2, which are pointers to the first nodes of two linked lists.
The function should compare the count of two-digit positive numbers (10-99) in each list and return:
-1if list1 has fewer two-digit numbers than list20if both lists have the same number of two-digit numbers1if list1 has more two-digit numbers than list2
Two-digit positive numbers: Numbers from 10 to 99 (inclusive).
Examples
Example 1
List 1: 13 -> 1 -> 4 -> X
List 2: 2 -> 71 -> 1 -> 18 -> 3 -> X
List 1 has 1 two-digit number (13)
List 2 has 2 two-digit numbers (71, 18)
Output: -1 (list1 has fewer)
Example 2
List 1: 30 -> 11 -> X
List 2: 19 -> 71 -> X
List 1 has 2 two-digit numbers (30, 11)
List 2 has 2 two-digit numbers (19, 71)
Output: 0 (same count)
Example 3
List 1: 10 -> 16 -> 21 -> 12 -> X
List 2: 16 -> 7 -> 8 -> 12 -> X
List 1 has 4 two-digit numbers (10, 16, 21, 12)
List 2 has 2 two-digit numbers (16, 12)
Output: 1 (list1 has more)
Example 4
List 1: 3 -> 1 -> 4 -> X
List 2: 2 -> 7 -> 1 -> 8 -> 3 -> X
List 1 has 0 two-digit numbers
List 2 has 0 two-digit numbers
Output: 0 (same count)
Example 5
List 1: 2 -> 4 -> 6 -> X
List 2: 42 -> X
List 1 has 0 two-digit numbers
List 2 has 1 two-digit number (42)
Output: -1 (list1 has fewer)
Example 6: Both Empty
List 1: (empty)
List 2: (empty)
Output: 0 (both have 0 two-digit numbers)
Function Signature
int compare_tens(struct node *head1, struct node *head2);
Data Structure
struct node {
struct node *next;
int data;
};
Constraints
- You may assume inputs will be positive integers.
- Two-digit numbers are those from 10 to 99 (inclusive).
- Return only one of three values:
-1,0, or1. - Do not change the linked lists provided.
- Do not change the
nextordatafields of list nodes. - Do not use arrays.
- Do not call
malloc. - Do not call
scanf,getchar, orfgets. - Do not call
printf.
Hints
- Create a helper function to count two-digit numbers in a single list.
- A number is two-digit if:
data >= 10 && data <= 99 - Compare the counts and return the appropriate value.
Testing
The command line uses - to separate the two lists.
./compare-tens 3 1 4 - 2 7 1 8 3
0
./compare-tens 10 16 21 12 - 16 7 8 12
1
./compare-tens 2 4 6 - 42
-1
./compare-tens -
0
./compare-tens 13 1 4 - 2 71 1 18 3
-1
./compare-tens 30 11 - 19 71
0