Unit 11: Data structures Flashcards
What is a data structure in programming?
A way of organizing and storing data so it can be accessed and modified efficiently.
What two things usually go together in program design?
The choice of data structures and the algorithms used to process them.
What are parallel arrays?
Multiple arrays used to represent a single group of records, where each array stores a different field.
Example use of parallel arrays?
An address book with names[], addresses[], and phoneNumbers[], where index i across all arrays relates to one person.
System.out.println(name[i] + " " + address[i] + " " + phone[i]);
What is a partially filled array?
A fixed-size array where only some elements are actively used. The rest of the array is unused but available for future data.
Why are partially filled arrays useful?
They allow flexibility within fixed-size arrays, helping manage memory while tracking only the meaningful (used) data.
What variable is typically used alongside a partially filled array?
currentSize – the index of the first unused element.
What is the boolean inUse[] array used for?
It tracks which indices of the main array actually hold valid data (true) and which don’t (false).
Advantage of using inUse[] over currentSize?
No need to shuffle on deletion; the position is simply marked as not in use.
How do you mark all array positions as unused?
for (int i = 0; i < size; i++) inUse[i] = false;
How do you count the number of used elements?
int count = 0; for (int i = 0; i < size; i++) if (inUse[i]) count++;
How do you mark a value as deleted?
inUse[i] = false;
How do you insert a new value?
for (int i = 0; i < size; i++) { if (!inUse[i]) { data[i] = value; inUse[i] = true; break; } }
How do you find a value in a partially filled array?
for (int i = 0; i < size; i++) if (inUse[i] && data[i] == value) return i; return -1;