采用二级指针实现单链表操作 单链表翻转 删除单链表结点

  • register_filesystem中的二级指针,刚开始没看懂。怒了,如果这个都没看懂,还搞什么C语言编程
  • leetcode中的反转链表,二级指针操作巨好用
  • 先上代码吧
  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
#include<stdio.h>
#include<stdlib.h>

typedef struct ListNode{
    int val;
    struct ListNode *next;
} ListNode;

void printList(ListNode *head){
    if(head){
        printf("%d\n", head->val);
        printList(head->next);
    }
}

void printList_r(ListNode *head){
    if(!head)
        return;
    if(head -> next)
        printList_r(head->next);
    printf("%d\n", head->val);
}

ListNode ** addToTail(ListNode **list){
    ListNode **p ;
    for( p = list; *p; p = & (*p)->next);
    return p;
}

void reverseList(ListNode **list){
    ListNode *head = NULL;
    ListNode **p = list;
    while(*p){
        ListNode *next = (*p)->next;
        (*p)->next = head;
        head = *p;
        *p = next;
    }
    *list = head;
}

/* 
 * delete first item whose val equals to val
 *
*/
void deleteNodeAll(ListNode **l, int val){

    while(*l){
        if((*l)->val == val){
            ListNode *tmp = (*l)->next;
            free(*l);
            *l = tmp;
            break;
        }
        l = &(*l)->next;
    }
}

//delete all the items whose val equals to val
void deleteNodeFirst(ListNode **l, int val){

    while(*l){
        if((*l)->val == val){
            ListNode *tmp = (*l)->next;
            free(*l);
            *l = tmp;
        }
        else
            l = &(*l)->next;
    }
}

int main()
{
    int a[] = {1, 2, 3, 2, 3 ,4};
    int len = sizeof(a) / sizeof(int);
    int i = 0;

    static ListNode *list;

    for( i = 0; i < len; i++)
    {
        ListNode *node = malloc(sizeof(struct ListNode));
        node->val = a[i];
        ListNode **p = addToTail(&list);
        *p = node;
    }

    printf("--------print list in sequence------------\n");
    printList(list);

    printf("--------print list after reversed------------------\n");
    reverseList(&list);
    printList(list);

    printf("--------delete first 1----------\n");
    deleteNodeFirst(&list, 1);
    printList(list);

    printf("--------delete first 3----------------\n");
    deleteNodeFirst(&list, 3);
    printList(list);

    printf("--------delete first 4-----------------\n");
    deleteNodeFirst(&list, 4);
    printList(list);

    printf("--------delete non-existed item-------------\n");
    deleteNodeFirst(&list, 4);
    printList(list);

    printf("--------delete the last item----------------\n");
    deleteNodeFirst(&list, 2);
    printList(list);

    printf("--------reverse an empty list---------------\n");
    reverseList(&list);
    printList(list);


}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy