Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

vaneger

dynamic stack class

This topic is 5790 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hows this stack as a linked list class look?
  
#ifndef DYSTACK_H_
#define DYSTACK_H_
template <class type>
class dyStack
{
	public: 
		dyStack();
		bool push(const type& x);
		bool pop(type& x);
		bool get_top(type& x)const;
		inline bool is_empty()const
		{
			return m_top == NULL;
		}
		inline bool is_full()const
		{
			node *x = new node;
			return (node == NULL);
		}

		bool m_return;
	private:
		struct node
		{
			type item;
			node *next;
		};
		node *m_top;
};
#include"dyStack.cpp"
#endif

#include<stdlib.h>

template <class type>
dyStack<type>::dyStack()
{
	m_top = NULL;
	m_return = false;
}

template <class type>
bool dyStack<type>::push(const type& x)
{
	node *m_old;
	bool success;
	m_old = m_top;
	m_top = new type;
	if(m_top ==NULL)
	{
		m_top = m_old;
		success = false;
	}
	else
	{
		m_top->next = m_old;
		m_top->item = x;
		success = true;
	}
	return success;
}

template <class type>
bool dyStack<type>::pop(type& x)
{
	node m_old;
	bool success = false;
	if(m_top != NULL)
	{
		x = m_top->item;
		m_old = m_top;
		m_top = m_old->next;
	}
	return success;
}
template <class type>
bool dyStack<type>::get_top(type& x)const
{
	bool success = false;
	if(m_top!=null)
	{
		x = m_top->item;
		success = true;
	}
	return get_top;
}
  

Share this post


Link to post
Share on other sites
Advertisement
My thoughts exactly. Use the std::stack. It has been tested, debugged and optimized. It seems you already understand the concept of a stack which is the one of the only reasons someone would want to implement their own stack.

Share this post


Link to post
Share on other sites
i use my own classes and containers all the time so i dont have to deal with using something else if i am forced to use a differnt compiler or an unfamilar machine, i cna just upload my classes from disk and run with it.plus its easier to modify my code then it is to modify a .pch file from the std.

[edited by - vaneger on August 11, 2002 6:06:13 PM]

Share this post


Link to post
Share on other sites
ok perhaps this is a better question : how can i make my dyStack class as efficent as teh std::stack without copiying the source and renaming it or something.

Share this post


Link to post
Share on other sites
It can be worth while to write your own contenners on occasion if you want something specific and/or know a way that is better than the std implementation for this. If you need the stack to have an Add_10_to_All() function, you''ll have to write your own. It may well be the same case for vaneger here.

Share this post


Link to post
Share on other sites
quote:
Original post by vaneger
ok perhaps this is a better question : how can i make my dyStack class as efficent as teh std::stack without copiying the source and renaming it or something.

Study how STL does things. Understand the concept of iterators. Profiling other version of std::stack with yours. See what''s the different between STLPort, Dinkumware, RogueWave or etc in term of actually implementation.... AND ... extensive DEBUG!

Share this post


Link to post
Share on other sites
Depending on your desired performance characteristics, an array-based implementation can be much more efficient than a linked-list based implementation.

Array: Assuming you use a double-on-copy approach to resizing the array, significantly faster overall. However, the few times it needs to resize the array it''ll be very slow for one access.

Linked List: Much more uniform access times. However, the overhead on memory allocation/freeing is relatively high, leading to slower speeds overall.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!