1. 程式人生 > >C++ 實現雙向連結串列

C++ 實現雙向連結串列

#include<iostream>
#include<assert.h>
using namespace std;

typedef int DataType;

struct ListNode
{
    ListNode* prev;
    ListNode* next;
    DataType data;
    ListNode(DataType x)
        :data(x)
        ,next(NULL)
        ,prev(NULL)
    {
        ;
    }
};                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

typedef ListNode Node;

class List
{ public: List() :_head(NULL) , _tail(NULL) {} void PushBack(const DataType x) { //1.連結串列為空 if (_head == NULL) { _head = Buynode(x); _tail = _head; } //2.連結串列不為空 else { Node* tmp =
_tail; _tail->next = Buynode(x); _tail = _tail->next; _tail->prev = tmp; } } void PushFront(const DataType&x) { //1.連結串列為空 if (_head == NULL) { _head = Buynode(x); _tail = _head; } //2.連結串列不為空
else { Node* tmp = _head; _head->prev = Buynode(x); _head = _head->prev; _head->next = tmp; } } void PopBack() { //1.連結串列為空 if (_tail == NULL) { return; } //2.只有一個節點 else if (_tail == NULL) { delete _tail; _head = _tail = NULL; } //3.有多個節點 else { Node* tmp = _tail; _tail = _tail->prev; _tail->next = NULL; delete tmp; } } void PopFront() { //1.連結串列為空 if (_head == NULL) { return; } //2.只有一個節點 else if (_head->next == NULL) { delete _head; _head = _tail = NULL; } //3.有多個節點 else { Node* tmp = _head; _head = _head->next; _head->prev = NULL; delete tmp; } } Node* Find(const DataType& x) { Node* cur = _head; while (cur) { if (cur->data == x) { return cur; } cur = cur->next; } return cur; } void Insert(Node* pos, const DataType&x)//目標節點前插 { assert(pos); //目標節點是頭結點直接前插 if (pos == _head) { PushFront(x); } else { Node*tmp = Buynode(x); tmp->prev = pos->prev; pos->prev->next = tmp; tmp->next = pos; pos->prev = tmp; } } void Erase(Node* pos) { assert(pos); //1.要刪除節點是頭結點直接呼叫頭刪 if (pos == _head) { PopFront(); } //2.目標節點是尾節點直接呼叫尾刪 else if (pos == _tail) { PopBack(); } else { pos->prev->next = pos->next; pos->next->prev = pos->prev; delete pos; pos = NULL; } } size_t Size() //求雙鏈表節點個數 { if (_head == NULL) { return 0; } size_t count = 0; Node* cur = _head; while (cur) { cur = cur->next; count++; } return count; } bool Empty()const //判斷連結串列是否為空 { if (_head == _tail == NULL) { return true; } return false; } List(const List& l) //連結串列的拷貝構造 :_head(NULL) ,_tail(NULL) { Node* tmp = l._head; while (tmp) { PushBack(tmp->data); tmp = tmp->next; } } void Swap(List &l) { swap(_head, l._head); swap(_tail, l._tail); } List&operator = (const List& l) //賦值運算子過載 { if (this != &l) { List tmp(l); Swap(tmp); } return *this; } void Display()const { if (_head == NULL) { cout << "NULL" << endl; } Node* cur = _head; while (cur) { cout << cur->data << "->"; cur = cur->next; } cout << endl; Node* tmp = _tail; while (tmp) { cout << tmp->data << "->"; tmp = tmp->prev; } cout << endl; } private: Node* Buynode(const DataType& data) { return new Node(data); } private: Node* _head; Node* _tail; }; void test1() { List lel; lel.PushBack(1); lel.PushBack(2); lel.PushBack(3); lel.PushBack(4); lel.Display(); lel.PushFront(0); lel.Display(); lel.PopFront(); lel.PopBack(); lel.Display(); Node* aim = lel.Find(3); lel.Insert(aim, 8); lel.Display(); lel.Erase(aim); lel.Display(); List tet; tet = lel; tet.Display(); int i = lel.Empty(); cout << i << endl; } int main() { test1(); system("pause"); return 0; }