summaryrefslogtreecommitdiff
path: root/c/dataStructure/线性表/线性链表/hmain.c
blob: 20aaa92082878007dd60fa103064e674f10751bf (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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;
}