View difference between Paste ID: zrXQGhhc and Lygw2bUj
SHOW: | | - or go back to the newest paste.
1
#include <stdio.h>
2
#include <stdlib.h>
3
4
struct dll_node
5
{
6
    int data;
7
    struct dll_node *prev, *next;
8
};
9
10
struct dll_node *create_list(int data)
11
{
12
    struct dll_node *new_node = (struct dll_node *)
13
        malloc(sizeof(struct dll_node));
14
    if (NULL != new_node)
15
    {
16
        new_node->data = data;
17
        new_node->prev = new_node->next = new_node;
18
    }
19
    return new_node;
20
}
21
22
struct dll_node *find_max_node(struct dll_node *node)
23
{
24
    struct dll_node *start = node, *result = node;
25
    int maximum = node->data;
26
    do
27
    {
28
        if (maximum < node->data)
29
        {
30
            maximum = node->data;
31
            result = node;
32
        }
33
        node = node->next;
34
    } while (node != start);
35
    return result;
36
};
37
38
struct dll_node *find_next_node(struct dll_node *node, int data)
39
{
40
    node = find_max_node(node);
41
    struct dll_node *start = node;
42
    do
43
    {
44
        if (node->data < data)
45
            break;
46
        node = node->next;
47
    } while (node != start);
48
    return node;
49
};
50
51
void insert_node(struct dll_node *node, int data)
52
{
53
    if (NULL == node)
54
        return;
55
56
    struct dll_node *new_node = (struct dll_node *)
57
        malloc(sizeof(struct dll_node));
58
    if (NULL != new_node)
59
    {
60
        new_node->data = data;
61
        node = find_next_node(node, data);
62
        new_node->next = node;
63
        new_node->prev = node->prev;
64
        node->prev->next = new_node;
65
        node->prev = new_node;
66
    }
67
}
68
69
struct dll_node *delete_node(struct dll_node *node, int data)
70
{
71
    if (NULL == node)
72
        return NULL;
73
74
    node = find_next_node(node, data);
75
    node = node->prev;
76
    if (node->data == data)
77
    {
78
        if (node == node->next)
79
        {
80
            free(node);
81
            return NULL;
82
        }
83
        else
84
        {
85
            struct dll_node *next = node->next;
86
            node->prev->next = node->next;
87
            node->next->prev = node->prev;
88
            free(node);
89
            node = next;
90
        }
91
    }
92
    return node;
93
}
94
95
void print_list(struct dll_node *node)
96
{
97
    if (NULL == node)
98
        return;
99
100
    node = find_max_node(node);
101
    struct dll_node *start = node;
102
    do
103
    {
104
        printf("%d ", node->data);
105
        node = node->next;
106
    } while (node != start);
107
    printf("\n");
108
}
109
110
void remove_list(struct dll_node **node)
111
{
112
    if (NULL == *node)
113
        return;
114
115
    struct dll_node *start = *node;
116
    do
117
    {
118
        struct dll_node *next = (*node)->next;
119
        free(*node);
120
        *node = next;
121
    } while (*node != start);
122
    *node = NULL;
123
}
124
125
void usun_rek(struct dll_node **ptr,struct dll_node *poczatek)
126
{
127
    if (*ptr)
128
    {
129
        if((*ptr)->next!=poczatek)
130
            usun_rek(&((*ptr)->next),poczatek);
131
132
        *ptr = delete_node(*ptr,(*ptr)->data);
133
    }
134
}
135
136
int main()
137
{
138
    struct dll_node *dlcl = create_list(1);
139
    int i;
140
141
    for (i=2; i<5; i++)
142
        insert_node(dlcl, i);
143
    for (i=6; i<10; i++)
144
        insert_node(dlcl, i);
145
    printf("List elements:\n");
146
    print_list(dlcl);
147
148
    insert_node(dlcl, 0);
149
    printf("List elements after insertion of 0:\n");
150
    print_list(dlcl);
151
    insert_node(dlcl, 5);
152
    printf("List elements after insertion of 5:\n");
153
    print_list(dlcl);
154
    insert_node(dlcl, 7);
155
    printf("List elements after insertion of 7:\n");
156
    print_list(dlcl);
157
    insert_node(dlcl, 10);
158
    printf("List elements after insertion of 10:\n");
159
    print_list(dlcl);
160
161
    dlcl = delete_node(dlcl, 0);
162
    printf("List elements after deletion of 0:\n");
163
    print_list(dlcl);
164
    dlcl = delete_node(dlcl, 1);
165
    printf("List elements after deletion of 1:\n");
166
    print_list(dlcl);
167
    dlcl = delete_node(dlcl, 1);
168
    printf("List elements after deletion of 1:\n");
169
    print_list(dlcl);
170
    dlcl = delete_node(dlcl, 5);
171
    printf("List elements after deletion of 5:\n");
172
    print_list(dlcl);
173
    dlcl = delete_node(dlcl, 7);
174
    printf("List elements after deletion of 7:\n");
175
    print_list(dlcl);
176
    dlcl = delete_node(dlcl, 10);
177
    printf("List elements after deletion of 10:\n");
178
    print_list(dlcl);
179
180
    usun_rek(&dlcl,dlcl);
181
    puts("aaa");
182
    print_list(dlcl);
183
184
    remove_list(&dlcl);
185
    return 0;
186
}