用队列实现栈(C++)

83 阅读2分钟

1. 环形队列实现

1.1 基于数组

// 08_环形队列.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
using namespace std;

// 环形队列   queue  push  pop  front  back  empty size
class Queue
{
public:
    Queue(int size = 10)
        : cap_(size)
        , front_(0)
        , rear_(0)
        , size_(0)
    {
        pQue_ = new int[cap_];
    }
    ~Queue()
    {
        delete[]pQue_;
        pQue_ = nullptr;
    }

public:
    // 入队 O(1)
    void push(int val)
    {
        if ((rear_ + 1) % cap_ == front_)
        {
            expand(2 * cap_);
        }
        pQue_[rear_] = val;
        rear_ = (rear_ + 1) % cap_;
        size_++;
    }
    // 出队 O(1)
    void pop()
    {
        if (front_ == rear_)
            throw "queue is empty!";
        front_ = (front_ + 1) % cap_;
        size_--;
    }
    // 队头元素
    int front() const
    {
        if (front_ == rear_)
            throw "queue is empty!";
        return pQue_[front_];
    }
    // 队尾元素
    int back() const
    {
        if (front_ == rear_)
            throw "queue is empty!"; 
        return pQue_[(rear_ - 1 + cap_) % cap_];
    }
    // 队空
    bool empty() const
    {
        return front_ == rear_;
    }
    // 队列元素的个数
    int size() const
    {
        // return size;  O(1)
        // 遍历一遍统计队列元素个数 O(n)
        int size = 0;
        for (int i = front_; i != rear_; i = (i + 1) % cap_)
        {
            size++;
        }
        return size;
    }

private:
    // 扩容接口
    void expand(int size)
    {
        int* p = new int[size];
        int i = 0;
        int j = front_;
        for (; j != rear_; i++, j = (j + 1) % cap_)
        {
            p[i] = pQue_[j];
        }
        delete[]pQue_;
        pQue_ = p;
        cap_ = size;
        front_ = 0;
        rear_ = i;
    }

private:
    int* pQue_;
    int cap_;   // 空间容量
    int front_; // 队头
    int rear_;  // 队尾
    int size_;  // 队列元素个数
};

int main()
{
    int arr[] = { 12,4,56,7,89,31,53,75 };
    Queue que;

    for (int v : arr)
    {
        que.push(v);
    }
    
    cout << que.front() << endl;
    cout << que.back() << endl;

    que.push(100);
    que.push(200);
    que.push(300);
    cout << que.front() << endl;
    cout << que.back() << endl;

    while (!que.empty())
    {
        cout << que.front() << " " << que.back() << endl;
        que.pop();
    }
}

1.2 基于链表

// 09_链式队列.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
using namespace std;

// 链式队列
class LinkQueue
{
public:
	LinkQueue()
	{
		head_ = new Node();
		head_->next_ = head_;
		head_->pre_ = head_;
	}
	~LinkQueue()
	{
		Node* p = head_->next_;
		while (p != head_)
		{
			head_->next_ = p->next_;
			p->next_->pre_ = head_;
			delete p;
			p = head_->next_;
		}
		delete head_;
		head_ = nullptr;
	}

public:
	// 入队
	void push(int val)
	{
		Node* node = new Node(val);
		node->next_ = head_;
		node->pre_ = head_->pre_;
		head_->pre_->next_ = node;
		head_->pre_ = node;
	}
	// 出队
	void pop()
	{
		Node* p = head_->next_;
		head_->next_ = p->next_;
		p->next_->pre_ = head_;
		delete p;
	}
	// 获取队头元素
	int front() const
	{
		if (head_->next_ == head_)
		{
			throw "queue is empty!";
		}
		return head_->next_->data_;
	}
	// 获取队尾元素
	int back() const
	{
		if (head_->next_ == head_)
		{
			throw "queue is empty!";
		}
		return head_->pre_->data_;
	}
	// 判空
	bool empty() const
	{
		return head_->next_ == head_;
	}

private:
	struct Node
	{
		Node(int data = 0) 
			: data_(data)
			, next_(nullptr)
			, pre_(nullptr) 
		{}
		int data_;
		Node* next_;
		Node* pre_;
	};

	Node* head_; // 指向头节点
};

int main()
{
	int arr[] = { 12,4,56,7,89,31,53,75 };
	LinkQueue que;

	for (int v : arr)
	{
		que.push(v);
	}

	cout << que.front() << endl;
	cout << que.back() << endl;

	que.push(100);
	que.push(200);
	que.push(300);
	cout << que.front() << endl;
	cout << que.back() << endl;

	while (!que.empty())
	{
		cout << que.front() << " " << que.back() << endl;
		que.pop();
	}
}

用两个队列实现栈

225. 用队列实现栈 - 力扣(LeetCode)

思路:利用二个队列,队列2存放新元素,其余的交给队列1

class MyStack {
public:
    MyStack() {

    }
    
    void push(int x) {
        que2.push(x); // que2的唯一作用,存储新进来的元素
        while(!que1.empty()) {
            que2.push(que1.front());
            que1.pop();
        }
        swap(que1, que2);
    }
    
    int pop() {
        int num = que1.front();
        que1.pop();
        return num;
    }
    
    // 从front出去的
    int top() {
        return que1.front();
    }
    
    bool empty() {
        return que1.empty();
    }
private:
    queue<int>que1;
    queue<int>que2;
};

用一个队列

class MyStack {
public:
    MyStack() {

    }
    
    //每次放元素都会调整为栈的位置
    void push(int x) {
        int n = que.size();
        que.push(x);
        for (int i = 0; i < n; i++) {
            que.push(que.front());
            que.pop();
        }
    }
    
    int pop() {
        int ret = que.front();
        que.pop();
        return ret;
    }
    
    int top() {
         int ret = que.front();
        return ret;
    }
    
    bool empty() {
        return que.empty();
    }
private:
    queue<int>que;
};