summaryrefslogtreecommitdiff
path: root/c/dataStructure/线性表/线性链表/hlist.c
diff options
context:
space:
mode:
Diffstat (limited to 'c/dataStructure/线性表/线性链表/hlist.c')
-rwxr-xr-xc/dataStructure/线性表/线性链表/hlist.c198
1 files changed, 198 insertions, 0 deletions
diff --git a/c/dataStructure/线性表/线性链表/hlist.c b/c/dataStructure/线性表/线性链表/hlist.c
new file mode 100755
index 0000000..fc4cf8a
--- /dev/null
+++ b/c/dataStructure/线性表/线性链表/hlist.c
@@ -0,0 +1,198 @@
+#include "list.h"
+
+void InitList (List * L)
+{
+ *L = (LNode *) malloc (sizeof(LNode));
+
+ if (!*L)
+ {
+ fprintf(stderr,"Initialization Failed\n");
+ exit(EXIT_FAILURE);
+ }
+
+ (*L)->next = NULL;
+
+ printf("Initialization Successful!\n");
+}
+
+void DestroyList (List * L)
+{
+ // reserve head pointer
+ List T = *L;
+ List tmp;
+
+ if (!T->next)
+ printf("No element in list!\n");
+ else
+ {
+ while (T->next)
+ {
+ tmp = T->next->next;
+ free(T->next);
+ T->next = tmp;
+ }
+
+ if (!T->next)
+ printf("Destroyed list!\n");
+ else
+ printf("Error during destroy list");
+ }
+}
+
+bool AddElem (List * L, ElemType e)
+{
+ List new = *L;
+
+ while (new->next)
+ new = new->next;
+
+ new->next = (LNode *) malloc (sizeof(LNode));
+
+ if (!new)
+ return false;
+ new = new->next;
+ new->data = e;
+ new->next = NULL;
+
+ return true;
+}
+
+int ListLength (List * L)
+{
+ int count = 0;
+
+ List tmp = *L;
+
+ while (tmp->next)
+ {
+ tmp = tmp->next;
+ count++;
+ }
+
+ return count;
+}
+
+ElemType GetElem (List * L, int i)
+{
+ List tmp = *L;
+
+ for (int j = 1; j < i; j++)
+ tmp = tmp->next;
+ return tmp->next->data;
+}
+
+int LocateElem (List * L, ElemType e)
+{
+ int i = 1;
+ List tmp = *L;
+
+ while (tmp->next && tmp->next->data != e)
+ {
+ tmp = tmp->next;
+ i++;
+ }
+
+ if (!tmp->next)
+ return 0;
+
+ return i;
+}
+
+ElemType PriorElem(List * L, ElemType e)
+{
+ List tmp = *L;
+ while (tmp->next->data != e)
+ tmp = tmp->next;
+
+ return tmp->data;
+}
+
+ElemType NextElem(List * L, ElemType e)
+{
+ List tmp = *L;
+ while (tmp->next->data != e)
+ tmp = tmp->next;
+
+ tmp = tmp->next->next;
+
+ return tmp->data;
+}
+
+bool InsertElem (List * L, ElemType e, int i)
+{
+ List scan = *L;
+ List new;
+ int tmp = 1;
+
+ new = (LNode *) malloc (sizeof(LNode));
+
+ if (!new)
+ return false;
+
+ while (scan->next && tmp < i)
+ {
+ scan = scan->next;
+ tmp++;
+ }
+
+
+ if (!scan->next)
+ printf("Reached list end. Element appended on.\n");
+
+ new->data = e;
+ new->next = scan->next;
+ scan->next = new;
+
+ return true;
+}
+
+bool DeleteElem (List * L, int i)
+{
+ List tmp1 = *L;
+ List tmp2 = tmp1->next;
+
+ for (int j = 1; j < i; j++)
+ {
+ tmp1 = tmp1->next;
+ tmp2 = tmp2->next;
+ }
+
+ if (!tmp2)
+ return false;
+
+ tmp1->next = tmp2->next;
+ free(tmp2);
+
+ return true;
+}
+
+void TraverseList (List * L)
+{
+ int i = 1;
+ List tmp = (*L)->next;
+
+ if (!tmp)
+ printf("No element could be traversed!\n");
+ else
+ {
+ puts("Traverse list:");
+ while (tmp)
+ {
+ printf("Node %d:\t%d\n",i++,tmp->data);
+ tmp = tmp->next;
+ }
+ puts("Traverse done!\n");
+ }
+}
+
+void OperateMenu (void)
+{
+ puts("List Operation Menu\n");
+ puts("\e[34ma. Add element b. Initialize list");
+ puts("c. List status d. Get element from specific position");
+ puts("e. Search element f. Find prior element");
+ puts("g. Find next element h. Insert an element");
+ puts("i. Delete an element j. Traverse list");
+ puts("k. Show Menu l. Destroy list");
+ puts("q. Quit");
+}