标签:des style blog class code java
1 /* 2 ============================================================================ 3 Name : sqlist.c 4 Author : codecup 5 Version : 6 Copyright : Your copyright notice 7 Description : Hello World in C, Ansi-style 8 ============================================================================ 9 */ 10 11 #include <stdio.h> 12 #include <stdlib.h> 13 14 #define MAXSIZE 20 15 #define OK 1 16 #define ERROR 0 17 #define TRUE 1 18 #define FALSE 0 19 20 typedef int Status; 21 22 typedef int ElementType; 23 typedef struct { 24 ElementType data[MAXSIZE]; //定义数组用于存储数据 25 int length; //线性表当前的长度 26 } SqList; 27 28 //初始化线性表 29 void initSqList(SqList *L) { 30 L->length = 0; 31 } 32 33 //获取线性表中的元素 34 Status getElem(SqList L, int i, ElementType *e) { 35 if (L.length == 0 || i < 1 || i > L.length) { 36 return ERROR; 37 } 38 *e = L.data[i - 1]; 39 return OK; 40 } 41 42 //插入元素 43 Status insert(SqList *L, int i, ElementType e) { 44 int j; 45 if (L->length == MAXSIZE) { 46 return ERROR; 47 } 48 if (i < 1 || i > L->length + 1) { 49 return ERROR; 50 } 51 if (i < L->length) { 52 for (j = L->length - 1; j > i; j--) { 53 L->data[j + 1] = L->data[j]; 54 } 55 } 56 L->data[i - 1] = e; 57 L->length++; 58 return OK; 59 } 60 61 //删除元素 62 Status delete(SqList *L, int i, ElementType *e) { 63 int k; 64 if (L->length == 0) { 65 return ERROR; 66 } 67 if (i < 1 || i > L->length) { 68 return ERROR; 69 } 70 *e = L->data[i - 1]; 71 if (i < L->length) { 72 for (k = i; k < L->length; ++k) { 73 L->data[k - 1] = L->data[k]; 74 } 75 } 76 L->length--; 77 return OK; 78 } 79 80 int main(void) { 81 SqList L; 82 ElementType e; 83 initSqList(&L); 84 insert(&L, 1, 187); 85 getElem(L, 1, &e); 86 printf("%d", e); 87 return EXIT_SUCCESS; 88 }
标签:des style blog class code java
原文地址:http://www.cnblogs.com/codecup/p/3705023.html