行编辑程序 栈 c语言,【数据结构】栈的应用-行编辑程序(c++)

【数据结构】栈的应用--行编辑程序(c++)

头文件:

#pragma once

#include

#include

using namespace std;

template

class SeqStack

{

public:

SeqStack(size_t sz = INIT_SZ);

~SeqStack();

public:

bool empty()const;

bool full()const;

void show()const;

bool push(const Type &x);

bool pop();

void gettop(Type &x);

int length()const;

void clear();

void destory();

void quit_system(Type &x);

private:

enum{ INIT_SZ = 64 };

Type *base;

int capacity;

int top;

};

template

SeqStack::SeqStack(size_t sz = INIT_SZ)

{

capacity = sz > INIT_SZ ? sz : INIT_SZ;

base = new Type[capacity];

assert(base != NULL);

top = 0;

}

template

SeqStack::~SeqStack()

{

destory();

}

// 判断栈是否满了

template

bool SeqStack::full()const

{

return (top >= capacity);

}

// 判断是否为空栈

template

bool SeqStack::empty()const

{

return (top == 0);

}

// 显示

template

void SeqStack::show()const

{

if (top == 0)

{

cout << "the stack is empty!" << endl;

return;

}

for (int i = top - 1; i >= 0; --i)

{

cout << base[i] << endl;

}

}

// 入栈

template

bool SeqStack::push(const Type &x)

{

if (full())

{

cout << "the stack is full,can not enter!" << endl;

return false;

}

else

{

base[top] = x;

top++;

return true;

}

}

// 出栈

template

bool SeqStack::pop()

{

if (empty())

{

cout << "the stack is empty,can not pop!" << endl;

return false;

}

else

{

top--;

return true;

}

}

// 获得栈顶元素

template

void SeqStack::gettop(Type &x)

{

x = base[top - 1];

}

// 求栈长度

template

int SeqStack::length()const

{

return top;

}

// 清空栈

template

void SeqStack::clear()

{

top = 0;

}

// 摧毁栈

template

void SeqStack::destory()

{

delete[]base;

base = NULL;

capacity = top = 0;

}

// 退出系统

template

void SeqStack::quit_system(Type &x)

{

x = 0;

}

主函数:

#include "Edlin.h"

int main()

{

SeqStack mystack;

char ch;

cout << "please enter:" << endl;

cout << "* @:delete all $:delete one *" << endl;

while ((ch = getchar()) != EOF)

{

switch (ch)

{

case '@':

mystack.clear();

cout << "please enter:" << endl;

break;

case '$':

mystack.pop();

break;

default:

mystack.push(ch);

break;

}

}

mystack.show();

return 0;

}

174155633.png

174155634.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值