Untitled
By: a guest | Mar 21st, 2010 | Syntax:
C | Size: 1.34 KB | Hits: 44 | Expires: Never
#include <cstdio>
class List {
private:
int head;
List *tail;
void displayInternal() {
printf("%d", this->head);
if (this->tail != NULL) {
printf(", ");
this->tail->displayInternal();
}
else {
printf("]\n");
}
}
public:
List() {
this->tail = NULL;
}
List(int value) {
this->head = value;
this->tail = NULL;
}
List(int value, List *rest) {
this->head = value;
this->tail = rest;
}
~List() {
if (this->tail != NULL) {
delete this->tail;
}
}
void display() {
printf("[");
this->displayInternal();
}
int length() {
// TODO: implement this
return 0;
}
int max() {
// TODO: implement this
return 0;
}
void append(List *l) {
// TODO: implement this
}
};
List *makeList(int first, int last, int step) {
List *l = new List(last);
while (last != first) {
last -= step;
l = new List(last, l);
}
return l;
}
int main(int argc, char *argv[]) {
List *test = makeList(1, 10, 1);
test->display(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
test->append(makeList(9, 1, -1));
test->display(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
printf("length: %d\n", test->length()); // 19
printf("max: %d\n", test->max()); // 10
delete test;
return(0);
}