【数据结构】栈的实现——顺序栈和链栈

67 阅读2分钟

top=-1;

data=new DataType[MAX_SIZE];

}

//有参构造 栈的大小给定

template Stack::Stack(int s){

size=s;

top=-1;

data=new DataType[size];

}

template bool Stack::isFull(){

if(top+1==size){

return true;

}else{

return false;

}

}

template bool Stack::isEmpty(){

if(top==-1){

return true;

}else{

return false;

}

}

//压栈 先判断栈是否已满 然后将top++

template void Stack::push(DataType e){

if(isFull()){

throw Stack::Full();

}else{

data[++top]=e;

}

}

//弹出 先判断栈是否为空 然后top--

template DataType Stack::pop(){

if(isEmpty()){

throw Stack::Empty();

}else{

return data[top--];

}

}

//得到栈顶元素 不弹出

template DataType Stack::getTop(){

if(isEmpty()){

throw Stack::Empty();

}else{

return data[top];

}

}

//栈的长度

template int Stack::length(){

return top+1;

}

//将栈清空

template void Stack::setNull(){

top=-1;

}

template class Stack;

template class Stack;

//主函数

#include

#include "Stack.h"

#include "Sta.cpp"

using namespace std;

//顺序栈

int main(){

CharStack charStack;

IntStack intStack(3);

try{

intStack.push(2);

intStack.push(60);

intStack.push(100);

cout<<"栈的长度:"<<intStack.length()<<endl;

}catch(IntStack::Full){

cout<<"STACK FULL!"<<endl;

}

try{

intStack.pop();

}catch(IntStack::Empty){

cout<<"STACK EMPTY!"<<endl;

}

cout<<"栈的长度:"<<intStack.length()<<endl;

return 0;

}

三、链栈


基于链表实现(不带头节点)

在这里插入图片描述

#ifndef LINKSTACK_H_INCLUDED

#define LINKSTACK_H_INCLUDED

template

struct Node{

DataType data; //数据域

Node *next; //指针域

};

template

class LinkStack{

private:

Node *top; //栈顶指针

public:

LinkStack();

~LinkStack();

void push(DataType x);

DataType pop();

DataType getTop();

bool isEmpty();

bool isFull();

};

typedef LinkStack CharLinkStack;

typedef LinkStack IntLinkStack;

#endif // LINKSTACK_H_INCLUDED

#include "LinkStack.h"

//构造函数 创建top指针

template LinkStack::LinkStack(){

top=NULL;

}

//析构函数 释放资源

template LinkStack::~LinkStack(){

Node *p=NULL;

while(top!=NULL){

p=top->next;

delete top;

top=p;

}

}

//判断链栈是否为空

template bool LinkStack::isEmpty(){

if(top==NULL){

return true;

}else{

return false;

}

}

/*template bool LinkeStack::isFull(){

}*/

//压入元素

template void LinkStack::push(DataType x){

Node *s=new Node; //新定义一个Node结点指针

s->data=x;

s->next=top; //将结点的指针域指向栈顶top(即压入了一个新的元素)

top=s; //再让top指向结点

}

//弹出栈顶元素

template DataType LinkStack::pop(){

if(isEmpty()){

throw "链栈为空,无法弹出元素";

}else{

DataType x=top->data;

Node *p=new Node;

p=top; //将栈顶指针先赋给p

top=top->next; //栈顶指针指向栈顶的下一个存储空间(即栈顶元素弹出)

delete p; //销毁p的空间

return x;

}

}

//返回栈顶元素

template DataType LinkStack::getTop(){

return top->data;

}

template class LinkStack;

template class LinkStack;

#include

#include "LinkStack.cpp"

using namespace std;

//链栈

int main()

{

IntLinkStack intLinkStack;

intLinkStack.push(20);

intLinkStack.push(15);

cout<<"栈顶元素:"<<intLinkStack.getTop()<<endl;

intLinkStack.pop();

cout<<"栈顶元素:"<<intLinkStack.getTop()<<endl;

return 0;

}

注意:

  • C++分文件编写时需要在   .h中尾部加入代码,相当于为每个类型的模板定义了一个类型

typedef Stack CharStack;

  • .cpp文件中尾部加入代码,显示的声明要使用的模板类实例

template class Stack;

  • main 函数中实例化类对象

CharStack charStack;

  • 利用模板实现,每个类的方法前都要加上

template