标签:
栈:栈是一种数据结构,栈里元素的添加和删除只能在栈的末端进行。它是一种“后进先出”(LIFO)的数据结构。
栈的操作:
initializeStack:初始化栈,使得为一个空栈。
destroyStack:清空栈里所有的元素,使得为一个空栈。
isEmptyStack:判断栈是否为空,如果为空,返回true,否则返回false。
isFullStack : 判断栈是否溢出,如果溢出,返回true,否则返回false。
push : 添加一个新的元素到栈顶。前提是栈存在,且栈没有溢出。
top : 返回栈顶元素。前提是栈存在,且栈没有溢出。
pop : 删除栈顶元素。前提是栈存在,且栈不为空。
栈的实现可以用数组和链表两种类型来实现。
1.用数组来实现栈:
因为栈里所有的元素都是同一数据类型,所以可以用数组来实现一个栈。栈的第一个元素放在数组的第一个位置,栈的第二个元素放在数组的第二个位置,以此类推。
为了跟踪数组的top position ,我们声明另一个变量stackTop.
下面的类stackType定义了一个栈为ADT。
template<class Type> class stackType { public: const stackType<Type>& operator=(const stackType<Type>&);//重载赋值运算符 void initializeStack(); //Function to initialize the stack to an empty state //precondition:stackTop=0 bool isEmptyStack() const; //Function to determine whether the stack is empty //postcondition:Returns true if the stack is // empty,otherwise returns false bool isFullStack() const; //Function to determine whether the stack is full //postcondition:Returns true if the stack is full // otherwise returns false void destroyStack(); //Function to remove all the elements from the stack //Postcondition: stackTop = 0. void push(const Type& newItem); //Function to add newItem to the stack. //precondition:The stack exists and is not full //postcondition:the stack is changed and newItem is // added to the top of the stack. Type top() const; //Function to return the top element of the stack. //precondition:the stack exists and is not empty. //postcondition:If the stack is empty,the program // terminates;otherwise ,the top element // of the stack is returned. void pop(); //Function to remove the top element of the stack. //precondition:The stack exists and is not empty. //postcondition:The stack is changed and the top // element is removed from the stack. stackType(int stackSize = 100); //constructor //Create an array of the size stackSize to hold the //stack elements.the default stack size is 100. //Postcondition:The variable list contains the base //address of the array,stackTop=0, //and maxStackSize =stackSize. stackType(const stackType<Type>&otherStack); //copy constructor ~stackType(); //desturctor; //Remove all the elements from the stack. //Postcondition:The array (list)holding the stack //elements is deleted. private: int maxStackSize;//variable to store the maximum //stack size. int stackTop;//variable to point to the top of the //stack Type *list;//pointer to the array that holds the stack //elements void copyStack(const stackType<Type>& otherStack); //Function to make a copy of otherStack. //Postcondition:A copy of otherStack is created and //assigned to this stack. };
定义栈的初始化函数:
template <class Type> void stackType<Type>::initializeStack() { stackTop = 0; }
定义Destroy Stack函数:
在用数组实现的栈的过程中,销毁栈的操作和初始化栈操作相似。如果我们把stackTop=0,所有栈元素就被销毁了,尽管元素仍然在栈里,但是stackTop的值表明栈是否为空。
template <class Type> void stackType<Type>::destroyStack() { stackTop = 0; }
定义Empty Stack函数
既然stackTop的值决定栈是否为空,如果stackTop = 0,则栈为空,否则栈不为空。
template <class Type> bool stackType<Type>::isEmptyStack() const { return (stackTop == 0); }
定义Full Stack函数:
如果stackTop = maxStackSize,栈满。
template<class Type> bool stackType<Type>::isFullStack() const { return (stackTop == maxStackSize); }
定义入栈函数:push函数
template<class Type> void stackType<Type>::push(const Type &newItem) { if (!isFullStack()) { list[stackTop]=newItem; stackTop++; } else cout<<"Cannot add to a full stack."<<endl; }
定义top函数:return the top element
template<class Type > Type stackType<Type>::top() const { assert(stackTop !=0);//if the stack is empty, //terminate the program. return list[stackTop-1]; }
定义pop函数
template<class Type > void stackType<Type >::pop() { if(!isEmptyStack()) stackTop--; else cout<<"Cannot remove from an empty stack,"<<endl; }//end pop
定义Copy Stack 函数:
template<class Type > void stackType<Type>::copyStack(const stackType<Type> &otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type(maxStackSize); assert(list != NULL ); //copy otherStack into this stack. for(int j=0;j<stackTop;j++) list[j] = otherStack.list[j]; }//end copyStack
定义Constructor和Destructor函数:
//constructor template<class Type > stackType<Type>::stackType(int stackSize) { if(stackSize <= 0) { cout<<"The size of the array to hold the stack" <<"must be positive"<<endl; cout<<"Creating an array of size 100"<<endl; maxStackSize = 100; } else maxStackSize = stackSize; stackTop=0; list=new Type[maxStackSize]; assert(list != NULL); }//end constructor
template <class Type > stackType<Type>::~stackType<Type>() { delete [] list;//deallocate memory occupied //by the array. }
定义Copy Constructor
template<class Type > stackType<Type>::stackType(const stackType<Type>& otherStack) { list = NULL; copyStack(otherStack); }//end copy constructor
重载运算符(=)
template<class Type > const stackType<Type>& stackType<Type>::operator=(const stackType<Type> & otherStack) { if(this != &otherStack)//avoid self-copy copyStack(otherStack); return *this; }//end operator =
2.用链表实现栈
因为数组的大小是固定的,所以用数组实现的栈的元素数量是固定的。当入栈元素数量超过数组大小,程序就会终止。所以我们用链表来克服这个问题。
下面定义一个链表栈的ADT:
template<class Type> struct nodeType { Type info; nodeType<Type> *link; };
template<class Type> class linkedStackType { public: const linkedStackType<Type>& operator=(const linkedStackType<Type>&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void destroyStack(); void push(const Type& newItem); Type top() const; void pop(); linkedStackType(); linkedStackType(const linkedStackType<Type>& otherStack); ~linkedStack(); private: nodeType<Type> *stackTop; void copyStack(const linkedStackType<Type>& otherStack); };
说明:链表实现的栈在存储元素时,内存是动态分配的,因此栈不会满。只有当内存满了栈才会满,因此不需要用isFullStack来判断栈是否满。为了和数组实现的栈保持一致,链表实现的栈也包含了这个操作。
对链表实现的栈的基本操作:
默认构造函数(default constructor)
//default constructor template <class Type> linkedStackType<Type>::linkedStackType() { stackTop = NULL; }
销毁函数(destroy stack)
在数组里只需将stackTop设为0即可,但是在链表中,由于数据存储是动态的,所以需要把stackTop设置为NULL,然后释放栈元素占用的内存空间。
template<class Type> void linkedStackType<Type>::destroyStack() { nodeType<Type> *temp; while (stackTop!=NULL) { temp = stackTop; stackTop = stackTop->link; delete temp; }
初始化栈:(initializeStack)
初始化操作是将栈重新初始化为空的状态。
template<class Type> void linkedStackType<Type>::initializeStack() { destroyStack(); }
判断栈是否为空和判断栈是否满:
template<class Type> bool linkedStackType<Type>::isEmptyStack() const { return(stackTop==NULL); } template<class Type> bool linkedStackType<Type>::isFullStack() const { return false; }
接下来是入栈操作push:
template<class Type> void linkedStackType<Type>::push(const Type&newItem) { nodeType<Type> *newNode; newNode = new nodeType<Type>;//create the new node assert(newNode !=NULL); newNode->info=newItem; newNode->link=stackTop; stackTop=newNode; }
返回栈顶元素(Return the Top element )
template <class Type> Type linkedStackType<Type>::top()const { assert(stackTop != NULL); return(stackTop->info); }//end top
出栈操作:pop
template <class Type> void linkedStackType<Type>::pop() { nodeType <Type> *temp; if (stackTop != NULL) { temp = stackTop; stackTop=stackTop->link; delete temp; } else cout<<"cannot remove from an empty stack"<<endl; }
复制栈操作:
template<class Type> void linkedStackType<Type>::copyStack(const linkedStackType<Type>& otherStack) { nodeType<Type> *newNode,*current,*last; if(stackTop != NULL) destroyStack(); if(otherStack.stackTop == NULL) stackTop=NULL; else { current = otherStack.stackTop; stackTop = new nodeType<Type>; assert(stackTop!=NULL); stackTop->info = current->info; stackTop->link=NULL; last = stackTop; current=current->link; while (current!=NULL) { newNode = new nodeType<Type>; assert(newNode != NULL); newNode->info = current->info; newNode->link = NULL; last->link = newNode; last = newNode; current = current->link; }//end while }//end else }
构造函数和解析函数:
template <class Type> linkedStackType<Type>::linkedStackType(const linkedStackType<Type>& otherStack) { stackTop = NULL; copyStack(otherStack); } template<class Type> linkedStackType<Type>::~linkedStackType() { destroyStack(); }
重载赋值运算符(=)
template<class Type> const linkedStackType<Type>& linkedStackType<Type>::operator =(const linkedStackType<Type>& otherStack) { if(this != &otherStack) copyStack(otherStack); return *this; }//end operator =
说明:
linkedStackType<int > stack;
这个语句声明了一个类型为linkedStackType的对象,并且这个stack里的元素类型为int型
比如:linkedStackType<stirng > stringStack;
这个语句声明了一个类型为linkedStackType的对象,并且这个stringStack里的元素类型为string型
标签:
原文地址:http://www.cnblogs.com/weekend/p/4855714.html