当前位置:   article > 正文

LeetCode-707-设计链表-C语言_设计链表c语言

设计链表c语言
typedef struct ListNode Node;

typedef struct {
    /* 一个虚拟的节点,node.next才是链表的头节点 */
    Node node;
    
    /* 用于记录链表的尾节点 */
    Node *rear;
    
    /* 用于记录链表的长度,根据长度判断,检索插入等操作是否合法 */
    int len;
} MyLinkedList;

/** Initialize your data structure here. */

void print(Node *head){
    return;
    
    while(head){
        printf("%d->", head->val);
        head = head->next;
    }
    printf("\n");
}

MyLinkedList* myLinkedListCreate() {
    MyLinkedList * list = (MyLinkedList *)malloc(sizeof(MyLinkedList));
    list->node.next = NULL;
    
    /* 尾节点指向虚拟节点,便于从尾添加 */
    list->rear = &(list->node);
    
    list->len = 0;
    
    return list;
}

/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int myLinkedListGet(MyLinkedList* obj, int index) {
   Node *head = obj->node.next;
    int i=0;
    
    /* 先判断是否合法 */
    if(index<0 || index>= obj->len){
        return -1;
    }
    
    while(head){
        if(i == index){
            return head->val;
        }
        head = head->next;
        i++;
    }
    
    return -1;
}


/** Append a node of value val to the last element of the linked list. */
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
    Node *node = (Node *)malloc(sizeof(Node));
    
    node->val = val;
    node->next = NULL;

    obj->rear->next = node;
    
    obj->rear = node;

    obj->len++;
    
    print(obj->node.next);
    
    return;
}

/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
    Node *head = obj->node.next;
    
    /* 当添加第一个节点时, 从尾巴添加,因为要修改rear指针,所以直接调用尾添加的接口即可 */
    if(obj->len == 0){
        myLinkedListAddAtTail(obj, val);
        return;
    }

    Node *node = (Node *)malloc(sizeof(Node));
    
    node->val = val;
    node->next = head;
    obj->node.next = node;
    
    obj->len++;
    
    print(obj->node.next);
    
    return;
}



/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
    Node *head = obj->node.next;
    Node *last = &obj->node;
    int i = 0;
  
    /* 先判断是否合法, 小于0的时候,转化为在0位置添加 */
    if(index < 0){
        index = 0;
    }
    
    /* 判断是否合法 */
    if( index>obj->len){
        return;
    }
    
    /* 0位置添加表示从头添加 */
    if(index==0){
        myLinkedListAddAtHead(obj,val);
        return;
    }
    
    /* 当位置为在len位置时,表示从尾添加,直接调用尾添加接口即可 */
    if(index  == obj->len){
        myLinkedListAddAtTail(obj, val);
        return;
    }
    
    Node *node = (Node *)malloc(sizeof(Node));    
    node->val = val;
    
    obj->len++;
    
    while(head){
        if(i == index){
            last->next = node;
            node->next = head;
            print(obj->node.next);
            return;
        }
        
        last = head;
        head = head->next;
        i++;
    }
    
    print(obj->node.next);

    free(node);
}

/** Delete the index-th node in the linked list, if the index is valid. */
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
    Node *head = obj->node.next;
    Node *last = &obj->node;
    int i= 0;
    
    /* 对于非法的位置,以及长度为零时,直接返回 */
    if(obj->len <=0 ||index<0 || index>= obj->len){
        return;
    }
    
    obj->len--;
    
    while(head){
        if(i == index){
            if(head == obj->rear){
                obj->rear = last;
            }
            
            last->next = head->next;
            free(head);
            print(obj->node.next);
            return;
        }
        
        last = head;
        head = head->next;
        i++;
    }
    
}

void myLinkedListFree(MyLinkedList* obj) {
    Node *head = obj->node.next;
    Node *last = &obj->node;

    while(head){
        if(last != &obj->node){
            free(last);
        }
        
        last = head;
        head = head->next;
    }
    
    free(obj);
    
}

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/618122
推荐阅读
相关标签
  

闽ICP备14008679号