#ifndef TEST_TSTACK1_H
#define TEST_TSTACK1_H
template <class T>
class Stack
{
public:
Stack(int = 10); //the stack has 10 elements
~Stack() { delete[] stackPtr; } //destructor
bool push(const T&); //inserts an element into the stack
bool pop(T&); //delets an element from the stack
private:
int size; //number of elements in stacks
int top; //finds the element in the top of the stack
T* stackPtr; //pointer to the stack
bool isEmpty() const {return top == -1;} //functions
bool isFull() const {return top == size-1;}
};
template<class T>
Stack<T>::Stack(int s)
{
size = s > 0 ? s : 10;
top = -1; //the stack is empty at the beginning
stackPtr = new T[size]; //the array for elements
}
//Introduce an element into the stack
//return : 1 if it could be done and 0 if could not be done
template<class T>
bool Stack<T>::push(const T& pushValue)
{
if(!isFull())
{
stackPtr[++top] = pushValue; //introduces an element in the stack
returntrue; //the insert was sucessful
}
returnfalse; //the insert was not successful
}
//takes an element from the stack
template<class T>
bool Stack<T>::pop(T& popValue)
{
if(!isEmpty())
{
popValue = stackPtr[top--]; //extracts an element from a stack
returntrue; //the extract was sussesfully done
}
returnfalse; //it could not be done
}
#endif
but i want a different kind of stack....i want a stack which can delete or add an element in the middle of the stack. If anyone knows a link to such a template please tell me
well what you're asking for isn't really a stack by definition, hence the confusion.
You might want a linked list (see std::list, or look it up on wikipedia). Can do the same push/pop stuff as a normal stack, but also can easily erase elements anywhere in the middle.
i need make a program which manages the students of a group in an university. I need store data about students in a kind of array. I need be able delete or add new students. I thought of making a template class which stores their data in such kind of array. I know stacks work on first in last out but i need something which can delete students whose data are in the middle of the array