194k views
0 votes
Given a list ` = (x1 x2 . . . xn−1 xn), we define two rotations of the list: The left-rotation is (x2 . . . xn−1 xn x1) and, likewise, the right-rotation is (xn x1 x2 . . . xn−1). (These rotation operations do not change the empty list, or any list of length 1.) Define the functions rotate-right and rotate-left to carry out these operations on a list. O

User XavM
by
4.2k points

1 Answer

7 votes

Step-by-step explanation:

The below code has been written in C language

void rotateright(int list[], int n)

{

int x = list[n-1]

int i;

for (i = n-1; i > 0; i--)

list[i] = list[i-1];

list[0] = x;

}

void rotateleft(int list[], int n)

{

int x = list[0]

int i;

for (i = 1; i < n-1 ; i++)

list[i] = list[i+1];

list[n-1] = x;

}

int main()

{

int list[] = {x1, x2, x3, ... x(n-1),xn}

int i;

int n = sizeof(list);

rotateright(list, n);

rotateleft(list, n);

return 0;

}

User Skjagini
by
5.0k points