------ 1. b) c) d) 2. ----------------- 29 PC ? -- G 28 SL 00 27 DL 27 30 l 6 -> 8 -> 10 -> 12 29 PC ? -- F 28 SL 03 27 DL 23 26 i 3 25 PC ? -- F 24 SL 03 23 DL 19 22 i 2 21 PC ? -- F 20 SL 03 19 DL 15 18 i 1 17 PC ? -- F 16 SL 03 15 DL 03 14 i 0 13 nodes[3].next NULL -- main 12 nodes[3].value 3 11 nodes[2].next 12 10 nodes[2].value 2 09 nodes[1].next 10 08 nodes[1].value 1 07 nodes[0].next 08 06 nodes[0].value 0 05 PC ? 04 SL 0 03 DL 0 02 PC ? -- start 01 SL ? 00 DL ? ----------------- ------ 3. #include #include #include typedef struct Node { bool active; int value; struct Node *next; } Node, *List; static List newNode(int value, List next) { List n = malloc(sizeof(Node)); if( n == NULL ) return NULL; n->active = true; n->value = value; n->next = next; return n; } a) int length(List l) { int len = 0; for( ; l != NULL ; l = l->next ) if( l->active ) len++; return len; } b) List remove(List l, int value) { List start = l; for( ; l != NULL ; l = l->next ) // logo que se encontra o primeiro nestas condicoes esta' decidido... if( l->active && l->value >= value ) { if( l->value == value ) l->active = false; break; } return start; } c) List insert(List l, int value) { Node dummy ; dummy.next = l ; dummy.active = true ; l = &dummy ; while( l->next != NULL ) { // logo que se encontra o primeiro nestas condicoes esta' decidido... if( l->next->active && l->next->value >= value ) { if( l->active ) l->next = newNode(value, l->next); else { l->active = true; l->value = value; } return dummy.next; } } // insert at end l->next = newNode(value, l->next); return dummy.next; } ------ 4. var JSRoot = { SUPER: function(method) { return method.apply(this, Array.prototype.slice.apply(arguments).slice(1)); }, INIT: function() { throw "*** MISSING INITIALIZER ***"; } }; function NEW(clazz) { // Create an object and applies INIT(...) to it function F() {} F.prototype = clazz; var obj = new F(); obj.INIT.apply(obj, Array.prototype.slice.apply(arguments).slice(1)); return obj; }; function EXTENDS(clazz, added) { // Creates a subclass of a given class function F() {} F.prototype = clazz; var subclazz = new F(); for(prop in added) subclazz[prop] = added[prop]; return subclazz; }; // "print" é opcional, mas introduz compatibilidade com o Node.js var print = typeof(console) !== 'undefined' ? console.log : print; var Sky = EXTENDS(JSRoot, { name: "", col: [], level: 0, INIT: function(name, col) { this.name = name; this.col = col; }, belongs: function(name) { var i, len = this.col.length; if( this.name == name ) return true; for( i = 0 ; i < len ; i++ ) if( this.col[i].belongs(name) ) return true; return false; }, calcMass: function() { var i, a = 0, len = this.col.length; for( i = 0 ; i < len ; i++ ) a += this.col[i].calcMass(); return a; }, validate: function() { var len = this.col.length; for( i = 0 ; i < len ; i++ ) if( this.level - 1 != this.col[i].level ) return false; return true; } }); var Agreggate = EXTENDS(Sky, { }); var Centered = EXTENDS(Sky, { mass: 0, INIT: function(name, col, mass) { this.SUPER(Sky.INIT, name, col); this.mass = mass; }, calcMass: function() { return this.SUPER(Sky.calcMass) + this.mass; } }); var Galaxy = EXTENDS(Agreggate, { level: 5 }); var Cluster = EXTENDS(Agreggate, { level: 6 }); var Star = EXTENDS(Centered, { level: 4 }); var Planet = EXTENDS(Centered, { level: 3 }); var Satellite = EXTENDS(Centered, { level: 2, validate2: function() { var i, len = this.col.length; for( i = 0 ; i < len ; i++ ) if( this.level != this.col[i].level ) return false; return true; } }); // testing var earth = NEW(Planet, "EARTH", [], 2); var mars = NEW(Planet, "MARS", [], 3); var sun = NEW(Star, "SUN", [earth, mars], 100); print(sun); print(sun.belongs("EARTH")); print(sun.calcMass()); print(sun.validate());