设计链表

 

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



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

        MyLinkedList() {
            len = 0;
            head = 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);
            new_Node->next = head;
            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;
            }
            else{
                Node *p = head;
                while (p->next != nullptr)
                {
                    p = p->next;
                }
                p->next = 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;
                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){
                if(index == 0){
                    head = head->next;
                    len--;
                }
                else if(0 < index && index < len)
                {
                    Node *p = head;
                    for(int i = 1; i < index; i++){
                        p = p->next;
                    }
                    p->next = p->next->next;
                    len--;
                }
            }
        }

        void printAllNode(){
            Node *p = head;
            if(p == nullptr){
                cout<<"No Node."<<endl;
            }
            else
            {
                while (p != nullptr)
                {
                    cout<<p->val<<endl;
                    p=p->next;
                }
            }
        }
};

 

这里运行的内存占用比较多,有时间的话可以想一想是哪里的问题以及优化。

posted @ 2020-03-10 18:33  jenningszheng  阅读(154)  评论(0编辑  收藏  举报