Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: C | Size: 1.34 KB | Hits: 44 | Expires: Never
Copy text to clipboard
  1. #include <cstdio>
  2.  
  3. class List {
  4.         private:
  5.                 int head;
  6.                 List *tail;
  7.                
  8.                 void displayInternal() {
  9.                         printf("%d", this->head);
  10.                         if (this->tail != NULL) {
  11.                                 printf(", ");
  12.                                 this->tail->displayInternal();
  13.                         }
  14.                         else {
  15.                                 printf("]\n");
  16.                         }
  17.                 }
  18.                
  19.         public:
  20.                 List() {
  21.                         this->tail = NULL;
  22.                 }
  23.                 List(int value) {
  24.                         this->head = value;
  25.                         this->tail = NULL;
  26.                 }
  27.                 List(int value, List *rest) {
  28.                         this->head = value;
  29.                         this->tail = rest;
  30.                 }
  31.                 ~List() {
  32.                         if (this->tail != NULL) {
  33.                                 delete this->tail;
  34.                         }
  35.                 }
  36.                
  37.                 void display() {
  38.                         printf("[");
  39.                         this->displayInternal();
  40.                 }
  41.                
  42.                 int length() {
  43.                         // TODO: implement this
  44.                         return 0;
  45.                 }
  46.                
  47.                 int max() {
  48.                         // TODO: implement this
  49.                         return 0;
  50.                 }
  51.                
  52.                 void append(List *l) {
  53.                         // TODO: implement this
  54.                 }
  55. };
  56.  
  57. List *makeList(int first, int last, int step) {
  58.         List *l = new List(last);
  59.         while (last != first) {
  60.                 last -= step;
  61.                 l = new List(last, l);
  62.         }
  63.         return l;
  64. }
  65.  
  66. int main(int argc, char *argv[]) {
  67.         List *test = makeList(1, 10, 1);
  68.         test->display(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  69.  
  70.         test->append(makeList(9, 1, -1));
  71.         test->display(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
  72.         printf("length: %d\n", test->length()); // 19
  73.         printf("max: %d\n", test->max()); // 10
  74.         delete test;
  75.         return(0);
  76. }