顺序栈

来源:互联网 发布:2015全球云计算大会 编辑:程序博客网 时间:2024/06/05 07:30
一、实验目的
1、 熟练掌栈的结构特点,掌握栈的顺序存储和链式存储结构和实现。
2、 学会使用栈解决实际问题。
二、实验内容
1、 自己确定结点的具体数据类型和问题规模:
分别建立一个顺序栈,实现栈的压栈和出栈操作。
三、实验过程
// 顺序栈.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>using namespace std;const int StackSize=100;template <typename DT>class SeqStack{public:    SeqStack(){top=-1;}    void Push(DT x);    DT Pop();    DT GetTop();    int Empty();private:DT data[StackSize];int top;};template <typename DT>void SeqStack<DT>::Push(DT x){if(top==StackSize-1)cout<<"上溢"; data[++top]=x;}template <typename DT>DT SeqStack<DT>::Pop(){if(top==-1)cout<<"下溢"; DT x=data[top--]; return x;}template <typename DT>DT SeqStack<DT>::GetTop(){   if(top==-1) cout<<"栈空"; else cout<<data[top]; return 0; }template <typename DT>int SeqStack<DT>::Empty(){cout<<"此时栈是否为空?"<<endl;if(top==-1)return 1;else return 0; }int main(){   SeqStack<int> s;/*<int>易漏掉*/    int a[5]={85,79,96,74,92};for (int i=0;i<5;i++){s.Push(a[i]);}cout<<"输出五位学生的成绩:"<<endl;for (int j=0;j<5;j++){cout<<s.Pop()<<" ";}cout<<endl;cout<<s.Empty()<<endl;s.GetTop();return 0;}




原创粉丝点击