Reputation: 5
I am doing a task for my project in college and in the implementation of the list using arrays (sequential implementation), I need to do a task where all elements move from the left partition to the right partition, while elements from the right partition switch to the left partition.
For example, for the initial list configuration (8, 5, 11, 3 | 12, 7, 1, 15, 6, 14, 2), the method should create the following list configuration (12, 7, 1, 15, 6, 14, 2 | 8, 5, 11, 3).
Here is the method and the code that I wrote. I will write the explanation of every single variable:
template <typename Partition>
Partition& NizLista<InfoTip>::replacePartition() {
int temp = 0;
int beginPostion = 0;
int endOfPosition = current - 1;
while (beginPosition < endOfPosition)
{
temp = L[beginPosition];
L[beginPosition] = L[endOfPosition];
L[endOfPosition] = temp;
beginPosition++;
endOfPosition--;
}
}
This is just a representation of what I am trying to do, this is not a finished task. So as you can see from my explanation above, I want to do the "simple swap" method. The |
from the explanation of the list is the current
element and in the configuration above it is the element 3
.
My goal is to swap them by going from the first element in the array to the current one, and then maybe collecting those elements in another array and storing them again in the L[]
array. What do you think, would it work?
The L[]
is the list array where all elements can be stored in. It has a capacity of 11
and the variable is called capacity
, while the number of elements in the array is represented by numberOfElements
.
Upvotes: 0
Views: 147
Reputation: 60238
You can simply use std::rotate
to do this:
std::rotate(L, L + current, L + numberOfElements);
Upvotes: 4