abbeyroadd
01-13-2005, 11:01 PM
I have a project that I have been working on for a day or two now and I am stuck. I just can't "see" the code that needs to be written...here are the instructions and the code I have.
Thanks for any help.
Instructions:
Modify your Stack class to be a templated class that can hold any number of items using Nodes. Then write an Iterator class that can be used to move from node to node in the Stack. The Iterator class should be a friend of the Stack and have the following members:
typename Stack<Item>::Node *peek;
Points to the current node we are looking at in the stack.
Iterator(Stack<Item> *s)
The constructor should accept a pointer to the stack you wish to move in and store it in a member variable.
void Front()
Sets our “peek” member to the top of the stack.
void Move_Down()
Moves our “peek” member to the next node in the stack.
Item *Get_Current_Item()
Gives us a pointer to the Item in the stack that “peek” is currently looking at.
bool End()
Tells us if “peek” has reached the bottom of the stack.
Write a program to test your Stack and Iterator classes. The videogame store is having a sale. Allow the store manager to input as many videogames as he wants and push them into his bargain bin. After he is done, use your Iterator class to reduce the price of any game over $30 by 25%. Also use it to display all the games and show him his average bargain price. Remember to keep the stack intact by not popping anything off.
I have already created the Stack class and templated it...I have the Iterator classes created but I am not sure how to go about creating the 6 functions.
Here is the code:
// Stack.h - Interface for a First-In, Last-Out (FILO) Stack container.
#ifndef _STACK_H_
#define _STACK_H_
template <typename Item>
class Stack
{
private:
friendclass Iter;
// Each link in the chain...
struct Node
{
// What are we holdin?
Item data;
// Who's the Node after this one?
Node *next;
// Who's the Node before this one?
Node *prev;
Node(const Item &data)
: data(data), next(0), prev(0) { }
// Return: Our next pointer.
const Node * getNext() const
{
returnthis->next;
}
};
// Who is the first Node in the Stack?
Node *front;
// No copies allowed!
Stack(const Stack &s);
Stack &operator=(const Stack &s);
public:
// Constructor
Stack(void)
{
// Nuttin' in the Stack yet!
this->front = 0;
}
// Destructor
~Stack(void);
// Push (add) something to the Stack
//
// In: i The Item to add to the Stack.
void push(const Item &i);
// Pop (remove) something FROM the Stack
//
// In: i Where to put the front Node's data.
//
// Out: i The filled-in Item.
//
// Returns True if there was at least one Item
// in the Stack.
bool pop(Item &i);
};
// Destructor
template <typename Item>
Stack<Item>::~Stack(void)
{
// Free all the Nodes, if any...
Node *temp = this->front;
while (this->front)
{
// Delete each node, one at a time.
this->front = const_cast<Node *>(this->front->getNext());
delete temp;
temp = this->front;
}
}
// Push (add) something to the Stack
//
// In: i The Item to add to the Stack.
template <typename Item>
void Stack<Item>::push(const Item &i)
{
// Create our new node and fill it out.
Node *temp = new Node(i);
// Link up the new node.
if (this->front)
this->front->prev = temp;
temp->next = this->front;
this->front = temp;
}
// Pop (remove) something FROM the Stack
//
// In: i Where to put the front Node's data.
//
// Out: i The filled-in Item.
//
// Returns True if there was at least one Item
// in the Stack.
template <typename Item>
bool Stack<Item>::pop(Item &i)
{
// Is there anything in the Stack?
if (!this->front)
returnfalse;
// Fill in the Item the user passed...
i = this->front->data;
// Kill that front node.
Node *temp = this->front;
this->front = this->front->next;
delete temp;
// Make sure that wasn't the last Node...
if (this->front)
this->front->prev = 0;
// Success!
returntrue;
}
#endif
Thanks for any help.
Instructions:
Modify your Stack class to be a templated class that can hold any number of items using Nodes. Then write an Iterator class that can be used to move from node to node in the Stack. The Iterator class should be a friend of the Stack and have the following members:
typename Stack<Item>::Node *peek;
Points to the current node we are looking at in the stack.
Iterator(Stack<Item> *s)
The constructor should accept a pointer to the stack you wish to move in and store it in a member variable.
void Front()
Sets our “peek” member to the top of the stack.
void Move_Down()
Moves our “peek” member to the next node in the stack.
Item *Get_Current_Item()
Gives us a pointer to the Item in the stack that “peek” is currently looking at.
bool End()
Tells us if “peek” has reached the bottom of the stack.
Write a program to test your Stack and Iterator classes. The videogame store is having a sale. Allow the store manager to input as many videogames as he wants and push them into his bargain bin. After he is done, use your Iterator class to reduce the price of any game over $30 by 25%. Also use it to display all the games and show him his average bargain price. Remember to keep the stack intact by not popping anything off.
I have already created the Stack class and templated it...I have the Iterator classes created but I am not sure how to go about creating the 6 functions.
Here is the code:
// Stack.h - Interface for a First-In, Last-Out (FILO) Stack container.
#ifndef _STACK_H_
#define _STACK_H_
template <typename Item>
class Stack
{
private:
friendclass Iter;
// Each link in the chain...
struct Node
{
// What are we holdin?
Item data;
// Who's the Node after this one?
Node *next;
// Who's the Node before this one?
Node *prev;
Node(const Item &data)
: data(data), next(0), prev(0) { }
// Return: Our next pointer.
const Node * getNext() const
{
returnthis->next;
}
};
// Who is the first Node in the Stack?
Node *front;
// No copies allowed!
Stack(const Stack &s);
Stack &operator=(const Stack &s);
public:
// Constructor
Stack(void)
{
// Nuttin' in the Stack yet!
this->front = 0;
}
// Destructor
~Stack(void);
// Push (add) something to the Stack
//
// In: i The Item to add to the Stack.
void push(const Item &i);
// Pop (remove) something FROM the Stack
//
// In: i Where to put the front Node's data.
//
// Out: i The filled-in Item.
//
// Returns True if there was at least one Item
// in the Stack.
bool pop(Item &i);
};
// Destructor
template <typename Item>
Stack<Item>::~Stack(void)
{
// Free all the Nodes, if any...
Node *temp = this->front;
while (this->front)
{
// Delete each node, one at a time.
this->front = const_cast<Node *>(this->front->getNext());
delete temp;
temp = this->front;
}
}
// Push (add) something to the Stack
//
// In: i The Item to add to the Stack.
template <typename Item>
void Stack<Item>::push(const Item &i)
{
// Create our new node and fill it out.
Node *temp = new Node(i);
// Link up the new node.
if (this->front)
this->front->prev = temp;
temp->next = this->front;
this->front = temp;
}
// Pop (remove) something FROM the Stack
//
// In: i Where to put the front Node's data.
//
// Out: i The filled-in Item.
//
// Returns True if there was at least one Item
// in the Stack.
template <typename Item>
bool Stack<Item>::pop(Item &i)
{
// Is there anything in the Stack?
if (!this->front)
returnfalse;
// Fill in the Item the user passed...
i = this->front->data;
// Kill that front node.
Node *temp = this->front;
this->front = this->front->next;
delete temp;
// Make sure that wasn't the last Node...
if (this->front)
this->front->prev = 0;
// Success!
returntrue;
}
#endif