📘 CodingMarble Learn

Advanced Programming and Applications

Advanced programming means choosing the right data structure (array, linked list, stack, queue), writing and testing code in an IDE, drawing charts and animations, using spreadsheet functions, and storing data safely in a relational database that you query with SQL.

🎬 Step-by-step story

  1. An array has a fixed number of slots, here six. The size is decided first. If it is full, you cannot add more.
  2. A linked list grows when needed. Each node holds a value and a link to the next node. The last one points to nothing (null). Press Add.
  3. A stack works like a pile of plates. You push on top and pop from the top. The last item in is the first out.
  4. A queue works like a ticket line. You join at the back and leave from the front. The first item in is the first out.
  5. A database keeps data in tables. Two tables are joined by a key, the id. An SQL query picks only the rows you want, here marks of 50 or more.
  6. Your turn. Choose list, stack or queue and press the buttons to add and remove items.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why can't I add a seventh item to the array?

The size of an array is fixed when it is made. In step 0 all six slots are the whole array. For a growing collection use a list.

Where does the linked list keep the order?

In the links. Each node knows only the next node, so you walk along the arrows. See step 1.

Why does pop give the last item I added?

A stack has one open end, the top. The newest item sits on top, so it is the first to leave. See step 2.

How is a queue different from a stack?

In a queue you add at the back and remove from the front, so the oldest leaves first. Press the buttons in step 3.

Why use two tables and not one?

So each fact is stored once. The names live in one table and marks in another, joined by the id. Then fixing a name needs a change in one place only. See step 4.

Which structure should I use?

Need a fixed size and fast index access: array. Growing collection: list. Undo and back: stack. Waiting line: queue. Try each in step 5.

Data structures: array, linked list, stack, queue

A data structure is a way to arrange data so a program can use it fast and easily.

Libraries already contain these tools. In Python a list works as a stack (append, pop), and collections.deque works as a fast queue. Use a library when it exists; do not rewrite it.

stack = []
stack.append(5); stack.append(8)
stack.pop()      # gives 8
from collections import deque
q = deque([4, 9]); q.append(2)
q.popleft()      # gives 4

Using an IDE: write, run, test

An IDE (integrated development environment) puts an editor, a run button, a debugger and tools in one place. It colours your code, suggests names, and shows errors as you type.

2D and 3D visualisation and animation

Pictures help us see patterns. Programs can draw charts (bar, line, scatter), 2D drawings and 3D scenes. An animation is the same picture drawn again and again with a small change each time (about 30 to 60 times a second). A simple way: keep a variable such as x, add a little to it each frame and redraw. The 3D on this page is made the same way, with a 3D library.

Advanced spreadsheet functions

Spreadsheets can do program-like work with functions:

Relational databases and SQL

A relational database keeps data in tables of rows and columns. Each table has a primary key, a column whose values are unique (the id). Another table uses that value as a foreign key to link back. Good design stores each fact once, so there is no repeated data.

SQL is the language for asking questions and changing data:

SELECT name, score FROM students
JOIN marks ON students.id = marks.id
WHERE score >= 50 ORDER BY score DESC;

INSERT INTO marks (id, score) VALUES (5, 67);
UPDATE marks SET score = 55 WHERE id = 2;
DELETE FROM marks WHERE id = 5;

Integrity means the data stays correct: keys are unique, a foreign key must match a real row, and values must have the right type. Security means passwords for users, giving each user only the rights they need, backups, and never building SQL by joining user text into the query (use parameters), which stops SQL injection.

Contributing to open resources

Many tools and libraries are open source: anyone can read, use and improve them under a licence. You can help by fixing a bug, improving a guide, translating a page or adding an example. Always read the licence, give credit, and write clear, polite messages when you suggest a change.

Try it

In the 3D step 5, push 3 items on a stack, then pop them. Write the order. Do the same with a queue. Then predict first, check second: after adding 4, 9, 2 and removing one item, which value is left at the front of the queue and at the top of the stack?

Key formulas and definitions

Worked examples

1. Items 5, 8 and 2 are pushed on a stack in this order. Then pop is done twice. What is on top now?

Stack after pushes: 5, 8, 2 (2 on top). Pop removes 2, then 8. Left: 5. Top = 5.

2. Items 4, 9, 2 join a queue in this order. One dequeue is done. Which item is at the front now?

The first one in leaves, so 4 is removed. The front is 9.

3. Marks table: ids 1 to 4 with scores 72, 45, 88, 51. How many rows does WHERE score >= 50 return?

72, 88 and 51 are 50 or more. That is 3 rows.

4. Cells B2:B5 hold 72, 45, 88, 51. What does =SUMIF(B2:B5,">=50") give?

It adds only 72, 88 and 51: 72 + 88 + 51 = 211.

5. Why use a stack to check brackets such as ( [ ] ) in an expression?

Push each opening bracket. When a closing bracket comes, pop and check that it matches. The most recent opening must be closed first (LIFO), which is what a stack gives. If the stack is empty at the end, the brackets match.

Common mistakes

Practice quiz

1. Which structure is LIFO?
2. Which structure grows one node at a time with links?
3. In SQL, which clause picks rows that match a rule?
4. A column with a unique value for every row is a:
5. What does a debugger breakpoint do?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is the difference between an array and a linked list?

An array has a fixed size and lets you jump to any item by index. A linked list can grow and shrink, but you must follow the links one by one to reach an item.

What is a primary key and a foreign key?

A primary key is a column that gives every row a unique value, like a roll number. A foreign key is a column in another table that holds that value to link the two tables.

Why is SQL injection dangerous and how do we stop it?

If you paste user text straight into an SQL command, a clever user can add their own commands and read or delete data. Use parameterised queries so the text is always treated as data only.

Learn first

Learn next

Related lessons

All Computer Science lessons