#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int length;
}SequenceList;
void insert(SequenceList* L, int index, int element) {
if (L->length == MAX_SIZE) {
printf("Error: the SequenceList if full.\n");
return;
}
if (index < 0 || index > L->length) {
printf("Error index");
return;
}
for (int i = L->length - 1; i >= index; i--) {
L->data[i + 1] = L->data[i];
}
L->data[index] = element;
L->length++;
}
int main() {
SequenceList L = { {1,2,3,4,5,6},6 };
printf("Original sequence list length: %d and element: ", L.length);
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
insert(&L, 3, 7);
printf("After insertion sequence list length: %d and element: ", L.length);
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
system("pause");
return 0;
}
顺序表的插入
最新推荐文章于 2024-05-20 19:43:04 发布