visual loook: 0 1 2 3 4 5 6 7
visual loook: | a | b | c | d | e | f | g |
To represent the cursor within the ADT, we use two Node pointers, which are called beforeCursor and afterCusor in the .h file. These pointers always straddle the vertical cursor, pointing to the Node objects immediately before and after the cursor position. If the cursor is at the front of the List (position 0), then beforeCursor will point to node frontDummy. Likewise if the cursor is at the back of the List (position 𝑛), then afterCursor will point to node backDummy. A List containing 𝑛 elements will therefore have exactly 𝑛𝑛 + 1 possible cursor positions, namely 0, signifying the front of the List, through 𝑛𝑛 at the back.
The following code is what I have right now for my goal, and it deletes repeated elements but the produced cursor position is not correct.
// cleanup()
// Removes any repeated elements in this List, leaving only unique data
// values. The order of the remaining elements is obtained by retaining
// the frontmost occurrance of each element, and removing all other
// occurances. The cursor is not moved with respect to the retained
// elements, i.e. it lies between the same two retained elements that it
// did before cleanup() was called.
void List::cleanup()
{
Node* A = frontDummy->next;
while (A != backDummy)
{
int position = 1;
Node* B = frontDummy->next;
Node* next = A->next;
bool found = false;
while (found == false && B != A)
{
found = (B->data == A->data);
if (found == true) // delete node
{
A->prev->next = A->next;
A->next->prev = A->prev;
num_elements--;
if (A == beforeCursor)
{
beforeCursor = beforeCursor->prev;
}
else if (A == afterCursor)
{
afterCursor = afterCursor->next;
}
if (pos_cursor >= position)
{
pos_cursor--;
}
}
else
{
B = B->next;
}
}
A = next;
position++;
}
}
So say the position of the cursor is at index 15, then:
A = (10, 1, 9, 2, 8, 3, 7, 4, 6, 5, 5, 6, 4, 7, 3, 8, 2, 9, 1, 10)
indexPosition = 15
After calling cleanup(), correct output should be:
A = (10, 1, 9, 2, 8, 3, 7, 4, 6, 5)
indexPposition = 10
However, my output is giving:
A = (10, 1, 9, 2, 8, 3, 7, 4, 6, 5)
indexPposition = 5
Aucun commentaire:
Enregistrer un commentaire