C++ stack和queue 模拟实现

stack和queue 模拟实现

模拟栈实现

1 栈是一种容器适配器,专门设计用于后进先出的后进先出环境,在这种环境中,元素只从容器的一端插入和提取。
2 栈是作为容器适配器实现的,这些适配器是使用特定容器类的封装对象作为其底层容器的类,提供一组特定的成员函数来访问其元素。元素从特定容器的“后面”(即堆栈的顶部)被推入/弹出。
3 底层容器可以是任何标准容器类模板或其他特定设计的容器类。集装箱应支持以下操作:

  • empty
  • size
  • back
  • push_back
  • pop_back
//头文件
#include <list>
#include <vector>
#include <deque>

namespace test
{
	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_stack()
	{
		stack<int> st1;
		st1.push(1);
		st1.push(2);
		st1.push(3);
		st1.push(4);

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

在这里插入图片描述

模拟队实现

1 先进先出队列队列是一种容器适配器,专门设计用于在FIFO上下文中操作(先进先出),其中将元素插入容器的一端并从另一端提取。
2 队列是作为容器适配器实现的,容器适配器是使用特定容器类的封装对象作为其底层容器的类,提供一组特定的成员函数来访问其元素。元素被推入特定容器的“后面”,并从其“前面”弹出。
3 底层容器可以是标准容器类模板之一,也可以是其他专门设计的容器类模板容器类。该底层容器应至少支持以下操作:

  • empty
  • size
  • front
  • back
  • push_back
  • pop_front
//头文件
#include <vector>
#include <list>
#include <deque>

namespace test
{
	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();
		}
		
		T& back()
		{
			return _con.back();
		}

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

		bool empty()
		{
			return _con.empty();
		}
	private:
		Container _con;
	};
	void test_queue()
	{
		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;
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以用两个队列来模拟一个栈的数据结构,具体实现如下: 1. 定义两个队列queue1和queue2; 2. 将元素压入栈时,将元素放入queue1中; 3. 将元素弹出栈时,先将queue1中的元素依次出队并放入queue2中,直到queue1中只剩一个元素,将该元素出队返回即可; 4. 在弹出元素时,如果queue1中只有一个元素,直接出队返回即可,不需要将元素放入queue2中; 5. 在弹出元素时,交换queue1和queue2的指针,使得下一次弹出时可以从另一个队列中取出元素; 6. 使用两个队列实现的栈,其空间复杂度为O(n),其中n为栈中元素的个数。 以下是使用C语言实现的代码: ``` #include <stdio.h> #include <stdlib.h> typedef struct Queue { int* data; int front; int rear; } Queue; typedef struct Stack { Queue* queue1; Queue* queue2; } Stack; Queue* createQueue(int size) { Queue* queue = (Queue*)malloc(sizeof(Queue)); queue->data = (int*)malloc(sizeof(int) * size); queue->front = 0; queue->rear = 0; return queue; } Stack* createStack(int size) { Stack* stack = (Stack*)malloc(sizeof(Stack)); stack->queue1 = createQueue(size); stack->queue2 = createQueue(size); return stack; } void push(Stack* stack, int value) { stack->queue1->data[stack->queue1->rear] = value; stack->queue1->rear++; } int pop(Stack* stack) { int value = 0; if(stack->queue1->front == stack->queue1->rear) { printf("Stack is empty.\n"); return -1; } while(stack->queue1->front < stack->queue1->rear - 1) { stack->queue2->data[stack->queue2->rear] = stack->queue1->data[stack->queue1->front]; stack->queue1->front++; stack->queue2->rear++; } value = stack->queue1->data[stack->queue1->front]; stack->queue1->front++; Queue* temp = stack->queue1; stack->queue1 = stack->queue2; stack->queue2 = temp; return value; } int main() { Stack* stack = createStack(10); push(stack, 1); push(stack, 2); push(stack, 3); printf("%d\n", pop(stack)); push(stack, 4); printf("%d\n", pop(stack)); printf("%d\n", pop(stack)); printf("%d\n", pop(stack)); printf("%d\n", pop(stack)); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值