Results 1 to 2 of 2

Thread: C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date: Aug:2012
    Location:
    Posts: 297

    C++

    , . . - .
    print-, ( )? Print-, , , Element, - ? , ( print-a Element) . Element , . , , , . class Stack, Element, Element .. , , Element ( , class public ). - private

    :
    Code:
    class Element
    {
    public:
    	int value;
    	Element* previous;
    	void recursive_print_from_start_to_the_end() const;
    	
    };
    
    class Stack
    {
    public:
    	Stack() : last(NULL) {};
    
    	void copy_stack(const Stack&);
    	void add_element(int const &);
    	bool remove_last(int&);
    	int get_value() const;
    	void print_from_end_to_beginning() const;
    	void print_from_beginning_to_end_recursively() const;
    	void print_from_beginning_to_end() const;
    
    private:
    	Element* last; // should be private
    };
    
    
    
    void Element::recursive_print_from_start_to_the_end() const
    {
    	if (!previous)
    	{
    		cout << value;
    		return;
    	}
    
    	previous->recursive_print_from_start_to_the_end();
    	cout << value << endl;
    }
    
    
    
    void Stack::add_element(int const& number)
    {
    	Element* swap_pointer = last;
    	last = new Element;
    	last->value = number;
    	last->previous = swap_pointer;
    }
    
    bool Stack::remove_last(int& num)
    {
    	if (!last) return 0;
    
    	Element* del_pointer = last;
    	num = last->value;
    	last = last->previous;
    	delete del_pointer;
    	return 1;
    }
    
    int Stack::get_value() const
    {
    	return last->value;
    }
    
    void Stack::print_from_end_to_beginning() const
    {
    	Element* pointer = last;
    	while (pointer)
    	{
    		cout << pointer->value << endl;;
    		pointer = pointer->previous;
    	}
    }
    
    void Stack::print_from_beginning_to_end_recursively() const
    {
    	if (!last) cout << "The stack is empty!" << endl;
    
    	last->recursive_print_from_start_to_the_end();
    }
    
    void Stack::print_from_beginning_to_end() const
    {
    	
    }
    - , , , .
    .

  2. #2
    Registered User
    Join Date: Jun:2012
    Location: /
    Posts: 13,663
    Element *p = last;
    while(p != NULL)
    {
    cout << p->value << " ";
    p = p-> previous;
    }
    cout << endl;
    }

    . p .

    : private . .
    print() //

Posting Permissions

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

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |