summaryrefslogtreecommitdiff
path: root/c/dataStructure/线性表/顺序表/sequence.c
blob: 8a2648810c3dd3187d77fb81c610c978867a3e67 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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;
}