【源代码】C++算法(三)堆栈的基本操作(出栈,入栈,销毁,数组初始化和空栈初始化)

日常说明:首先博主也是菜鸟一枚,有错误欢迎大家指正。另外本博客所有的代码博主编写后均调试
通过。重要提醒!!!!博主使用的是VS2017,如果有低版本的小伙伴
最好新建空项目将此代码复制上去。

运行结果:
这里写图片描述
这里写图片描述
stack.h

#pragma once
/**********************************
* algorithms.h :堆栈的基本操作 *
* author : shilei                 *
* created : 2018.3.22             *
***********************************/
#include<iostream>
#include<cstdlib>
#include"malloc.h"
#include"corecrt_malloc.h"
#include<exception>
using namespace std;
#define STACK_INIT_SIZE 20
#define STACKINCREAMENT 10
class Stack
{
private :
    int *base;
    int *top;
    int StackSize;//堆栈的容量
    int Stack_len;//当前堆栈的数据元素个数
public :
    Stack();
    /*~Stack();*/
    Stack(int values[], int n);
    void InitStack(int values[], int n);
    void Push(int e);
    int Pop();
    void Clear_Stack();
    bool Empty();
    int GetTop();
    int Getlength();
    friend ostream& operator<<(ostream&out, Stack&list);

};
Stack::Stack()//构造空表
{
    this->base = (int*)malloc(STACK_INIT_SIZE * sizeof(int));
    this->StackSize = STACK_INIT_SIZE;
    this->top = this->base;
    int Stack_len = this->top - this->base;
}
//Stack::~Stack()
//{
//  Clear_Stack();
//  delete[]base;
//}
 Stack::Stack(int values[],int n)
{
    this->InitStack(values, n);
}
void Stack::InitStack(int values[],int n)
{
    this->Stack_len = n;
    this->StackSize = n * 2;
    this->base = new int[this->StackSize];
    for (int i = 0; i < Stack_len; i++)
    {
        base[i] = values[i];
    }
    this->top = this->base + this->Getlength();
}
bool Stack::Empty()
{
    return this->base == this->top;
}
void Stack::Push(int e)
{
    if (this->Stack_len >= this->StackSize)//超容扩容
    {
        this->base = (int*)realloc(this->base, (this->StackSize + STACKINCREAMENT) * sizeof(int));
        this->top = this->base + this->StackSize;
        this->StackSize += STACKINCREAMENT;
    }
    *this->top++ = e;
    Stack_len++;
}
int Stack::Pop()
{
    if (!Empty())
    {
        int e = *--this->top;
        Stack_len--;
        return e;
    }
    throw out_of_range("此栈为空不能进行出栈操作");
}
int Stack::GetTop()
{
    if (!Empty())
    {
        int e = *(this->top - 1);
        int t = base[Stack_len - 1];
        /*cout<<this->Stack_len<<endl;*/
        /*cout << e<<endl;*/
        /*cout << t << endl;*/
        return e;
    }

}

void Stack::Clear_Stack()
{
    this->top = this->base;
}
int Stack::Getlength()
{
    return this->Stack_len;
}
ostream& operator<<(ostream&out, Stack&list)
{
    for (int i = 0; i < list.Getlength(); i++)
    {
        if(list.Getlength() == 1)
        {
            out << list.GetTop();
        }
        out << list.GetTop() << ",";    
        list.top--;
    }
    return out;
}

stack.cpp

/**********************************
* algorithms.cpp :堆栈的基本操作 *
* author : shilei                 *
* created : 2018.3.22             *
***********************************/
#include "stack.h"

void arr_init_stack()
{
    int n;
    cout << "请输入堆栈的数据元素个数:";
    cout << endl;
    cin >> n;
    cout << endl;
    int *A = new int[n];
    cout << "请输入上述个数的元素:";
    for (int i = 0; i < n; i++)
    {
        cin >> A[i];
    }
    Stack stack(A,n);
    cout << endl;
    cout<<"确认输出元素的个数:"<<stack.Getlength();
    cout << endl;
    cout << endl;
    cout<<"确认栈顶数据元素:"<<stack.GetTop();
    cout << endl;
    cout << endl;
    stack.Push(3);
    stack.Push(10);
    cout << "将数据3、10入栈后确认输出其元素个数:" << stack.Getlength();
    cout << endl;
    cout << endl;
    cout << "输出栈的所有数据元素,后进先出:"<<stack<<endl;
}






int main()
{
    Stack list;
    cout << "*******构造空栈后入栈出栈操作*********";
    cout << endl;
    cout << endl;
    list.Push(1);
    cout<<"入栈数据1后输出栈顶元素:"<<list.GetTop();
    cout << endl;
    cout << endl;
    list.Push(3);
    list.Push(4);
    list.Push(5);
    list.Pop();
    cout<<"入栈数据3,4,5,一次出栈后输出栈顶元素:"<<list.GetTop();
    cout << endl;
    cout << endl;
    list.Push(6);
    cout <<"入栈数据“6”后,输出栈的元素个数:"<< list.Getlength() ;
    cout << endl;
    cout << endl;
    cout << endl;
    cout <<"***********数组初始化的堆栈入栈出栈操作***************"<<endl;
    cout << endl;
    cout << endl;
    arr_init_stack();
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值