调整顺序表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); }