1、顺序表
a) 通过数组下标访问元素
/*
* 通过数组下标访问
*/
void reverse_sqlist( int c[], int n )
{
int i = 0, j = n - 1;
int t;
for( ; i < j; ++i, --j )
{
t = c[i];
c[i] = c[j];
c[j] = t;
}
}
b) 通过指针访问元素
/*
* 指针版
*/
void reverse_sqlist( int c[], int n )
{
int * pl = c, * pr = c + n - 1;
int t;
while( pl < pr )
{
t = *pl;
*pl = *pr;
*pr = t;
++pl; --pr;
}
}
2、单链表
/*
* reverse the list in place
*/
void reverse_llist( link_list * lst )
{
node_ptr h = *lst, p = h->next, q = NULL;
if( p != NULL )
{
q = p->next;
h->next = p;
p->next = NULL;
}
p = q;
while( p != NULL )
{
q = p->next;
p->next = h->next;
h->next = p;
p = q;
}
}