110 likes | 226 Views
This lab session focuses on the essential concepts of queues and recursion in computer science. We explore the FIFO (First In-First Out) principle of queues, including their implementation as circular arrays. Participants will examine various scenarios to calculate the number of empty slots based on different front and rear positions in a queue of size 12. Additionally, we discuss recursive functions to compute the sum of digits, total items in an integer array, and print characters of a string in reverse order. Important office hours and assignment details are also included.
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