【数据结构】 栈的模拟

学习数据结构基础,如有错误,请指正



/***
数据结构:栈的模拟
***/

#ifndef __STACK_H__
#define __STACK_H__

#define MAXSIZE 100

typedef char ElemType;
typedef struct{
	ElemType *top;
	ElemType *base;
	int stackSize;
} sqStack;

class Stack{
public:
	Stack();
	~Stack();

	void initStack();
	void push(ElemType e);
	ElemType pop();
	int stackLongth();
	void clearStack();
	void destoryStack();
	void print();

private:
	sqStack *items;

};

#endif // __STACK_H__



#include "Stack.h"
#include <iostream>
#include <cmath>
using namespace std;

Stack::Stack()
{
	this->initStack();
}
Stack::~Stack()
{
	this->destoryStack();
}

void Stack::initStack()
{
	this->items = new sqStack();
	this->items->base = (ElemType *) new ElemType[MAXSIZE];
	if (!this->items->base)
	{
		exit(0);
	}
	this->items->top = this->items->base;
	this->items->stackSize = MAXSIZE;
}
void Stack::push(ElemType e)
{
	*(this->items->top) = e;
	++(this->items->top);
}
ElemType Stack::pop()
{
	if (this->items->base == this->items->top)
	{
		cout<<"the stack is NULL"<<endl;
		exit(0);
	}

	--(this->items->top);
	return *(this->items->top);
}
int Stack::stackLongth()
{
	return (this->items->top - this->items->base);
}
void Stack::clearStack()
{
	this->items->top = this->items->base;
}
void Stack::destoryStack()
{
	for (int i=0;i!=this->stackLongth();++i)
	{
		delete this->items->base;
		++(this->items->base);
	}

	this->items->base = NULL;
	this->items->top = NULL;
	this->items->stackSize = 0;
}
void Stack::print()
{
	cout<<"show items:"<<endl;
	for (ElemType *e=this->items->base;e!=this->items->top;++e)
	{
		cout<<*e<<endl;
	}
}

int main()
{
	Stack *stack = new Stack();

	char ch[11] = "1011011001";
	for (int i=0;i!=10;++i)
	{
		stack->push(ch[i]);
	}
	stack->print();
	stack->push('1');
	stack->print();
	cout<<"temp char is:"<<stack->pop()<<endl;
	stack->print();
	cout<<"stack longth:"<<stack->stackLongth()<<endl;
	char c;
	int sum = 0;
	int lon = stack->stackLongth(); 
	for (int i=0;i<lon;++i)
	{
		c = stack->pop();
		cout<<"item:"<<c<<endl;
		sum += (c - 48)*pow(2.0,i);
	}
	cout<<"decimail :"<<sum<<endl;

	getchar();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
     [问题描述]   停车场一个停放n辆汽车的下狭长通道,且只有一个大门可供汽车进出,汽车停车场内按车辆到达的时间先后排序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在车厂的最北端),若车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路,待该车开出大门外,其他车辆再按照原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。试为停车场编制上述要求的管理模拟程序。 [基本要求]    以模拟停车场,以队列模拟车场外的便道,按照从终端输入的数据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“达到”或“离去”信息、汽车牌照号码以及到达或离去的时刻。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输入汽车停车场内或便道上的停车位置;若是车辆到达,则输出汽车停车场内或便道上的停车位置,若是车辆离去,则输出在汽车停车场内停留的时间和应缴纳的费用(在便道上停留的时间不收费)。以顺序结构实现,队列以链表结构实现。      [测试数据]    n=2,输入数据:    (”A’,1,5),(‘A’,2,10),(‘D’,1,15),(‘A’,3,20),(‘A’,4,25),(‘A’,5,30),(‘D’,2,35),(‘D’,4,40),(‘E’,0,0)    其中:’A’表示到达(Arrival);’D’表示离去(Departure),‘E’表示输入结束(End)。      [实现提示]    需另一个,临时停放为要给离去的汽车让路而从停车场退出来的汽车,也哟个顺序存储结构实现。输入数据按到达或离去的时刻有序。中每个元素表示一辆汽车,包含两个数据项:汽车的排照号码和进入停车场的时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值