刪除順序表下標從i到j的所有元素
阿新 • • 發佈:2018-11-05
#include <stdio.h> /* 題目:天勤40頁(4) 從一給定的順序表L中刪除下標i至j的所有元素, i<=j,包括i和j, 假定i和j都是合法的 演算法: 本質是元素的覆蓋操作,並把順序表的length作及時的更新: list.arr[i]被list.arr[j+1]覆蓋 list.arr[i+1]被list.arr[j+2]覆蓋 ...... list.arr[i+length-2-j]被list.arr[j+length-1-j]覆蓋 list.length更新為list.length-(j-i+1) */ #define maxlen 50 #define DATATYPE int typedef struct{ DATATYPE arr[maxlen]; int length; }Seqlist; void print_SQ(Seqlist list) { for(int i=0;i<list.length;i++){ printf("%d ",list.arr[i]); } printf("\n"); } int main(void) { Seqlist list = {{1,2,3,4,5,6,7,8,9,10},10}; int i = 3; int j = 7; for(int k=1;k<=list.length-j-1;k++){ list.arr[i+k-1] = list.arr[j+k]; } list.length = list.length-(j-i+1); print_SQ(list); }