双向循环链表及其实现

目录

1.什么是双向循环链表?

2.双向循环链表的具体实现方法有哪些?


1.什么是双向循环链表?

       双向循环链表结构是两个指针,一个头指针(指向链表的头部),一个尾指针(指向链表的尾部),当前循环链表的长度size. 每个节点LinkedNode由一个前向的指针,一个后向的指针,一个当前节点的值。

2.双向循环链表的具体实现方法有哪些?

        int get(int index)  获得指定的元素

        int addAtHead(int val) 头结点新增元素

        int addAtTail(int val)尾节点新增元素

        int addAtIndex(int index,int val) 指向索引的值

       void deleteAtIndex(int index) 按照索引删除元素

      void printLinkedList()  输出链表中的元素

代码如下

//双向循环链表
class MyLinkedList {
    int size;//当前链表的长度
    // sentinel nodes as pseudo-head and pseudo-tail
    ListNode head; //头结点
    ListNode tail; //尾节点

    //双向链表的初始化
    public MyLinkedList() {
        size = 0;
        head = new ListNode(-1);
        tail = new ListNode(-1);
        head.next = tail;
        tail.prev = head;
    }

    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public int get(int index) {
        ListNode node = getNode(index);
        if (node == null) {
            return -1;
        }
        return node.val;
    }

    public ListNode getNode(int index) {
        // if index is invalid
        if (index < 0 || index >= size){
            return null;
        }

        // choose the fastest way: to move from the head
        // or to move from the tail
        ListNode curr = head;
        if (index < (size - 1) / 2) {
            //在左边从头结点进行寻找
            for (int i = 0; i < index + 1; ++i) {
                curr = curr.next;
            }
        } else {
            //在右边从尾节点进行寻找
            curr = tail;
            for (int i = 0; i < size - index; ++i) {
                curr = curr.prev;
            }
        }
        return curr;
    }

    /** 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. */
    public void addAtHead(int val) {
        ListNode pred = head, succ = head.next;

        ++size;
        ListNode newNode = new ListNode(val);
        newNode.prev = pred;
        newNode.next = succ;

        pred.next = newNode;
        succ.prev = newNode;
    }

    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        ListNode succ = tail, pred = tail.prev;

        ++size;
        ListNode newNode = new ListNode(val);
        newNode.prev = pred;
        newNode.next = succ;

        pred.next = newNode;
        succ.prev = newNode;
    }

    /** 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. */
    public void addAtIndex(int index, int val) {
        // If index is greater than the length,
        // the node will not be inserted.
        if (index <0) {
            addAtHead(val);
            return;
        }
        if (index >(size - 1)) {
            addAtTail(val);
            return;
        }

        //找到前驱和后继
        // find predecessor and successor of the node to be added

        ListNode node = getNode(index);

        ListNode pred = node.prev;
        ListNode succ = node;
        // insertion itself
        ++size;
        ListNode toAdd = new ListNode(val);
        toAdd.prev = pred;
        toAdd.next = succ;
        pred.next = toAdd;
        succ.prev = toAdd;
    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        // if the index is invalid, do nothing
        if (index < 0 || index >= size) {
            return;
        }

        // find predecessor and successor of the node to be deleted
        ListNode node = getNode(index);
        ListNode pred = node.prev;
        ListNode succ = node.next;
        // delete pred.next
        --size;
        pred.next = succ;
        succ.prev = pred;
    }

    public void printLinkedList() {
        for (int i = 0; i < size; i++) {
            System.out.println(get(i));
        }
    }

    class ListNode {
        int val;
        ListNode next;
        ListNode prev;

        ListNode(int x) {
            val = x;
        }
    }

    public static void main(String[] args) {
//        ["MyLinkedList","addAtIndex","addAtIndex","addAtIndex","get"]
//[[],[0,10],[0,20],[1,30],[0]]
//        MyLinkedList myLinkedList = new MyLinkedList();
//        myLinkedList.addAtTail(1);
//        myLinkedList.addAtTail(2);
//        myLinkedList.addAtTail(3);
//        myLinkedList.addAtHead(4);
//        System.out.println("====================");
//        myLinkedList.printLinkedList();
//        System.out.println("====================");
//        int num = myLinkedList.get(1);
//        System.out.println(num);
//        System.out.println("====================");
//        myLinkedList.addAtIndex(1,10);
//        myLinkedList.printLinkedList();

//        MyLinkedList myLinkedList = new MyLinkedList();
//        myLinkedList.addAtIndex(0,10);
//        myLinkedList.addAtIndex(0,20);
//        myLinkedList.addAtIndex(1,30);
//        System.out.println("===================");
//        myLinkedList.printLinkedList();
//        System.out.println("===================");
//        int num = myLinkedList.get(0);
//        System.out.println(num);

//        ["MyLinkedList","addAtHead","addAtTail","addAtIndex","get","deleteAtIndex","get"]
//[[],[1],[3],[1,2],[1],[1],[1]]

        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addAtHead(1);
        myLinkedList.addAtTail(3);
        myLinkedList.addAtIndex(1,2);
        System.out.println("===================");
        myLinkedList.printLinkedList();
        System.out.println("===================");
        int num = myLinkedList.get(1);
        System.out.println(num);
        myLinkedList.deleteAtIndex(1);
        System.out.println("===================");
        int num2 = myLinkedList.get(1);
        System.out.println(num2);
    }
}

