链表栈的简单实现
来源:互联网 发布:什么软件看课表 编辑:程序博客网 时间: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> ©);LinkStack<T> &operator=(const LinkStack<T> ©);};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> ©){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> ©){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;}#endifLinkStack.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
- 链表栈的简单实现
- 杀毒软件的简单实现的简单实现
- 杀毒软件的简单实现
- 俄罗斯方块的简单实现
- 简单文件系统的实现
- 杀毒软件的简单实现
- 杀毒软件的简单实现
- strstr()的简单实现
- printf 的简单实现
- 简单文件系统的实现
- 简单的ajax实现
- 简单的时钟实现
- ICollection的简单实现
- P2P的简单实现
- 简单的JNI实现
- 简单的string实现
- Ajax的简单实现
- 线程的简单实现
- storm启动异常 util [INFO] Halting process: ("Error when processing an event")
- JS Canvas context API 画布
- win7安装theano
- Linux上的free命令详解
- web.xml中<security-constraint>和四种认证类型
- 链表栈的简单实现
- 网络挖掘技术——微博文本特征提取
- Android开发--访问WebSerivice
- SeaJS之use函数
- IDF实验室:牛刀小试--被改错的密码
- 博客被盗,盗号者无耻
- linux非常有用的指令
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- ios多点触摸范例