C++ 顺序栈

主程序 main.cpp

//Written by Xuebi
//本程序用于构建顺序栈
//本程序包含3个程序main.cpp ,My_Stack.h,My_Unity.h。
#include <iostream>
#include "My_Until.h"
#include <stdio.h>
#include "My_Stack.h"
using namespace std;
int main()
{
    Mystack<int> st(4);//创建长度为4的int类型数组
    st.Push(10);//top=0
    st.Push(20);//top=1
    st.Push(30);//top=2
    st.Push(40);//top=3,到达栈顶
    st.Push(50);//先检测到top=3在栈顶,利用ChangeSize函数,扩大数组,然后top=4
    st.Push(60);//top=5
    cout<<st.Top()<<endl;//输出top的数据 输出60
    st.Pop();//top-1,top=4,相当于删除
    cout<<st.Top()<<endl;输出top的数据 输出50
    return 0;
}





头文件 My_Stack.h

#ifndef _MYSTACK_Stack_H
#define _MYSTACK_Stack_H
#include "My_Until.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
template<class T>
class Mystack
{
    private:
        int top;//top is to enregister the top of mystack
        int Size;//Size is to enregister the Size of mystack
        T*Stack;//the pointer to a stack
        int SizeFixed;//每次扩展数组是,扩展长度SizeFixed
    public:
        Mystack(int StackSize=10);//默认构造数组长度为10
        ~Mystack();
        void Pop();//to delete to top in this moment
        void Push(const T&item);//to add a new data
        bool IsEmpty() const;//const此函数不能修改private的变量
        T&Top() const ;
 };
template<class T>//构造函数
Mystack<T>::Mystack(int StackSize):Size(StackSize),SizeFixed(StackSize)//利用构造函数定义private变量
{
    Stack=new T[Size];//动态创建一个数组赋与指针Stack,对应要写上析构函数
    top=-1;//一开始top在-1.当Push一个数据,top为0,对应数组第一个下标
}
template<class T>//析构函数,private定义了Stack的指针
Mystack<T>::~Mystack()
{
    delete []Stack;
}
template<class T>
void Mystack<T>::Pop()//在顺序栈里删除元素
{
    if(IsEmpty())
    {
        cout<<"This stack is empty, cannot pop. This program is over"<<endl;
        exit(1);
    }
    else
    {
        Stack[top].~T();//此行可写可不写,本行针对Stack类型是一个类,如T=dog,则需要析构~T()
        top--;
    }
}
template<class T>
void Mystack<T>::Push(const T&item)
{
    if(top==Size-1)
    {
        ChangeSize(Stack,Size,Size+SizeFixed);//如果top到到达栈顶,调用ChangeSize函数进行扩大Stack数组
        Size=Size+SizeFixed;
    }
    top++;
    Stack[top]=item;
}
template<class T>
T&Mystack<T>::Top()const//记得加const
{
    return Stack[top];//返回top指标的数据类型
}
template<class T>
bool Mystack<T>::IsEmpty()const//记得加const
{
    if(top==-1)
        return 1;
    else
        return 0;
}
#endif // _MYSTACK_Stack_H


头文件 My_Unity.h

#ifndef MY_UNTIL_H
#define MY_UNTIL_H
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
template<class T>
void ChangeSize(T*Stack,int oldsize,int newsize)
{
    if(newsize<1)
    {
        cout<<"The function ChangeSize has an error: newsize<1. This program is over. ";
        exit(1);
    }
    else
    {
        T*a=new T[newsize];
        int Num=min(newsize,oldsize);
        std::copy(Stack,Stack+Num,a);
        delete []Stack;
        Stack=a;
    }
}
#endif // MY_UNTIL_H

本程序只用于学习参考之用,转载请注明出处。
记得点赞哦!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值