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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| #include <stdlib.h> #include <stdio.h> #include <stdbool.h> #define MAXSIZE 10 #define ERROR -1
typedef int Position; typedef int ElementType; typedef struct LNode *PtrToLNode; struct LNode { ElementType Data[MAXSIZE]; Position Last; }; typedef PtrToLNode List;
List MakeEmpty() { List L; L = (List) malloc(sizeof(struct LNode)); L->Last = -1; return L; }
Position Find(List L, ElementType X) { Position i = 0; while (i <= L->Last && L->Data[i] != X) { i++; } if (i > L->Last) { return ERROR; } return i; }
bool Insert(List L, ElementType X, int i) { if (L->Last == MAXSIZE - 1) { printf("表满"); return false; } if (i < 1 || i > L->Last + 2) { printf("位序不合法"); return false; } Position j; for (j = L->Last; j >= i - 1; j--) { L->Data[j + 1] = L->Data[j]; } L->Data[i - 1] = X; L->Last++; return true; }
bool Delete(List L, int i) { if (i < 1 || i > L->Last) { printf("位序%d不正确", i); return false; } Position j; for (j = i - 1; j < L->Last; j++) { L->Data[j] = L->Data[j + 1]; } L->Last--; return true; }
|