Below is my class to create stackType objects, I have a problem in particular with the member function update(). update() is suppose to search the stack for parameter "t" -If found "t" must be removed from its location in the stack and put on top of the stack. -If not found, the bottom element in the stack must be removed and "t" put on top of the stack. I've tried everything I could to solve this one but I've failed. PLEASE HELP.
//Header file: myStack.h
#ifndef H_StackType
#define H_StackType
#include <iostream>
#include <cassert>
#include "stackADT.h"
usingnamespace std;
//*************************************************************
// Author: D.S. Malik
//
// This class specifies the basic operation on a stack as an
// array.
//*************************************************************
template <class Type>
class stackType: public stackADT<Type>
{
public:
const stackType<Type>& operator=(const stackType<Type>&);
//Overload the assignment operator.
void initializeStack();
//Function to initialize the stack to an empty state.
//Postcondition: 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 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.
void update(Type t);
//Function to move t from whatever position in the stack to
//the top of the stack.
//Precondition: the stack exist and it is not empty.
//Postcondition: t is moved from its position in the top to the
//top of 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();
//Destructor
//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;
}//end initializeStack
template <class Type>
bool stackType<Type>::isEmptyStack() const
{
return(stackTop == 0);
}//end isEmptyStack
template <class Type>
bool stackType<Type>::isFullStack() const
{
return(stackTop == maxStackSize);
} //end isFullStack
template <class Type>
void stackType<Type>::push(const Type& newItem)
{
if (!isFullStack())
{
list[stackTop] = newItem; //add newItem to the
//top of the stack
stackTop++; //increment stackTop
}
else
cout << "Cannot add to a full stack." << endl;
}//end push
template <class Type>
Type stackType<Type>::top() const
{
assert(stackTop != 0); //if stack is empty,
//terminate the program
return list[stackTop - 1]; //return the element of the
//stack indicated by
//stackTop - 1
}//end top
template <class Type>
void stackType<Type>::pop()
{
if (!isEmptyStack())
stackTop--; //decrement stackTop
else
cout << "Cannot remove from an empty stack." << endl;
}//end pop
template<class Type>
void stackType<Type>::update(Type t)
{
int temp = stackTop;
while(list[stackTop] != t)
stackTop--;
if(list[stackTop] == t)
{
while(stackTop < temp)
{
list[stackTop] = list[stackTop+1];
stackTop++;
}
list[stackTop] = t;
}
}
template <class Type>
stackType<Type>::stackType(int stackSize)
{
if (stackSize <= 0)
{
cout << "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; //set the stack size to
//the value specified by
//the parameter stackSize
stackTop = 0; //set stackTop to 0
list = new Type[maxStackSize]; //create the array to
//hold the stack elements
}//end constructor
template <class Type>
stackType<Type>::~stackType() //destructor
{
delete [] list; //deallocate the memory occupied
//by the array
}//end destructor
template <class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type[maxStackSize];
//copy otherStack into this stack
for (int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
} //end copyStack
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=
#endif
There are two things to point out.
your update function will access the memory before your allocated array if the element is not present in your stack. Add a stackTop > 0 check in your first while loop in line 150. Also if you remove line 153 that will effectively accomplish the "not found" case as you described in the beginning of your post. You should also run the loop in line 155 not like while(stackTop < temp), but like while(stackTop < temp - 1), because the last valid element is temp - 1 will be copied when stackTop equals temp - 2.
@KRAkatau, changing the loop condition to stackTop > 0 at line 153 causes the stack to remove all its elements without checking if the "t" is in the stack.
-If found "t" must be removed from its location in the stack and put on top of the stack. -If not found, the bottom element in the stack must be removed and "t" put on top of the stack.
If this is indeed what you want to do - then change the while in line 150 to while (stackTop > 0 && list[stackTop] != t) and remove the if in line 153.
@KRAkatau, changing the loop condition to stackTop > 0 at line 153 causes the stack to remove all its elements without checking if the "t" is in the stack.
I didn't say change the condition, I said add it... Please read more carefully.