Skip to main content
gamedev.net
🔒 Locked

Implementing Stacks

Started by The Bag 10 replies 350+ views
Original Post
The Bag
The Bag
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]
Zipster
Zipster
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.
daerid
daerid

    
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]
daerid@gmail.com
Miserable
Miserable
daerid, when this seems like a very clear homework question and the OP specifically asked for mere hints, it seems to be in rather poor taste to post a complete solution ...
civguy
civguy
hey daerid:
1.what miserable said
2.try using your stack with an object that has a constructor or a destructor. Or with an object that lacks a default constructor
3.queue can be implemented with array too (cyclic). And should be a lot smaller in code size
The Bag
The Bag
As far I can see it using an array is a safer option, but using dynamic memory allocation - is, well, dynamic and does not take all the memory at the start.

One other piece of info is that this is for a console program so there is limited memory.
The Bag
The Bag
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]
civguy
civguy
quote:
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.
Not necessarily. You can allocate an array with enough memory to hold the objects, and then use placement new to construct the objects there.

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.
CWizard
CWizard
quote:
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.
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?


CWizard
CWizard
quote:
Original post by civguy
Look here
Thanks. It might come in handy one day
daerid
daerid
Sorry about that, but I really didn''t get the impression that this was a homework problem, and it didn''t seem like he didn''t know how to do it. True, I didn''t necessarily answer his question, but I don''t think I screwed up his educational experience that much.
daerid@gmail.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.