用数组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 小时前
【哈希表】
数据结构·哈希算法·散列表
重生之后端学习4 小时前
283. 移动零[简单]✅
开发语言·数据结构·算法·leetcode·职场和发展
动词ing5 小时前
【C语言】结构体+文件基础
c语言·开发语言·数据结构
晚风醉蝶8 小时前
1-11-奇偶排序-OddEvenSort
java·数据结构·算法
旖旎夜光8 小时前
LeetCode 852:山脉数组的峰顶索引(二分查找) —— 题解
数据结构·c++·算法·leetcode·二分查找
小雪崩10 小时前
嵌入式学习 day25:哈希表及排序与查找
linux·c语言·数据结构·学习·排序算法
冻柠檬飞冰走茶12 小时前
《数据结构实验指导-C++语言版》 求单链表list中的元素个数,即表长
开发语言·数据结构·c++·算法·list
_Narcissus_12 小时前
动态规划初探(含完整代码实现)
c语言·数据结构·c++·算法·动态规划·背包问题·最短路径
_Narcissus_14 小时前
枚举和模拟算法笔记
c语言·数据结构·c++·笔记·算法·模拟·枚举
冻柠檬飞冰走茶14 小时前
《数据结构实验指导-C++语言版》 在顺序表 list 中查找元素 x
开发语言·数据结构·c++·算法·list