c: CLion 2023.1.1

 

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
/**
# encoding: utf-8
# 版权所有 2023 涂聚文有限公司
# 许可信息查看:
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : CLion 2023.1.1 c17  windows 10
# Datetime  : 2023/11/11 6:09
# User      : geovindu
# Product   : CLion
# Project   : ctest
# File      : geovindu.c
# explain   : 学习
*/
//
// Created by geovindu on 2023/11/11.
//
 
#ifndef CTEST_GEOVINDU_H
#define CTEST_GEOVINDU_H
 
#include <stdio.h>
#include <stdlib.h>
#include <sdpblb.h>
 
/**
 * @brief
 *
 */
typedef struct node
{
    /**
     * @brief
     */
    int data;
    /**
     * @brief
     *
     */
    struct node* next;
} node;
 
/**
 * @brief 显示
 */
void displaynode();
 
 
 
#endif //CTEST_GEOVINDU_H

  

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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/**
# encoding: utf-8
# 版权所有 2023 涂聚文有限公司
# 许可信息查看:https://www.learnc.net/c-data-structures/c-linked-list/
# 描述:https://blog.jetbrains.com/clion/2016/05/keep-your-code-documented/
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : CLion 2023.1.1 c17  windows 10
# Datetime  : 2023/11/11 6:10
# User      : geovindu
# Product   : CLion
# Project   : ctest
# File      : geovindu.c
# explain   : 学习
*/
 
//
// Created by geovindu on 2023/11/11.
//
 
#include "../includes/geovindu.h"
 
/**!
 * @brief
 * @param data
 *
 */
typedef void (*callback)(node* data);
 
/**
* @brief create a new node
    initialize the data and next field
    return the newly created node
* @param data
* @param next
@return
*/
node* create(int data,node* next)
{
    node* new_node = (node*)malloc(sizeof(node));
    if(new_node == NULL)
    {
        printf("Error creating a new node.\n");
        exit(0);
    }
    new_node->data = data;
    new_node->next = next;
 
    return new_node;
}
 
/**
  @brief  add a new node at the beginning of the list
  @param head
  @param data
*/
node* prepend(node* head,int data)
{
    node* new_node = create(data,head);
    head = new_node;
    return head;
}
 
/**
 *@brief   add a new node at the end of the list
 * @param head
 * @param data
*/
node* append(node* head, int data)
{
    if(head == NULL)
        return NULL;
    /* go to the last node */
    node *cursor = head;
    while(cursor->next != NULL)
        cursor = cursor->next;
 
    /* create a new node */
    node* new_node =  create(data,NULL);
    cursor->next = new_node;
 
    return head;
}
 
/**
 @brief insert a new node after the prev node
 @param head
 @param data
 @param prev
 @return
*/
node* insert_after(node *head, int data, node* prev)
{
    if(head == NULL || prev == NULL)
        return NULL;
    /* find the prev node, starting from the first node*/
    node *cursor = head;
    while(cursor != prev)
        cursor = cursor->next;
 
    if(cursor != NULL)
    {
        node* new_node = create(data,cursor->next);
        cursor->next = new_node;
        return head;
    }
    else
    {
        return NULL;
    }
}
 
/**
  *@brief  insert a new node before the nxt node
  *@param head
  * @param data
  * @param nxt
*/
node* insert_before(node *head, int data, node* nxt)
{
    if(nxt == NULL || head == NULL)
        return NULL;
 
    if(head == nxt)
    {
        head = prepend(head,data);
        return head;
    }
 
    /* find the prev node, starting from the first node*/
    node *cursor = head;
    while(cursor != NULL)
    {
        if(cursor->next == nxt)
            break;
        cursor = cursor->next;
    }
 
    if(cursor != NULL)
    {
        node* new_node = create(data,cursor->next);
        cursor->next = new_node;
        return head;
    }
    else
    {
        return NULL;
    }
}
 
