链表栈的简单实现

今天实现了链表栈的简单实现,贴出来以后可以看一看。链表栈就是用链表来实现栈的一些操作。

补充下栈的使用,设计一个算法判别用字符串表示的表达式中符号(,),[,],{,}是否配对出现。

LinkStack.h

#ifndef LINKSTACK_H_
#define LINKSTACK_H_
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
template <typename T>
struct Node
{
	T data;
	Node<T> *next;
	Node();
	Node(T item, Node<T> *link=NULL);
};
template <typename T>
Node<T>::Node()
{
	next=NULL;
}
template <typename T>
Node<T>::Node(T item, Node<T> *link)
{
	data=item;
	next=link;
}
template<typename T>
class LinkStack
{
protected:
	Node<T> *top;
	void Init();
	Node<T> *GetElemPtr(int position) const;
public:
	LinkStack();
	virtual ~LinkStack();
    bool IsEmpty();
	int Length() const;
	void Clear();
	void Push(T e);
	T Top();
	T Pop();
	LinkStack(const LinkStack<T> &copy);
	LinkStack<T> &operator=(const LinkStack<T> &copy);
};
template <typename T>
void LinkStack<T>::Init()
{
	top=new Node<T>;
}
template <typename T>
Node<T> *LinkStack<T>::GetElemPtr(int position) const
{
	int Len=Length();
	Node<T> *tempPtr=top;
	int curPosition=0;
	while(tempPtr->next!=NULL && curPosition != position)
	{
		tempPtr=tempPtr->next;
		++curPosition;
	}
	if(/*tempPtr->next!=NULL && */curPosition == position)
	{
		return tempPtr;
	}
	else 
		return NULL;
}
template <typename T>
LinkStack<T>::LinkStack()
{
	Init();
}
template<typename T>
LinkStack<T>:: ~LinkStack()
{
	Clear();
	delete top;
}
template <typename T>
bool LinkStack<T>::IsEmpty()
{
	return top->next==NULL;
}
template <typename T>
int LinkStack<T>::Length() const
{
	Node<T> *tempPtr=top;
	int count=0;
	while(tempPtr->next!=NULL)
	{
		++count;
		tempPtr=tempPtr->next;
	}
	return count;
}
template <typename T>
void LinkStack<T>::Clear()
{
	Node<T> *tempPtr=top;
	int Len=Length();
	while(tempPtr->next!=NULL)
	{
		Node<T> *newPtr=GetElemPtr(Len-1);
		delete newPtr->next;
		newPtr->next=NULL;
		

	}
}
template <typename T>
void LinkStack<T>::Push(T e)
{
	int Len=Length();
	Node<T> *tempPtr;
	tempPtr=GetElemPtr(Len);
	Node<T> *newPtr=new Node<T>;
	tempPtr->next=newPtr;
	newPtr->data=e;
}
template <typename T>
T LinkStack<T>::Top()
{
	int Len=Length();
	Node<T> *tempPtr=GetElemPtr(Len);
	return tempPtr->data;
}
template<typename T>
T LinkStack<T>::Pop()
{
	int Len=Length();
	Node<T> *tempPtr=GetElemPtr(Len-1);
	Node<T> *newPtr=tempPtr->next;
	tempPtr->next=NULL;
	return newPtr->data;
}
template <typename T>
LinkStack<T>::LinkStack(const LinkStack<T> &copy)
{
	Init();
	/*Node<T> *tempPtr=copy.top;*/
	Node<T> *newPtr=top;
	Node<T> *nowPtr;
	int Len=copy.Length();
	int count=0;
	while(Len--)
	{
		++count;
		nowPtr=copy.GetElemPtr(count);
		Node<T>* tempPtr=new Node<T>;
		newPtr->next=tempPtr;
		tempPtr->data=nowPtr->data;
		newPtr=tempPtr;
	}
}
template <typename T>
LinkStack<T> &LinkStack<T>::operator=(const LinkStack<T> &copy)
{
	if(copy!=this)
	{
		Init();
		/*Node<T> *tempPtr=copy.top;*/
		Node<T> *newPtr=top;
		Node<T> *nowPtr;
		int Len=copy.Length();
		int count=0;
		while(Len--)
		{
			++count;
			nowPtr=copy.GetElemPtr(count);
			Node<T>* tempPtr=new Node<T>;
			newPtr->next=tempPtr;
			tempPtr->data=nowPtr->data;
			newPtr=tempPtr;
		}
	}
	return *this;
}
template <typename T>
ostream &operator <<(ostream &os, LinkStack<T> &Ls)
{
	cout<<"Last in first out: ";
	while(!Ls.IsEmpty())
	{
		cout<<Ls.Pop()<<" ";
	}
	cout<<endl;
	return os;
}

#endif
LinkStack.cpp

// LinkStack.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "LinkStack.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	LinkStack<int> Stack;
	for(int i=0; i<100; i++)
	{
		Stack.Push(i);
	}
	cout<<"链表的头结点为:"<<Stack.Top()<<endl;
	LinkStack<int> copy=Stack;
	LinkStack<int> copy1(Stack);
	cout<<"链表栈为:"<<Stack;
	cout<<"复制链表栈为:"<<copy;
	cout<<"复制链表栈为:"<<copy1;
	system("pause");
	return 0;
}

结果就不贴了。

补充实验程序:头文件还是LinkStack.h 源文件为Main.cpp

#include "stdafx.h"
#include "LinkStack.h"
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	LinkStack<char> Stack;
	string str;
	cout<<"请输入式子:";
	cin>>str;
	int Len=str.size();
	char c,e;
	for(int i=0; i<Len; i++)
	{
		c=str.at(i);
		if(c=='(' || c=='[' || c=='{')
		{
			Stack.Push(c);
		}
		else if(c==')' || c==']' || c=='}')
		{
			if(!Stack.IsEmpty())
			{
				e=Stack.Pop();
				if(c==')' && e=='(' || c==']' && e=='[' || c=='}' && e=='{')
				{
					cout<<c<<"和"<<e<<"匹配"<<endl;
				}
				else
				{
					cout<<c<<"和"<<e<<"不匹配"<<endl;
				}
			}
			else
			{
				cout<<"左括号数量不匹配"<<endl;
			}
			
		}
	}
	if(!Stack.IsEmpty())
	{
		cout<<"右括号数量不匹配"<<endl;
	}
	//cout<<str<<endl;
	system("pause");
	return 0;
}
结果为:




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值