将顺序表A逆置,只允许在原表的存储空间外,再增加一个附加的工作单元

#include<iostream.h>
#include<stdlib.h>
#define LIST_INIT_SIZE   100
#define OK     1
#define OVERFLOW    -2
#define ERROR 0
typedef int Status;

typedef int ElemType;
typedef struct
{
    ElemType *elem;
    int   length;     //当前长度
    int listsize;      //当前分配的存储容量
}SqList;

Status InitList_Sq(SqList &L)
{
    //构造一个空的线性表
    L.elem=new ElemType[LIST_INIT_SIZE];
    if(!L.elem)
        exit(OVERFLOW);
    L.length=0;
    L.listsize=LIST_INIT_SIZE;
    return OK;
}//InitList_Sq

Status ListCreate_Sq(SqList &L,int n)
{
    //创建顺序表
    //i的合法范围为 1<=i<=L.length+1
    ElemType x;
    cout<<"input x(n)="<<endl;
    for(int i=1;i<=n;++i)
    {
        cin>>x;
        L.elem[i-1]=x;
        ++L.length;
    }
    return OK;
}//ListCreat_Sq

void invert_Sq(SqList L,int n)
{
    //将包含n个元素的顺序表A逆置
    int i=0,m=n/2;
    ElemType temp;
    for(i=0;i<m;++i)
    {
        temp=L.elem[i];
        L.elem[i]=L.elem[n-i-1];
        L.elem[n-i-1]=temp;
    }
}//invert_Sq

void print(SqList L)
{
    int i;
    for(i=1;i<=L.length;++i)
        cout<<L.elem[i-1]<<"  ";
    cout<<endl;
}


void main()
{
    SqList LA;
    InitList_Sq(LA);
    cout<<"create LA\n"<<endl;
    ListCreate_Sq(LA,6);
    cout<<"output LA\n"<<endl;
    print(LA);
    cout<<endl;

    invert_Sq(LA,6);
    print(LA);
}

 

posted on 2013-04-23 21:47  绕指柔yy  阅读(208)  评论(0编辑  收藏  举报

导航