12S-Section-Solution

12S-Section-Solution - * would be returned: * *...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 12S Spring 2007 April 13, 2007 Section Solution Problem 1: Meet The Flintstones typedef struct rubble { int betty; char barney[4]; struct rubble *bammbamm; } rubble; typedef struct { short *wilma[2]; short fred[2]; rubble dino; } flintstone; rubble *simpsons; flintstone jetsons[4]; simpsons = &jetsons[0].dino; jetsons[1].wilma[3] = (short *) &simpsons; strcpy(simpsons[2].barney, "Bugs Bunny"); ((flintstone *)(jetsons->fred))->dino.bammbamm = simpsons; *(char **)jetson[4].fred = simpsons->barney + 4; simpsons jetsons[0] jetsons[1] jetsons[2] jetsons[3] 'B' 'u' 'g' 's' ' ' 'B' 'u' 'n' 'n' 'y' 0
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Problem 2: Scheme /** * Traverses a properly structured list, and returns the ordered * concatenation of all strings, including those in nested sublists. * * When applied to the two lists drawn above, the following strings
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: * would be returned: * * ConcatAll(gameThree) would return YankeesDiamondbacks * ConcatAll(nestedNumbers) would return onethreesix */ typedef enum { Integer, String, List, Nil } nodeType; static char *ConcatStrings(const char *first, const char *second) { char *result = malloc(strlen(first) + strlen(second) + 1); strcpy(result, first); strcat(result, second); return result; } char *ConcatAll(nodeType *list) { switch (*list) { case Integer: case Nil: return strdup(""); case String: return strdup((char *)(list + 1)); } nodeType **lists = (nodeType **)(list + 1); char *front = ConcatAll(lists[0]); char *back = ConcatAll(lists[1]); char *result = ConcatStrings(front, back); free(front); free(back); return result; }...
View Full Document

This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 2

12S-Section-Solution - * would be returned: * *...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online