Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2004
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Some quick help please!

    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:



    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

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the functions you have mentioned here just make calls to the iterator class and change the location that the pointer points to to match either the "front" or "end" while "current item" just displays the vlaue of what is being stored at that location and "next item" just increments the pointer...


    Jason

  • #3
    New Coder
    Join Date
    Nov 2004
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the quick reply...yea, I figured the functions out but it's not working correctly. I can append one thing to the Stack but I can't append, two, three, etc...

    Here is my newer Stack + Iterator class:

    Code:
    #ifndef _STACK_H_
    #define _STACK_H_
    
    // Forward decleration of the iterator class.
    template <typename Item>
    class Iterator;
    
    template <typename Item>
    class Stack
    {
    
    private:
    
    	// 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
    		{
    			return this->next;
    		}
    	};
    
    	// Who is the first Node in the Stack?
    	Node *front;
    
    	// No copies allowed!
    	Stack(const Stack &s);
    	Stack &operator=(const Stack &s);
    
    public:
    
    	// Make the Iterator a freind class.
    	friend class Iterator;
    
    	// 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);
    
    	// Ceck to see if the stack is empty.
    	//
    	// Return:	False if the stack is full
    	bool is_empty() const;
    
    	// Ceck to see if the stack is full.
    	//
    	// Return:	False if stack is not totaly full.
    	bool is_full() const;
    };
    
    template <typename Item>
    class Iterator
    {
    
    private:
    
    	// Points to the current node we are looking at in the stack.
    	typename Stack<Item>::Node *peek;
    
    	Stack<Item> *stack;
    
    public:
    
    	///////////////////////////////////////////////////////////////////////////////////////////////
    	// Member functions
    	///////////////////////////////////////////////////////////////////////////////////////////////
    
    	// The constructor.
    	//
    	// In:	s	The stack you wish to move in.
    	Iterator(Stack<Item> *s)
    	{
    		this->stack = s;
    		this->peek = 0;
    	}
    
    	// Sets our peek member to the top of the stack.
    	void Front() 
    	{
    		if (Stack::front)
    			this->peek = this->stack->front;
    	}
    
    	// Moves our peek member to the next node in the stack.
    	void Move_Down()
    	{
    		this->peek = this->peek->next;
    	}
    
    	// Gives us a pointer to the Item in the stack that “peek” is currently looking at.
    	Item *Get_Current_Item()
    	{
    		return this->peek->data;
    	}
    
    	// Tells us ifpeek has reached the bottom of the stack.
    	bool End()
    	{
    		return this->peek == 0;
    	}
    
    };
    
    // 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)
    		return false;
    
    	// 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!
    	return true;
    };
    
    // Ceck to see if the stack is empty.
    //
    // Return:	False if the stack is full
    template <typename Item>
    bool Stack<Item>::is_empty() const
    {
    	return Top == 0;
    }
    
    // Check to see if the stack is full.
    //
    // Return:	False if stack is not totaly full.
    template <typename Item>
    bool Stack<Item>::is_full() const
    {
    	return Top == 100;
    }
    
    #endif

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what do you mean append? to add things to the stack you have to push and then if you want to add another you have to push again...to take things off the stack you pop...your variables are all undefined so Node *first doesn't point anywhere same with the other Node instances...your syntax for node is correct down in your Stack method...


    Jason


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •