栈——顺序存储结构及其基本运算

该文章主要介绍栈的顺序存储结构以及相关运算。

头文件:SqStack.h

template <typename T>
class SqStackClass				//顺序栈类
{
	T *data;					//存放栈中元素
	int top;       				//栈顶指针
public:
	//===============顺序栈的基本运算算法======================
	SqStackClass();				//构造函数
	~SqStackClass();			//析构函数
	bool StackEmpty();			//判断栈是否为空
	bool Push(T e);				//进栈算法
	bool Pop(T &e);				//出栈算法
	bool GetTop(T &e);			//取栈顶元素算法

};

源文件:SqStack.cpp

#include <iostream>
#include "SqStack.h"
using  namespace std;
const int MaxSize = 100;
//===============顺序栈的基本运算算法======================
template <typename T>
SqStackClass<T>::SqStackClass()		//构造函数
{
	data = new T[MaxSize];			
	top = -1;							
}
template <typename T>
SqStackClass<T>::~SqStackClass()	
{
	delete[] data;
}
template <typename T>
bool SqStackClass<T>::StackEmpty()	//判断栈是否为空
{
	return(top == -1);
}
template <typename T>
bool SqStackClass<T>::Push(T e)		//进栈算法
{
	if (top == MaxSize - 1)			
		return false;
	top++;							
	data[top] = e;				
	return true;
}
template <typename T>
bool SqStackClass<T>::Pop(T &e)		//出栈算法
{
	if (StackEmpty())				
		return false;
	e = data[top];				
	top--;							
	return true;
}
template <typename T>
bool SqStackClass<T>::GetTop(T &e)	//取栈顶元素算法
{
	if (StackEmpty())				
		return false;
	e = data[top];				
	return true;
}

头文件:StackOperate.h

#ifndef STACKOPRTATE_H_
#define STACKOPRTATE_H_
#include<iostream>
using namespace std;
//================= 顺序栈的其他运算算法==================

bool isSerial(int str[], int n);		//判断str是否是合适的出栈序列
void Disp(int str[], int n);

bool isMatch(char str[], int n);		//利用顺序栈判断表达式中的括号是否匹配

bool isPalindrome(char str[], int n);	//用顺序栈判断一个字符串是否是回文


#endif

源文件:StackOperate.cpp

#include "SqStack.cpp"
#include"StackOperate.h"
//======================顺序栈的其他运算算法===================
bool isSerial(int str[], int n)
{
	int i, j, e;
	SqStackClass<int> st;			//建立一个顺序栈
	int a[MaxSize];
	for (i = 0; i<n; i++)			//将1~n放入数组a中
		a[i] = i + 1;
	i = 0; j = 0;
	while (i<n &&j<n)
	{
		if (st.StackEmpty() || (st.GetTop(e) && e != str[j]))
		{
			st.Push(a[i]);
			cout << "  元素" << a[i] << "进栈\n";
			i++;
		}
		else
		{
			st.Pop(e);
			cout << "  元素" << e << "出栈\n";
			j++;
		}
	}
	while (!st.StackEmpty() && st.GetTop(e) && e == str[j])
	{
		st.Pop(e);
		cout << "  元素" << e << "出栈\n";
		j++;
	}
	if (j == n) return true;			//是出栈序列时返回true
	else return false;					//不是出栈序列时返回false		
}
void Disp(int str[], int n)
{
	int i;
	for (i = 0; i<n; i++)
		cout << str[i];
}

bool isMatch(char str[], int n)
{
	int i = 0;
	char e;
	SqStackClass<char> st;			//建立一个顺序栈
	while (i<n)
	{
		if (str[i] == '(' || str[i] == '[' || str[i] == '{')
			st.Push(str[i]);		//将左括号进栈
		else
		{
			if (str[i] == ')')
			{
				if (!st.Pop(e))			//栈空返回false
					return false;
				if (e != '(')			//栈顶不是相匹配的左括号返回false
					return false;
			}
			if (str[i] == ']')
			{
				if (!st.Pop(e))			//栈空返回false
					return false;
				if (e != '[')			//栈顶不是相匹配的左括号返回false
					return false;
			}
			if (str[i] == '}')
			{
				if (!st.Pop(e))			//栈空返回false
					return false;
				if (e != '{')			//栈顶不是相匹配的左括号返回false
					return false;
			}
		}
		i++;						
	}
	if (st.StackEmpty())				//栈空返回true
		return true;
	else								//栈不空返回false
		return false;
}
bool isPalindrome(char str[], int n)	//用顺序栈判断一个字符串是否是回文
{
	int i = 0;
	char e;
	SqStackClass<char> st;			
	while (i<n)						
	{
		st.Push(str[i]);
		i++;						
	}
	i = 0;
	while (i<n)						
	{
		st.Pop(e);					
		if (str[i] != e)				
			return false;
		i++;
	}
	return true;					
}

主函数:main.cpp

#include "SqStack.cpp"
#include"StackOperate.h"
using namespace std;
//============== 顺序栈的基本运算算法====================
void main1()
{	
	SqStackClass<char> st;			//定义一个字符顺序栈st
	char e;
	cout << "建立空栈st\n";
	cout << "栈st" << (st.StackEmpty()?"空":"不空") << endl;
	cout << "字符a进栈\n"; st.Push('a');
	cout << "字符b进栈\n"; st.Push('b');
	cout << "字符c进栈\n"; st.Push('c');
	cout << "字符d进栈\n"; st.Push('d');
	cout << "字符e进栈\n"; st.Push('e');
	cout << "栈st" << (st.StackEmpty()?"空":"不空") << endl;
	st.GetTop(e);
	cout << "栈顶元素:" << e << endl;
	cout << "所有元素出栈次序:";
	while (!st.StackEmpty())		//栈不空循环
	{	
		st.Pop(e);				
		cout << e << " ";
	}
	cout << endl;
	cout << "销毁栈st" << endl;
}
//================顺序栈的其他运算算法=======================
void main()
{
	//===
	int n = 4;
	int str[] = { 3, 4, 2, 1 };
	cout << "由1~" << n << "产生"; Disp(str, n);
	cout << "的操作序列:\n";
	if (isSerial(str, n))
	{
		Disp(str, n);
		cout << "是合适的出栈序列\n";
	}
	else
	{
		Disp(str, n);
		cout << "不是合适的出栈序列\n";
	}
	//===
	int n1 = 4;
	char str1[] = "([)]";
	if (isMatch(str1, n1))
		cout << str1 << "中括号是匹配的\n";
	else
		cout << str1 << "中括号不匹配\n";
	//===
	int n2 = 5;
	char str2[] = "abcba";
	if (isPalindrome(str2, n2))
		cout << str2 << "是回文\n";
	else
		cout << str2 << "不是回文\n";
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值