链表实现的Stack类

来源:互联网 发布:域名购买后不使用 编辑:程序博客网 时间:2024/05/29 18:55
//---------------LStack.h-------------------#include<iostream>using namespace std;#ifndef LSTACK#define LSTACKtypedef int StackElement;class Stack{public:Stack();Stack(const Stack & original);~Stack();const Stack & operator=(const Stack & rightHandSide);bool empty() const;void push(const StackElement & value);void display(ostream & out) const;StackElement top() const;void pop();private:class Node{public:StackElement data;Node *next;Node(StackElement value,Node *link=0):data(value),next(link)     {}};typedef Node *NodePointer;NodePointer myTop;};#endif

//---------------LStack.cpp-------------------#include<new>using namespace std;#include"LStack.h"Stack::Stack():myTop(0)    {}Stack::Stack(const Stack & original){myTop=0;if(!original.empty()){myTop=new Stack::Node(original.top());Stack::NodePointer lastPtr=myTop,               origPtr=original.myTop->next;while(origPtr!=0){lastPtr->next=new Stack::Node(origPtr->data);lastPtr=lastPtr->next;origPtr=origPtr->next;}}}Stack::~Stack(){Stack::NodePointer currPtr=myTop,                   nextPtr;while(currPtr!=0){nextPtr=currPtr->next;delete currPtr;currPtr=nextPtr;    }}const Stack & Stack::operator=(const Stack & rightHandSide){if(this!=&rightHandSide){this->~Stack();if(rightHandSide.empty())myTop=0;else{myTop=new Stack::Node(rightHandSide.top());Stack::NodePointer lastPtr=myTop,               rhsPtr=rightHandSide.myTop->next;while(rhsPtr!=0){lastPtr->next=new Stack::Node(rhsPtr->data);lastPtr=lastPtr->next;rhsPtr=rhsPtr->next;}}}return *this;}bool Stack::empty() const{return (myTop==0);}void Stack::push(const StackElement & value){myTop=new Stack::Node(value,myTop);}void Stack::display(ostream & out) const{Stack::NodePointer ptr;for(ptr=myTop;ptr!=0;ptr=ptr->next)out<<ptr->data<<endl;}StackElement Stack::top() const{if(!empty())return (myTop->data);else{cerr<<"***Stack is empty " "-- returning garbage ***\n";StackElement *temp=new(StackElement);StackElement garbage=*temp;delete temp;return garbage;}}void Stack::pop(){if(!empty()){Stack::NodePointer ptr=myTop;myTop=myTop->next;delete ptr;}elsecerr<<"*** Stack is empty -- can't remove a value ***\n";}

//---------------LStack_main.cpp-------------------#include<iostream>using namespace std;#include"LStack.h"void print(Stack st){ st.display(cout); }int main(){Stack s;cout<<"Stack created. Empty? "<<boolalpha<<s.empty()<<endl;cout<<"How many elements to add to the stack? ";int numItems;cin>>numItems;for(int i=1;i<=numItems;i++)s.push(100*i);cout<<"Stack empty? "<<s.empty()<<endl;cout<<"Content of stacks s (via print):\n";print(s); cout<<endl;cout<<"Check that the stack wasn't modified by print:\n";s.display(cout); cout<<endl;Stack t,u;t=u=s;cout<<"Content of stacks t and u after t=u=s(via print):\n";cout<<"u:\n";print(u);cout<<endl;cout<<"t:\n";print(t);cout<<endl;cout<<"Top value in t: "<<t.top()<<endl;while(!t.empty()){cout<<"Popping t: "<<t.top()<<endl;t.pop();}cout<<"Stack t empty? "<<t.empty()<<endl;cout<<"Top value in t: "<<t.top()<<endl;cout<<"Trying to pop t: "<<endl;t.pop();}//会闪退?

原创粉丝点击