Thread: FIFO 2 LIFO
View Single Post
  #3 (permalink)  
Old April 28th, 2004, 07:55 PM
nikolai nikolai is offline
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, after reviewing your code, I have a couple questions.

Are you programming in C or C++? You use primarily C conventions, but use 'new' to allocate a new node, which is a C++ operator. You deallocate nodes using free(), which is a C function that frees pointers allocated by malloc(). The C++ operator to deallocate memory allocated w/ 'new' is 'delete'.

Either way, I suggest you create a struct or class to represent the Stack itself, that way, you don't have to maintain head and tail pointers to the structure. The stack is not very useful to the programmer that uses it because there's too much knowledge involved in it's use. You should be able to say:

  stack.enqueue('c'); // the C++ OOP way.

or

  enqueue(pStack, 'c'); /* the C way */


Also, if there is a subtle logic flaw in your code, then stepping through your code in a debugger will likely point out where you've gone wrong.

If you don't have access to a debugger, or don't really know how to use one effectively, I suggest you get one and learn. In addition (or instead of), you can trace your logic manually (on paper, whiteboard, chalkboard, etc). Just draw pictures to represent your structure when the program starts, as you insert a couple items, and as you remove them.



Take care,

Nik
http://www.bigaction.org/
Reply With Quote