1.算法描述
a.实现二个栈,在一个数组里面,除非没有任何空间剩余,否则不能有溢出声明
b.实现一个没有头尾结点的栈(单链表)
c.实现带有头结点的栈(单链表)
2.双栈
对于双栈,我们还可以添加resize()方法,当空间满了重新自动分配空间(new),就是将原来的两个栈,拷贝到新建立的数组上面去
a.dsexceptions.h
#ifndef DSEXCEPTIONS_H
#define DSEXCEPTIONS_H
//栈溢出异常
class StackOverFlowException{ //public: exception
public:
const char* what() const throw()
{
return "stack over flow exception, the size<=0 !\n";
}
};
//数组越界异常
class ArrayOverFlowException{
public:
const char* what() const throw()
{
return "array over flow exception, the size over array length !\n";
}
};
//栈索引异常(栈序号只能是1,2)
class ErrorIndexException{
public:
const char* what() const throw()
{
return "the stack index only be 1 and 2 !\n";
}
};
#endif
b.stack2.h
/**
*实现二个栈,在一个数组里面,除非没有任何空间剩余,否则不能有溢出声明
**/
#ifndef STACK2_H
#define STACK2_H
#include <iostream>
#include "dsexceptions.h"
enum{
LEFT = 1, //左栈
RIGHT = 2 //右栈
};
template<class T>
class stack2{
public:
stack2(int len){
array = new T[len];
leftTop = -1;
rightTop = len;
max = len;
}
~stack2(){
delete [] array;
}
//出栈,flag为栈编号(1为栈1,2为栈2)
T pop(int flag){
if(flag == LEFT){
if(leftTop == -1)
throw StackOverFlowException();
return array[leftTop--];
}else if(flag == RIGHT){
if(rightTop == max)
throw StackOverFlowException();
return array[rightTop++];
}else{
throw ErrorIndexException();
}
}
void push(int flag, T x){
if(leftTop == rightTop)
throw ArrayOverFlowException();
if(flag == LEFT){
array[++leftTop] = x;
}else if(flag == RIGHT){
array[--rightTop] = x;
}else{
throw ErrorIndexException();
}
}
T top(int flag) const{
if(flag == LEFT){
if(leftTop == -1)
throw StackOverFlowException();
return array[leftTop];
}else if(flag == RIGHT){
if(rightTop == max)
throw StackOverFlowException();
return array[rightTop];
}else{
throw ErrorIndexException();
}
}
bool empty(int flag) const{
if(flag == LEFT){
if(leftTop == -1)
return true;
}else if(flag == RIGHT){
if(rightTop == max)
return true;
}else{
throw ErrorIndexException();
}
return false;
}
void printStack(int flag) const{
if(flag == LEFT){
std::cout<<"[";
for(int i=leftTop; i>-1; i--){
std::cout<<array[i]<<" ";
}
std::cout<<"]"<<std::endl;
}else if(flag == RIGHT){
std::cout<<"[";
for(int i=rightTop; i<max; i++){
std::cout<<array[i]<<" ";
}
std::cout<<"]"<<std::endl;
}else{
throw ErrorIndexException();
}
}
int size(int flag) const{
if(flag == LEFT){
return leftTop+1;
}else if(flag == RIGHT){
return max-rightTop;
}else{
throw ErrorIndexException();
}
}
private:
int leftTop; //左边数组栈顶
int rightTop; //右边数组栈顶
int max; //数组长度
T* array; //数组
};
#endif
c.main.cpp
#include <iostream>
#include "stack2.h"
using namespace std;
int main(){
try{
stack2<int> s(10);
for(int i=0; i<4; i++){
s.push(1, i);
s.push(2, i);
}
cout<<"stack1:"<<endl;
s.printStack(1);
cout<<s.top(1)<<endl;
cout<<s.pop(1)<<endl;
cout<<s.size(1)<<endl;
cout<<s.empty(1)<<endl;
s.push(1, 9);
s.push(1, 9);
s.push(1, 9);
cout<<s.size(1)<<endl;
s.printStack(1);
cout<<"stack2:"<<endl;
s.printStack(2);
cout<<s.top(2)<<endl;
for(int i=0; i<4; i++)
s.pop(2);
cout<<s.size(2)<<endl;
cout<<s.empty(2)<<endl;
s.printStack(2);
}catch(StackOverFlowException &e){
cout<<e.what();
}catch(ArrayOverFlowException &e){
cout<<e.what();
}catch(ErrorIndexException &e){
cout<<e.what();
}
return 0;
}
3.单链表实现没有头尾结点的栈
stack.h
#ifndef STACK_H
#define STACK_H
#include <iostream>
#include "dsexceptions.h"
template<class T>
struct Node{
Node<T>* next;
T data;
};
template<class T>
class stack{
public:
stack(){
first = new Node<T>;
first->next = NULL;
length = 0;
}
stack(T a[], int n){
first = new Node<T>;
first->next = NULL;
length = n;
first->data = a[0];
for(int i=1; i<n; i++){
Node<T> *t = new Node<T>;
t->data = a[i];
t->next = first;
first = t;
}
}
~stack(){
freeStack();
}
T pop(){
T data;
if(length != 0){
Node<T> *r = first;
data = r->data;
if(length == 1) //当只剩下一个节点的时候不删除,保留方便下次push
first->data = 0;
else{
first = first->next;
delete r;
}
length--;
}else{
throw StackOverFlowException();
}
return data;
}
void push(T x){
if(length == 0) first->data = x;
else{
Node<T> *r = new Node<T>;
r->data = x;
r->next = first;
first = r;
}
length++;
}
T top() const{
if(length == 0)
throw StackOverFlowException();
return first->data;
}
bool empty() const{
if(length == 0) return true;
return false;
}
int size() const{
return length;
}
void printStack() const{
std::cout<<"[";
Node<T> *r = first;
while(r){
if(length == 0) break; //当没有元素的时候,first并没删除!故而会输出data
std::cout<<r->data<<" ";
r = r->next;
}
std::cout<<"]"<<std::endl;
}
private:
Node<T>* first;//栈顶(指向栈顶)
int length;
void freeStack(){
Node<T> *r = first, *t;
while(r){
t = r;
r = r->next;
delete t;
}
}
};
#endif
main.cpp
#include <iostream>
#include "stack.h"
using namespace std;
int main(){
stack<int> k;
k.push(2);
cout<<k.size()<<endl;
k.pop();
k.printStack();
int a[] = {1,2,3,4,5,6};
stack<int> s(a, 6);
s.printStack();
s.push(7);
s.printStack();
cout<<"size:"<<s.size()<<endl;
s.pop();
s.printStack();
try{
for(int i=0; i<6; i++){
s.pop();
}
s.push(7);
s.push(8);
s.printStack();
cout<<"size:"<<s.size()<<endl;
s.pop();
s.pop();
s.pop();
s.printStack();
}catch(StackOverFlowException &e){
cout<<e.what();
}
return 0;
}
4.带有头结点的栈
stack.h
#ifndef STACK_H
#define STACK_H
#include <iostream>
#include "dsexceptions.h"
template<class T>
struct Node{
Node<T>* next;
T data;
};
template<class T>
class stack{
public:
stack(){
first = new Node<T>;
first->next = NULL;
end = first;
length = 0;
}
stack(T a[], int n){
first = new Node<T>;
length = n;
Node<T> *r = first;
for(int i=0; i<n; i++){
Node<T> *t = new Node<T>;
t->data = a[i];
t->next = r;
r = t;
}
end = r;
}
~stack(){
freeStack();
}
T pop(){
T data;
if(length != 0){
Node<T>* r = end;
end = end->next;
data = r->data;
delete r;
length--;
}else{
throw StackOverFlowException();
}
return data;
}
void push(T x){
Node<T> *r = new Node<T>;
r->data = x;
r->next = end;
end = r;
length++;
}
T top() const{
if(length == 0)
throw StackOverFlowException();
return end->data;
}
bool empty() const{
if(length == 0) return true;
return false;
}
int size() const{
return length;
}
void printStack() const{
std::cout<<"[";
Node<T> *r = end;
while(r != first){
std::cout<<r->data<<" ";
r = r->next;
}
std::cout<<"]"<<std::endl;
}
private:
Node<T>* first;//栈底(不存储元素)
Node<T>* end;//栈顶(指向栈顶)
int length;
void freeStack(){
Node<T> *r = end, *t;
while(r != first){
t = r;
r = r->next;
delete t;
}
delete r;
}
};
#endif