c++线性栈及链栈

  • 黑书作者的异常头文件
    myexception.h

// exception classes for various error types

#ifndef myExceptions_
#define myExceptions_
#include <string>
#include<iostream>
using namespace std;

// illegal parameter value
class illegalParameterValue
{
public:
	illegalParameterValue(string theMessage = "Illegal parameter value")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};

// illegal input data
class illegalInputData
{
public:
	illegalInputData(string theMessage = "Illegal data input")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};

// illegal index
class illegalIndex
{
public:
	illegalIndex(string theMessage = "Illegal index")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};

// matrix index out of bounds
class matrixIndexOutOfBounds
{
public:
	matrixIndexOutOfBounds
	(string theMessage = "Matrix index out of bounds")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};

// matrix size mismatch
class matrixSizeMismatch
{
public:
	matrixSizeMismatch(string theMessage =
		"The size of the two matrics doesn't match")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};

// stack is empty
class stackEmpty
{
public:
	stackEmpty(string theMessage =
		"Invalid operation on empty stack")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};

// queue is empty
class queueEmpty
{
public:
	queueEmpty(string theMessage =
		"Invalid operation on empty queue")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};

// hash table is full
class hashTableFull
{
public:
	hashTableFull(string theMessage =
		"The hash table is full")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};

// edge weight undefined
class undefinedEdgeWeight
{
public:
	undefinedEdgeWeight(string theMessage =
		"No edge weights defined")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};

// method undefined
class undefinedMethod
{
public:
	undefinedMethod(string theMessage =
		"This method is undefined")
	{
		message = theMessage;
	}
	void outputMessage() { cout << message << endl; }
private:
	string message;
};
#endif

  • 虚基类以及派生类
    stack.h
#pragma once
#include<iostream>
#include "myexception.h"
#include <sstream>
using std::ostringstream;
template<class T>
class stack
{
public:
	virtual ~stack(){}
	virtual bool empty()const = 0;
	virtual int size()const = 0;
	virtual T& top() = 0;
	virtual void pop() = 0;
	virtual void push(const T &) = 0;
};
template<class T>
class arraystack : public stack<T>
{
public:
	arraystack(int initialcapacity = 10);
	~arraystack() { delete[] stack; }
	bool empty()const { return stacktop == -1; }
	int size()const { return stacktop + 1; }
	T& top()
	{
		if (stacktop == -1)
			throw stackEmpty();
		return stack[stacktop];
	}
	void pop()
	{
		if (stacktop == -1)
			throw stackEmpty();
		stack[stacktop--].~T();
	}
	void push(const T& theelement);
private:
	int stacktop;
	int arraylength;
	T * stack;
};
template<class T>
arraystack<T>::arraystack(int initialcapacity)
{
	if (initialcapacity < 1)
	{
		ostringstream s;
		s << "Initial capacity" << initialcapacity << "must be > 0";
		throw illegalParameterValue(s.str());
	}
	arraylength = initialcapacity;
	stack = new T[arraylength];
	stacktop = -1;
}
template<class T>
void arraystack<T>::push(const T&theelement)
{
	stack[++stacktop] = theelement;
}
template<class T>
struct chainnode
{
	T element;
	chainnode<T> * next;
	chainnode() {}
	chainnode(const T &theelement)
	{
		this->element = theelement;
	}
	chainnode(const T &theelement, chainnode<T> * next)
	{
		this->element = theelement;
		this->next = next;
	}

};
template<class T>
class linkedstack : public stack<T>
{
private:
	int stacksize;
	chainnode<T> * stacktop;
public:
	linkedstack()
	{
		stacksize = 0;
		stacktop = NULL;
	}
	~linkedstack();
	bool empty() const { return stacksize == 0; }
	int size()const { return stacksize; }
	T& top()
	{
		if (stacksize == 0)
			throw stackEmpty();
		return stacktop->element;
	}
	void pop();
	void push(const T& theelement)
	{
		stacktop = new chainnode<T>(theelement, stacktop);
		stacksize++;
	}
};
template<class T>
linkedstack<T>::~linkedstack()
{
	while (stacktop != NULL)
	{
		chainnode<T> * nextnode = stacktop->next;
		delete stacktop;
		stacktop = nextnode;
	}
}
template<class T>
void linkedstack<T>:: pop()
{
	if (stacksize == 0)
		throw stackEmpty();
	chainnode<T>* nextnode = stacktop->next;
	delete stacktop;
	stacktop = nextnode;
	stacksize--;
}
  • 测试文件,没写全…
#include <iostream>
#include"mystack.h"
int main()
{
    std::cout << "Hello World!\n";
	arraystack<int> j (5) ;
	j.push(2);
	j.top();
	linkedstack<int>a;
	a.push(6);
	a.pop();
	std::cout<<a.top();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值