summaryrefslogtreecommitdiff
path: root/c/dataStructure/线性表/线性链表/nhlist.c
blob: d37248da6c1f6083becf6ee9b0664ae4189a89eb (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
/* Function declaration -- Linked list without head pointer */

#include "list.h"

void InitList(List * L)
{
    *L = NULL;
}

void DestroyList(List * L)
{
    List tmp;

    while (*L)
    {
        tmp = (*L)->next;
        free(*L);
        *L = tmp;
    }
}

bool AddElem(List * L, ElemType e)
{
    List scan = *L;
    
    List new = (LNode *) malloc (sizeof(LNode));

    if (!new)
    {
        fprintf(stderr,"Failed Adding element\n");
        return false;
    }

    new->data = e;
    new->next = NULL;

    if (!*L)
        *L = new;
    else
    {
        while (scan->next)
            scan = scan->next;
        scan->next = new;
    }

    return true;
}

bool ListEmpty (List * L)
{
    if (!*L)
        return true;
    return false;
}

int ListLength (List * L)
{
    List tmp = *L;
    int count = 0;
    while (tmp)
    {
        tmp = tmp->next;
        count++;
    }

    return count;
}

ElemType GetElem (List * L, int i)
{
    int n = ListLength(L);
    if (i > n)
    {
        fprintf(stderr,"Position error, Please restricted between 0 and %d",n);
        return EOF;
    }
    List tmp = *L;

    for (int j = 0; j < i; j++)
        tmp = tmp->next;
    
    if (tmp)
        return tmp->data;
    return 0;
}

void TraverseList (List * L)
{
    List tmp = *L;

    while (tmp)
    {
        printf("%7d",tmp->data);
        tmp = tmp->next;
    }
    putchar('\n');
}

bool InsertElem (List * L, ElemType e, int i)
{
    bool flag = false;
    List tmp = *L;
    List new = (LNode *) malloc (sizeof(LNode));

    int n = ListLength(&tmp);

    if (!new)
    {
        fprintf(stderr,"Failed to create new node!\n");
        return flag;
    }

    new->data = e;

    if (i < 1)
        fprintf(stderr,"Invalid position!\n");

    else if (i == 1)
    {
        new->next = tmp;
        *L = new;
        flag = true;
    }

    else
    {
        for (int j = 2; j < i; j++)
            if (tmp->next)
                tmp = tmp->next;
            else
                break;
        
        if (i > n)
            printf("List length is %d, append element to last!\n",n);

        new->next = tmp->next;
        tmp->next = new;
        flag = true;
    }
    return flag;
}

bool DeleteElem (List * L, int i)
{
    List t1 = *L;
    List t2;
    bool flag = false;
    int n = ListLength(&t1);

    if (i < 1)
        fprintf(stderr, "Position error!\n");

    else if (i == 1)
    {
        t2 = t1;
        *L = t1->next;
        free(t2);
        flag = true;
    }

    else if (i <= n)
    {
        for (int j = 2; j < i; j++)
            if (t1->next)
                t1 = t1->next;
            else
                break;
                
        if (t1->next)
        {
            t2 = t1->next;
            t1->next = t2->next;
            free(t2);
            flag = true;
        }
    }

    else
        printf("No element at position %d\n",i);
    return flag;
}