日常说明:首先博主也是菜鸟一枚,有错误欢迎大家指正。另外本博客所有的代码博主编写后均调试
通过。重要提醒!!!!博主使用的是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;
}