1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <stdio.h> #include <stdlib.h> #define MaxSize 10
typedef struct { int data[MaxSize]; int length; } SqList;
bool ListInsert(SqList *L, int i, int e) { if (i < 1 || i > L->length + 1) return false; if (L->length >= MaxSize) return false; for (int j = L->length; j >= i; j--) L->data[j] = L->data[j - 1]; L->data[i - 1] = e; L->length++; return true; }
void InitList(SqList *&L) { L = (SqList *)malloc(sizeof(SqList)); L->length = 0; }
void AddList(SqList *L) { int i = 0; while (i <= 5) { L->length++; L->data[i] = i; i++; } }
void printList(SqList *L) { for (int i = 0; i < L->length; i++) { printf("%d", L->data[i]); } }
int main() { SqList *L; InitList(L);
AddList(L);
ListInsert(L, 3, 3);
printList(L); return 0; }
|