美文网首页
数据结构(C++)之Double Linked List(typ

数据结构(C++)之Double Linked List(typ

作者: _Nullptr | 来源:发表于2017-03-25 10:44 被阅读0次

    只写了int类型,代码感觉还可以再优化,至于用模板则还要学习; 简单起见,全写在了一个cpp文件中


    //double linked list (type int)
    #include <iostream>
    using namespace std;
    
    class Node
    {
    public:
        Node() {};
        Node(int val) {
            this->val =val;
            prior = next = nullptr;
        }
        ~Node() {};
    
        void setVal(int val) { this->val = val; }
        int getVal() {
            return this->val;
        }
    
        Node* prior;   //指向前节点指针
        Node* next;   //指向后节点指针
        
    private:
        int val=0;
    };
    
    
    class DLlist
    {
    public:
    
        DLlist(int size) {
            this->size = size;
            head = new Node;
            head->prior = nullptr;
            head->next = nullptr;
            tail = head;
            for (int i = 0; i < this->size; i++)
            {
                int v;
                cout << "Enter the value of No. " << i +1<< " Node: ";
                cin >> v;
                if (i==0)
                {
                    head->setVal(v);
                }
                else
                {
                    Node* temp=new Node;
                    temp->setVal(v);
                    temp->next = nullptr;
                    temp->prior = tail;
                    tail->next = temp;
                    tail = temp;
                }
            }
        }
        ~DLlist() { Clear(); }
    
    
        int getSize()
        {
            return this->size;
        }
    
        void insert(int num,int pos)   //在两节点间插入,插入位置的前后节点必须都存在
        {
            Node* temp = new Node;
            Node* position;
            temp->setVal(num);
            position = GetPointer(pos);
            (position->prior)->next = temp;
            temp->next = position;
            temp->prior = position->prior;
            position->prior = temp;
            size++;
        }
    
        void deleteList(int pos)   //删除节点,删除位置的前后节点必须都存在
        {
            Node* position;
            position = GetPointer(pos);
            (position->prior)->next = position->next;
            (position->next)->prior = position->prior;
            delete position;
            size--;
        }
    
        void addFirst(int element)     //pushFront
        {
            
            if (head == nullptr)
            {
                head->setVal(element);
                head->prior = nullptr;
                head->next = nullptr;
                tail = head;
                size++;
            }
            else 
            {
                /*我们要在头结点前再插入一个结点,需要先创建一个新的结点,将头结点的值保存在新节点,然后让新节点的下
                个结点指向头结点的下一个结点,再让新节点的prior指向头结点,这样就将新节点与原来的链表融合了,然后我  
                们将头结点的数据换成element即可*/
                
                Node* temp=new Node;
                temp->setVal(head->getVal());
                temp->next = head->next;
                temp->prior = head;
                if (size > 1)
                {
                    (head->next)->prior = temp;
                }
                head->next = temp;
                head->setVal(element);
                size++;
            }
        }
    
        void addLast(int element)   //pushBack
        {
            
            if (head==nullptr)
            {
                head->setVal(element);
                head->prior = nullptr;
                head->next = nullptr;
                tail = head;
                size++;
            }
            else
            {
                Node* temp = new Node;
                temp->setVal(tail->getVal());
                temp->prior = tail->prior;
                temp->next = tail;
                if (size-1!=0)
                {
                    (tail->prior)->next = temp;
                }
                tail->prior = temp;
                tail->setVal(element);
                size++;
            }
        }
    
        int removeFirst()
        {
            int v = head->getVal();
    
            head = head->next;
            if (size > 1)
            {
                head->prior = nullptr;
            }
    
            size--;
            return v;
            
        }
    
        int removeLast()
        {
            int v = tail->getVal();
            
            tail =tail->prior;
            if (size>1)
            {
                tail->next = nullptr;
            }
    
            size--;
            return v;
        }
    
        int returnNth(int pos)
        {
            return GetPointer(pos)->getVal();
        }
    
        bool isEmpty()
        {
            if (size==0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        void printList()
        {
            for (int i = 0; i < size; i++)
            {
                cout << "No. " << i +1<< " = " << GetPointer(i)->getVal() << endl;
            }
        }
    
        
        void Clear()
        {
            while (head != nullptr)
            {
                Node * temp = head->next;
                delete head;
                head = temp;
            }
            tail = nullptr;
            size = 0;
        }
    
    private:
        int size = 0;
        Node* head;  //指向头节点的指针
        Node* tail;     //指向尾节点的指针
    
        Node* GetPointer(int pos)   //查找节点
        {
            Node* pNode = nullptr;
            if (pos<0 || pos>size-1)
            {
                cout << "Out of range! " << endl;
            }
            else
            {
                pNode = head;
                for (int i = 0; i < pos; i++)
                {
                    pNode = pNode->next;
                }
                return pNode;
            }
        }
    };
    
    int main()
    {
        DLlist d(3);
        cout << endl;
        cout << "Size: " << d.getSize() << endl;
        d.printList();
        cout << endl;
    
        cout << "Insert 10 at position 1" << endl;
        d.insert(10, 1);
        cout << "Size: " << d.getSize() << endl;
        d.printList();
    
        cout << endl;
        cout << "Addfirst 100" << endl;
        d.addFirst(100);
        cout << "Now No.1's value= " << d.returnNth(0) << endl;
    
        cout << endl;
        cout << "Remove first" << endl;
        d.removeFirst();
        d.printList();
    
        cout << endl;
        cout << "Remove last" << endl;
        d.removeLast();
        d.printList();
    
        cout << endl;
        d.~DLlist();
        cout << "Empty: " <<boolalpha<< d.isEmpty() << endl;
    
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:数据结构(C++)之Double Linked List(typ

          本文链接:https://www.haomeiwen.com/subject/kztcottx.html