數(shù)據(jù)結(jié)構(gòu)(C++)之Double Linked List(type int)

只寫了int類型,代碼感覺還可以再優(yōu)化,至于用模板則還要學(xué)習(xí); 簡(jiǎn)單起見,全寫在了一個(gè)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;   //指向前節(jié)點(diǎn)指針
    Node* next;   //指向后節(jié)點(diǎn)指針
    
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)   //在兩節(jié)點(diǎn)間插入,插入位置的前后節(jié)點(diǎn)必須都存在
    {
        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)   //刪除節(jié)點(diǎn),刪除位置的前后節(jié)點(diǎn)必須都存在
    {
        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 
        {
            /*我們要在頭結(jié)點(diǎn)前再插入一個(gè)結(jié)點(diǎn),需要先創(chuàng)建一個(gè)新的結(jié)點(diǎn),將頭結(jié)點(diǎn)的值保存在新節(jié)點(diǎn),然后讓新節(jié)點(diǎn)的下
            個(gè)結(jié)點(diǎn)指向頭結(jié)點(diǎn)的下一個(gè)結(jié)點(diǎn),再讓新節(jié)點(diǎn)的prior指向頭結(jié)點(diǎn),這樣就將新節(jié)點(diǎn)與原來的鏈表融合了,然后我  
            們將頭結(jié)點(diǎn)的數(shù)據(jù)換成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;  //指向頭節(jié)點(diǎn)的指針
    Node* tail;     //指向尾節(jié)點(diǎn)的指針

    Node* GetPointer(int pos)   //查找節(jié)點(diǎn)
    {
        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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評(píng)論 25 708
  • ^函數(shù)重載的匹配: 當(dāng)函數(shù)名被重載后,函數(shù)的匹配過程:首先尋找能精確匹配的函數(shù),如果未能精確匹配,則嘗試...
    魯大帥閱讀 1,145評(píng)論 0 1
  • *面試心聲:其實(shí)這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,658評(píng)論 30 472
  • 網(wǎng)上各種流傳著階層越來越固化,一個(gè)人越來越難以獲得成功。但是,內(nèi)心中總有那么一種小小的吶喊與沖動(dòng),難道我們一個(gè)沒有...
    黯然楓雪閱讀 759評(píng)論 0 1
  • 最近忙,因?yàn)槊Χ?,從明天起更從容地去活,追求?nèi)心的平靜。
    自分少年閱讀 160評(píng)論 0 0

友情鏈接更多精彩內(nèi)容