数据结构(四)队列 和 栈

队列.h

#include <queue>

void Test() {
	std::queue<int>		q;

	q.push(1);
	q.push(2);
	q.push(3);		// 1, 2, 3

	q.front();		// 1
	q.pop();		// 2, 3
	q.front();		// 2
	q.pop();		// 3
	q.size();		// 1
	q.empty();		// false
	q.front();		// 3
	q.pop();		//
	q.size();		// 0
	q.empty();		// true
}

栈.h

#include <stack>

/*
standard	标准
std::stack<int>	s;
// 压栈		把数据放到栈里
void s.push(100);
// 弹出 弹栈	把数据从栈里处理
void s.pop();
// 获取栈顶元素
int s.top();
// 获取栈内数据
int s.size();
// 判断栈是否为空
bool s.empty();		true | false
*/

void Test() {
	std::stack<int>	 s;
	s.push(1);	s.push(3);	s.push(9);	// 1, 3, 9
	printf("%d\n", s.top());			// 9
	s.pop();							// 1, 3
	printf("%d\n", s.top());			// 3
	s.pop();							// 1
	printf("%d\n", s.top());			// 1
	printf("size: %d\n", s.size());		// 1
}

循环队列.h

class MyCircularQueue {
public:
	int size;
	int capacity;
	int *array;

	int front;
	int rear;

	/* Initialize your data structure here. Set the size of the queue to be k. */
	/* 在这里初始化数据结构。将队列的大小设置为k。*/
	MyCircularQueue(int k) {
		array = (int *)malloc(sizeof(int)* k);
		size = 0;
		capacity = k;

		front = 0;
		rear = 0;
	}

	/* Insert an element into the circular queue. Return true if the operation is successful. */
	/* 在循环队列中插入一个元素。如果操作成功,返回true。*/
	bool enQueue(int value) {
		if (size >= capacity) {
			return false;
		}

		array[rear] = value;
		rear = (rear + 1) % capacity;
		size++;
		return true;
	}

	/* Delete an element from the circular queue. Return true if the operation is successful. */
	/* 从循环队列中删除一个元素。如果操作成功,返回true。*/
	bool deQueue() {
		if (size <= 0) {
			return false;
		}

		size--;
		front = (front + 1) % capacity;
		return true;
	}

	/* Get the front item from the queue. */
	/* 从队列中获取前端项。*/
	int Front() {
		if (size <= 0) {
			return -1;
		}
		return array[front];
	}

	/* Get the last item from the queue. */
	/* 从队列中获取最后一项。*/
	int Rear() {
		if (size <= 0) {
			return -1;
		}

		int index = (rear - 1 + capacity) % capacity;
		return array[index];
	}

	/* Checks whether the circular queue is empty or not. */
	/* 检查循环队列是否为空。*/
	bool isEmpty() {
		return size == 0;	//判断size == 0
	}

	/* Checks whether the circular queue is full or not. */
	/* 检查循环队列是否已满。*/
	bool isFull() {
		return size == capacity;
	}
};

用链表实现队列.h

#pragma once

// 用链表实现栈
// 单向 不循环 不带头
// 头删 + 尾插

typedef struct Node {
	int value;
	struct Node *next;
}	Node;

typedef struct Queue {
	Node *head;		// 链表的第一个结点
	Node *last;		// 链表的最后一个结点
}	Queue;

void QInit(Queue *q) {
	q->head = NULL;
	q->last = NULL;
}

void QPush(Queue *q, int v) {
	Node *node = (Node *)malloc(sizeof(Node));
	node->value = v;
	node->next = NULL;

	if (q->head == NULL) {
		q->head = node;
		q->last = node;
	}
	else {
		q->last->next = node;
		q->last = node;
	}
}

void QPop(Queue *q) {
	assert(q != NULL);
	assert(q->head != NULL);

	Node *head = q->head;
	q->head = q->head->next;
	free(head);

	if (q->head == NULL) {
		q->last = NULL;
	}
}

