链式栈

#include <iostream>
#include "Node.h"
#include "SimpleLinkStack.h"
using namespace std;

bool Merg(char *s)
{
	char tmpsChar;
	SimpleLinkStack<char> la;
	for(int i=0;i<strlen(s);i++)
	{
		if(s[i]=='('||s[i]=='['||s[i]=='{')
			la.Push(s[i]);
		else if(s[i]==')')
		{
			if(la.Top(tmpsChar),tmpsChar=='(')
				if(la.Empty())
					return false;
				else
					la.Push(tmpsChar);
			else
				return false;
		}else if(s[i]=='}')
		{
			if(la.Top(tmpsChar),tmpsChar=='{')
			{
				if(la.Empty())
					return false;
				else
					la.Push(tmpsChar);
			}
			else
				return false;
		}else if(s[i]==']')
		{
			if(la.Top(tmpsChar),tmpsChar=='[')
			{
				if(la.Empty())
					return false;
				else
					la.Push(tmpsChar);
			}
			else
				return false;
		}
	}
	if(la.Empty())
		return true;
	else
		return false;
}

void main()
{
	//SimpleLinkStack<int> a;
	char s[20];
	cout<<"input characters:";
	cin>>s;
	cout<<Merg(s)<<endl;
}



#ifndef _NODE_H_
#define _NODE_H_
#include <iostream>
using namespace std;

template<class ElemType>
struct Node
{
	ElemType data;
	Node<ElemType> *next;

	Node();
	Node(ElemType elem,Node<ElemType> *link=NULL);

};

template<class ElemType>
Node<ElemType>::Node()
{
	next=NULL;
}
template<class ElemType>
Node<ElemType>::Node(ElemType elem,Node<ElemType> *link)
{
	data=elem;
	next=link;
}

#endif

#ifndef _SIMPLELINKSTACK_H_
#define _SIMPLELINKSTACK_H_
#include "Node.h"
#include <iostream>
using namespace std;

enum StatusCode{UNDER_FLOW,SUCCESS};
template<class ElemType>
class SimpleLinkStack
{
protected:
	Node<ElemType> *top;
	
	void Init();
public:
	SimpleLinkStack();
	virtual ~SimpleLinkStack();

	int Length()const;
	bool Empty()const;
	void Clear();

	StatusCode Top(ElemType &e)const;
	StatusCode Push(const ElemType &e);
	StatusCode Pop(ElemType &e);


};

template<class ElemType>
SimpleLinkStack<ElemType>::SimpleLinkStack()
{
	Init();
}

template<class ElemType>
SimpleLinkStack<ElemType>::~SimpleLinkStack()
{
	Clear();
}

template<class ElemType>
void SimpleLinkStack<ElemType>::Init()
{
	top=NULL;
}
template<class ElemType>
int SimpleLinkStack<ElemType>::Length()const
{
	int count=0;
	for(Node<ElemType> *ptr=top;ptr!=NULL;ptr=ptr->next)
	{
		count++;
	}
	return count;
}

template<class ElemType>
bool SimpleLinkStack<ElemType>::Empty()const
{
	return top==NULL;
}

template<class ElemType>
void SimpleLinkStack<ElemType>::Clear()
{
	ElemType elem;
	while(!Empty())
		Pop(elem);
}

template<class ElemType>
StatusCode SimpleLinkStack<ElemType>::Top(ElemType &e)const
{
	if(Empty())
		return UNDER_FLOW;
	else
	{
		e=top->data;
		return SUCCESS;
	}
}

template<class ElemType>
StatusCode SimpleLinkStack<ElemType>::Push(const ElemType &e)
{
	Node<ElemType> *ptr=new Node<ElemType>(e,top);
	top=ptr;
	return SUCCESS;
}

template<class ElemType>
StatusCode SimpleLinkStack<ElemType>::Pop(ElemType &e)
{
	if(Empty())
		return UNDER_FLOW;
	else
	{
		e=top->data;
		Node<ElemType> *ptr=top;
		top=top->next;
		delete ptr;
		return SUCCESS;
	}
}

#endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值