Saturday, January 30, 2010

List Class – Updated

The List class needs to be updated to include more list like functions. The first implementation was really oriented towards the support of stack like functions (i.e. push and pop). For a list, elements need to be added to any position in the list, not just the end. This will be needed for the Translator as the RPN list is built. The following operations are therefore needed:
  • Add a value to a new element after an element (append)
  • Add a value to a new element before an element (insert)
  • Get a value and remove it's element (remove)
Several other changes will be made to the list class.

There will be two append functions, one for appending to the end of the list and one for appending after an element. Both will return a pointer to the new element. The push function will be changed to an inline function that calls the append to the end of the list function and does not need to return a pointer to the new element.

There will be two insert functions, one for inserting to the beginning of the list and one for inserting before an element. Both will return a pointer to the new element.

There will be two remove functions, one for removing the last element and one for removing any element. Both remove functions will return the value by pointer of the element removed. Both remove functions will return a status. The remove last element function will return true if an item was removed (in other words, can be called with an empty list, in which case, no value is returned and a status of false is returned). The remove any element function will return the status if the list contains more elements after the item is removed.

The pop by pointer function (meant for lists containing structures) will be changed to an inline function that calls the remove last element function. The pop by value function (meant for lists containing simple types like int, double, etc.) will be changed to an inline function that contains a local value, will call the remove last element function and return the local value.

The top function for getting a pointer to the element at the top of the a stack will be changed to last to return a pointer to the last element in the last (also the top of a stack). The first function will be changed from setting the element argument to having no argument and returning the pointer to the first element in the list to parallel the last function.

New top functions will be implemented to return the value of the top element without removing the element, one for by value (for stacks containing simple type like int, double, etc.) and one for by pointer (for stacks containing structures). The pop by value function can only be called if the stack is not empty. The pop by pointer will return false if the stack is empty (no value returned) or true if a value was returned.

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)