int QFront(Queue *q) {
	return q->head->value;
}

int QSize(Queue *q) {
	int size = 0;
	for (Node *c = q->head; c != NULL; c = c->next) {
		size++;
	}

	return size;
}

int QEmpty(Queue *q) {
	return q->head == NULL ? 1 : 0;
}

栈实现队列.h

class MyQueue {
public:
	stack<int>  in;
	stack<int>  out;

	/* Initialize your data structure here. */
	/* 在这里初始化数据结构。*/
	MyQueue() {}

	/* Push element x to the back of queue. */
	/* 将元素x推到队列的后面。*/
	void push(int x) {
		in.push(x);
	}

	/* Removes the element from in front of queue and returns that element. */
	/* 从队列前面移除元素并返回该元素。*/
	int pop() {
		if (out.empty()) {
			while (!in.empty()) {
				int v = in.top();
				in.pop();
				out.push(v);
			}
		}

		int v = out.top();
		out.pop();
		return v;
	}

	/* Get the front element. */
	/* 获取front元素。*/
	int peek() {
		if (out.empty()) {
			while (!in.empty()) {
				int v = in.top();
				in.pop();
				out.push(v);
			}
		}

		int v = out.top();
		return v;
	}

	/* Returns whether the queue is empty. */
	/* 返回队列是否为空。*/
	bool empty() {
		return in.empty() && out.empty();
	}
};

队列实现栈.h

class MyStack {
public:
	queue<int>  q;

	/* Initialize your data structure here. */
	/* 在这里初始化数据结构。*/
	MyStack() {}

	/* Push element x onto stack. */
	/* 将元素x推入堆栈。*/
	void push(int x) {
		q.push(x);
	}

	/* Removes the element on top of the stack and returns that element. */
	/* 删除堆栈顶部的元素并返回该元素。*/
	int pop() {
		int size = q.size();
		for (int i = 0; i < size - 1; i++) {
			int v = q.front();
			q.pop();
			q.push(v);
		}

		int v = q.front();
		q.pop();
		return v;
	}

	/* Get the top element. */
	/* 获取顶部元素。*/
	int top() {
		int size = q.size();
		for (int i = 0; i < size - 1; i++) {
			int v = q.front();
			q.pop();
			q.push(v);
		}

		int v = q.front();
		q.pop();
		q.push(v);
		return v;
	}

	/* Returns whether the stack is empty. */
	/* 返回堆栈是否为空。*/
	bool empty() {
		return q.empty();
	}
};

min栈.h

//最小栈
class MinStack {
public:
	stack<int>  normal;
	stack<int>  min;

	/* initialize your data structure here. */
	/* 在这里初始化数据结构。*/
	MinStack() {}

	void push(int x) {
		normal.push(x);
		if (min.empty() || x <= min.top()) {
			min.push(x);
		}
		else {
			min.push(min.top());
		}
	}

	void pop() {
		normal.pop();
		min.pop();
	}

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

	int getMin() {
		return min.top();
	}
};

此图用于理解最小栈:
在这里插入图片描述

括号匹配.h

class Solution {
public:
	// 字符串类型
	// char 类型的顺序表
	// size() 返回字符个数
	bool isValid(string s) {
		stack<char> st;
		int size = s.size();
		for (int i = 0; i < size; i++) {
			char ch = s[i];
			switch (ch) {
				case '(':
				case '[':
				case '{':
					st.push(ch);
					break;
				case ')':
				case ']':
				case '}': {
					if (st.empty()) {
						// 右括号多
						return false;
					}
					char left = st.top();
					if ((left == '(' && ch == ')')
						|| (left == '[' && ch == ']')
						|| (left == '{' && ch == '}')) {
						st.pop();
					}
					else {
						// 左右不是一种括号
						return false;
					}
				}
			}
		}
		if (!st.empty()) {
			// 左括号多
			return false;
		}
		return true;
	}
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值