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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| #include<stdio.h> #define maxSize 7 typedef struct { char data; int cur; }component;
void reserveArr(component *array);
int initArr(component *array);
void insertArr(component * array,int body,int add,char a);
void deletArr(component * array,int body,char a);
int selectElem(component * array,int body,char elem);
void amendElem(component * array,int body,char oldElem,char newElem);
void displayArr(component * array,int body);
int mallocArr(component * array); void freeArr(component * array,int k); int main() { component array[maxSize]; int body=initArr(array); printf("静态链表为:\n"); displayArr(array, body); printf("在第3的位置上插入结点‘e’:\n"); insertArr(array, body, 3,'e'); displayArr(array, body); printf("删除数据域为‘a’的结点:\n"); deletArr(array, body, 'a'); displayArr(array, body); printf("查找数据域为‘e’的结点的位置:\n"); int selectAdd=selectElem(array,body ,'e'); printf("%d\n",selectAdd); printf("将结点数据域为‘e’改为‘h’:\n"); amendElem(array, body, 'e', 'h'); displayArr(array, body); return 0; }
void reserveArr(component *array){ for (int i=0; i<maxSize; i++) { array[i].cur=i+1; } array[maxSize-1].cur=0; }
int mallocArr(component * array){ int i=array[0].cur; if (array[0].cur) { array[0].cur=array[i].cur; } return i; }
int initArr(component *array){ reserveArr(array); int body=mallocArr(array); int tempBody=body; for (int i=1; i<5; i++) { int j=mallocArr(array); array[tempBody].cur=j; array[j].data='a'+i-1; tempBody=j; } array[tempBody].cur=0; return body; }
void insertArr(component * array,int body,int add,char a){ int tempBody=body; for (int i=1; i<add; i++) { tempBody=array[tempBody].cur; } int insert=mallocArr(array); array[insert].cur=array[tempBody].cur; array[insert].data=a; array[tempBody].cur=insert; }
void deletArr(component * array,int body,char a){ int tempBody=body; while (array[tempBody].data!=a) { tempBody=array[tempBody].cur; if (tempBody==0) { printf("链表中没有此数据"); return; } } int del=tempBody; tempBody=body; while (array[tempBody].cur!=del) { tempBody=array[tempBody].cur; } array[tempBody].cur=array[del].cur; freeArr(array, del); }
int selectElem(component * array,int body,char elem){ int tempBody=body; while (array[tempBody].cur!=0) { if (array[tempBody].data==elem) { return tempBody; } tempBody=array[tempBody].cur; } return -1; }
void amendElem(component * array,int body,char oldElem,char newElem){ int add=selectElem(array, body, oldElem); if (add==-1) { printf("无更改元素"); return; } array[add].data=newElem; }
void displayArr(component * array,int body){ int tempBody=body; while (array[tempBody].cur) { printf("%c,%d ",array[tempBody].data,array[tempBody].cur); tempBody=array[tempBody].cur; } printf("%c,%d\n",array[tempBody].data,array[tempBody].cur);
} void freeArr(component * array,int k){ array[k].cur=array[0].cur; array[0].cur=k; }
|