Design doubly linked list

Implement the design list. You can choose to use single or double-linked list. Single node in the list should have two properties: val and  next. val Is the current value of the node, next is a pointer pointing to the next node / reference. To use a doubly linked list, an attribute is also required  prev to indicate a node in the linked list. Suppose all the nodes in the list are the 0-index.

Implementing these features in the list class:

  • get (index): Get the list of  index values of nodes. If the index is invalid return -1.
  • addAtHead (val): to add a value before the first element of a linked list  val of nodes. After insertion, the new node will be the first node of the list.
  • addAtTail (val): The value  val of a node is added to the last element of the list.
  • addAtIndex (index, val): the first in the list  index is added before the node is  val  a node. If  index equal to the length of the list, then the node will be appended to the end of the list. If  index greater than the length of the list, the node will not be inserted. If indexless than 0, then the head insertion node.
  • deleteAtIndex (index): If the index  index is valid, delete the list of the first  index node.

 

struct Node
{
    int val;
    Node *next, *prev;
    Node(int x): val(x), next(nullptr), prev(nullptr){};
};

class MyLinkedList {
public:
        /** Initialize your data structure here. */
        int len;
        Node *head, *tail;

        MyLinkedList() {
            len = 0;
            head = nullptr;
            tail = nullptr;
        }
        
        /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
        int get(int index) {
            if(index < 0 || len == 0 || index >= len)
                return -1;
            else{
                Node *p = head;
                for(int i = 0; i < index; i++){
                    p = p->next;
                }
                return p->val;
            }
        }
        
        /** 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 addAtHead(int val) {
            Node *new_Node = new Node(val);
            if(head == nullptr){
                head = new_Node;
                tail = new_Node;
            }
            else{
                new_Node->next = head;
                head->prev = new_Node;
                head = new_Node;
            }
            len++;
        }
        
        /** Append a node of value val to the last element of the linked list. */
        void addAtTail(int val) {
            Node *new_Node = new Node(val);
            if(head == nullptr){
                head = new_Node;
                tail = new_Node;
            }
            else{
                new_Node->prev = tail;
                tail->next = new_Node;
                tail = new_Node;
            }
            len++;
        }
        
        /** 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 addAtIndex(int index, int val) {
            Node *new_Node = new Node(val);
            if(index == len){
                addAtTail(val);
            }
            else if(index <= 0){
                addAtHead(val);
            }
            else
            {
                Node *p = head;
                for(int i = 1; i < index; i++){
                    p = p->next;
                }

                new_Node->next = p->next;
                new_Node->prev = p;
                p->next->prev = new_Node;
                p->next = new_Node;
                len++;
            }
        }
        
        /** Delete the index-th node in the linked list, if the index is valid. */
        void deleteAtIndex(int index) {
            if(head == nullptr || index >= len || index < 0){
                return;
            }
            else{
                if(index == 0){
                    if(len == 1){
                        head = nullptr;
                        tail = nullptr;
                    }
                    else{
                        head = head->next;
                        head->prev->next = nullptr;
                        head->prev = nullptr;
                    }
                }
                else if(index == len-1){
                    tail = tail->prev;
                    tail->next->prev = nullptr;
                    tail->next = nullptr;
                }
                else{
                    Node *p = head;
                    for(int i = 0; i < index; i++){
                        p = p->next;
                    }
                    p->next->prev = p->prev;
                    p->prev->next = p->next;
                    p->prev = nullptr;
                    p->next = nullptr;
                }
                len--;
            }
        }
};

 

Guess you like

Origin www.cnblogs.com/olajennings/p/12466875.html