fork(11) download
  1. #include <iostream>
  2.  
  3.  
  4. template<typename T>
  5. class slist {
  6. struct node {
  7. node* next;
  8. T val;
  9. };
  10. private:
  11. node* lst;
  12. public:
  13. slist(void):lst(NULL){}
  14. ~slist(){
  15. this->clear();
  16. }
  17. public:
  18.  
  19. slist& operator + (const T& val){
  20. node* p = new (std::nothrow) node();
  21. if(p != NULL){
  22. p->val = val;
  23. p->next = lst;
  24. lst = p;
  25. }
  26. return *this;
  27. }
  28.  
  29. slist& operator -- (void){
  30. node* t;
  31. if(lst != NULL){
  32. t = lst;
  33. lst = lst->next;
  34. delete t;
  35. }
  36. return *this;
  37. }
  38.  
  39. void clear(void){
  40. node* t;
  41. while(lst != NULL){
  42. t = lst;
  43. lst = lst->next;
  44. delete t;
  45. }
  46. }
  47.  
  48. T& operator *(void) const { return lst->val; }
  49. T& operator *(void) { return lst->val; }
  50.  
  51. bool empty(void) const { return (lst == NULL); }
  52. };
  53.  
  54.  
  55. int main(void){
  56. slist<char> lc;
  57. for(char c = 'A'; c <= 'Z'; ++c)
  58. lc = lc + c;
  59.  
  60. while(! lc.empty()){
  61. std::cout << *lc;
  62. --lc;
  63. }
  64. return 0;
  65. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
ZYXWVUTSRQPONMLKJIHGFEDCBA