summaryrefslogtreecommitdiff
path: root/c/dataStructure/线性表/线性链表/blist/bhlist.c
blob: 1c0dbc8303531b3df650353f69fc3f799e152f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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!");
}