bool Del_Min(SqList &L, ElemType &value)
{
if (L.length == 0)
return false;
value = L.data[0];
int pos = 0;
for (int i = 1; i < L.length; i ++ )
if (L.data[i] < value)
{
value = L.data[i];
pos = i;
}
L.data[pos] = L.data[L.length - 1];
L.length -- ;
return true;
}
void Reverse(SqList &L)
{
ElemType temp;
for (int i = 0; i < L.length / 2; i ++ )
{
temp = L.data[i];
L.data[i] = L.data[L.length - i - 1];
L.data[L.length - i - 1] = temp;
}
}
void del_x_1(SqList &L, ElemType x)
{
int k = 0;
for (int i = 0; i < L.length; i ++ )
if (L.data[i] != x)
{
L.data[k] = L.data[i];
k ++ ;
}
L.length = k;
}
void del_x_2(SqList &L, ElemType x)
{
int i = 0, k = 0;
while (i < L.length)
{
if (L.data[i] == x) k ++ ;
else L.data[i - k] = L.data[i];
i ++ ;
}
L.length -= k;
}
bool Del_s_t2(SqList &L, ElemType s, ElemType t)
{
if (s >= t || L.length == 0) return false;
int i, j;
for (i = 0; i < L.length && L.data[i] < s; i ++ );
if (i >= L.length) return false;
for (j = i; j < L.length && L.data[j] <= t; j ++ );
for (; j < L.length; i ++ , j ++ )
L.data[i] = L.data[j];
L.length = i;
return true;
}
bool Del_s_t(SqList &L, ElemType s, ElemType t)
{
if (s >= t || L.length == 0) return false;
int i, k = 0;
for (i = 0; i < L.length; i ++ )
{
if (L.data[i] >= s && L.data[i] <= t)
k ++ ;
else
L.data[i - k] = L.data[i];
}
L.length -= k;
return true;
}
bool Delete_Same(SqList &L)
{
if (L.length == 0) return false;
int i, j; // i为第一个不相同的元素,j为工作指针
for (i = 0, j = 1; j < L.length; j ++ )
if (L.data[i] != L.data[j])
L.data[ ++ i] = L.data[j];
L.length = i + 1;
return true;
}
bool Merge(SqList A, SqList B, SqList &C)
{
if (A.length + B.length > C.MaxSize) return false;
int i = 0, j = 0, k = 0;
while (i < A.length && j < B.length)
{
if (A.data[i] <= B.data[j])
C.data[k ++ ] = A.data[i ++ ];
else
C.data[k ++ ] = B.data[j ++ ];
}
while (i < A.length)
C.data[k ++ ] = A.data[i ++ ];
while (j < B.length)
C.data[k ++ ] = B.data[j ++ ];
C.length = k;
return true;
}
typedef int DataType;
void Reverse(DataType A[], int left, int right, int arraySize)
{
if (right <= left || right >= arraySize) return ;
int mid = (left + right) / 2;
for (int i = 0; i <= mid - left; i ++ )
{
DataType temp = A[left + i];
A[left + i] = A[right - i];
A[right - i] = temp;
}
}
void Exchange(DataType A[], int m, int n, int arraySize)
{
Reverse(A, 0, m + n - 1, arraySize);
Reverse(A, 0, n - 1, arraySize);
Reverse(A, n, m + n - 1, arraySize);
}
void SearchExchangeInsert(ElemType A[], ElemType x)
{
int low = 0, high = n - 1, mid;
while (low <= high)
{
mid = (low + high) / 2;
if (A[mid] == x) break;
if (A[mid] < x) low = mid + 1;
else high = mid - 1;
}
if (A[mid] == x && mid != n - 1)
{
t = A[mid], A[mid] = A[mid + 1], A[mid + 1] = t;
}
if (low > high)
{
for (i = n - 1; i > high; i -- )
A[i + 1] = A[i];
A[i + 1] = x; // A[high + 1] = x;
}
}
p(0<p<n)
个位置,即将R中的数据由 ( X 0 , X 1 , . . . , X n − 1 ) (X_0,X_1,...,X_{n-1}) (X0,X1,...,Xn−1)变换为 ( X p , X p + 1 , . . . , X n − 1 , X 0 , X 1 , . . . , X p − 1 ) (X_p,X_{p+1},...,X_{n-1},X_0,X_1,...,X_{p-1}) (Xp,Xp+1,...,Xn−1,X0,X1,...,Xp−1)。要求:void Reverse(int R[], int from, int to)
{
int i, temp;
for (i = 0; i < (to - from + 1) / 2; i ++ )
{
temp = R[from + i];
R[from + i] = R[to - i];
R[to - i] = temp;
}
}
void Converse(int R[], int n, int p)
{
Reverse(R, 0, p - 1);
Reverse(R, p, n - 1);
Reverse(R, 0, n - 1);
}