c语言问题 for (j = l->length - 1; j >= i; j--) l->data[j + 1] = l->data[j]; 这一段有什么用??

#include <stdio.h>#define Listsize 100struct sqlist{ int data[Listsize]; int length;};void InsertList(struct sqlist *l, int t, int i){ int j; if (i < 0 || i > l->length) { printf("position error"); exit(1); } if (l->length >= Listsize) { printf("overflow"); exit(1); } for (j = l->length - 1; j >= i; j--) l->data[j + 1] = l->data[j]; l->data[i] = t; l->length++;}void main(){ struct sqlist *sq; int i, n, t; sq = (struct sqlist*)malloc(sizeof(struct sqlist)); sq->length =0; printf("Please input the size of the list :"); scanf("%d", &n); printf("Please input the elements of the list:尀n"); for (i = 0; i < n; i++) { scanf("%d", &t); InsertList(sq, t, i); } printf("Now the list is:尀n"); for (i = 0; i < sq->length; i++) { printf("%d ", sq->data[i]); } getch();}
2026年09月25日 14:38
有2个网友回答
网友(1):

for (j = l->length - 1; j >= i; j--)
l->data[j + 1] = l->data[j];
这句话可以这样理解:
len = l->lenth;
for(j = len - 1; j >= i; j--) //for循环
{
l->data[j + 1] = l->data[j];
}
j从大到小,也就是说从数组的最后一位开始取数一直取到第一位

网友(2):

把最新输入的内容插入到第一个位置,并且把旧的数据依次往后移动一个位置