刪除陣列中特定的元素或者某一範圍的元素
阿新 • • 發佈:2019-02-16
//刪除順序表中某範圍的數 #include<iostream> #include<math.h> #include<string.h> using namespace std; typedef struct { int a[100]; int length; } Seqlist; int main() { bool del_s_t(Seqlist&L,int s,int t); Seqlist l; int i; for( i=0; i<10; i++) { l.a[i]=i+1; } l.length=i; del_s_t(l,3,7); for( i=0; i<l.length; i++) { printf("%d ",l.a[i]); } printf("\n"); cout<<l.length<<endl; return 0; } bool del_s_t(Seqlist&L,int s,int t) { int i,k=0; if(L.length==0||s>=t) { return false; } for(i=0; i<L.length; i++) { if(L.a[i]>=s&&L.a[i]<=t) k++; else L.a[i-k]=L.a[i]; } L.length-=k; return true; }