设计一个算法,从一给定的顺序表L中删除下标i~j(i<=j,包括i、j)的所有元素,假定i、j都是合法的。
c语言
#include<stdio.h>
int deleteElemij(int sqList[], int &length, int i,int j)
{
if (i<0 || j>length - 1)
return 0;
int c = j - i + 1;
for (int n = i-1; n < length ; ++n)
sqList[n] = sqList[n + c];
length = length - c;
return 1;
}
int main()
{
int A[] = {1,2,3,4,5,6,7,8,9,10};
int length = 10;
deleteElemij(A, length, 3, 5);
for (int i = 0; i < length; ++i)
printf("%d ",A[i]);
return 0;
}
c++
#include<iostream>
using namespace std;
void del(int arr[],int &length,int i,int j)
{
int k, delta;
delta = j - i + 1;
for (k = j + 1; k < length; ++k)
arr[k - delta] = arr[k];
length = length - delta;
}
int main()
{
int A[] = { 1,2,3,4,5,6,7,8,9,10 };
int length = 10;
del(A, length, 3, 5);
for (int i = 0; i < length; ++i)
cout << A[i] << " ";
cout << endl << length;
return 0;
}