设计链表-单链表和双链表(C/Python)

通过链表的各种操作熟悉链表的使用。

单链表:C语言实现

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

MyLinkedList* myLinkedListCreate() {
    MyLinkedList* head = (MyLinkedList*)malloc(sizeof(MyLinkedList));
    head->next = NULL;
    return head;
}

int myLinkedListGet(MyLinkedList* obj, int index) {
    MyLinkedList *cur = obj->next;
    int i;
    for(i=0;cur;i++)
    {
        if(i == index)
            return cur->val;
        else
            cur = cur->next;
    }
    return -1;
}

void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
    MyLinkedList *nhead = (MyLinkedList *)malloc(sizeof (MyLinkedList));
    nhead->val = val;
    nhead->next = obj->next;
    obj->next = nhead;
}

void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
    MyLinkedList *cur = obj;
    MyLinkedList *ntail = (MyLinkedList *)malloc(sizeof (MyLinkedList));
    ntail->val = val;
    while(cur->next)
    {
        cur = cur->next;
    }
    cur->next = ntail;
    ntail->next = NULL;
    
}

void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
    MyLinkedList *cur = obj->next;
    int i;
    if(index == 0)
    {
        myLinkedListAddAtHead(obj,val);
        return;
    }
    for(i = 1; cur; i++)
    {
        if(i == index)
        {
            MyLinkedList *nindex = (MyLinkedList *)malloc(sizeof (MyLinkedList));
            nindex->val = val;
            nindex->next = cur->next;
            cur->next = nindex;
            return;
        }
        else
            cur = cur->next;
    }
}

void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
    if(index == 0)
    {
        MyLinkedList *temp = obj->next;
        if(temp)
        {
            obj->next = temp->next;
            free(temp);
        }
    }
    MyLinkedList *cur = obj->next;
    int i;
    for(i = 1; cur&&cur->next; i++)
    {
        if(i == index)
        {
            MyLinkedList *temp = cur->next;
            if(temp)
            {
                cur->next = temp->next;
                free(temp);
            }
            return;
        }
        else{
            cur = cur->next;
        }
    }
}

void myLinkedListFree(MyLinkedList* obj) {
    while(obj)
    {
        MyLinkedList *temp = obj;
        obj = obj->next;
        free(temp);
    }
}

单链表:Python实现

class ListNode:
     def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class MyLinkedList:

    def __init__(self):
        self.dummy_head = ListNode()
        self.size = 0

    def get(self, index: int) -> int:
        if index < 0 or index >= self.size:
            return -1
        cur = self.dummy_head.next
        for i in range(index):
            cur = cur.next
        return cur.val


    def addAtHead(self, val: int) -> None:
        self.dummy_head.next = ListNode(val,self.dummy_head.next)
        self.size += 1


    def addAtTail(self, val: int) -> None:
        cur = self.dummy_head
        while cur.next:
            cur = cur.next
        cur.next = ListNode(val)
        self.size += 1


    def addAtIndex(self, index: int, val: int) -> None:
        if index < 0 or index > self.size:
            return 
        cur = self.dummy_head
        for i in range(index):
            cur = cur.next
        cur.next = ListNode(val,cur.next)
        self.size += 1

    def deleteAtIndex(self, index: int) -> None:
        if index < 0 or index >= self.size:
            return 
        cur = self.dummy_head
        for i in range(index):
            cur = cur.next
        cur.next = cur.next.next
        self.size -= 1

双链表:C语言实现

#define MAX(a, b) ((a) > (b) ? (a) : (b))

typedef struct DLinkListNode {
    int val;
    struct DLinkListNode *prev, *next;
} DLinkListNode;

typedef struct {
    struct DLinkListNode *head, *tail;
    int size;
} MyLinkedList;

DLinkListNode *dLinkListNodeCreat(int val) {
    DLinkListNode * node = (DLinkListNode *)malloc(sizeof(struct DLinkListNode));
    node->val = val;
    node->prev = NULL;
    node->next = NULL;
    return node;
}

MyLinkedList* myLinkedListCreate() {
    MyLinkedList * obj = (MyLinkedList *)malloc(sizeof(MyLinkedList));
    obj->size = 0;
    obj->head = dLinkListNodeCreat(0);
    obj->tail = dLinkListNodeCreat(0);
    obj->head->next = obj->tail;
    obj->tail->prev = obj->head; 
    return obj;
}

