【707】Design Linked List 设计链表

1。题目
2。解答

typedef struct A{
    int val;
    struct A *next;
} MyLinkedList;

/** Initialize your data structure here. */
MyLinkedList* myLinkedListCreate() {
    MyLinkedList *head;

    head = (MyLinkedList *)malloc(sizeof(MyLinkedList));
    head->val = 0;
    head->next = 0;

    return head;
}

/** 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) {
    MyLinkedList* pst;
    int i=0;

    if (obj == 0 || obj->next == 0)
    {
        return -1;
    }

    #if 0
    {
        pst = obj->next;
        printf("\r\n[%d]--------",__LINE__);
        while (pst!=0)
        {
            printf("%d,",pst->val);
            pst = pst->next;
        }
    }
    #endif
    pst = obj->next;
    while (pst!=0)
    {
        if (i==index)
        {
            return pst->val;
        }
        else
        {
            i++;
            pst = pst->next;
        }
    }

    return -1;
}

/** 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) {
    MyLinkedList* pst;

    if (obj == 0)
    {
        return;
    }

    pst =  (MyLinkedList *)malloc(sizeof(MyLinkedList));
    pst->val = val;
    pst->next = obj->next;
    obj->next = pst;
    #if 0
    {
        pst = obj->next;
        printf("\r\n[%d]--------",__LINE__);
        while (pst!=0)
        {
            printf("%d,",pst->val);
            pst = pst->next;
        }
    }
    #endif
}

/** Append a node of value val to the last element of the linked list. */
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
    MyLinkedList* pst,*tmp;

    if (obj == 0)
    {
        return;
    }

    pst =  (MyLinkedList *)malloc(sizeof(MyLinkedList));
    pst->val = val;
    pst->next = 0;
    if (obj->next == 0)
    {
        obj->next = pst;
    }
    else
    {
        tmp = obj;
        while (tmp->next!=0)
        {
            tmp = tmp->next;
        }
        tmp->next = pst;
    }
    #if 0
    {
        pst = obj->next;
        printf("\r\n[%d]--------",__LINE__);
        while (pst!=0)
        {
            printf("%d,",pst->val);
            pst = pst->next;
        }
    }
    #endif
}

/** 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) {
    MyLinkedList* pst,*pre,*tmp;
    int i=0;

    if (obj == 0 || index < 0)
    {
        return;
    }   

    if (index == 0)
    {
        tmp =  (MyLinkedList *)malloc(sizeof(MyLinkedList));
        tmp->val = val;
        tmp->next = obj->next;
        obj->next = tmp;
    }
    else
    {
        pre = obj;
        pst = obj->next;
        i=0;
        while (i<index && pst!=0)
        {
            pre = pst;
            pst = pst->next;
            i++;
        }
        if (i==index)
        { 
            tmp =  (MyLinkedList *)malloc(sizeof(MyLinkedList));
            tmp->val = val;
            tmp->next = pst;
            pre->next = tmp;
        }
    }
    #if 0
    {
        pst = obj->next;
        printf("\r\n[%d]--------",__LINE__);
        while (pst!=0)
        {
            printf("%d,",pst->val);
            pst = pst->next;
        }
    }
    #endif
}

/** Delete the index-th node in the linked list, if the index is valid. */
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
    MyLinkedList* pst,*pre,*tmp;
    int i=0;

    if (obj == 0 || obj->next == 0)
    {
        return;
    }

    if (index == 0)
    {
        tmp = obj->next;
        obj->next = tmp->next;
        free(tmp);
    }
    else
    {
        pre = obj;
        pst = obj->next;
        while (i<index && pst!=0)
        {
            pre = pst;
            pst = pst->next;
            i++;
        }
        if (i==index && pst!=0)
        {
            tmp = pst;
            pre->next = pst->next;
        }
    }
    #if 0
    {
        pst = obj->next;
        printf("\r\n[%d]--------",__LINE__);
        while (pst!=0)
        {
            printf("%d,",pst->val);
            pst = pst->next;
        }
    }
    #endif
}

void myLinkedListFree(MyLinkedList* obj) {
    MyLinkedList* pst;

    if (obj == 0)
    {
        return;
    }

    pst = obj->next;
    while(pst!=0)
    {
        obj->next = pst->next;
        free(pst);
        pst = obj->next;
    }
    free(obj);
}

/**
 * Your MyLinkedList struct will be instantiated and called as such:
 * struct MyLinkedList* obj = myLinkedListCreate();
 * int param_1 = myLinkedListGet(obj, index);
 * myLinkedListAddAtHead(obj, val);
 * myLinkedListAddAtTail(obj, val);
 * myLinkedListAddAtIndex(obj, index, val);
 * myLinkedListDeleteAtIndex(obj, index);
 * myLinkedListFree(obj);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值