#define COMM   MPI_COMM_WORLD
#define STATUS MPI_STATUS_IGNORE
#define TR 1 // "to right" communications
#define TL 2 // "to left" communications
...
if(rank == 0)
{ buffer[0] = a[N-2];
  MPI_Send(buffer,1,MPI_DOUBLE,rank+1,TR,COMM);
  MPI_Recv(buffer,1,MPI_DOUBLE,rank+1,TL,COMM,STATUS);
  a[N-1] = buffer[0]; }
if(rank == size-1)
{ MPI_Recv(buffer,1,MPI_DOUBLE,rank-1,TR,COMM,STATUS);
  a[0] = buffer[0];
  buffer[0] = a[1];
  MPI_Send(buffer,1,MPI_DOUBLE,rank-1,TL,COMM); }
if(rank!=0 && rank!=size-1)
{ MPI_Recv(buffer,1,MPI_DOUBLE,rank-1,TR,COMM,STATUS);
  a[0] = buffer[0];
  buffer[0] = a[N-2];
  MPI_Send(buffer,1,MPI_DOUBLE,rank+1,TR,COMM); 
  MPI_Recv(buffer,1,MPI_DOUBLE,rank+1,TL,COMM,STATUS);
  a[N-1] = buffer[0];
  buffer[0] = a[1];
  MPI_Send(buffer,1,MPI_DOUBLE,rank-1,TL,COMM); }  
...