Advertisement

does this DYnamic STaCK class look good?

Started by October 04, 2002 06:48 PM
32 comments, last by vaneger 22 years, 1 month ago

  
/**********************************
dyStack.h
Matt Silvernail Aug 11 2002
Dynamic Stack class
Linked list implementation of a Stack
**********************************/
#ifndef DYSTACK_H_
#define DYSTACK_H_
#include<stdlib.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 (x == NULL);
		}

		bool m_return;
	private:
		struct node
		{
			type item;
			node *next;
		};
		node *m_top;
};
#include"dyStack.cpp"
#endif  
and .cpp::::::
  
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 node;
	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 success;
}
  
I like it. The indentation gives it a pleasing shape, and your color scheme is very professional.
Advertisement
It is wrong. You include the .cpp at the end of the .h, but it should be just the other way around (include .h at the beginning of the .cpp). Of course it works, but your nice template class sucks ;P
it really doesnt matter how you include the files :-\, but when i include .h at top of .cpp it never works so i dont do it
i dont care if my code looks nice :-\ i want to know how good the code is not how nice it looks
Sorry, you''re trying to show off some advanced template stuff but you don''t know how to use header files. That doesn''t add.
Try something like: can anybody explain me how to use header files properly?
No offense.
Advertisement
quote: Original post by vaneger
i dont care if my code looks nice :-\ i want to know how good the code is not how nice it looks


I know.

In any case, I''m not sure what kind of critique you''re looking for. A stack is a pretty basic thing, and a cursory inspection tells me that everything is pretty much in order. I generally don''t bother with writing my own versions of simple structures like that; I use the STL. It''s much more powerful and flexible that anything I could design.

Nice to see that you''ve used const properly. const is good

A standards-compliant implementation of new will never return NULL. If memory runs out, it''ll throw std::bad_alloc.

You seem to rely on return values to represent errors. If someone fails to check the return values (which is going to happen sooner or later), the probability of bugs is increased. If you were to use exceptions, you''d avoid that problem. After all, if a program tries to pop more values than it pushes, then it means that there''s a logical error in the program - throw a std::logic_error or something.

Consider changing dyStack:op so that it doesn''t take parameters. You''ve already got a mechanism for examining the topmost element; it would be nice to be able to pop an unwanted value without making a copy.

By the way, since it''s a template, you can just put the entire implementation in the .h file; you won''t get unwanted copies.

And finally, consider using std::stack . Of course, if you wrote this for the purpose of learning, then keep at it
quote: Original post by Beer Hunter
By the way, since it's a template, you can just put the entire implementation in the .h file; you won't get unwanted copies.

And finally, consider using std::stack . Of course, if you wrote this for the purpose of learning, then keep at it


I concur.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on October 4, 2002 10:44:46 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
ok mister annoymous poster you''re an a$$hole, i explained to you that including .h at top of .cpp doesnt work for my compiler why i dont know but ive always done it by including .cpp at bottom of .h because thast the way i was taught and it works so shove it, and beer hunter whats thsi unwanted copies thing you speak of i dont think i get it :-\, and yes this is just so i can learn to code the thing

This topic is closed to new replies.

Advertisement