/**
  *@brief  traverse the linked list
  * @param head
  * @param f
*/
void traverse(node* head,callback f)
{
    node* cursor = head;
    while(cursor != NULL)
    {
        f(cursor);
        cursor = cursor->next;
    }
}
/**
 *  @brief remove node from the front of list
 *  @param head
 
*/
node* remove_front(node* head)
{
    if(head == NULL)
        return NULL;
    node *front = head;
    head = head->next;
    front->next = NULL;
    /* is this the last node in the list */
    if(front == head)
        head = NULL;
    free(front);
    return head;
}
 
/**
 *@brief   remove node from the back of the list
 * @param head
*/
node* remove_back(node* head)
{
    if(head == NULL)
        return NULL;
 
    node *cursor = head;
    node *back = NULL;
    while(cursor->next != NULL)
    {
        back = cursor;
        cursor = cursor->next;
    }
 
    if(back != NULL)
        back->next = NULL;
 
    /* if this is the last node in the list*/
    if(cursor == head)
        head = NULL;
 
    free(cursor);
 
    return head;
}
 
/**
  *@brief  remove a node from the list
  * @param head
  * @param nd
  *
*/
node* remove_any(node* head,node* nd)
{
    if(nd == NULL)
        return NULL;
    /* if the node is the first node */
    if(nd == head)
        return remove_front(head);
 
    /* if the node is the last node */
    if(nd->next == NULL)
        return remove_back(head);
 
    /* if the node is in the middle */
    node* cursor = head;
    while(cursor != NULL)
    {
        if(cursor->next == nd)
            break;
        cursor = cursor->next;
    }
 
    if(cursor != NULL)
    {
        node* tmp = cursor->next;
        cursor->next = tmp->next;
        tmp->next = NULL;
        free(tmp);
    }
    return head;
 
}
/**
 *@brief   display a node
 * @param p
 *
*/
void display(node* n)
{
    if(n != NULL)
        printf("%d ", n->data);
}
 
/**
 *@brief   Search for a specific node with input data
 
    return the first matched node that stores the input data,
    otherwise return NULL
 *@param head
 * @param data
*/
node* search(node* head,int data)
{
 
    node *cursor = head;
    while(cursor!=NULL)
    {
        if(cursor->data == data)
            return cursor;
        cursor = cursor->next;
    }
    return NULL;
}
 
/**
 *@brief   remove all element of the list
 * @param head
 *
*/
void dispose(node *head)
{
    node *cursor, *tmp;
 
    if(head != NULL)
    {
        cursor = head->next;
        head->next = NULL;
        while(cursor != NULL)
        {
            tmp = cursor->next;
            free(cursor);
            cursor = tmp;
        }
    }
}
/**
 *@brief   return the number of elements in the list
 * @param head
*/
int count(node *head)
{
    node *cursor = head;
    int c = 0;
    while(cursor != NULL)
    {
        c++;
        cursor = cursor->next;
    }
    return c;
}
/**
 *@brief   sort the linked list using insertion sort
 * @param head
*/
node* insertion_sort(node* head)
{
    node *x, *y, *e;
 
    x = head;
    head = NULL;
 
    while(x != NULL)
    {
        e = x;
        x = x->next;
        if (head != NULL)
        {
            if(e->data > head->data)
            {
                y = head;
                while ((y->next != NULL) && (e->data> y->next->data))
                {
                    y = y->next;
                }
                e->next = y->next;
                y->next = e;
            }
            else
            {
                e->next = head;
                head = e ;
            }
        }
        else
        {
            e->next = NULL;
            head = e ;
        }
    }
    return head;
}
 
