Results 1 to 2 of 2
Thread: C++
Hybrid View
-
14th April 2014 22:46 #1Registered 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 { }
.
-
14th April 2014 22:59 #2Registered 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() //




Reply With Quote
Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in