Data structures: array, stack, queue
A data structure is a planned way to keep many values together so the program can use them easily.
An array is a row of boxes of the same kind. Each box has an index, and we start counting at 0. So an array of 6 boxes has indexes 0 to 5. You can reach any box straight away: marks[3].
A stack is LIFO: Last In, First Out. Push puts an item on top. Pop takes the top item. Undo buttons and the back button of a browser use a stack.
A queue is FIFO: First In, First Out. Enqueue adds at the back. Dequeue removes from the front. A printer waiting list is a queue.
A record groups different facts about one thing, for example a student: roll number, name, marks.
Program standardisation
Standardisation means everybody in a team follows the same rules, so anyone can read and fix any part of the program.
- Naming rules: use clear names such as
totalMarks, notx1. - Layout rules: same indentation, one statement per line.
- Comments: short notes that say why, not just what.
- Reuse: keep common parts (like a date check) in one library and use them everywhere.
Standard programs are easier to read, test and change, and new team members learn them fast.
File processing
A file keeps data on a disk so it stays after the program stops. A file is made of records, and each record has fields. In a sequential file the records are read one after another from the start.
The usual pattern is: open the file, read a record, process it, write the result, repeat until the end of file, then close. Common jobs are search, sort, merge two sorted files, and update records using a key such as the roll number.
Input and output design
Input and output design is about how people talk to the program: the forms they fill and the reports they read.
Good input screens are simple, in a natural order, and check the data before using it: a presence check (not empty), a type check (a number, not letters), a range check (marks from 0 to 100) and a check digit (one extra digit that catches typing mistakes in long numbers).
Good output is clear: headings, aligned columns, units, and total lines. Show helpful messages such as "Marks must be between 0 and 100".
Structured program design
Structured design builds a program from three basic shapes: sequence (steps in order), selection (if / else) and iteration (loops). It avoids messy jumping around.
We use top-down design: write the big job first (Main), then break it into smaller modules, and again into smaller ones, until each is easy. A good module does one job (high cohesion) and depends little on other modules (low coupling). Modules can be built and tested by different people and reused later.
Try it
In the 3D, go to the last step. Choose Stack, add four items, then take out one. Which came out? Now choose Queue and do the same. Predict before you press! At home, write the steps for "make tea" as three modules (Boil, Mix, Serve) and check each does only one job.
Key formulas and definitions
- Array: first index = 0, last index = size − 1
- Stack: LIFO. push adds on top, pop removes from top
- Queue: FIFO. enqueue adds at back, dequeue removes from front
- File size = number of records × bytes per record
- File loop: open → read → process → write → … → close
Worked examples
1. An array marks = [7, 3, 9, 4, 6, 2]. What is marks[3]?
Index 0 is 7, 1 is 3, 2 is 9, 3 is 4. So marks[3] = 4.
2. You push 5, then 8, then 2 on a stack and pop once. What is popped and what is on top now?
Pop removes the last one in: 2. The top is now 8.
3. You enqueue 4, 1, 6 and dequeue once. What comes out?
First In First Out: 4 comes out. The queue now holds 1, 6.
4. An array has 12 items. What is the last index?
Indexes run 0 to 11, so the last index is 11.
5. A file has 250 records of 40 bytes each. How big is it?
250 × 40 = 10,000 bytes (10 KB).
6. A marks field must accept 0 to 100. Which check catches 150?
A range check. 150 is outside 0 to 100, so the program shows an error.
Common mistakes
- Counting array boxes from 1. The first index is 0.
- Mixing up stack and queue. Stack takes the newest first; queue takes the oldest first.
- Forgetting to close a file, or to stop at end of file.
- Trusting user input without checks. Always validate before use.