Thread: Need Help with Stack

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    25

    Need Help with Stack

    Hi everybody,
    My Stack class here is used to performance a simply task of stack. I modified this class from another that has 2 parts, one is a structure making up the frame of the list's nodes , and the class is just responsible for building up the list. It's much clearer and easier to understand, but I try to combine them as one class as shown below.
    Take a look at the destructor, each time one dynamic object created by push() function, right after that, it destroys that object once the push() function end. If I remove the destructor, this class work well, since pop() function is responsible for discarding the top node of the list. However, I want to implement the destructor for the class, is there the best way to deal with this ? Thanks in advance.
    Here is my code :

    Code:
    #include <iostream>
    #include "stack.h"
    using namespace std;
    
    //the class definition
    
    class Stack
    {
    	public:
            Stack( );
            //Initializes the object to an empty stack.
    
            Stack(const Stack& a_stack);
            //Copy constructor.
    
            ~Stack( );
            //Destroys the stack and returns all the memory to the freestore.
    
            void push(char the_symbol);
        
    
            char pop( );
            
    
            bool empty( ) const;
            //Returns true if the stack is empty. Returns false otherwise.
        private:
            char data;
    		Stack *link;
    		Stack *top;
    
    };
    
    #endif //STACK_H
    
        //Uses cstddef:
    Stack::Stack( ) : data(NULL), link(NULL),top(NULL)
    {
            //empty.
    }
    
        
    Stack::Stack(const Stack& a_stack)
    {
    	if (a_stack.top == NULL)
    		top = NULL;
    	else
           {
    	    Stack *temp = a_stack.top;//temp moves
                    //through the nodes from top to bottom of 
                    //a_stack.
                Stack *end;//Points to end of the new stack.
    
    	    end = new Stack;
                end->data = temp->data;
                top = end;
                //First node created and filled with data.
                //New nodes are now added AFTER this first node.
    
                temp = temp->link;
                while (temp != NULL)
               {
    	       end->link = new Stack;
                   end = end->link;
                   end->data = temp->data;
                   temp = temp->link;
    	   }
    
                end->link = NULL;
    	}
    }
    
    Stack::~Stack( )
    {
    		//destructor will be called when we push one node into the list
    		//so the node just borned will be destroyed immediately.
    	cout<<"check "<<pop()<<"-";
    // I NEED HELP HERE ... ************************
    
    }
    
        //Uses cstddef:
    bool Stack::empty( ) const
    {
    	return (top == NULL);
    }
    
        //Uses cstddef:
    void Stack::push(char the_symbol)
    {
        Stack *temp_ptr;
        temp_ptr = new Stack;
    
        temp_ptr->data = the_symbol;
    
        temp_ptr->link = top;
        top = temp_ptr;
    		//the destructor will be called when the dynamic variable pointed to by temp_ptr
    		//goes out of scope.
    }
    
        //Uses iostream:
    char Stack::pop( )
    {
    	if (empty( ))
        {
    	cout << "Error: popping an empty stack.\n";
            exit(1);
         }
    
         char result = top->data;
    
         Stack *temp_ptr;
         temp_ptr = top;
         top = top->link;
    
         delete temp_ptr;
    
         return result;
    }
    
    
    // program body
    
    int main( )
    {
        Stack s;
        char next, ans;
    
        do
        {
            cout << "Enter a word: ";
            cin.get(next);
            while (next != '\n')
            {
                s.push(next);
                cin.get(next);
            }
    
            cout << "Written backward that is: ";
            while ( ! s.empty( ) )
                cout << s.pop( );
            cout << endl;
    
            cout << "Again?(y/n): ";
            cin >> ans;
            cin.ignore(10000, '\n');
        }while (ans != 'n' && ans != 'N');
    
        return 0;
    }
    Last edited by trongsi; 05-22-2006 at 09:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM