加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

【数据结构】顺序栈的实现(c++)

发布时间:2020-12-15 06:01:05 所属栏目:安全 来源:网络整理
导读:头文件: #pragma once#include iostream#include assert.husing namespace std;templateclass Typeclass SeqStack{public:SeqStack(size_t sz = INIT_SZ);~SeqStack();public:bool empty()const;bool full()const;void show()const;bool push(const Type x)

头文件:


#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

template<class Type>
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 = 8 };
		Type *base;
		int capacity;
		int top;
};

template<class Type>
SeqStack<Type>::SeqStack(size_t sz = INIT_SZ)
{
	capacity = sz > INIT_SZ ? sz : INIT_SZ;
	base = new Type[capacity];
	assert(base != NULL);
	top = 0;
}

template<class Type>
SeqStack<Type>::~SeqStack()
{
	destory();
}

// 判断栈是否满了
template<class Type>
bool SeqStack<Type>::full()const
{
	return (top >= capacity);
}

// 判断是否为空栈
template<class Type>
bool SeqStack<Type>::empty()const
{
	return (top == 0);
}

// 显示
template<class Type>
void SeqStack<Type>::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<class Type>
bool SeqStack<Type>::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<class Type>
bool SeqStack<Type>::pop()
{
	if (empty())
	{
		cout << "the stack is empty,can not pop!" << endl;
		return false;
	}
	else
	{
		top--;
		return true;
	}
}

// 获得栈顶元素
template<class Type>
void SeqStack<Type>::gettop(Type &x)
{
	x = base[top - 1];
}

// 求栈长度
template<class Type>
int SeqStack<Type>::length()const
{
	return top;
}

// 清空栈
template<class Type>
void SeqStack<Type>::clear()
{
	top = 0;
}

// 摧毁栈
template<class Type>
void SeqStack<Type>::destory()
{
	delete []base;
	base = NULL;
	capacity = top = 0;
}

// 退出系统
template<class Type>
void SeqStack<Type>::quit_system(Type &x)
{
	x = 0;
}



主函数:


#include "SeqStack.h"

int main()
{
	SeqStack<int> mystack;
	int input = 1;
	int value;
	while (input)
	{
		cout << "****************************************************" << endl;
		cout << "*       [1] show                 [2] push          *" << endl;
		cout << "*       [3] pop                  [4] gettop        *" << endl;
		cout << "*       [5] length               [6] clear         *" << endl;
		cout << "*       [7] destory              [8] quit_syntem   *" << endl;
		cout << "****************************************************" << endl;
		cout << "please choose:";
		cin >> input;
		switch (input)
		{
			case 1:
				mystack.show();
				break;
			case 2:
				cout << "please enter the number:";
				while (cin >> value,value != -1)
				{
					mystack.push(value);
				}
				break;
			case 3:
				mystack.pop();
				break;
			case 4:
				mystack.gettop(value);
				cout << value << endl;
				break;
			case 5:
				cout << mystack.length() << endl;
				break;
			case 6:
				mystack.clear();
				break;
			case 7:
				mystack.destory();
				break;
			case 8:
				mystack.quit_system(input);
				break;
			default:
				break;
		}
	}
	return 0;
}








(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读