jmu-ds-順序表區間元素刪除
阿新 • • 發佈:2018-12-12
若一個線性表L採用順序儲存結構儲存,其中所有的元素為整數。設計一個演算法,刪除元素值在[x,y]之間的所有元素,要求演算法的時間複雜度為O(n),空間複雜度為O(1)。
輸入格式:
三行資料,第一行是順序表的元素個數,第二行是順序表的元素,第三行是x和y。
輸出格式:
刪除元素值在[x,y]之間的所有元素後的順序表。
輸入樣例:
10
5 1 9 10 67 12 8 33 6 2
3 10
輸出樣例:
1 67 12 33 2
#include <bits/stdc++.h>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10//陣列增量
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
} SqList;
Status InitList_Sq(SqList &L)
{
L.elem = (ElemType * ) malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem)
{
exit(OVERFLOW);
}
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
int main()
{
SqList L,t;
int x,y;
int n;
cin>>n;
L.length=n;
L.elem=(ElemType *)malloc(L.length*sizeof(ElemType) );
for(int i=0; i<L.length; i++)
{
cin>>L.elem[i];
}
cin>>x>>y;
t.elem=(ElemType *)malloc(L.length*sizeof(ElemType));
t.length=0;
int r=0;
for(int i=0; i<L.length; i++)
{
if(L.elem[i]<x||L.elem[i]>y)
{
t.elem[r++]=L.elem[i];
}
}
t.length+=r;
for(int i=0;i<t.length-1;i++)
cout<<t.elem[i]<<" ";
cout<<t.elem[t.length-1];
}