数据结构第三章栈的基本操作

本文介绍了栈的基本概念,并详细展示了两种栈的实现方式:顺序栈和链栈。通过具体的C++代码示例,读者可以了解如何在程序中创建、操作及使用这两种栈结构。

定义:栈是限定仅在表尾进行插入和删除操作的线性表,允许插入和删除的一段称为栈顶,另一端称为栈底。

栈的顺序存储结构——顺序栈

头文件:

#ifndef Seqstack_H                                   //避免重复包含Seqstack.h
#define Seqstack_H
const int StackSize=10;


template<class DataType>
class SeqStack
{
public:
SeqStack();
~SeqStack(){};
void Push(DataType x);
DataType Pop();
        DataType GetTop();
int Empty();
private:
DataType data[StackSize];
int top;
};
#endif

源文件1:

#include "Seqstack.h"


template<class DataType>                        //构造函数,栈的初始化
SeqStack<DataType>::SeqStack()
{
top=-1;
}


template<class DataType>                        //进栈
void SeqStack <DataType>::Push(DataType x)
{
    if(top==StackSize-1) throw "上溢";
data[top++]=x;
cout<<"进栈的数为"<<x<<endl;
}


template<class DataType>                        //弹栈
DataType SeqStack <DataType>::Pop()
{
DataType x;
if(top==-1) throw "下溢";
x=data[top];
top--;
cout<<x<<endl;
return 0;
}


template<class DataType>                        //取栈顶元素
DataType SeqStack <DataType>::GetTop()
{
if(top!=-1)
return data[top-1];
else 
cout<<"栈为空"<<endl;
return 0;
}


template<class DataType>                        //判空
int SeqStack <DataType>::Empty()
{
if(top==-1) 
cout<<"栈为空"<<endl;
else if(top==StackSize)
   cout<<"栈为满"<<endl;
else
cout<<"栈非空"<<endl;

return 0;
}

源文件2:

#include<iostream>
#include<iomanip>
using namespace std;
#include "Seqstack.cpp"


void main()
{
int a,b,c,d,e,f,g,h,i,j;
int k=11;
SeqStack<long int> S;                                     //创建模板类的实例
S.Empty();


cout<<"\n请输入要进栈的数十个数为:";
cin>>a>>b>>c>>d>>e>>f>>g>>h>>i>>j;
cout<<"\n"<<endl;
S.Push(a);
S.Push(b);
S.Push(c);
S.Push(d);
S.Push(e);
S.Push(f);
S.Push(g);
    S.Push(h);
S.Push(i);
S.Push(j);
cout<<"\n"<<endl;
S.Empty();


cout<<"\n"<<endl;

cout<<"取出栈顶的数 "<<S.GetTop()<<"\n"<<endl;
    S.Pop();
}



调试结果:





栈的链接存储结构------链栈

头文件:

#ifndef LinkStack_H
#define LinkStack_H

template<class DataType>
struct Node
{
     DataType data;
Node<DataType>* next;
};

template<class DataType>
class LinkStack
{
public:
LinkStack();
// ~LinkStack();
void Push(DataType x);
DataType Pop();
DataType GetTop();
int Empty();
private:
Node<DataType> * top;
};

#endif

源文件1:

#include "LinkStack.h"

template<class DataType>
LinkStack<DataType>::LinkStack()
{
top=NULL;
}


template<class DataType>
void LinkStack<DataType>::Push(DataType x)
{
Node<DataType> *s=NULL;
s=new Node<DataType>;
s->data=x;
s->next=top;
top=s;
    cout<<"进栈的数为:"<<top->data;
}

template<class DataType>
DataType LinkStack<DataType>::Pop()
{
DataType x;
Node<DataType>* p;
if(top==NULL) throw "下溢";
x=top->data;
p=top;
top=top->next;
delete p;
cout<<x<<endl;
return 0;
}

template<class DataType>
DataType LinkStack<DataType>::GetTop()
{
return top->data;
}

template<class DataType>
int LinkStack<DataType>::Empty()
{
if(top==NULL)
cout<<"\n栈为空"<<endl;
else
cout<<"\n栈非空"<<endl;
return 0;
}

源文件2:

#include<iostream>
using namespace std;
#include "LinkStack.cpp"


void main()
{
int a0,a1,a2,a3,a4,a5;
LinkStack<int> S;
S.Empty();
cout<<"\n请输入进栈的数:";
cin>>a0>>a1>>a2>>a3>>a4>>a5;
cout<<"\n"<<endl;
S.Push(a0);
cout<<endl;
S.Push(a1);
    cout<<endl;
S.Push(a2);
cout<<endl;
S.Push(a3);
cout<<endl;
S.Push(a4);
cout<<endl;
S.Push(a5);
cout<<"\n"<<endl;
cout<<"取栈顶的元素:"<<S.GetTop()<<"\n"<<endl;
S.Pop();
S.Empty();
cout<<"\n"<<endl;

}


调试结果:



















评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值