diff options
Diffstat (limited to 'c/dataStructure/线性表/线性链表/blist')
-rwxr-xr-x | c/dataStructure/线性表/线性链表/blist/bhlist.c | 76 | ||||
-rwxr-xr-x | c/dataStructure/线性表/线性链表/blist/bhlist.h | 23 | ||||
-rwxr-xr-x | c/dataStructure/线性表/线性链表/blist/main.c | 24 |
3 files changed, 123 insertions, 0 deletions
diff --git a/c/dataStructure/线性表/线性链表/blist/bhlist.c b/c/dataStructure/线性表/线性链表/blist/bhlist.c new file mode 100755 index 0000000..1c0dbc8 --- /dev/null +++ b/c/dataStructure/线性表/线性链表/blist/bhlist.c @@ -0,0 +1,76 @@ +// Implement +#include "bhlist.h" + +void InitList (Blist * L) +{ + *L = NULL; +} + +bool AddElem(Blist * L, int e) +{ + Blist scan = *L; + BNode * new = (BNode *) malloc (sizeof(BNode)); + + if (!new) + { + fprintf(stderr,"Memory error!\n"); + return false; + } + + new->data = e; + new->next = NULL; + + if (!scan) + { + new->prior = NULL; + *L = new; + } + + else + { + while (scan->next) + scan = scan->next; + new->prior = scan; + scan->next = new; + } + + return true; +} + +void DestroyList (Blist * L) +{ + Blist t1 = *L; + Blist t2; + + while (t1) + { + t2 = t1->next; + free(t1); + t1 = t2; + } +} + +void TraverseList(Blist * L) +{ + Blist tmp = *L; + Blist ta,tb; + + while (tmp) + { + printf("Element is %d\n",tmp->data); + tb = tmp->prior; + ta = tmp->next; + if (tb) + printf("It's prior element is %d, ",tb->data); + else + printf("No element before it, "); + + if (ta) + printf("and it's next element is %d!\n",ta->data); + else + printf("No element after it!\n"); + + tmp = tmp->next; + } + puts("Traverse DONE!"); +}
\ No newline at end of file diff --git a/c/dataStructure/线性表/线性链表/blist/bhlist.h b/c/dataStructure/线性表/线性链表/blist/bhlist.h new file mode 100755 index 0000000..659750a --- /dev/null +++ b/c/dataStructure/线性表/线性链表/blist/bhlist.h @@ -0,0 +1,23 @@ +#ifndef _BHLIST_H +#define _BHLIST_H + +#include<stdio.h> +#include<stdbool.h> +#include<stdlib.h> + +typedef struct bnode { + int data; + struct bnode * prior; + struct bnode * next; +} BNode; + +typedef BNode * Blist; + +void InitList (Blist * L); +bool AddElem (Blist * L, int e); +bool InsertElem (Blist * L, int e, int i); +bool DeleteElem (Blist * L, int e, int i); +void DestroyList (Blist * L); +void TraverseList (Blist * L); + +#endif
\ No newline at end of file diff --git a/c/dataStructure/线性表/线性链表/blist/main.c b/c/dataStructure/线性表/线性链表/blist/main.c new file mode 100755 index 0000000..09b4247 --- /dev/null +++ b/c/dataStructure/线性表/线性链表/blist/main.c @@ -0,0 +1,24 @@ +// Driver +#include "bhlist.h" + +int main(void) +{ + Blist number; + + int num; + + InitList(&number); + + printf("Please enter number(q to quit): "); + while (scanf("%d", &num) == 1) + AddElem(&number,num); + + printf("Add element done! Now shows them!\n"); + printf("%d\n",number->data); + + TraverseList(&number); + + DestroyList(&number); + + return 0; +}
\ No newline at end of file |