基于List实现适配器Queue(链式队列)

queue是一种先进先出的数据结构。它有两个出口,queue允许新增元素、移除元素、从最底端加入元素、取得最顶端的元素。queue不允许遍历,不提供迭代器。
这里写图片描述
代码如下:

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<stdlib.h>
using namespace std;

template<class T>//关于节点的模板类
struct ListNode
{
    ListNode(const T& x)//构造函数
    :_data(x)
    , _next(NULL)
    , _prev(NULL){}

    T _data;

    ListNode<T>* _next;
    ListNode<T>* _prev;
};

template <class T>
class List
{
    typedef ListNode<T> Node;
public:
    List()
        :_head(NULL)
        , _tail(NULL){}


    List(const List<T>& l)//拷贝构造
        :_head(NULL)
        , _tail(NULL)
    {
        Node* cur = l._head;
        while (cur)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }

    ~List()//析构函数
    {
        Node* cur = _head;
        while (cur)
        {
            Node* next = cur->_next;
            delete cur;
            cur = next;
        }
        _head = _tail = NULL;
    }


    bool Empty()//判NULL
    {
        return _head == 0;
    }

    T& Back()
    {
        return _tail->_data;
    }

    Node* Find(const T& x)//查找
    {
        if (_head == NULL)
        {
            return NULL;
        }
        int i = 0;
        Node* cur = _head;
        while (cur)
        {
            if (cur->_data == x)
            {
                return cur;
            }
            cur = cur->_next;
        }
        return NULL;
    }

    size_t Size()//大小
    {
        Node* cur = _head;
        int count = 0;
        while (cur)
        {
            count++;
            cur = cur->_next;
        }
        return count;
    }

    void PushBack(const T& x)//后插
    {
        if (_head == NULL)//链表为空
        {
            _head = _tail = new Node(x);
        }
        else 
        {
            Node* tmp = new Node(x);//开辟一块新的空间
            _tail->_next = tmp;
            tmp->_prev = _tail;//双向链表
            _tail = tmp;
        }
    }

    void PopBack()//后删
    {
        if (_head == NULL)
        {
            return;
        }
        else if (_head == _tail)
        {
            delete _head;
            _head = _tail = NULL;
        }
        else
        {
            Node* prev = _tail->_prev;
            delete _tail;
            _tail = prev;
            _tail->_next = NULL;
        }
    }

    void PushFront(const T& x)//前插
    {
        if (_head == NULL)
        {
            _head = _tail = new Node(x);
        }
        else
        {
            Node* tmp = new Node(x);
            _head->_prev = tmp;
            tmp->_next = _head;
            _head = tmp;
        }
    }

    void PopFront()//前删
    {
        if (_head == NULL)
        {
            return;
        }
        else if (_head == _tail)
        {
            delete _head;
            _head = NULL;
        }
        else
        {
            Node* tmp = _head;
            _head = _head->_next;
            delete tmp;
        }
    }

    void Insert(Node* pos,const T& x)//指定位置插入
    {
        assert(pos);

        Node* prev = pos->_prev;
        Node* next = pos;
        Node* cur = new Node(x);

        if (prev)
        {
            prev->_next = cur;
            cur->_next = prev;
        }

        cur->_next = next;
        next->_prev = cur;
    }

    void Erase(Node* pos)//指定位置删除
    {
        assert(pos);
        Node* prev = pos->_prev;
        Node* next = pos->_next;
        if (_head == NULL)
        {
            return;
        }
        if (_head == _tail)
        {
            delete _head;
            _head = _tail = NULL;
        }
        else if (prev == NULL)
        {
            Node* cur = _head;
            cur = cur->_next;
            delete _head;
            _head = cur;
        }
        else if (next == NULL)
        {
            Node* cur = _tail;
            cur = cur->_prev;
            delete _tail;
            _tail = cur;
        }
        else
        {
            prev->_next = next;
            next->_prev = prev;
            delete pos;
        }
    }

    void Print()//打印
    {
        Node* cur = _head;
        while (cur)
        {
            cout << cur->_data<< " ";
            cur = cur->_next;
        }
        cout << endl;
    }

    List<int>& operator = (List<T> l)//赋值运算符重载
    {
        swap(l._head, _head);
        swap(l._tail, _tail);
        return *this;
    }

    T& Front()
    {
        return _head->_data;
    }

private:
     Node* _head;
     Node* _tail;
};

//测试用例
void Funteat()
{
    List<int> l1;
    l1.PushBack(1);
    l1.PushBack(2);
    l1.PushBack(3);
    l1.PushBack(4);   
    l1.Print();
}

template<class T,class Container>
class Queue
{
public:
    void Push(const T& x)
    {
        _con.PushBack(x);
    }

    void Pop()
    {
        _con.PopFront();
    }

    T& Back()
    {
        return _con.Back();
    }

    bool Empty()
    {
        return _con.Empty();
    }

    void PushFront(const T& x)
    {
        Insert(_head, x);
    }

    T& Front()
    {
        return _con.Front();
    }
protected:
    Container _con;
};

void TestQueue()
{
    Queue<int, List<int>> q;
    q.Push(1);
    q.Push(2);
    q.Push(3);
    while (!q.Empty())
    {
        cout << q.Front() << " ";
        q.Pop();
    }
    cout << endl;
}

int main()
{
    TestQueue();
    system("pause\n");
    return 0;
}

打印结果如下:
这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值