/**
  *@brief  reverse the linked list
  * @param head
*/
node* reverse(node* head)
{
    node* prev    = NULL;
    node* current = head;
    node* next;
    while (current != NULL)
    {
        next  = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    head = prev;
    return head;
}
/**
   *@brief display the menu
*/
void menu()
{
    printf("--- C Linked List Demonstration --- \n\n");
    printf("0.menu\n");
    printf("1.prepend an element\n");
    printf("2.append an element\n");
    printf("3.search for an element\n");
    printf("4.insert after an element\n");
    printf("5.insert before an element\n");
    printf("6.remove front node\n");
    printf("7.remove back node\n");
    printf("8.remove any node\n");
    printf("9.sort the list\n");
    printf("10.Reverse the linked list\n");
    printf("-1.quit\n");
 
}
 
/**
 * @brief 显示
 */
void displaynode()
{
    int command = 0;
    int data;
 
    node* head = NULL;
    node* tmp = NULL;
    callback disp = display;
 
    menu();
    while(1)
    {
        printf("\nEnter a command(0-10,-1 to quit):");
        scanf("%d",&command);
 
        if(command == -1)
            break;
        switch(command)
        {
            case 0:
                menu();
                break;
            case 1:
                printf("Please enter a number to prepend:");
                scanf("%d",&data);
                head = prepend(head,data);
                traverse(head,disp);
                break;
            case 2:
                printf("Please enter a number to append:");
                scanf("%d",&data);
                head = append(head,data);
                traverse(head,disp);
                break;
            case 3:
                printf("Please enter a number to search:");
                scanf("%d",&data);
                tmp = search(head,data);
                if(tmp != NULL)
                {
                    printf("Element with value %d found.",data);
                }
                else
                {
                    printf("Element with value %d not found.",data);
                }
                break;
            case 4:
                printf("Enter the element value where you want to insert after:");
                scanf("%d",&data);
                tmp = search(head,data);
                if(tmp != NULL)
                {
                    printf("Enter the element value to insert after:");
                    scanf("%d",&data);
                    head = insert_after(head,data,tmp);
                    if(head != NULL)
                        traverse(head,disp);
                }
                else
                {
                    printf("Element with value %d not found.",data);
                }
                break;
            case 5:
                printf("Enter the element value where you want to insert before:");
                scanf("%d",&data);
                tmp = search(head,data);
                if(tmp != NULL)
                {
                    printf("Enter the element value to insert before:");
                    scanf("%d",&data);
                    head = insert_before(head,data,tmp);
 
                    if(head != NULL)
                        traverse(head,disp);
                }
                else
                {
                    printf("Element with value %d not found.",data);
                }
                break;
            case 6:
                head = remove_front(head);
                if(head != NULL)
                    traverse(head,disp);
                break;
            case 7:
                head = remove_back(head);
                if(head != NULL)
                    traverse(head,disp);
                break;
            case 8:
                printf("Enter the element value to remove:");
                scanf("%d",&data);
                tmp = search(head,data);
                if(tmp != NULL)
                {
                    remove_any(head,tmp);
                    if(head != NULL)
                        traverse(head,disp);
                }
                else
                {
                    printf("Element with value %d not found.",data);
                }
                break;
            case 9:
                head = insertion_sort(head);
                if(head != NULL)
                    traverse(head,disp);
                break;
            case 10:
                head = reverse(head);
                if(head != NULL)
                    traverse(head,disp);
                break;
        }
 
    }
    dispose(head);
}

 

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
/**
# encoding: utf-8
# 版权所有 2013 涂聚文有限公司
# 许可信息查看:linked list
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : CLion 2023.1.1 c17
# Datetime  : 2023 11.11
# User      : geovindu
# Product   : ctest
# Project   : ctest
# File      : main.c
# explain   : 学习
*/
#include <stdio.h>
#include "includes/geovindu.h"
 
 
/**
 * @brief
 * @return
 */
int main() {
 
    SetConsoleOutputCP(CP_UTF8);//终端中文乱码问题
    printf("Hello, World!涂聚文!\n");
    displaynode();
 
    return 0;
}

  

 

 

 

 

 

 

 

 

 

 

posted @   ®Geovin Du Dream Park™  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示