调整顺序表A,使其左边的所有元素均小于0,使其右边的所有元素均大于0

#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 DestroyList(SqList L)
{
    delete[]L.elem;
    L.length=0;
    L.listsize=0;
}

void adjust_Sq(SqList LA,int n)
{
    //调整顺序表A,使其左边的所有元素均小于0,
    //使其右边的所有元素均大于0
    SqList LB;
    int i=0,x,y;
    InitList_Sq(LB);
    x=0;
    y=n-1;
    for(i=0;i<n;++i)
    {
        if(LA.elem[i]<0)
        {
            LB.elem[x]=LA.elem[i];
            x++;
        }
        else
        {
            LB.elem[y]=LA.elem[i];
            y--;
        }
        for(i=0;i<n;++i)
            LA.elem[i]=LB.elem[i];
        DestroyList(LB);
    }
}//adjust_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,LB,LC;
    InitList_Sq(LA);
    cout<<"create LA\n"<<endl;
    ListCreate_Sq(LA,6);
    cout<<"output LA\n"<<endl;
    print(LA);
    cout<<endl;

    adjust_Sq(LA,6);
    cout<<"output LA\n"<<endl;
    print(LA);
}

 

转载于:https://www.cnblogs.com/yanyanwen/archive/2013/04/23/3038978.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值