顺序栈的实现
1.MyStack.h
#ifndef MYSTACK_H_
#define MYSTACK_H_
#include<iostream>
template<class ElementType>
class LinearStack
{
private:
ElementType* data;
int length;
int MaxSiaze;
public:
inline ElementType& GetElement(int pos)
{
return this->data[pos];
}
LinearStack(int size);
~LinearStack();
bool isEmpty();
bool isFull();
int GetLength();
bool Push(ElementType& e);
bool Pop(ElementType& e);
bool GetTop(ElementType& e);
void Output(std::ostream& os) const;
friend std::ostream& operator<<(std::ostream& os, LinearStack<ElementType>& stack)
{
stack.Output(os);
return os;
}
};
#endif
2.MyStack.cpp
#include"MyStack.h"
template<class ElementType>
LinearStack<ElementType>::LinearStack(int size)
{
this->data = new ElementType[size];
this->MaxSiaze = size;
this->length = 0;
}
template<class ElementType>
LinearStack<ElementType>::~LinearStack()
{
delete[] this->data;
this->length = 0;
this->MaxSiaze = 0;
this->data = nullptr;
}
template<class ElementType>
bool LinearStack<ElementType>::isEmpty()
{
return this->length == 0;
}
template<class ElementType>
bool LinearStack<ElementType>::isFull()
{
return this->MaxSiaze == this->length;
}
template<class ElementType>
int LinearStack<ElementType>::GetLength()
{
return this->length;
}
template<class ElementType>
bool LinearStack<ElementType>::Push(ElementType& e)
{
if (this->isFull())
{
std::cout << "栈已满!" << std::endl;
return false;
}
this->data[length++] = e;
return true;
}
template<class ElementType>
bool LinearStack<ElementType>::Pop(ElementType& e)
{
if (this->isEmpty())
{
std::cout << "栈空!" << std::endl;
return false;
}
e = this->data[length-1];
this->length--;
return true;
}
template<class ElementType>
bool LinearStack<ElementType>::GetTop(ElementType& e)
{
if (this->isEmpty())
{
std::cout << "栈空!" << std::endl;
return false;
}
e = this->data[length];
return true;
}
template<class ElementType>
void LinearStack<ElementType>::Output(std::ostream& os) const
{
for (int i = 0; i < length; i++)
os << this->data[i] << "\t";
}
2.链式栈的实现
1.LinkNode.h
#pragma once
template<class T>
class LinkStack;
template<class ElementType>
class LinkNode
{
private:
ElementType data;
LinkNode<ElementType>* next;
public:
template<class T>
friend class LinkStack;
LinkNode()
{
this->next = nullptr;
}
ElementType& GetData()
{
return this->data;
}
LinkNode<ElementType>* GetNext()
{
return this->next;
}
};
2.LinkStack.h
#pragma once
#include<iostream>
#include"LinkList.h"
template<class ElementType>
class LinkStack
{
private:
LinkNode<ElementType>* top;
int size;
public:
LinkStack();
~LinkStack();
bool isEmpty() const;
bool Push(ElementType& e);
bool Pop(ElementType& e);
bool GetTop(ElementType& e);
void OutPut(std::ostream& os) const;
friend std::ostream& operator<<(std::ostream& os, LinkStack<ElementType>& stack)
{
stack.OutPut(os);
return os;
}
};
3.LinkStack.cpp
#include "LinkStack.h"
template<class ElementType>
inline LinkStack<ElementType>::LinkStack()
{
this->size = 0;
}
template<class ElementType>
LinkStack<ElementType>::~LinkStack()
{
ElementType e;
while (top != nullptr)
this->Pop(e);
}
template<class ElementType>
bool LinkStack<ElementType>::isEmpty() const
{
return this->size == 0;
}
template<class ElementType>
bool LinkStack<ElementType>::Push(ElementType& e)
{
LinkNode<ElementType>* new_node = new LinkNode<ElementType>;
if (new_node == nullptr)
return false;
new_node->data = e;
new_node->next = this->top;
this->top = new_node;
this->size++;
return true;
}
template<class ElementType>
bool LinkStack<ElementType>::Pop(ElementType& e)
{
if (this->top == nullptr)
return false;
LinkNode<ElementType>* temp = new LinkNode<ElementType>;
if (temp == nullptr)
return false;
e = this->top->data;
temp = this->top;
this->top = this->top->next;
this->size--;
delete temp;
return true;
}
template<class ElementType>
bool LinkStack<ElementType>::GetTop(ElementType& e)
{
if (this->isEmpty())
return false;
e = this->top->data;
return true;
}
template<class ElementType>
void LinkStack<ElementType>::OutPut(std::ostream& os) const
{
LinkNode<ElementType>* p = this->top;
for (int i = 0; i < this->size; i++)
{
os << p->data << "\t";
p = p->next;
}
}