Remove consecutive duplicate elements
Back to JHAVEPOP exercises
Problem statement
Write a C++ program that, given a linked list of characters, removes
all the elements in the list that are immediately preceded by an
element with the same data. In other words, after executing your code,
the list should not contain any consecutive duplicates
(non-consecutive duplicates need not be deleted). All elements
remaining in the linked list must appear in their original order.
As shown in the test cases below, your code may assume that the
pointers head and tail are initialized to the first and
last nodes of the linked list, respectively, or to NULL when
the linked list is empty. This invariant must still hold after
execution of your code. Note that two additional pointers
p1 and p2 are declared. For this exercise, you may
not declare any additional pointers.
Make sure that your code works on all possible linked lists. The following
8 test cases are provided to help you test your code.
Test case #1
Description: The linked list is empty.
Initial setup:
Final configuration:
Ready to test your program?
Test case #2
Description: The linked list starts with one duplication.
Initial setup:
Final configuration:
Ready to test your program?
Test case #3
Description: The linked list ends with one duplication.
Initial setup:
Final configuration:
Ready to test your program?
Test case #4
Description: The linked list contains no duplications.
Initial setup:
Final configuration:
Ready to test your program?
Test case #5
Description: The linked list starts with multiple duplications.
Initial setup:
Final configuration:
Ready to test your program?
Test case #6
Description: The linked list ends with multiple duplications.
Initial setup:
Final configuration:
Ready to test your program?
Test case #7
Description: Random general case.
Initial setup:
Final configuration:
Ready to test your program?
Test case #8
Description: Random general case with non-consecutive duplicates.
Initial setup:
Final configuration:
Ready to test your program?
Back to JHAVEPOP exercises