test2-2 - CS 2213 Test 2 ' rite a function, revstrO, which...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 2213 Test 2 ' rite a function, revstrO, which will reverse a string in place. The prototype is l \V/ ’ v01d revstr(char *S); 2. [20] Write the function inslist () function which will insert an integer into a sorted doubly— linked list of integers. Assume that the list contains both a header and a footer node. You must also specify the structure declaration of the list head as well as the node structure. The function will return -1 on error and O on success. A prototype: int inslist(struct list *l,int it); 3. [20] Write the function leaf sum() which will sum all of the integers stored in the leaves of a binary tree and print the sum on stdout. You must specify the node structure. A prototype: int 1eafsum<struct node *node); 4. [20] Write the function nodes23() which will return the number of nodes of the specified 2-3 tree. You will need to specify the structure declaration of the 2-3 node (node23). A prototype: ‘ int node523(struct node23 *node); 5. [20] Write the function ins clist () which will insert a string into a dynamic list u The dynamic list should be a dynamic list of characters i.e. no wasted space if only inserting). You must specify the dynamic list structure and may assume that INCRCSZ and INITCSZ have been defined. A prototype: int insclist(struct dlist **d, char *str); Be very careful with your realloc (). CS 2413 Solutions to Test 2 s5 1.[20] void revstr(char *p) { char *q, tmp; q = p; // Start q at first of string while ( *q++ != ’\O’ ); // Advance q to null terminator + 1 q-~; // Back q up to null terminator q—-; // Back q up to last character while ( p < q ) { // Swap *p and *q twp = *P; *p++ = *q; *q-- = tmp; } } 2.[20] struct dnode { ' V -3“f: int item; 1* E E 72 a ‘ _ ! struct dnode *next; %'Q” 1“ I i r :7 A struct dnode *prev; }; ,,,,, ,, struct list { struct dnode *head; struct dnode *tail; ‘~ }; int inslist(struct list *1, int it) { struct dnode *dn; struct dnode *p; dn = l—>head->next; // may point to tail while ( dn->next != NULL && dn—>item < it ) { dn = dn—>next; } p = malloc<sizeof<struct dnode)); // Allow multiple copies of same data if ( p == NULL ) return(-1); p—>item = it; p—>nexT = dn; // dn points to following node p—>prev = dn—>prev; // insert p before dn dn—>prev = p; p—>prev->next = p; return<0); 3. [20] struct node { int item; struct node *left; struct node *right; }; void leafsum(struct node *node) { printf(”Zd\n”, lsum(node)); } \ Sgié lsum(struct node *node) { if ( node := NULL ) return(0); XX if ( node->left == NULL && node~>right == NULL ) return(node->item); return(leafsum(node->left) + leafsum(node—>right)); } 4. [20] struct node23 { int item; int yl; int y2; struct node23 *sonl; struct node23 *son2; struct node23 *son3; }; int node523(struct node23 *node) { if ( node == NULL ) return(0); if ( node—>son1 == NULL ) // Actually not necessary because of ‘i return(1); // ' return(node323(node—>son1)+node323(node—>son2)+node323(node->son3) + 1); } it: 5. [25] C,\/Lm// struct dlist { int sz; int maxsz; ‘ char ch[1]; i_ E g _? }' Li¥ ~n&¢wwuw€ w.xv géihfifhfif¥\flQ ETutf 7‘ ’ flol V Qv‘mx n, n z. ¢ ,g m. \ ‘ , “I, .7 a E>>#define LISTSZ(fi) (sizeof(struct dlist) + ((n)-1)*sizeof(char)) int insclist(struct dlist **d, char *str) { struct dlist *1; > k ,i . f ‘ \ . ‘ M. I. ’14" > 1111: strsz; i _l l} y.“ \i. A»; = - » I) 1‘ [gal g; strsz = strlen(str) + 1? D if ( (*d)->sz (*d)">maxsz ) { é MERE ‘ 1 =_, realloc(*d, (Size_t) (LISTSZ( (*d) ->maxsz ++ INCRCSZ))) ; , == NULL ) ‘ \ ‘1 return(-1); .‘ *d = l; \_.‘= (*d)->maxsz' +=+ INCRSZ; } v x J w ;:> strcpy((*d)->ch + (*d)->sz, str); // or use address &((*d)->ch[(*d)—>sz]) >>(*d)->sz += strsz; I! E return(0); g ‘=*” U“‘4*~ } 1% 1 i‘\ n I {x ...
View Full Document

Page1 / 4

test2-2 - CS 2213 Test 2 ' rite a function, revstrO, which...

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

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