试分别以顺序表和单链表作存储结构,各写一实现线性表就地逆置的算法


对数组的倒置

void SeqList<DataType>::inversion()
{
    int i = 0, j = length - 1;
    DataType temp;
    while(i < j)
    {
        temp = data[i];
        data[i++] = data[j];
        data[j--] = temp;
    }
} 

对链表的倒置

void SeqList<DataType>::inversion()
{
    Node<DataType> * p, * q;
    p = first->next;
    q = p->next;
    while(q != NULL)
    {
        p = q;
        q = q->next;
        p->next = first->next;
        first->next = p;
    }

} 



    原文作者:dancheng_work
    原文地址: https://blog.csdn.net/dancheng1/article/details/52737652
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