summaryrefslogtreecommitdiff
path: root/c/dataStructure/线性表/线性链表/hmain.c
diff options
context:
space:
mode:
Diffstat (limited to 'c/dataStructure/线性表/线性链表/hmain.c')
-rwxr-xr-xc/dataStructure/线性表/线性链表/hmain.c207
1 files changed, 207 insertions, 0 deletions
diff --git a/c/dataStructure/线性表/线性链表/hmain.c b/c/dataStructure/线性表/线性链表/hmain.c
new file mode 100755
index 0000000..20aaa92
--- /dev/null
+++ b/c/dataStructure/线性表/线性链表/hmain.c
@@ -0,0 +1,207 @@
+#include "list.h"
+
+static void eatline(void)
+{
+ while (getchar() != '\n')
+ continue;
+}
+
+static char get_answer(void)
+{
+ printf("\e[39m\nPlease enter choice > ");
+ return tolower(getchar());
+}
+
+static bool CheckList(bool flag)
+{
+ if (!flag)
+ {
+ printf("List does not exist! Please initialize it!\n");
+ return false;
+ }
+ return true;
+}
+
+int main(void)
+{
+ List L;
+ char ans;
+ ElemType e;
+ int pos;
+ bool flag = false;
+
+ printf("\e[1;1H\e[2J");
+
+ OperateMenu();
+
+ while ((ans = get_answer()) != 'q')
+ {
+ if (strchr("abcdefghijklq",ans) == NULL)
+ {
+ fprintf(stderr,"Please enter choice labeled above!\n");
+ if (ans != '\n')
+ eatline();
+ continue;
+ }
+
+ eatline();
+
+ if (ans == 'k') // Show menu
+ {
+ putchar('\n');
+ OperateMenu();
+ continue;
+ }
+
+ if (ans == 'b') // Initialize list
+ {
+ InitList(&L);
+ flag = true;
+ continue;
+ }
+
+ if (CheckList(flag) == false) // Check initialization
+ continue;
+
+ else if (ans == 'l') // Destroy list
+ DestroyList(&L);
+
+ else if (ans == 'a') // Add element
+ {
+ printf("Please enter an number: ");
+ scanf("%d", &e);
+ eatline();
+ if (AddElem(&L, e))
+ printf("Element adding successful!\n");
+ else
+ printf("Element adding failed, please run debug mode!\n");
+ }
+
+ else if (ans == 'c') // Show list length
+ printf("List contains %d element!\n",ListLength(&L));
+
+ else if (ans == 'd') // Get element by position
+ {
+ printf("Enter position you want to get: ");
+ scanf("%d",&pos);
+ eatline();
+ int i = ListLength(&L);
+
+ if (pos > i || pos <= 0)
+ {
+ printf("Position error, List now has %i elements!\n", i);
+ continue;
+ }
+
+ printf("Position %d: %d",pos,GetElem(&L,pos));
+ }
+
+ else if (ans == 'e') // Get position by element
+ {
+ printf("Enter element value: ");
+ scanf("%d", &e);
+ eatline();
+
+ if ((pos = LocateElem(&L,e)) == 0)
+ {
+ printf("Element %d is not in this list!\n",e);
+ continue;
+ }
+
+ printf("Element position: %d\n",pos);
+ }
+
+ else if (ans == 'f') // Get prior element
+ {
+ printf("Enter element: ");
+ scanf("%d", &e);
+ eatline();
+
+ if ((pos = LocateElem(&L,e)) == 0)
+ {
+ printf("Element %d is not in this list!\n",e);
+ continue;
+ }
+
+ else if (pos == 1)
+ {
+ printf("First element, no prior element!\n");
+ continue;
+ }
+
+ printf("Prior element of element %d is %d\n",
+ e,PriorElem(&L,e));
+ }
+
+ else if (ans == 'g') // Get next element
+ {
+ printf("Enter element: ");
+ scanf("%d", &e);
+ eatline();
+
+ if ((pos = LocateElem(&L,e)) == 0)
+ {
+ printf("Element %d is not in this list!\n",e);
+ continue;
+ }
+
+ else if(pos == ListLength(&L))
+ {
+ printf("Last element, no next element!\n");
+ continue;
+ }
+
+ printf("Next element of element %d is %d\n",
+ e,NextElem(&L,e));
+ }
+
+ else if (ans == 'h') // Insert an element
+ {
+ printf("Enter position you want to put: ");
+ scanf("%d", &pos);
+ printf("Enter element you want to add: ");
+ scanf("%d", &e);
+ eatline();
+
+ if (!InsertElem(&L,e,pos))
+ {
+ printf("Insert error!\n");
+ continue;
+ }
+ printf("Insert successful!\n");
+ }
+
+ else if (ans == 'i') // Delete an element
+ {
+ int i = ListLength(&L);
+ printf("Enter position you want to delete: ");
+ scanf("%d", &pos);
+ eatline();
+
+ if (pos <= 0)
+ {
+ printf("position should be greater than 0!\n");
+ continue;
+ }
+
+ else if (pos > i || !DeleteElem(&L,pos))
+ {
+ printf("Position error, %d elements in list\n",
+ i);
+ puts("Delete failed");
+ continue;
+ }
+ puts("Delete successful!");
+ }
+
+ else if (ans == 'j')
+ TraverseList(&L);
+ }
+
+ if (flag)
+ free(L);
+
+ puts("Thanks for using this utility!\n");
+
+ return 0;
+}