链表栈的简单实现

来源:互联网 发布:什么软件看课表 编辑:程序博客网 时间:2024/06/17 22:08

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

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

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;}
结果为:




0 0
原创粉丝点击