18 Wang, 2010, exam computer Zhenti n (n> 1) integer R into one-dimensional array, the stored sequence sequentially rotate left R p (0 <p <n) position,
i.e., the R by the data (X0, X1 ... Xn-1 ) is converted into (Xp, Xp + 1 ... Xn -1, X0, ... Xp-1)
Thought: dynamically creating an auxiliary array of size S p, the integer R among a first p temporarily stored in S, while the left in R np integers, then the number of S p,
sequentially into the R subsequent unit.
CycleLeft (SqList & L, int p) parameters: sequence table (array) L, the number of the left function p: p rotate left positions
Time complexity: O (n) space complexity: O (p)
Note that, L is equivalent to the Program R. L.length equivalent to n
Manual Rendering:
Function Code:
//循环左移函数
bool CycleLeft(SqList &L,int p)
{
if(p<0||p>L.length)return false;//p违法
ElemType* S = new ElemType[p];//动态创建辅助数组S
for(int i=0;i<p;i++) //前p个元素保存S中
{
S[i]=L.data[i];
}
for(int i=p;i<L.length;i++) //后p个元素依次左移 p个位置
{
L.data[i-p]=L.data[i];
}
for(int i=0;i<p;i++) //S中元素放在后面
{
L.data[i+L.length-p]=S[i];
}
return true;
}
Original: https: //blog.csdn.net/lady_killer9/article/details/82755750