[C++] stack and queue


describe

Stacks and queues are very important data structures in computer science and they are used in many applications such as compilers, operating systems, network routers, etc. In this article, we'll discuss the basic concepts, operations, and applications of stacks and queues.

1. The concept and operation of the stack The stack is a last-in-first-out (LIFO) data structure, and its basic operations include push and pop. The top element of the stack is the last element pushed, so it is the first element to be popped. Stacks are usually implemented using arrays or linked lists.

The following are some basic stack operations:
push(item): Push the element item onto the top of the stack.
pop(): Pop the top element of the stack and return it.
top(): Returns the top element of the stack without popping it.
isEmpty(): Checks if the stack is empty.
isFull(): Checks if the stack is full (only for array implementations).

Second, the concept and operation of the queue The queue is a first-in-first-out (FIFO) data structure, and its basic operations include enqueue and dequeue. The tail of the queue is the last element enqueued, so it is the first element dequeued. Queues are usually implemented using arrays or linked lists.

The following are some basic queue operations:
enqueue(item): Insert the element item into the tail of the queue.
dequeue(): Pop an element from the head of the queue and return it.
front(): returns the element at the head of the queue, but does not pop it.
isEmpty(): Checks if the queue is empty.
isFull(): Checks if the queue is full (only for array implementations).

1. stack

common interface

insert image description here

Simulation implementation

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;
	}
}

Two, queue

common interface

insert image description here

Simulation implementation

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;
	}
}

3. Stack and queue OJ questions

①Minimum stack
insert image description here

untie

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;
};

②Stack push, pop sequence

insert image description here

untie

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();
    }
};

Guess you like

Origin blog.csdn.net/Tianzhenchuan/article/details/131815499