Stack的C ++数组实现

时间:2014-11-24 05:08:03

标签: c++ arrays stack

这是我到目前为止对堆栈的数组实现的实现。但是我在函数isFullStack()中遇到断点错误,我一直很困惑。解释会有所帮助,谢谢!

 //Header file: myStack.h

 #ifndef H_StackType
 #define H_StackType

 #include <iostream>
 #include <cassert>

 #include "stackADT.h"

  using namespace std


  template <class Type>
 class stackType: public stackADT<Type>
{
  public:



  const stackType<Type>& operator=(const stackType<Type>&otherStack)
//Overload the assignment operator.
{
    if (this != &otherStack) //avoid self-copy
        copyStack(otherStack);

    return *this;

}

void initializeStack()
//Function to initialize the stack to an empty state.
//Postcondition: stackTop = 0;
{
    stackTop=0;
}


bool isEmptyStack() const
//Function to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty,
//    otherwise returns false.
{
    return (stackTop==0);
}

bool isFullStack() const
//Function to determine whether the stack is full.
//Postcondition: Returns true if the stack is full,
//    otherwise returns false.
{
    return (stackTop == maxStackSize);  //** CAN CAUSE PROBLEMS

}

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.
{
    assert(!isFullStack());

    list[stackTop]= newItem; //**CAN CAUSE PROBLEMS
    stackTop++;

}

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.
{
    assert(!isEmptyStack());
    return list[stackTop-1];

}

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.
{
    assert(!isEmptyStack());
    stackTop--;
}

bool operator==(const stackType<Type>& otherStack) const
{
    if ( this->stackTop != otherStack.stackTop ) //checks if sizes are equal
        return false;

    //check for equality of elements at corresponding positions
    for ( int i = 0; i < stackTop; i++ )
    {
        if ( this->list[ i ] != otherStack.list[ i ] ) //checks if values are equal
            return false;
    }

    return true;
}





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
{
  //  list=new Type[maxStackSize]; //maybe list=new Type[maxStackSize]
  //  maxStackSize=stackSize;
  //  stackTop=0;

    if (stackSize <= 0)
    {
        maxStackSize = 100;
    }
    else
        maxStackSize = stackSize;

    stackTop = 0;
    list = new Type[maxStackSize];

}

stackType(const stackType<Type>& otherStack)
//Copy constructor
{
    list=NULL;
    copyStack(otherStack); //calls the private function to copy
}

~stackType()
//Destructor
//Remove all the elements from the stack.
//Postcondition: The array (list) holding the stack
//    elements is deleted.
{
    delete[] list;
}

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.
{
    delete[] list; //deletes existing array
    maxStackSize=otherStack.stackTop;
    list=new Type[maxStackSize];

    for(int i=0; i<stackTop; i++)   //copying other stack onto this stack
    {
        list[i]=otherStack.list[i];
    }
}
};



#endif

这是主要功能:

 #include <iostream>
 #include "myStack.h"

 using namespace std;

 int main()
 {
stackType<int> stack1(50);
stackType<int> stack2(50);
stackType<int> stack3(100);
int x;

stack1.initializeStack();
stack1.push(23);
stack1.push(45);
stack1.push(38);

stack2 = stack1;

if (stack1 == stack2)
    cout << "stack1 and stack2 are identical" << endl;
else
    cout << "stack1 and stack2 are not identical" << endl;

x = stack2.top();
stack2.pop();
stack2.push(32);

cout << "**** After pop and push operations on stack2 ****"
<< endl;
if (stack1 == stack2)
    cout << "stack1 and stack2 are identical" << endl;
else
    cout << "stack1 and stack2 are not identical" << endl;

stack2.push(11);

cout << "**** After another push operation on stack2 ****" << endl;
if (stack1 == stack2)
    cout << "stack1 and stack2 are identical" << endl;
else
    cout << "stack1 and stack2 are not identical" << endl;
system("PAUSE");

return 0;
 }

0 个答案:

没有答案