Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Ada

Idiom #19 Reverse a list

Reverse the order of the elements of the list x.
This may reverse "in-place" and destroy the original ordering.

with Ada.Containers.Vectors;
use Ada.Containers;
X.Reverse_Elements;
int *p1 = x;
int *p2 = x + N-1;

while (p1 < p2)
{
    int temp = *p1;
    *(p1++) = *p2;
    *(p2--) = temp;
}

Reverses an array of N ints, in-place.

New implementation...
< >
programming-idioms.org