用数组or链表实现队列和栈

cpp 复制代码
#include<stdio.h>
#include<iostream>

#define SIZE 1000

using namespace std;

//用数组实现栈
class mystack_array {
public:
	mystack_array(int _top = -1) :top(_top) {}
	void push(int num) {
		top++;
		stack[top] = num;
	}

	int pop() {
		if (top != -1) {
			int tmp = stack[top];
			top--;
			return tmp;
		}
		return -1;
	}

	bool isEmpty() {
		return top == -1;
	}

private:
	int stack[SIZE];
	int top;

};

//用链表实现栈
class mystack_List {
private:
	class Node {
	public:
		Node(int _val, Node* p) :val(_val), next(p) {}

		Node* next;
		int val;
	};
	Node* top;
	int size;


public:
	mystack_List(int _size = 0,Node*p=nullptr) :size(_size),top(p) {}
	void push(int num) {//头插法
		Node* node = (Node*)malloc(sizeof(Node));
		node->val = num;

		if (size == 0) {
			top = node;
			top->next = nullptr;
		}
		node->next = top->next;
		top->next = node;
		size++;
	}
	int pop() {
		if (size == 0)
			return -1;

		Node* tmp = top->next;
		int num = tmp->val;
		top = top->next;
		size--;
		return num;		
	}

};


//用数组实现队列
class myqueue_array {
public:
	myqueue_array() {
		this->front = -1;
		this->rear = -1;
	}
	bool isEmpty() {
		return rear == front;
	}
	bool isFull() {
		return rear == SIZE - 1;
	}

	int getfront() {
		if (!isEmpty()) {
			return arr[front + 1];
		}
		return -1;
	}
	void push(int num) {
		rear++;
		arr[rear] = num;
	}
	int pop() {
		if (!isEmpty()) {
			front++;
			return arr[front];
		}
		return -1;
		
	}

private:
	int rear;
	int front;
	int  arr[SIZE];
};

//用单链表实现队列
class myqueue_list {
private:
	struct Node {
		Node* next;
		int val;
		//Node() {}
		Node(int num2) :val(num2) {}
	};
	Node* rear;
	Node* front;

public:
	myqueue_list() {
		this->front = this->rear = nullptr;
	}
	
	bool isEmpty() {
		if (front == nullptr)
			return true;
		return false;
	}
	void push(int num) {
		Node* tmp = (Node*)malloc(sizeof(Node));
		tmp->val = num;

		if (isEmpty()) {
			rear = front = tmp;
		}
		else {
			tmp->next = nullptr;//尾插法

			rear->next = tmp;
			rear = tmp;
		
		}

	}
	int pop() {
		if (rear == nullptr)
			return -1;
		int num = front->val;
		front = front->next;
		return num;
	}

};

int main() {
	myqueue_list st;
	int n = 10;

	cout <<"进入顺序为:"<<endl;
	for (int i = 1; i < n; ++i) {
		st.push(i);
		cout << i << "  ";
	}		
	cout << endl;

	cout << endl;
	cout << "*******************" << endl;
	cout << endl;

	cout << "出顺序为:" << endl;
	for (int i = 1; i < n; ++i) {	
		cout << st.pop() << "  ";
	}
	cout << endl;
	return 0;
}
相关推荐
风一样的航哥2 小时前
LeetCode 2615 等值距离和:前缀和优化O(n)解法深度解析
数据结构·算法·leetcode
khalil10204 小时前
代码随想录算法训练营Day-34动态规划03 | 01背包问题 二维、01背包问题 一维、416. 分割等和子集
数据结构·c++·算法·leetcode·动态规划·背包问题·01背包
空中海5 小时前
Redis 从零到精通:9大数据结构 × 11个高频工程实战场景完全手册
数据结构·数据库·redis
地球资源数据云6 小时前
1951-2025年中国逐年1千米逐月总降水量区域统计数据集_年表_县
大数据·数据结构·数据库·数据仓库·人工智能
cpp_25017 小时前
P2639 [USACO09OCT] Bessie‘s Weight Problem G
数据结构·算法·动态规划·题解·洛谷·背包dp
郝学胜-神的一滴7 小时前
[力扣 227] 双栈妙解表达式计算:从思维逻辑到C++实战,吃透反向波兰式底层原理
java·前端·数据结构·c++·算法
菜鸟丁小真8 小时前
LeetCode hot100 -131.分割回文串
数据结构·算法·leetcode·知识点总结
数智化精益手记局8 小时前
8d报告案例分析:拆解8d报告案例分析的8个步骤,解决生产现场重复发生的质量难题
大数据·数据结构·数据库·人工智能·精益工程
笨笨饿8 小时前
66_C语言与微控制器底层开发
linux·c语言·网络·数据结构·算法·机器人·个人开发
AI人工智能+电脑小能手8 小时前
【大白话说Java面试题】【Java基础篇】第10题:HashMap中的元素是有序存放的吗
java·开发语言·数据结构·后端·面试·哈希算法·哈希表