from:shiyanlou
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define TRUE 1 5 #define FALSE 0 6 #define OK 1 7 #define ERROR 0 8 #define OVERFLOW -2 9 10 typedef int ElemType; 11 typedef int Status; 12 13 /* 14 * 存储结构 15 */ 16 typedef struct LNode 17 { 18 ElemType data; 19 struct LNode *next; 20 }LNode, *LinkList; 21 22 /* 23 * 初始化线性表 24 */ 25 void InitList(LinkList *L) 26 { 27 *L = (LinkList) malloc(sizeof(LNode)); 28 if (!L) 29 { 30 exit(OVERFLOW); 31 } 32 (*L)->next = NULL; 33 } 34 35 /* 36 * 销毁线性表 37 */ 38 void DestroyList(LinkList *L) 39 { 40 LinkList temp; 41 while (*L) 42 { 43 temp = (*L)->next; 44 free(*L); 45 *L = temp; 46 } 47 } 48 49 /* 50 * 清空线性表 51 */ 52 void ClearList(LinkList L) 53 { 54 LinkList p = L->next; 55 L->next = NULL; 56 DestroyList(&p); 57 } 58 59 /* 60 * 判断是否为空 61 */ 62 Status isEmpty(LinkList L) 63 { 64 if (L->next) 65 { 66 return FALSE; 67 } 68 else 69 { 70 return TRUE; 71 } 72 } 73 74 /* 75 * 获取长度 76 */ 77 int GetLength(LinkList L) 78 { 79 int i = 0; 80 LinkList p = L->next; 81 while (p) 82 { 83 i++; 84 p = p->next; 85 } 86 return i; 87 } 88 89 /* 90 * 根据位置获取元素 91 */ 92 Status GetElem(LinkList L, int i, ElemType *e) 93 { 94 int j = 1; 95 LinkList p = L->next; 96 while (p && j < i) 97 { 98 j++; 99 p = p->next; 100 } 101 if (!p || j > i) 102 { 103 return ERROR; 104 } 105 *e = p->data; 106 return OK; 107 } 108 109 /* 110 * 比较两个元素是否相等 111 */ 112 Status compare(ElemType e1, ElemType e2) 113 { 114 if (e1 == e2) 115 { 116 return 0; 117 } 118 else if (e1 < e2) 119 { 120 return -1; 121 } 122 else 123 { 124 return 1; 125 } 126 } 127 128 /* 129 * 查找指定元素的位置 130 */ 131 int FindElem(LinkList L, ElemType e, Status (*compare)(ElemType, ElemType)) 132 { 133 int i = 0; 134 LinkList p = L->next; 135 while (p) 136 { 137 i++; 138 if (!compare(p->data, e)) 139 { 140 return i; 141 } 142 p = p->next; 143 } 144 return 0; 145 } 146 147 /* 148 * 获取前驱元素 149 */ 150 Status PreElem(LinkList L, ElemType cur_e, ElemType *pre_e) 151 { 152 LinkList q, p = L->next; 153 while (p->next) 154 { 155 q = p->next; 156 if (q->data == cur_e) 157 { 158 *pre_e = p->data; 159 return OK; 160 } 161 p = q; 162 } 163 return ERROR; 164 } 165 166 /* 167 * 获取后继元素 168 */ 169 Status NextElem(LinkList L, ElemType cur_e, ElemType *next_e) 170 { 171 LinkList p = L->next; 172 while (p->next) 173 { 174 if (p->data == cur_e) 175 { 176 *next_e = p->next->data; 177 return OK; 178 } 179 p = p->next; 180 } 181 return ERROR; 182 } 183 184 /* 185 * 插入元素 186 */ 187 Status InsertElem(LinkList L, int i, ElemType e) 188 { 189 int j = 0; 190 LinkList s, p = L; 191 while (p && j < i - 1) 192 { 193 j++; 194 p = p->next; 195 } 196 if (!p || j > i - 1) 197 { 198 return ERROR; 199 } 200 s = (LinkList) malloc(sizeof(LNode)); 201 s->data = e; 202 s->next = p->next; 203 p->next = s; 204 return OK; 205 } 206 207 /* 208 * 删除元素并返回值 209 */ 210 Status DeleteElem(LinkList L, int i, ElemType *e) 211 { 212 int j = 0; 213 LinkList q, p = L; 214 while (p->next && j < i - 1) 215 { 216 j++; 217 p = p->next; 218 } 219 if (!p->next || j > i - 1) 220 { 221 return ERROR; 222 } 223 q = p->next; 224 p->next = q->next; 225 *e = q->data; 226 free(q); 227 return OK; 228 } 229 230 /* 231 * 访问元素 232 */ 233 void visit(ElemType e) 234 { 235 printf("%d ", e); 236 } 237 238 /* 239 * 遍历线性表 240 */ 241 void TraverseList(LinkList L, void (*visit)(ElemType)) 242 { 243 LinkList p = L->next; 244 while (p) 245 { 246 visit(p->data); 247 p = p->next; 248 } 249 } 250 251 int main() 252 { 253 LinkList L; 254 InitList(&L); 255 ElemType e; 256 int i; 257 if (L) 258 { 259 printf("init success\n"); 260 } 261 262 if (isEmpty(L)) 263 { 264 printf("list is empty\n"); 265 } 266 267 for (i = 0; i < 10; i++) 268 { 269 InsertElem(L, i + 1, i); 270 } 271 272 if (GetElem(L, 1, &e)) { 273 printf("The first element is %d\n", e); 274 } 275 276 printf("length is %d\n", GetLength(L)); 277 278 printf("The 5 at %d\n", FindElem(L, 5, *compare)); 279 280 PreElem(L, 6, &e); 281 printf("The 6‘s previous element is %d\n", e); 282 283 NextElem(L, 6, &e); 284 printf("The 6‘s next element is %d\n", e); 285 286 DeleteElem(L, 1, &e); 287 printf("delete first element is %d\n", e); 288 289 printf("list:"); 290 TraverseList(L,visit); 291 292 DestroyList(&L); 293 if (!L) { 294 printf("\ndestroy success\n"); 295 } 296 }
时间: 2024-11-08 20:56:42