summaryrefslogtreecommitdiff
path: root/c/dataStructure/线性表/顺序表/sequence.c
diff options
context:
space:
mode:
Diffstat (limited to 'c/dataStructure/线性表/顺序表/sequence.c')
-rwxr-xr-xc/dataStructure/线性表/顺序表/sequence.c151
1 files changed, 151 insertions, 0 deletions
diff --git a/c/dataStructure/线性表/顺序表/sequence.c b/c/dataStructure/线性表/顺序表/sequence.c
new file mode 100755
index 0000000..8a26488
--- /dev/null
+++ b/c/dataStructure/线性表/顺序表/sequence.c
@@ -0,0 +1,151 @@
+#include "sequence.h"
+
+void InitList(Elem * L)
+{
+ L->elem = (ElemType *) malloc (sizeof(ElemType) * MAXSIZE);
+
+ if (!(L->elem))
+ {
+ fprintf(stderr,"Initialization Failed\nPlease run as debug mode\n");
+ exit(EXIT_FAILURE);
+ }
+
+ L->length = 0;
+
+ puts("Initialization Successful!");
+ printf("You now have %d storage sequence list!\n",MAXSIZE);
+}
+
+void DestroyList(Elem * L)
+{
+ if (L->elem)
+ free(L->elem);
+ else
+ printf("Sequence list is not exist!\n");
+}
+
+void AddElem (Elem * L, ElemType e)
+{
+ if (L->length >= MAXSIZE)
+ {
+ printf("No memory for more data\n"
+ "You could try to release some unused item.\n");
+ puts("Add failed");
+ }
+
+ else
+ {
+ int i = 0;
+
+ while (i < L->length)
+ i++;
+
+ *(L->elem + i) = e;
+ L->length++;
+ }
+}
+
+bool ListEmpty (Elem * L)
+{
+ return L->length = 0 ? true:false;
+}
+
+ElemType ListLength (Elem * L)
+{
+ return L->length;
+}
+
+ElemType GetElem (Elem * L, int i)
+{
+ if (i > L->length)
+ {
+ fprintf(stderr,"%d is out of index\n"
+ "Please keep index between 0 and %d\n",
+ i, L->length);
+ return -1;
+ }
+ return *(L->elem + i - 1);
+}
+
+ElemType LocateElem (Elem * L, ElemType e)
+{
+ for (int i = 0; i < L->length; i++)
+ {
+ if (e == *(L->elem + i))
+ return i + 1;
+ }
+
+ return 0;
+}
+
+ElemType PriorELem (Elem * L, ElemType e)
+{
+ ElemType i = 0;
+
+ while (e != *(L->elem + i) && (i < L->length))
+ i++;
+ if (i == 0)
+ {
+ printf("The value is at first, so no element before it\n");
+ free(L->elem);
+ exit(EXIT_FAILURE);
+ }
+ else if (i >= L->length)
+ {
+ printf("The element is not found.\n");
+ free(L->elem);
+ exit(EXIT_FAILURE);
+ }
+ return *(L->elem + i - 1);
+}
+
+ElemType NextElem (Elem * L, ElemType e)
+{
+ ElemType i = 0;
+
+ while (e != *(L->elem + i) && (i < L->length - 1))
+ i++;
+ if (i == L->length - 1)
+ {
+ printf("Not found this element in list.\n");
+ free(L->elem);
+ exit(EXIT_FAILURE);
+ }
+ return *(L->elem + i);
+}
+
+bool ListInsert (Elem * L, ElemType e, int i)
+{
+ if (i < 1 || i >= L->length)
+ {
+ fprintf(stderr,"Index should be between 0 and %d\n",
+ L->length);
+ return false;
+ }
+
+ for (int n = L->length; n > i - 1; n--)
+ L->elem[n] = L->elem[n - 1];
+ L->elem[i-1] = e;
+ L->length++;
+
+ return true;
+}
+
+bool ListDelete (Elem * L, int i)
+{
+ if (i < 1 || i >= L->length)
+ {
+ fprintf(stderr,"Index should be between 0 and %d\n",
+ L->length);
+ return false;
+ }
+
+ printf("Element %d has been deleted\n", L->elem[i-1]);
+
+ for (int n = i; n < L->length; n++)
+ {
+ L->elem[n-1] = L->elem[n];
+ }
+ L->length--;
+ return true;
+} \ No newline at end of file