🔒 Locked
Implementing Stacks
Advertisement
Original Post
Hi, I have to implement a stack and a queue in a factory method. This is to be done in C++, using templates for generality and the max number of elements in either will be 100.
My question is what would be better to use: an array, a vector or a linked list (or an other dynamic memory allocation).
I cannot use the STLs.
[edited by - The Bag on February 17, 2003 7:01:03 PM]
Well, if you already know the maximum number of elements, then an array would be a great solution. Copying memory is easy since it''s in one big chunk, and you won''t have to worry about reallocation.
If you can''t use STL, then you can at least follow their design model. Create your generalized queue class first, and then derive from it to make a stack class.
If you can''t use STL, then you can at least follow their design model. Create your generalized queue class first, and then derive from it to make a stack class.
template<class T,int max_size = 100>
class stack
{
public:
stack() : m_end(m_arr) {}
~stack() {}
unsigned int size() { return (unsigned int)(m_end - m_arr); }
T& top() { return *(m_end - 1); }
void pop() { if(m_end > m_arr) --m_end; }
// should throw an exception,
// but just return true or false for simplicity
bool push(const T& val) {
if(size() < max_size)
{
*m_end = val;
++m_end;
return true;
}
else
return false;
}
private:
T m_arr[max_size];
T* m_end;
};
Smallest I could think of.
Also, this is the simplest form of queue I could think of (my brain must be fried)
template<class T,int max_size = 100>
class queue
{
public:
queue() : m_size(0), m_front(0), m_back(0) {}
~queue(){}
bool push(const T& val)
{
if(m_size < max_size)
{
node* n = new_node();
n->val = val;
if(m_front == 0)
m_front = m_back = n;
else
{
m_back->prev = n;
m_back = n;
}
++m_size;
return true;
}
else
return false;
}
void pop()
{
if(m_size)
{
node* t = m_front->prev;
delete m_front;
m_front = t;
--m_size;
}
}
T& front()
{
return m_front->val;
}
unsigned int size()
{
return m_size;
}
private:
unsigned int m_size;
struct node
{
node* prev;
T val;
};
node* new_node()
{
node* ret = new node;
ret->prev = 0;
return ret;
}
node* m_front;
node* m_back;
};
[edited by - daerid on February 17, 2003 12:36:31 AM]
quote:
Original post by Miserable
daerid, when this seems like a very clear homework question
It is a test, but not for homework as such. It is, however, an important test.
tbh if I was writing this myself, with the same details, I would have done it with an array - but then I got talking to someone who was pointing out one of the features of a stack is its ability to grow dynamically (& thus grow in memory), but since there is a set upper level I would use an array as it's safer and takes all the memory at once.
Implementing it either way is no problem for me - I just really don't know which way would be better any more.
Right I've had another look at the spec and it says 'please make sure that your objects are being constructed and destroyed correctly' - this suggests dynamic allocation to me.
[edited by - The Bag on February 18, 2003 6:17:21 AM]
quote:Not necessarily. You can allocate an array with enough memory to hold the objects, and then use placement new to construct the objects there.
Original post by The Bag
Right I''ve had another look at the spec and it says ''please make sure that your objects are being constructed and destroyed correctly'' - this suggests dynamic allocation to me.
Also, if memory usage is critical, you can first allocate a small array and if too many objects are added, allocate a larger array and copy the objects there. This is how std::vector works.
quote:Hmm, sorry for hijacking the thread, but i must have missed how to do that. I''ve been thinking lately about just how to construct an object on already allocated memory, but was unable to recall any such method. Care to reveal how to do that?
Original post by civguy
You can allocate an array with enough memory to hold the objects, and then use placement new to construct the objects there.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement