#include #include #include using namespace std; template class Node { T data_; Node* next_; Node(const Node&) = delete; // disable copy ctor Node& operator=(const Node&) = delete; // disable assignment operator public: Node(); Node(T d, Node* n); const T& data() const; T& data(); Node* next() const; Node*& next(); }; template Node::Node() : data_(), next_(0) {} template Node::Node(T d, Node* n) : data_(d), next_(n) {} template const T& Node::data() const { return data_; } template T& Node::data() { return data_; } template Node* Node::next() const { return next_; } template Node*& Node::next() { return next_; } template ostream& operator<<(ostream& out, const Node& N) { out << N.data(); return out; } template class List { Node* top_; List(const List&) = delete; // disable copy ctor List& operator=(const List&) = delete; // disable assignment operator public: List(); ~List(); void push(T object); T pop(); const Node* top() const; bool remove(T object); const Node* find(T object) const; }; template ostream& operator<<(ostream& out, const List& L) { const Node* ptr = L.top(); while (ptr) { out << (*ptr) << '\t'; ptr = ptr -> next(); } return out; } template List::List() : top_(0) {} template List::~List() { Node* ptr = top_; while (ptr) { top_ = top_->next(); delete ptr; ptr = top_; } } template void List::push(T object) { Node* ptr = new Node(object, top_); top_ = ptr; } template const Node* List::top() const { return top_; } template T List::pop() { Node* ptr = top_; top_ = top_ -> next(); T data = ptr->data(); delete ptr; return data; } template const Node* List::find(T object) const { const Node* ptr = top(); while (ptr) { if (ptr->data() == object) { return ptr; } ptr = ptr->next(); } return 0; } template bool List::remove(T object) { if (!find(object)) { cerr << object << " not found\n"; return false; } Node* ptr2current = top_; Node* ptr2previous = top_; if (top_->data() == object) { top_ = top_ -> next(); delete ptr2current; return true; } while (ptr2current) { ptr2current = ptr2current->next(); if (ptr2current->data() == object) { ptr2previous->next() = ptr2current->next(); delete ptr2current; return true; } ptr2previous = ptr2current; } return false; } class Card { private: int pips, suit; static const string SuitName[4]; static const string PipsName[13]; public: Card() : pips(rand()%13), suit(rand()%4) {} Card(int n) : pips(n%13), suit(n%4) {} friend ostream& operator<<(ostream& out, const Card& object) { out << PipsName[object.pips] << " of " << SuitName[object.suit]; return out; } }; const string Card::SuitName[4] = {"clubs","diamonds","hearts","spades"}; const string Card::PipsName[13] = {"two","three","four","five","six","seven", "eight","nine","ten","jack","queen","king","ace" }; int main() { List Lint; Lint.push(2); Lint.push(4); Lint.push(6); Lint.push(8); Lint.push(10); cout << Lint << endl; Lint.pop(); cout << Lint << endl; Card C1; Card C2; Card C3(25); Card C4; Card C5; List LCard; LCard.push(C1); LCard.push(C2); LCard.push(C3); LCard.push(C4); LCard.push(C5); cout << LCard << endl; List Lstring; Lstring.push("day"); Lstring.push("nice"); Lstring.push("very"); Lstring.push("a"); Lstring.push("Have"); cout << Lstring << endl; Lstring.remove("very"); cout << Lstring << endl; }