我是厚积薄发,欢迎大家点赞,欢迎大家留言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向循环链表是一种常用的数据结构之一,可以在链表中进行快速的插入、删除和查找操作。以下是一种简单的C语言实现双向循环链表的代码: ``` #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node *createNode(int data) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = data; newNode->prev = NULL; newNode->next = NULL; return newNode; } void insertAtBegin(struct node **head, int data) { struct node *newNode = createNode(data); if (*head == NULL) { *head = newNode; (*head)->next = *head; (*head)->prev = *head; } else { struct node *last = (*head)->prev; newNode->next = *head; newNode->prev = last; last->next = newNode; (*head)->prev = newNode; *head = newNode; } } void insertAtEnd(struct node **head, int data) { struct node *newNode = createNode(data); if (*head == NULL) { *head = newNode; (*head)->next = *head; (*head)->prev = *head; } else { struct node *last = (*head)->prev; newNode->prev = last; newNode->next = *head; last->next = newNode; (*head)->prev = newNode; } } void deleteNode(struct node **head, int data) { if (*head == NULL) { printf("List is empty.\n"); return; } struct node *cur = *head; struct node *prev = NULL; while (cur->data != data && cur->next != *head) { prev = cur; cur = cur->next; } if (cur->data != data) { printf("%d not found in list.\n", data); return; } if (cur == *head && cur->next == *head) { *head = NULL; free(cur); return; } if (cur == *head) { *head = cur->next; } struct node *next = cur->next; prev->next = next; next->prev = prev; free(cur); } void displayList(struct node *head) { if (head == NULL) { printf("List is empty.\n"); return; } printf("List: "); struct node *cur = head; do { printf("%d ", cur->data); cur = cur->next; } while (cur != head); printf("\n"); } int main() { struct node *head = NULL; insertAtBegin(&head, 10); insertAtBegin(&head, 20); insertAtEnd(&head, 30); insertAtEnd(&head, 40); displayList(head); deleteNode(&head, 20); deleteNode(&head, 50); displayList(head); return 0; } ``` 在这个例子中,我们使用了一个结构体来存储每个节点的数据和指向前一个和后一个节点的指针。我们还定义了一些操作函数,如创建新节点、在链表头部和尾部插入新节点、删除节点和显示链表等。在主函数中,我们创建了一个双向循环链表并进行了一些操作,最后输出链表的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值