110 likes | 221 Views
COP 3502 Spring 2005 Christopher Elwell. Lab Session 6. Administrivia. Recitation notes duplicated online: http://www.cs.ucf.edu/~celwell Office Hours MTWF 2-4 PM Assignment 1 Discussion Grades should be posted by 2/17/05. Queues. FIFO (First In-First Out) data structure
E N D
COP 3502 Spring 2005 Christopher Elwell Lab Session 6
Administrivia • Recitation notes duplicated online: • http://www.cs.ucf.edu/~celwell • Office Hours • MTWF 2-4 PM • Assignment 1 Discussion • Grades should be posted by 2/17/05
Queues • FIFO (First In-First Out) data structure • Has head and tail pointers • Always enqueue at tail • Always dequeue at head • Can be implemented as a “circular array”
Queues • Consider a queue of size 12 using the “circular array” implementation. Indicate the number of empty slots when • Front =-1 and rear=-1 • Front = 0 and rear = 11 • Front = 9 and rear = 11 • Front = 3 and rear = 3
Queues • Consider a queue of size 12 using the “circular array” implementation. Indicate the number of empty slots when • Front =-1 and rear=-1 12 • Front = 0 and rear = 11 0 • Front = 9 and rear = 11 9 • Front = 3 and rear = 3 11
Recursion • Write a recursive function which takes a positive number and returns the sum of its digits. For example, digitsum(2304) returns 2+3+0+4=9.
Recursion int digitsum(int n){ if (n==0) return 0; else return (n%10+digitsum(n/10)); }
Recursion 2 • Write a recursive function int sum() that takes in an integer array and the number of items in the array and returns the summation of all items in the array.
int sum (int L[], int n){ if (n==0) return 0; else return (L[1]+sum(L+1, n-1)); } Recursion 2
Recursion 3 • Write a recursive function that prints out the characters of a string in reverse order.
void print_reverse(char *ptr){ if (*ptr=='\0') return; print_reverse(ptr+1); printf(“%c”, *ptr); } Recursion 3