s1->s2->s3->………->sn-1->sn.
Data structure that can be used to modified the list above is an array.
Algorithm
Input:
An array of integers representing the list.
Output:
s1->sn->s2-sn-1...
Explanation
Create an array to store the final list.
Traverse the whole array with a loop.
Pseudo-code
This code is written in C programming language.
The program uses an array of size 5 to implement the logic above.
The elements and the size of this array can be changed
#include <stdio.h>
#include <time.h>
int main()
{
double timeSpent = 0.0;
clock_t beginTime = clock();
int arr[5] = {3,4,5,6,7};
int arr4[5];
int n = 5;
int k= 1;
for(int j=0; j<5; j++){
int index=0;
if(j == 0){
index = j;
}
else if(j !=0 && j% 2!=0){
index = n-1;
n--;
}
else if(j%2==0){
index = k;
k = k + 1;
}
arr4[j] = arr[index];
}
printf("\nThe array becomes: \t");
for(int i=0; i<5; i++){
printf(" %d->\t",arr4[i]);
}
clock_t endTime = clock();
timeSpent += (double)(endTime - beginTime) / CLOCKS_PER_SEC;
printf("\nRunning time %f seconds", timeSpent);
return 0;
}
Running time is 0.002000 seconds
Comments
Leave a comment