【C++】栈和队列


描述

栈和队列是计算机科学中非常重要的数据结构,它们可用于许多应用程序中,如编译器、操作系统、网络路由器等。在本文中,我们将讨论栈和队列的基本概念、操作和应用。

一、栈的概念和操作 栈是一种后进先出(LIFO)的数据结构,它的基本操作包括压栈(push)和弹栈(pop)。栈的顶部元素是最后一个被压入的元素,因此它是第一个被弹出的元素。栈的实现通常使用数组或链表。

下面是一些基本的栈操作:
push(item):将元素item压入栈顶。
pop():弹出栈顶元素,并返回该元素。
top():返回栈顶元素,但不弹出。
isEmpty():检查栈是否为空。
isFull():检查栈是否已满(仅适用于数组实现)。

二、队列的概念和操作 队列是一种先进先出(FIFO)的数据结构,它的基本操作包括入队(enqueue)和出队(dequeue)。队列的尾部是最后一个入队的元素,因此它是第一个出队的元素。队列的实现通常使用数组或链表。

下面是一些基本的队列操作:
enqueue(item):将元素item插入队列尾部。
dequeue():从队列头部弹出一个元素,并返回该元素。
front():返回队列头部元素,但不弹出。
isEmpty():检查队列是否为空。
isFull():检查队列是否已满(仅适用于数组实现)。

一、stack

常见接口

在这里插入图片描述

模拟实现

namespace tzc
{
    
    
	//适配器是一种设计模式(设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结),
	//该种模式是将一个类的接口转换成客户希望的另外一个接口。
	//deque双端队列 优点是首尾端操作方便,一般用作栈和队列的容器适配器
	//容器适配器  
	template<class T, class Container= deque<T>>
	class stack
	{
    
    
	public:
		void push(const T& x)
		{
    
    
			_con.push_back(x);
		}
		void pop()
		{
    
    
			_con.pop_back();
		}

		T& top()
		{
    
    
			return _con.back();
		}

		size_t size()
		{
    
    
			return _con.size();
		}
		bool empty()
		{
    
    
			return _con.empty();
		}
	private:
		Container _con;
	};
	
	//测试
	void test_stack1()
	{
    
    
		//数组栈
		stack<int, vector<int>> st1;
		//链式栈
		stack<int, list<int>> st2;

		st1.push(1);
		st1.push(2);
		st1.push(3);
		st1.push(4);

		while (!st1.empty())
		{
    
    
			cout << st1.top() << " ";
			st1.pop();
		}
		cout << endl;

		st2.push(1);
		st2.push(2);
		st2.push(3);
		st2.push(4);

		while (!st2.empty())
		{
    
    
			cout << st2.top() << " ";
			st2.pop();
		}
		cout << endl;
	}
}

二、queue

常见接口

在这里插入图片描述

模拟实现

namespace tzc
{
    
    
	//容器适配器
	template<class T, class Container= deque<T>>
	class queue
	{
    
    
	public:
		void push(const T& x)
		{
    
    
			_con.push_back(x);
		}
		void pop()
		{
    
    
			_con.pop_front();
			//_con.erase(_con.begin());
		}

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

		size_t size()
		{
    
    
			return _con.size();
		}
		bool empty()
		{
    
    
			return _con.empty();
		}


	private:
		Container _con;
	};

	void test_queue1()
	{
    
    
		queue<int, list<int>> q;

		q.push(1);
		q.push(2);
		q.push(3);
		q.push(4);

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

三、 栈和队列OJ题

最小栈
在这里插入图片描述

class MinStack {
    
    
public:
    MinStack() {
    
    
    }
    //如果最小栈中没有元素或者比最小栈中元素还要小,就入栈
    void push(int val) {
    
    
        if (minst.empty() || minst.top() >= val)
        {
    
    
            minst.push(val);
        }
        st.push(val);
    }
    //如果要出栈的元素比最小栈中的元素还要小,最小栈中元素也需出栈
    void pop() {
    
    
        if (st.top() == minst.top())
        {
    
    
            minst.pop();
        }
        st.pop();
    }

    int top() {
    
    
        return st.top();
    }

    //最小栈中栈顶的元素就是最小元素
    int getMin() {
    
    
        return minst.top();
    }
private:
    stack<int> st;
    stack<int> minst;
};

栈的压入,弹出序列

在这里插入图片描述

class Solution {
    
    
public:
    bool IsPopOrder(vector<int>& pushV, vector<int>& popV) {
    
    
        // write code here
        stack<int> st;
        int pushIndex=0,popIndex=0;
        while(pushIndex<pushV.size())
        {
    
    
            st.push(pushV[pushIndex++]);
            while(!st.empty()&&popV[popIndex]==st.top())
            {
    
    
                st.pop();
                popIndex++;
            }
        }
        return st.empty();
    }
};

猜你喜欢

转载自blog.csdn.net/Tianzhenchuan/article/details/131815499