数据结构实验五

STL的栈stack类

程序的功能:STL的栈stack类 
(1)实现STL的栈stacke类。
(2)栈stacke类的简单应用。

输入的形式和输入值的范围:对于不同的功能函数输入形式不一样,可以按功能分为整型数字,与字符串等数据类型,本程序选取栈的进制转换作为应用,故为整型数据。

输出的形式:输出入栈过程及转化后的结果

测试数据:符合要求的十进制整数

/* 模拟栈的基本实现*/
#include<iostream>
#include<cstdlib>
#include<string.h>
using namespace std;
template <class type>
class STACK {
private:
    int top;
    int max;
    type* stack;
public:
    STACK();
    STACK(int n);
    ~STACK();
    bool fill();
    type pop();//弹出栈顶元素
    type front();//访问栈顶元素
    void insert(type num);//插入元素
    int size();//元素个数
    void clear();//清理栈
    bool empty();//判断空栈
};
template<class type>
STACK<type>::STACK(int n)
{
    max = n;
    stack = new type[n];
    top = -1;
    memset(stack, 0, max * sizeof(type));
}
template<class type>
STACK<type>::STACK() {
    max = 512;
    stack = new type[512];
    top = -1;
    memset(stack, 0, 512 * sizeof(type));
}
template<class type>
STACK<type>::~STACK() {
    if (stack == NULL) { cout << "已为空栈\n"; return; }
    delete[] stack; cout << "栈已清除\n"; stack = NULL;
}
template<class type>
type STACK<type>::pop() {
    if (stack == NULL) { cout << "栈已清除\n"; return 0; }
    if (top == -1) { cout << "栈为空返回一个零"; return 0; }
    top--;
    return stack[top + 1];
}
template<class type>
type STACK<type>::front() {
    if (stack == NULL) { cout << "栈已清除\n"; return 0; }
    if (top == -1) { cout << "栈为空返回一个零"; return 0; }
    return stack[top];
}
template<class type>
void STACK<type>::insert(type num) {
    if (stack == NULL) { cout << "栈已清除\n"; return; }
    if (top == max - 1) { cout << "栈已满" << endl;; return; }
    cout << "数字" << num << "已成功入栈" << endl;
    stack[++top] = num;
}
template<class type>
int STACK<type>::size() {
    if (stack == NULL) { cout << "栈已清除\n"; return 0; }
    return top + 1;
}//栈的元素个数
template<class type>
bool STACK<type>::empty() {
    if (top == -1)return true;
    return false;
}
template<class type>
bool STACK<type>::fill() {
    if (max == top - 1)return true;
    return false;
}
template<class type>
void STACK<type>::clear()
{
    delete[]stack;
    cout << "栈已清除\n";
    stack = NULL;
    top = -1;
}
void conversion()
{
    STACK<int>st(20);
    int N, n;
    cout << "请输入您想转换的数字(十进制转化为八进制)" << endl;
    cin >> N;
    n = N;
    while (N)
    {
        st.insert(N % 8);
        N = N / 8;
    }
    cout << "您输入的十进制数" << n << "转化为八进制数为" << endl;
    while (!st.empty())
    {
        cout << st.pop();
    }
    if (st.empty())
        cout << endl;
    system("pause");
}
int main()
{
    while (1) {
        int w; system("cls");
        cout << "1.数制转换" << endl;
        cout << "2.退出程序" << endl;
        cout << "输入您的选择:" << endl;
        cin >> w;
        switch (w)//若输入的W为1或2,则继续程序
        {
        case 1: {system("cls"); conversion(); break; }
        case 2: {return 0; }
        default:break;
        }
    }return 0;}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值