每日一题--一个数组实现三个栈(Google推荐面试书--Cracking the Coding Interview)

题目

  • 原文:
    Describe how you could use a single array to implement three stacks.
  • 译文:
    你如何只用一个数组实现三个栈?

分析

  • 方法一:将数组三等分,每一份长度用来实现一个栈,缺点是空间利用率低;
  • 方法二:在数组中顺序放置要入栈的元素,数组中的每一项不仅记录栈中的值还增加一个值记录该所在栈上一个元素的地址(即数组中的索引)。

代码

#include <iostream>

using namespace std;

class stackarray1 {
	public:	
		stackarray1(int size) {
			array = new int[size*3];
			this->msize = size;
			ptop[0] = ptop[1] = ptop[2] = -1;
		}
		
		~stackarray1() {
			delete[] array;
		}
		
		void push(int stackNum, int val) {
			if(ptop[stackNum] < (this->msize - 1)) {
				array[stackNum * this->msize + ptop[stackNum] + 1] = val;
				ptop[stackNum]++;
				cout << val << "入" << stackNum << "号栈成功。" << endl;
			}
			else {
				cout << stackNum << "号栈已满,暂无法入栈!" << endl; 
			}
		}
		
		void pop(int stackNum) {
			if(!empty(stackNum)) {
				int val = array[stackNum * this->msize + ptop[stackNum]];
				ptop[stackNum]--;
				cout << stackNum << "号栈出栈为" << val << "成功。" << endl;
			}
			else {
				cout << stackNum << "号栈已空,暂无法出栈!" << endl; 
			}
		}
		
		int top(int stackNum) {
			if(!empty(stackNum)) {
				int val = array[stackNum * this->msize + ptop[stackNum]];
				return val;
			}
			else {
				cout << stackNum << "号栈已空!" << endl; 
			}
		}
		
		bool empty(int stackNum) {
			return ptop[stackNum] ==-1;
		}
		
		int size(int stackNum) {
			return ptop[stackNum]+1;
		}
		
	private:
		int msize;
		int* array;
		int ptop[3];
};

typedef struct node{
	int val, plast;
} node;

class stackarray2 {
	public:
		stackarray2(int size) {
			array = new node[size];
			this->msize = size;
			ptop[0] = ptop[1] = ptop[2] = -1;
			cur = 0;
		}
		
		void push(int stackNum, int val) {
			if (cur < this->msize) {
				array[cur].val = val;
				array[cur].plast = ptop[stackNum];
				ptop[stackNum] = cur;
				cur++; 
				cout << val << "入" << stackNum << "号栈成功。" << endl;
			}
			else {
				cout << stackNum << "号栈已满,暂无法入栈!" << endl; 
			}
		}
		
		void pop(int stackNum) {
			if(!empty(stackNum)) {
				int val = array[ptop[stackNum]].val;
				ptop[stackNum] = array[ptop[stackNum]].plast;
				cout << stackNum << "号栈出栈为" << val << "成功。" << endl;
			}
			else {
				cout << stackNum << "号栈已空,暂无法出栈!" << endl; 
			}
		}
		
		int top(int stackNum) {
			if(!empty(stackNum)) {
				int val = array[ptop[stackNum]].val;
				return val;
			}
			else {
				cout << stackNum << "号栈已空!" << endl; 
			}
		}
		
		bool empty(int stackNum) {
			return ptop[stackNum] ==-1;
		}
		
		int size(int stackNum) {
			return ptop[stackNum]+1;
		}
		
	private:
		int msize;
		node * array;
		int ptop[3];
		int cur;
};

int main() {
	stackarray1 stack(1);
	stack.push(1,1);
	stack.top(0);
	cout << "1号栈头为" << stack.top(1) << endl;
	cout << stack.empty(1) << endl;
	stack.push(1,2);
	cout << "1号栈有" << stack.size(1) << "个元素" << endl; 
	stack.pop(1);
	cout << stack.empty(2) << endl;
	stack.top(1);
	cout << "----------------" << endl;
	stackarray2 stack2(4);
	stack2.push(0,1);
	stack2.push(1,2);
	stack2.push(2,3);
	stack2.push(1,4);
	cout << "0号栈头为" << stack2.top(0) << endl;
	cout << "1号栈头为" << stack2.top(1) << endl;
	cout << stack2.empty(0) << endl;
	stack2.push(0,2);
	cout << "0号栈有" << stack2.size(0) << "个元素" << endl; 
	stack2.pop(0);
	cout << stack2.empty(0) << endl;
	stack2.top(0);
}

结果
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值