int myLinkedListGet(MyLinkedList* obj, int index) {
    if (index < 0 || index >= obj->size) {
        return -1;
    }
    DLinkListNode *curr;
    if (index + 1 < obj->size - index) {
        curr = obj->head;
        for (int i = 0; i <= index; i++) {
            curr = curr->next;
        }
    } else {
        curr = obj->tail;
        for (int i = 0; i < obj->size - index; i++) {
            curr = curr->prev;
        }
    }
    return curr->val;
}

void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
    if (index > obj->size) {
        return;
    }
    index = MAX(0, index);
    DLinkListNode *pred, *succ;
    if (index < obj->size - index) {
        pred = obj->head;
        for (int i = 0; i < index; i++) {
            pred = pred->next;
        }
        succ = pred->next;
    } else {
        succ = obj->tail;
        for (int i = 0; i < obj->size - index; i++) {
            succ = succ->prev;
        }
        pred = succ->prev;
    }
    obj->size++;
    DLinkListNode *toAdd = dLinkListNodeCreat(val);
    toAdd->prev = pred;
    toAdd->next = succ;
    pred->next = toAdd;
    succ->prev = toAdd;
}

void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
    myLinkedListAddAtIndex(obj, 0, val);
}

void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
    myLinkedListAddAtIndex(obj, obj->size, val);
}

void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
    if (index < 0 || index >= obj->size) {
        return;
    }
    DLinkListNode *pred, *succ;
    if (index < obj->size - index) {
        pred = obj->head;
        for (int i = 0; i < index; i++) {
            pred = pred->next;
        }
        succ = pred->next->next;
    } else {
        succ = obj->tail;
        for (int i = 0; i < obj->size - index - 1; i++) {
            succ = succ->prev;
        }
        pred = succ->prev->prev;
    }
    obj->size--;
    DLinkListNode *p = pred->next;
    pred->next = succ;
    succ->prev = pred;
    free(p);
}

void myLinkedListFree(MyLinkedList* obj) {
    struct DLinkListNode *cur = NULL, *tmp = NULL;
    for (cur = obj->head; cur;) {
        tmp = cur;
        cur = cur->next;
        free(tmp);
    }
    free(obj);
}

双链表:Python语言实现

class ListNode:
    def __init__(self,val = 0, pre = None,next=None):
        self.val = val
        self.pre = pre
        self.next = next
class MyLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None
        self.size = 0
    
    def get(self, index: int) -> int:
        if index < 0 or index >= self.size:
            return -1 
        if index +1 < self.size -index:  # index在链表的前半段,那就从前往后找
            cur = self.head
            for i in range(index):
                cur = cur.next
        else:    # index在链表的后半段,那就从后往前找。
            cur = self.tail
            for i in range(self.size - index - 1):
                cur = cur.pre
        return cur.val
    def addAtHead(self, val: int) -> None:
        new_node = ListNode(val,None,self.head)
        if self.head:
            self.head.pre = new_node
        else:
            self.tail  = new_node
        self.head = new_node
        self.size += 1

    def addAtTail(self, val: int) -> None:
        new_node =  ListNode(val,self.tail,None)
        if self.tail:
            self.tail.next = new_node
        else:
            self.head = new_node
        self.tail = new_node
        self.size += 1

    def addAtIndex(self, index: int, val: int) -> None:
        if index < 0 or index > self.size:
            return -1
        if index == 0:
            self.addAtHead(val)
        elif index == self.size:
             self.addAtTail(val)
        else:
            if index < self.size - index:
                cur = self.head
                for i in range(index - 1):
                    cur = cur.next
            else:
                cur = self.tail
                for i in range(self.size - index):
                    cur = cur.pre
            new_node = ListNode(val,cur,cur.next)
            cur.next.pre = new_node
            cur.next = new_node
            self.size += 1

    def deleteAtIndex(self, index: int) -> None:
        if index < 0 or index >= self.size:
            return -1 
        if index == 0:  # 第一个结点
            self.head = self.head.next
            if self.head:
                self.head.pre = None
            else:
                self.tail = None
        elif index == self.size - 1:  # 最后一个结点
            self.tail = self.tail.pre
            if self.tail:
                self.tail.next = None
            else:
                self.head = None
        else:
            if index  < self.size - index:
                cur = self.head
                for i in range(index):
                    cur = cur.next
            else:
                cur = self.tail
                for i in range(self.size - index - 1):
                    cur = cur.pre
            cur.pre.next = cur.next
            cur.next.pre = cur.pre
        self.size -= 1

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值