鏈式棧

#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


發佈了445 篇原創文章 · 獲贊 27 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章