i need example of a different stack template

I have a source code of a stack which inserts elements and delets the elements that are at the end of the stack :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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
return true; //the insert was sucessful
}
return false; //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
return true; //the extract was sussesfully done
}
return false; //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
Mmmmmn, the whole idea of a stack is LIFO.
Inserting things in the middle of a 'stack' defeats the point.
but....is there any other kind of stack which is made for inserting elements in the middle? can you show me link to template of suck stack?
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.
This has "poor design" written all over it. Why do you need something like that? Could it be that what you really want is a priority queue?
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
Topic archived. No new replies allowed.